add ringbuffer func

This commit is contained in:
Carsten Schmiemann 2022-11-14 01:43:46 +01:00
parent 165831bac6
commit e55a1b5fef

View file

@ -120,13 +120,21 @@ tm tm;
//Globals //Globals
long lastMsg = 0; long lastMsg = 0;
char msg[50]; char msg[50];
float pv_v = 0, pv_w = 0, pv_i = 0, pv_kwh = 0, inv_p = 0, inv_i = 0, batt_v = 0, batt_i = 0, batt_p = 0, batt_c_min = 0, batt_c_max = 0, grid_p = 0, batt_soc = 0, load_ph1 = 0, load_ph2 = 0, load_ph3 = 0; float pv_v = 0, pv_w = 0, pv_i = 0, pv_kwh = 0, inv_p = 0, inv_i = 0, batt_v = 0, batt_i = 0, batt_p = 0, batt_c_min = 0, batt_c_max = 0, grid_p = 0, consum_p = 0, batt_soc = 0, load_ph1 = 0, load_ph2 = 0, load_ph3 = 0;
bool lastButtonRotationState = HIGH, lastButtonSetupState = HIGH; bool lastButtonRotationState = HIGH, lastButtonSetupState = HIGH;
bool currentButtonRotationState, currentButtonSetupState; bool currentButtonRotationState, currentButtonSetupState;
static unsigned long lastDispRefreshTime = 0; static unsigned long lastDispRefreshTime = 0;
static unsigned long lastScreenChangeTime = 0; static unsigned long lastScreenChangeTime = 0;
int display_screen = 0; int display_screen = 0;
//Display graph ringbuffer
void display_graph_ringbuffer_add(float value) {
float tmp = value;
display_graph_ringbuffer[0] = value;
memmove(display_graph_ringbuffer,display_graph_ringbuffer+1,sizeof(int)*(125));
display_graph_ringbuffer[125] = tmp;
}
//MQTT topics //MQTT topics
void callback(char* topic, byte* message, unsigned int length) { void callback(char* topic, byte* message, unsigned int length) {
logPrintlnD("Message arrived on topic: "); logPrintlnD("Message arrived on topic: ");
@ -174,10 +182,8 @@ void callback(char* topic, byte* message, unsigned int length) {
deserializeJson(json, messageTemp); deserializeJson(json, messageTemp);
grid_p = json["value"]; grid_p = json["value"];
//add to ringbuffer for testing //add every measurement to rbuffer
display_graph_ringbuffer[display_graph_ringbuffer_index++] = grid_p; display_graph_ringbuffer_add(grid_p);
if (display_graph_ringbuffer_index == 126)
display_graph_ringbuffer_index = 0;
} }
//Inverter - power //Inverter - power
@ -242,18 +248,21 @@ void callback(char* topic, byte* message, unsigned int length) {
DynamicJsonDocument json(512); DynamicJsonDocument json(512);
deserializeJson(json, messageTemp); deserializeJson(json, messageTemp);
load_ph1 = json["value"]; load_ph1 = json["value"];
consum_p = load_ph1 + load_ph2 + load_ph3; //calc consumption on each msg for precision
} }
//System - AC Load Phase 2 //System - AC Load Phase 2
if (String(topic) == mqtt_grid_power_l2) { if (String(topic) == mqtt_grid_power_l2) {
DynamicJsonDocument json(512); DynamicJsonDocument json(512);
deserializeJson(json, messageTemp); deserializeJson(json, messageTemp);
load_ph2 = json["value"]; load_ph2 = json["value"];
consum_p = load_ph1 + load_ph2 + load_ph3; //calc consumption on each msg for precision
} }
//System - AC Load Phase 3 //System - AC Load Phase 3
if (String(topic) == mqtt_grid_power_l3) { if (String(topic) == mqtt_grid_power_l3) {
DynamicJsonDocument json(512); DynamicJsonDocument json(512);
deserializeJson(json, messageTemp); deserializeJson(json, messageTemp);
load_ph3 = json["value"]; load_ph3 = json["value"];
consum_p = load_ph1 + load_ph2 + load_ph3; //calc consumption on each msg for precision
} }
} }
@ -618,7 +627,7 @@ void loop() {
if (display_refresh_counter <= display_refresh_time) if (display_refresh_counter <= display_refresh_time)
{ {
display_screen_grid(grid_p, inv_p, inv_i, pv_w); display_screen_grid(grid_p, consum_p, inv_p, inv_i, pv_w);
} }
if (display_refresh_counter >= display_refresh_time && display_refresh_counter <= (display_refresh_time*2)) if (display_refresh_counter >= display_refresh_time && display_refresh_counter <= (display_refresh_time*2))
{ {