diff --git a/platformio.ini b/platformio.ini index 8849e77..cc74ec3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,3 +21,4 @@ lib_deps = khoih-prog/ESP_WifiManager@^1.12.1 adafruit/Adafruit GFX Library@^1.11.7 knolleary/PubSubClient@^2.8 + lennarthennigs/Button2@^2.3.2 diff --git a/src/einkanalog.h b/src/einkanalog/einkanalog.cpp similarity index 84% rename from src/einkanalog.h rename to src/einkanalog/einkanalog.cpp index 0bdf069..c5e12a1 100644 --- a/src/einkanalog.h +++ b/src/einkanalog/einkanalog.cpp @@ -1,34 +1,4 @@ -#include -#include - -#include -#include "../include/fonts/Pixeltype8pt7b.h" -#include "../include/fonts/Pixeltype16pt7b.h" -#include "../include/fonts/Fipps_Regular8pt7b.h" -#include "../include/fonts/SilomBol7pt7b.h" - -const unsigned char gfx_wireless[] PROGMEM = { - 0x00, 0xe0, 0x10, 0xc8, 0x28, 0xa8, 0x00}; - -const unsigned char gfx_build[] PROGMEM = { - 0x00, 0x50, 0xf8, 0x88, 0x88, 0xf8, 0x00}; - -class EinkAnalogDisplay -{ -private: - int16_t min; - int16_t max; - uint8_t dac; - GxEPD2_BW *display; - void centerText(const char *text, int16_t x, int16_t y); - -public: - EinkAnalogDisplay(GxEPD2_BW *display, uint8_t dacPin); - ~EinkAnalogDisplay(); - void drawFace(int16_t min, int16_t max, char *name, char* unit, uint8_t lines = 5); - int setArm(float value); - void drawMeta(const char *wireless_str); -}; +#include "./einkanalog.h" EinkAnalogDisplay::EinkAnalogDisplay(GxEPD2_BW *display, uint8_t dacPin) { diff --git a/src/einkanalog/einkanalog.h b/src/einkanalog/einkanalog.h new file mode 100644 index 0000000..1874091 --- /dev/null +++ b/src/einkanalog/einkanalog.h @@ -0,0 +1,28 @@ +#ifndef EIAD_H +#define EIAD_H +#include + +const unsigned char gfx_wireless[] PROGMEM = { + 0x00, 0xe0, 0x10, 0xc8, 0x28, 0xa8, 0x00}; + +const unsigned char gfx_build[] PROGMEM = { + 0x00, 0x50, 0xf8, 0x88, 0x88, 0xf8, 0x00}; + +class EinkAnalogDisplay +{ +private: + int16_t min; + int16_t max; + uint8_t dac; + GxEPD2_BW *display; + void centerText(const char *text, int16_t x, int16_t y); + +public: + EinkAnalogDisplay(GxEPD2_BW *display, uint8_t dacPin); + ~EinkAnalogDisplay(); + void drawFace(int16_t min, int16_t max, char *name, char* unit, uint8_t lines = 5); + int setArm(float value); + void drawMeta(const char *wireless_str); +}; + +#endif \ No newline at end of file diff --git a/src/global.h b/src/global.h new file mode 100644 index 0000000..d07e7b2 --- /dev/null +++ b/src/global.h @@ -0,0 +1,35 @@ +#ifndef GLOBAL_H +#define GLOBAL_H + +#include + +// Wifi +#include +#include + +// MQTT +#include + +// DAC +#define DAC1 25 + +// Buttons +#include "Button2.h" +#define BTN_L 22 +#define BTN_R 21 + +// Display +#include + +// Fonts +#include +#include "../include/fonts/Pixeltype8pt7b.h" +#include "../include/fonts/Pixeltype16pt7b.h" +#include "../include/fonts/Fipps_Regular8pt7b.h" +#include "../include/fonts/SilomBol7pt7b.h" + +// Local helper files +#include "./einkanalog/einkanalog.h" +#include "./helpers/mqtt.h" + +#endif \ No newline at end of file diff --git a/src/helpers/mqtt.cpp b/src/helpers/mqtt.cpp new file mode 100644 index 0000000..9096f65 --- /dev/null +++ b/src/helpers/mqtt.cpp @@ -0,0 +1,14 @@ +#include "mqtt.h" + +void callback(char *topic, byte *payload, unsigned int length) +{ + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.println("] "); + + // char array[length + 1] = {}; + // memcpy(array, payload, length); + // array[length] = '\0'; + // Serial.println(String(array).toFloat() / 1000.0); + // ead.setArm(String(array).toFloat() / 1000.0); +} diff --git a/src/helpers/mqtt.h b/src/helpers/mqtt.h new file mode 100644 index 0000000..00478fc --- /dev/null +++ b/src/helpers/mqtt.h @@ -0,0 +1,7 @@ +#ifndef MQTT_H +#define MQTT_H +#include + +void callback(char *topic, byte *payload, unsigned int length); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9816ad2..72bd8a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,135 +1,137 @@ -#include +#include "global.h" // Wifi -#include -#include - WiFiMulti WiFiMulti; WiFiClient espClient; IPAddress server(192, 168, 2, 196); // MQTT -#include PubSubClient client(espClient); -// DAC -#define DAC1 25 - -// BTN -#define BTN_L -#define BTN_R +// Buttons +Button2 btn_L, btn_R; // Display -#include GxEPD2_BW display(GxEPD2_213_B72(/*CS=5*/ SS, /*DC=*/17, /*RST=*/16, /*BUSY=*/4)); -#include "./einkanalog.h" +// Helpers EinkAnalogDisplay ead(&display, DAC1); -// Fonts -#include -#include "../include/fonts/Pixeltype8pt7b.h" -#include "../include/fonts/Pixeltype16pt7b.h" -void callback(char *topic, byte *payload, unsigned int length) +void btnHandler(Button2 &btn) { - Serial.print("Message arrived ["); - Serial.print(topic); - Serial.print("] "); - - // char array[length + 1] = {}; - // memcpy(array, payload, length); - // array[length] = '\0'; - // Serial.println(String(array).toFloat() / 1000.0); - // ead.setArm(String(array).toFloat() / 1000.0); + switch (btn.getType()) + { + case single_click: + break; + case double_click: + Serial.print("double "); + break; + } + Serial.print("click "); + Serial.print("on button #"); + Serial.print((btn == btn_L) ? "L" : "R"); + Serial.println(); } void setup() { - Serial.begin(115200); - display.init(115200); - delay(10); + Serial.begin(115200); + display.init(115200); + delay(10); - Serial.println("T5 Dashboard " __DATE__ " " __TIME__); + Serial.println("T5 Dashboard " __DATE__ " " __TIME__); - display.fillScreen(GxEPD_WHITE); - display.setTextColor(GxEPD_BLACK); - display.setFont(&Pixeltype8pt7b); - display.setRotation(1); - display.setPartialWindow(0, 0, display.width(), display.height()); - display.firstPage(); + // Buttons + btn_L.begin(BTN_L, INPUT_PULLUP, true); + btn_L.setClickHandler(btnHandler); + btn_L.setDoubleClickHandler(btnHandler); - ead.drawFace(0, 100, "Welcome", "", 11); - display.nextPage(); + btn_R.begin(BTN_R, INPUT_PULLUP, true); + btn_R.setClickHandler(btnHandler); + btn_R.setDoubleClickHandler(btnHandler); - for (int8_t i = 0; i <= 100; i++) - { - ead.setArm(i); - delay(20); - } + display.fillScreen(GxEPD_WHITE); + display.setTextColor(GxEPD_BLACK); + display.setFont(&Pixeltype8pt7b); + display.setRotation(1); + display.setPartialWindow(0, 0, display.width(), display.height()); + display.firstPage(); - for (int8_t i = 100; i >= 0; i--) - { - ead.setArm(i); - delay(20); - } - - // display.setCursor(0, 6); - // // display.println("TTGO T5 V2.3 ePaper PlatformIO"); - // display.println("T5 Analog/Digital Display, " __DATE__ " " __TIME__); - // display.nextPage(); - - WiFiMulti.addAP("Langeboomgaard", "ACvI4152EK"); - WiFiMulti.addAP("ARNE-LAPTOPV3 6821", "P3900:q5"); - - if (int stat = WiFiMulti.run() != WL_CONNECTED) - { - Serial.println(WiFi.status()); - display.print("WiFi connection failed"); - display.println(stat); + ead.drawFace(0, 100, "Welcome", "", 11); display.nextPage(); - esp_deep_sleep_start(); - } - // display.printf("WiFi connected to "); - // display.println(WiFi.SSID()); - // display.print("IP address: "); - // display.println(WiFi.localIP()); - // display.nextPage(); + for (int8_t i = 0; i <= 100; i++) + { + ead.setArm(i); + delay(20); + } - // delay(1000); + for (int8_t i = 100; i >= 0; i--) + { + ead.setArm(i); + delay(20); + } - // display.fillScreen(GxEPD_WHITE); - // display.nextPage(); + // display.setCursor(0, 6); + // // display.println("TTGO T5 V2.3 ePaper PlatformIO"); + // display.println("T5 Analog/Digital Display, " __DATE__ " " __TIME__); + // display.nextPage(); - client.setServer(server, 1883); - client.setCallback(callback); + WiFiMulti.addAP("Langeboomgaard", "ACvI4152EK"); + WiFiMulti.addAP("ARNE-LAPTOPV3 6821", "P3900:q5"); - display.fillScreen(GxEPD_WHITE); - ead.drawFace(0, 9, "Solax Prod.", "kW", 10); - ead.drawMeta(WiFi.localIP().toString().c_str()); - display.nextPage(); + if (int stat = WiFiMulti.run() != WL_CONNECTED) + { + Serial.println(WiFi.status()); + display.print("WiFi connection failed"); + display.println(stat); + display.nextPage(); + esp_deep_sleep_start(); + } + + // display.printf("WiFi connected to "); + // display.println(WiFi.SSID()); + // display.print("IP address: "); + // display.println(WiFi.localIP()); + // display.nextPage(); + + // delay(1000); + + // display.fillScreen(GxEPD_WHITE); + // display.nextPage(); + + client.setServer(server, 1883); + client.setCallback(callback); + + display.fillScreen(GxEPD_WHITE); + ead.drawFace(0, 9, "Solax Prod.", "kW", 10); + ead.drawMeta(WiFi.localIP().toString().c_str()); + display.nextPage(); } void loop() { - while (!client.connected()) - { - Serial.print("Attempting MQTT connection..."); - // Attempt to connect - if (client.connect("espead", "arne", "ThinkCentreM58")) + while (!client.connected()) { - Serial.println("connected"); - client.subscribe("homeassistant/ead/#"); + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect("espead", "arne", "ThinkCentreM58")) + { + Serial.println("connected"); + client.subscribe("homeassistant/ead/#"); + } + else + { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } } - else - { - Serial.print("failed, rc="); - Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); - // Wait 5 seconds before retrying - delay(5000); - } - } - client.loop(); + + btn_L.loop(); + btn_R.loop(); + client.loop(); } \ No newline at end of file