From 557f61f48ea06353ea0e68917b5eecc9718aaba3 Mon Sep 17 00:00:00 2001 From: Marcel Walter Date: Wed, 14 Jan 2026 22:40:26 +0100 Subject: [PATCH] make it pretty, it should work as before --- src/main.cpp | 194 +++++++++++++++++++++++++++++---------------------- 1 file changed, 109 insertions(+), 85 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 24cb0dc..773312a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,73 +8,65 @@ #include "FlipDotDrv.h" #include "secrets.h" +/* most important global declaratioen ;-) */ +String Text = ""; +const char *const HOSTNAME = "flipdottelegram"; + +/* ------------ OTA section and variables -------------- */ bool OTAStarted; -void onOTAStart() { +void onOTAStart() +{ OTAStarted = true; } -// BearSSL::WiFiClientSecure secured_client; -WiFiClientSecure secured_client; -bool Start = false; - +/* ------------ FlipDot variables -------------- */ #define WIDTH 96 #define HEIGHT 16 +#define FLIPDOT_BUFFER_SIZE (WIDTH * HEIGHT / 2) #define ROTATION 0 // 0 or 2 for 0 degrees or 180 degrees -#define MAX_CHAR_PER_LINE WIDTH/6 +#define MAX_CHAR_PER_LINE WIDTH / 6 FlipDotDrv flip(WIDTH, HEIGHT, 0); GFXcanvas1 canvas(WIDTH, HEIGHT); -void displayText(const char* text, bool background, int16_t x = -1, int16_t y = -1); +void displayText(const char *text, bool background, int16_t x = -1, int16_t y = -1); -String Text = ""; +/* ------------ Telegram variables -------------- */ +// BearSSL::WiFiClientSecure secured_client; +WiFiClientSecure secured_client; -void printOpen(String Args) -{ - Text = "offen"; -} +// TelegramCommand callbacks must fulfill a function pointer: void (*) (const String arguments) +void printOpen(String Args); +void printClose(String Args); +void printText(String Args); -void printClose(String Args) -{ - Text = "leider zu"; -} - -void printText(String Args) -{ - Serial.print("Display would show '"); - Serial.print(Args); - Serial.println("'"); - Text = Args; -} - -TelegramCommand myCmdList[] { - {"/display_open", printOpen}, - {"/display_close", printClose}, - {"/display_text", printText}, +TelegramCommand myCmdList[]{ + {"/display_open", printOpen}, + {"/display_close", printClose}, + {"/display_text", printText}, }; uint32_t myCmdListCnt = sizeof(myCmdList) / sizeof(myCmdList[0]); Telegram tele(secured_client, myCmdList, myCmdListCnt); - void setup() { - uint8_t rawBuff[192] = {}; - Serial.begin(115200); Serial.println(); - //rotate the Canvas so it is displayed in the correct orientation on the FlipDotDisplay + // rotate the Canvas so it is displayed in the correct orientation on the FlipDotDisplay canvas.setRotation(ROTATION); + /* initialize the display with several checker template */ + uint8_t rawBuff[FLIPDOT_BUFFER_SIZE] = {}; Serial.println("Initializing Display"); flip.sendRaw(rawBuff, sizeof(rawBuff)); delay(1000); Serial.println("draw checker"); - memset(rawBuff, 0xAA, 192); - for (int i = 0; i < 192; ++i) + memset(rawBuff, 0xAA, FLIPDOT_BUFFER_SIZE); + for (int i = 0; i < FLIPDOT_BUFFER_SIZE; ++i) { if ((i % 4) < 2) rawBuff[i] = 0xAA; @@ -85,7 +77,7 @@ void setup() delay(1000); Serial.println("bigger checker"); - for (int i = 0; i < 192; ++i) + for (int i = 0; i < FLIPDOT_BUFFER_SIZE; ++i) { if ((i % 8) < 4) rawBuff[i] = 0x33; @@ -96,7 +88,7 @@ void setup() delay(1000); Serial.println("largest checker"); - for (int i = 0; i < 192; ++i) + for (int i = 0; i < FLIPDOT_BUFFER_SIZE; ++i) { if ((i % 16) < 8) rawBuff[i] = 0x0F; @@ -105,10 +97,10 @@ void setup() } flip.sendRaw(rawBuff, sizeof(rawBuff)); - // attempt to connect to Wifi network: + /* attempt to connect to Wifi network */ Serial.print("Connecting to Wifi SSID "); Serial.print(WIFI_SSID); - WiFi.setHostname("flipdottelegram"); + WiFi.setHostname(HOSTNAME); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { @@ -120,6 +112,7 @@ void setup() Serial.print("\nWiFi connected. IP address: "); Serial.println(WiFi.localIP()); + /* get internet time */ Serial.print("Retrieving time: "); configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "pool.ntp.org"); @@ -132,13 +125,15 @@ void setup() } Serial.println(now); - - ArduinoOTA.setHostname("flipdottelegram"); + /* setup ArduinoOTA */ + ArduinoOTA.setHostname(HOSTNAME); ArduinoOTA.begin(); ArduinoOTA.onStart(onOTAStart); + /* initialize and connect telegram bot */ tele.init(); + /* initialy print closed at the display */ printClose(""); } @@ -148,29 +143,32 @@ void loop() static unsigned long lastDisplayRefresh = now; static unsigned long lastTelegramRefresh = now; + /* OTA handling */ ArduinoOTA.handle(); yield(); + /* Telegram handling */ if ((now - lastTelegramRefresh) > 5000) { lastTelegramRefresh = now; tele.cyclic(now); } + /* FlipDot Handling */ if ((now - lastDisplayRefresh) > 2000) { lastDisplayRefresh = now; - //fill canvas with black pixels + // fill canvas with black pixels canvas.fillScreen(0); - //if textlength is larger than possible on the display we need to scroll + // if textlength is larger than possible on the display we need to scroll uint32_t TextLength = Text.length(); if (TextLength > (MAX_CHAR_PER_LINE - 6)) { static uint32_t offset = 0; - //get the substring ffor this iteration + // get the substring ffor this iteration String SubText = Text.substring(offset, offset + MAX_CHAR_PER_LINE); if (SubText.length() < MAX_CHAR_PER_LINE) @@ -180,71 +178,97 @@ void loop() } else { - //increase the offset and reset it at the end of the scrol + // increase the offset and reset it at the end of the scrol offset += 4; } - //write substring to canvas + // write substring to canvas displayText(SubText.c_str(), true, 0, 4); } else { - //display current time on the left + // display current time on the left tm timeinfo; - char buffer [6] {0}; - if(getLocalTime(&timeinfo)) + char buffer[6]{0}; + if (getLocalTime(&timeinfo)) { strftime(buffer, 6, "%R", &timeinfo); } displayText(buffer, true, 0, 4); - //display text on the right centered + // display text on the right centered displayText(Text.c_str(), true, (6 + (MAX_CHAR_PER_LINE - 6 - TextLength) / 2) * 6, 4); } - //send canvas to display + // send canvas to display flip.sendCanvas(&canvas); } } -void displayText(const char* text, bool background, int16_t x, int16_t y) +/* function to write text onto the canvas + * + * @param text - C string to be printed + * @param background - boolean switch if the background should be transparent (false) or black (true) + * @param x - position in X direction to write the Text, -1 centers the text + * @param y - position in Y direction to write the Text, -1 centers the text + * + * */ +void displayText(const char *text, bool background, int16_t x, int16_t y) { - // canvas.fillScreen(0); - canvas.setFont(NULL); - canvas.setTextSize(1); - canvas.setTextColor(1); + // canvas.fillScreen(0); + canvas.setFont(NULL); + canvas.setTextSize(1); + canvas.setTextColor(1); - int16_t cursor_x, cursor_y; - int16_t bounds_x, bounds_y; - uint16_t bounds_w, bounds_h; - canvas.getTextBounds(text, 0, 0, &bounds_x, &bounds_y, &bounds_w, &bounds_h); + int16_t cursor_x, cursor_y; + int16_t bounds_x, bounds_y; + uint16_t bounds_w, bounds_h; + canvas.getTextBounds(text, 0, 0, &bounds_x, &bounds_y, &bounds_w, &bounds_h); - if (x == -1) - { - // Zentriert - cursor_x = (WIDTH - bounds_w) / 2; - } - else - { - cursor_x = x; - } - if (y == -1) - { - // Zentriert - cursor_y = (HEIGHT - bounds_h) / 2; - } - else - { - cursor_y = y; - } + if (x == -1) + { + // centered + cursor_x = (WIDTH - bounds_w) / 2; + } + else + { + cursor_x = x; + } + if (y == -1) + { + // centered + cursor_y = (HEIGHT - bounds_h) / 2; + } + else + { + cursor_y = y; + } - if (background) - { - canvas.fillRect(cursor_x - 1, cursor_y - 1, bounds_w + 1, bounds_h + 1, 0); - } - - canvas.setCursor(cursor_x, cursor_y); - canvas.print(text); + /* draw a black rectangle in the background of the text if it should not be transparent */ + if (background) + { + canvas.fillRect(cursor_x - 1, cursor_y - 1, bounds_w + 1, bounds_h + 1, 0); + } + + /* print the text to the destination on canvas */ + canvas.setCursor(cursor_x, cursor_y); + canvas.print(text); } +void printOpen(String Args) +{ + Text = "offen"; +} +void printClose(String Args) +{ + Text = "leider zu"; +} + +void printText(String Args) +{ + Serial.print("Display would show '"); + Serial.print(Args); + Serial.println("'"); + Text = Args; +}