ESP_Solar_Monitor/src/display.cpp

169 lines
6.6 KiB
C++

/*
; Project: ESP32 GLCD SOLAR MONITOR
; Date: 3rd Sep 2022
;
; (C) 2022 Carsten Schmiemann
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
*/
#include <logger.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <display.h>
#include <screens.h>
#include <pins.h>
//U8G2_SSD1306_128X64_VCOMH0_F_HW_I2C display(U8G2_R0, OLED_RST, OLED_SCL, OLED_SDA); OLED on TTGO
U8G2_ST7920_128X64_F_HW_SPI display(U8G2_R2, LCD_CS, U8X8_PIN_NONE);
//U8G2_ST7920_128X64_F_SW_SPI display(U8G2_R2, LCD_E_SCLK, LCD_RW_SI, LCD_CS, U8X8_PIN_NONE);
bool pv_charging = false;
void display_begin() {
display.begin();
}
void display_header() {
display.setFont(u8g2_font_10x20_tr);
display.drawStr(0,20,"PV Monitor");
display.drawStr(48,35,"Project");
display.setFont(u8g2_font_6x10_tr);
}
void display_init(char* VERSION) {
display.firstPage();
do {
display_header();
display.drawStr(0,52,"by Carsten Schmiemann");
display.drawStr(30,60,"2022");
display.print(VERSION);
} while ( display.nextPage() );
}
void display_wifi(char* STATUS) {
display.firstPage();
do {
display_header();
display.drawStr(0,52,"Connecting wifi...");
display.drawStr(30,60, STATUS);
} while ( display.nextPage() );
}
void display_mqtt() {
display.firstPage();
do {
display_header();
display.drawStr(0,52,"Connect mqtt...");
} while ( display.nextPage() );
}
void display_category_0() {
display.setFont(u8g2_font_6x10_tr);
display.drawStr(2,7,"PV-Module");
display.drawLine(0, 8, 128, 8);
display.drawLine(100, 0, 100, 8);
display.drawXBM(106,0, down_width, down_height, down_bits);
display.drawXBM(0,9, Solarpanel_width, Solarpanel_height, Solarpanel_bits);
}
void display_category_1() {
display.setFont(u8g2_font_6x10_tr);
display.drawStr(2,7,"Batterie");
display.drawStr(46,45,"F");
display.drawStr(46,64,"E");
display.drawLine(0, 8, 128, 8);
display.drawLine(100, 0, 100, 8);
display.drawXBM(106,0, down_width, down_height, down_bits);
display.drawXBM(116,0, up_width, up_height, up_bits);
display.drawXBM(7,9, battery_width, battery_height, battery_bits);
display.drawFrame(5,38,40,26);
}
void display_category_2() {
display.setFont(u8g2_font_6x10_tr);
display.drawStr(2,7,"Netz");
display.drawLine(0, 8, 128, 8);
display.drawLine(100, 0, 100, 8);
display.drawXBM(116,0, up_width, up_height, up_bits);
display.drawXBM(0,9, grid_width, grid_height, grid_bits);
}
//PV Charger
void display_screen_0(float pv_voltage, float pv_wattage, float battery_voltage, float pv_amps, float pv_kwh)
{
logPrintlnD("Refresh display values...");
display.firstPage();
do {
display_category_0();
display.setCursor(72,18); display.print(pv_voltage); display.print("V");
display.setCursor(72,28); display.print(pv_wattage,0); display.print("W");
display.setCursor(72,38); display.print(pv_kwh); display.print("kWh");
display.setCursor(72,54); display.print(battery_voltage); display.print("V");
display.setCursor(72,63); display.print(pv_amps); display.print("A");
} while ( display.nextPage() );
}
//Battery
void display_screen_1(float battery_voltage, float battery_amps, float battery_wattage, float battery_soc, float batt_cell_v_min, float batt_cell_v_max)
{
logPrintlnD("Refresh display values...");
display.firstPage();
do {
display_category_1();
display.setCursor(9,60); display.print(batt_cell_v_min); display.print("V");
display.setCursor(9,50); display.print(batt_cell_v_max); display.print("V");
display.setCursor(72,18); display.print(battery_voltage); display.print("V");
display.setCursor(72,28); display.print(battery_amps,1); display.print("A");
display.setCursor(72,38); display.print(battery_wattage,0); display.print("W");
display.setFont(u8g2_font_inr19_mf);
display.setCursor(70,63); display.print(battery_soc,0); display.print("%");
display.setFont(u8g2_font_6x10_tr);
int batt_fill_y = map(battery_soc, 0, 100, 62, 37);
int batt_fill_height = map(battery_soc, 0, 100, 0, 25);
display.setDrawColor(2);
display.drawBox(6,batt_fill_y,38,batt_fill_height);
} while ( display.nextPage() );
}
//Grid
void display_screen_2(float grid_power, float inv_power, float inv_current, float load_ph1, float load_ph2, float load_ph3, float pv_wattage)
{
logPrintlnD("Refresh display values...");
display.firstPage();
do {
display_category_2();
display.setCursor(10,45); display.print(grid_power,0); display.print("W");
display.setCursor(40,54); display.print(inv_power,0); display.print("W");
display.setCursor(40,63); display.print(inv_current,1); display.print("A");
float ac_load = load_ph1 + load_ph2 + load_ph3;
display.setCursor(87,20); display.print(ac_load,0); display.print("W");
display.setCursor(101,38); display.print(pv_wattage,0); display.print("W");
int pv_chg_y_pos;
if (pv_wattage >= 10) {
if (pv_charging) {
pv_chg_y_pos = 45;
pv_charging = false;
} else {
pv_chg_y_pos = 47;
pv_charging = true;
}
display.drawXBM(89,pv_chg_y_pos, down_pv_width, down_pv_height, down_pv_bits);
}
} while ( display.nextPage() );
}