diff --git a/EPaperAnalog.code-workspace b/EPaperAnalog.code-workspace index a1244c3..36a7352 100644 --- a/EPaperAnalog.code-workspace +++ b/EPaperAnalog.code-workspace @@ -11,7 +11,23 @@ "settings": { "files.associations": { "*.html": "html", - "cmath": "cpp" + "cmath": "cpp", + "array": "cpp", + "chrono": "cpp", + "deque": "cpp", + "format": "cpp", + "forward_list": "cpp", + "initializer_list": "cpp", + "list": "cpp", + "vector": "cpp", + "xhash": "cpp", + "xstring": "cpp", + "xtree": "cpp", + "xutility": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "string_view": "cpp" } } } \ No newline at end of file diff --git a/src/einkanalog/einkanalog.cpp b/src/einkanalog/einkanalog.cpp index c5e12a1..76b7018 100644 --- a/src/einkanalog/einkanalog.cpp +++ b/src/einkanalog/einkanalog.cpp @@ -1,9 +1,24 @@ #include "./einkanalog.h" -EinkAnalogDisplay::EinkAnalogDisplay(GxEPD2_BW *display, uint8_t dacPin) +EinkAnalogDisplay::EinkAnalogDisplay(GxEPD2_BW *display, uint8_t dacPin, bool GxEPD_DEBUG) { this->display = display; this->dac = dacPin; + + if (GxEPD_DEBUG) + { + this->display->init(115200); + } else { + this->display->init(0); + } + + // Init display + display->fillScreen(GxEPD_WHITE); + display->setTextColor(GxEPD_BLACK); + display->setFont(&Pixeltype8pt7b); + display->setRotation(1); + display->setPartialWindow(0, 0, display->width(), display->height()); + display->firstPage(); } EinkAnalogDisplay::~EinkAnalogDisplay() diff --git a/src/einkanalog/einkanalog.h b/src/einkanalog/einkanalog.h index 1874091..bf29bca 100644 --- a/src/einkanalog/einkanalog.h +++ b/src/einkanalog/einkanalog.h @@ -1,7 +1,19 @@ #ifndef EIAD_H #define EIAD_H + +// Globals #include +// GxEPD2 Driver +#include + +// Fonts +#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}; @@ -18,9 +30,9 @@ private: void centerText(const char *text, int16_t x, int16_t y); public: - EinkAnalogDisplay(GxEPD2_BW *display, uint8_t dacPin); + EinkAnalogDisplay(GxEPD2_BW *display, uint8_t dacPin, bool GxEPD_DEBUG = false); ~EinkAnalogDisplay(); - void drawFace(int16_t min, int16_t max, char *name, char* unit, uint8_t lines = 5); + 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); }; diff --git a/src/global.h b/src/global.h index d07e7b2..65c9151 100644 --- a/src/global.h +++ b/src/global.h @@ -7,9 +7,6 @@ #include #include -// MQTT -#include - // DAC #define DAC1 25 @@ -18,16 +15,6 @@ #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" diff --git a/src/helpers/mqtt.cpp b/src/helpers/mqtt.cpp index 9096f65..ff6d609 100644 --- a/src/helpers/mqtt.cpp +++ b/src/helpers/mqtt.cpp @@ -1,6 +1,19 @@ #include "mqtt.h" -void callback(char *topic, byte *payload, unsigned int length) +MqttTopic::MqttTopic(WiFiClient* espClient): wifiClient(espClient), client(PubSubClient(*espClient)) +{ + IPAddress server(192, 168, 2, 196); + + this->client.setServer(server, 1883); + this->client.setCallback(this->callback); +} + +MqttTopic::~MqttTopic() +{ + this->client.disconnect(); +} + +void MqttTopic::callback(char *topic, byte *payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); @@ -12,3 +25,27 @@ void callback(char *topic, byte *payload, unsigned int length) // Serial.println(String(array).toFloat() / 1000.0); // ead.setArm(String(array).toFloat() / 1000.0); } + +void MqttTopic::loop() +{ + while (!this->client.connected()) + { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (this->client.connect("espead", "arne", "ThinkCentreM58")) + { + Serial.println("connected"); + this->client.subscribe("homeassistant/ead/#"); + } + else + { + Serial.print("failed, rc="); + Serial.print(this->client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } + + this->client.loop(); +} diff --git a/src/helpers/mqtt.h b/src/helpers/mqtt.h index 00478fc..b3834a7 100644 --- a/src/helpers/mqtt.h +++ b/src/helpers/mqtt.h @@ -1,7 +1,24 @@ #ifndef MQTT_H #define MQTT_H + +// Globals #include -void callback(char *topic, byte *payload, unsigned int length); +// MQTT +#include + +class MqttTopic +{ +private: + WiFiClient* wifiClient; // Pointer to WiFiClient, for use with PubSubClient + PubSubClient client; + uint8_t topic = 0; + +public: + MqttTopic(WiFiClient* espClient); + ~MqttTopic(); + static void callback(char *topic, byte *payload, unsigned int length); + void loop(); +}; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 72bd8a6..5d950d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,8 @@ -#include "global.h" +// Globals +#include -// Wifi WiFiMulti WiFiMulti; WiFiClient espClient; -IPAddress server(192, 168, 2, 196); - -// MQTT -PubSubClient client(espClient); // Buttons Button2 btn_L, btn_R; @@ -16,7 +12,7 @@ GxEPD2_BW display(GxEPD2_213_B72(/*CS=5* // Helpers EinkAnalogDisplay ead(&display, DAC1); - +MqttTopic mqtt(&espClient); void btnHandler(Button2 &btn) { @@ -37,7 +33,6 @@ void btnHandler(Button2 &btn) void setup() { Serial.begin(115200); - display.init(115200); delay(10); Serial.println("T5 Dashboard " __DATE__ " " __TIME__); @@ -51,13 +46,6 @@ void setup() btn_R.setClickHandler(btnHandler); btn_R.setDoubleClickHandler(btnHandler); - display.fillScreen(GxEPD_WHITE); - display.setTextColor(GxEPD_BLACK); - display.setFont(&Pixeltype8pt7b); - display.setRotation(1); - display.setPartialWindow(0, 0, display.width(), display.height()); - display.firstPage(); - ead.drawFace(0, 100, "Welcome", "", 11); display.nextPage(); @@ -101,9 +89,6 @@ void setup() // 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()); @@ -112,26 +97,7 @@ void setup() void loop() { - while (!client.connected()) - { - 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); - } - } - btn_L.loop(); btn_R.loop(); - client.loop(); + mqtt.loop(); } \ No newline at end of file