diff --git a/include/Menu.h b/include/Menu.h index 37705dd..8a9e76b 100644 --- a/include/Menu.h +++ b/include/Menu.h @@ -56,6 +56,8 @@ void setupNav(void); extern MD_Parola mainDisplay; extern WiFiSetup wifiSetup; extern MqttSetup mqttSetup; +extern const String MQTT_ECU_TOPIC; +extern const String MQTT_GPS_TOPIC; extern volatile char secondaryScreenMode[]; #endif // MENU_H diff --git a/include/SecondaryLoop.h b/include/SecondaryLoop.h index 45a9f5c..0032dc4 100644 --- a/include/SecondaryLoop.h +++ b/include/SecondaryLoop.h @@ -5,6 +5,7 @@ #include #include "LedController.hpp" +#include "MqttSetup.h" #define DIN 5 #define CS 4 @@ -56,6 +57,10 @@ extern bool timer2Started; extern volatile unsigned long timer1Value; extern volatile unsigned long timer2Value; +extern const String MQTT_TIMER1_TOPIC; +extern const String MQTT_TIMER2_TOPIC; +extern MqttSetup mqttSetup; + /** * @brief Callback function for timer 1. * @param xTimer The timer handle. @@ -87,4 +92,14 @@ void convertTimerToTime(unsigned long timerValue, int &hours, int &minutes, int */ void displayTime(int hours, int minutes, int seconds, int hundredths); +/** + * @brief Pushes time to MQTT + * @param timer Timer nr. + * @param hours Hours to display. + * @param minutes Minutes to display. + * @param seconds Seconds to display. + * @param hundredths Hundredths of seconds to display. + */ +void setTimeToMqtt(int timer, int hours, int minutes, int seconds, int hundredths); + #endif // SECONDARY_LOOP_H diff --git a/include/TimerButtons.h b/include/TimerButtons.h index 07d8846..aee3ed2 100644 --- a/include/TimerButtons.h +++ b/include/TimerButtons.h @@ -2,6 +2,7 @@ #define TIMER_BUTTON_H #include // Physical switch lib https://github.com/MajicDesigns/MD_UISwitch +#include "MqttSetup.h" // External declarations for global variables used in TimerButtons.cpp extern int activeTimer; @@ -11,6 +12,9 @@ extern TimerHandle_t timer1Handle; extern TimerHandle_t timer2Handle; extern volatile unsigned long timer1Value; extern volatile unsigned long timer2Value; +extern const String MQTT_TIMER1_TOPIC; +extern const String MQTT_TIMER2_TOPIC; +extern MqttSetup mqttSetup; // Pin assignments for timer buttons const uint8_t SW1_TME_PIN = 14; // Port for the first timer button diff --git a/src/Main.cpp b/src/Main.cpp index 022f1ae..b1179d8 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -26,6 +26,12 @@ const char *MQTT_TOPIC_BASE = "GOLF86"; const char *WELCOME_MSG = "Golf'86"; const char *WELCOME_MSG2 = "GOLF'86"; +// Extract this constant as it's being used in multiple places +const String MQTT_ECU_TOPIC = "/" + String(MQTT_TOPIC_BASE) + "/ECU/"; +const String MQTT_GPS_TOPIC = "/" + String(MQTT_TOPIC_BASE) + "/GPS/"; +const String MQTT_TIMER1_TOPIC = "/" + String(MQTT_TOPIC_BASE) + "/TM1/"; +const String MQTT_TIMER2_TOPIC = "/" + String(MQTT_TOPIC_BASE) + "/TM2/"; + // Global message buffers shared by Serial and Scrolling functions char notAvailableMsg[] = "..N/A.."; bool firstRun = true; diff --git a/src/Menu.cpp b/src/Menu.cpp index 1965140..034e902 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -2,20 +2,16 @@ #include "Menu.h" // Rotary switch and button initialization -const uint8_t RE_A_PIN = 25; ///< Port for rotary switch A channel -const uint8_t RE_B_PIN = 26; ///< Port for rotary switch B channel +const uint8_t RE_A_PIN = 26; ///< Port for rotary switch A channel +const uint8_t RE_B_PIN = 25; ///< Port for rotary switch B channel const uint8_t CTL_PIN = 27; ///< Port for the main input button // Initialize rotary encoder MD_REncoder RE(RE_A_PIN, RE_B_PIN); - + // Initialize switch library for the main input button MD_UISwitch_Digital swCtl(CTL_PIN); -// Extract this constant as it's being used in multiple places -const String MQTT_ECU_TOPIC = "/" + String(MQTT_TOPIC_BASE) + "/ECU/"; -const String MQTT_GPS_TOPIC = "/" + String(MQTT_TOPIC_BASE) + "/GPS/"; - // Transform helper variables char dataIndex[] = "RPM"; char dataIndex2[] = "RPM"; diff --git a/src/SecondaryLoop.cpp b/src/SecondaryLoop.cpp index 8af7b20..f1c5661 100644 --- a/src/SecondaryLoop.cpp +++ b/src/SecondaryLoop.cpp @@ -99,12 +99,26 @@ void secondaryDisplayLoop(void *parameter) void timer1Callback(TimerHandle_t xTimer) { timer1Value += 10; + int hours, minutes, seconds, hundredths; + convertTimerToTime(timer1Value, hours, minutes, seconds, hundredths); + if (strcmp(const_cast(secondaryScreenMode), "TIMER1") == 0) { - int hours, minutes, seconds, hundredths; - convertTimerToTime(timer1Value, hours, minutes, seconds, hundredths); displayTime(hours, minutes, seconds, hundredths); } + + static int counter = 0; + + // Check if 100ms has elapsed + if (counter == 10) + { + setTimeToMqtt(1, hours, minutes, seconds, hundredths); + counter = 0; // Reset the counter + } + else + { + counter++; + } } /** @@ -114,12 +128,27 @@ void timer1Callback(TimerHandle_t xTimer) void timer2Callback(TimerHandle_t xTimer) { timer2Value += 10; + + int hours, minutes, seconds, hundredths; + convertTimerToTime(timer2Value, hours, minutes, seconds, hundredths); + if (strcmp(const_cast(secondaryScreenMode), "TIMER2") == 0) { - int hours, minutes, seconds, hundredths; - convertTimerToTime(timer2Value, hours, minutes, seconds, hundredths); displayTime(hours, minutes, seconds, hundredths); } + + static int counter = 0; + + // Check if 100ms has elapsed + if (counter == 10) + { + setTimeToMqtt(2, hours, minutes, seconds, hundredths); + counter = 0; // Reset the counter + } + else + { + counter++; + } } /** @@ -206,3 +235,26 @@ void displayTime(int hours, int minutes, int seconds, int hundredths) } } +/** + * @brief Pushes time to MQTT + * @param timer Timer nr. + * @param hours Hours to display. + * @param minutes Minutes to display. + * @param seconds Seconds to display. + * @param hundredths Hundredths of seconds to display. + */ +void setTimeToMqtt(int timer, int hours, int minutes, int seconds, int hundredths) +{ + char timeText[12]; + + snprintf(timeText, sizeof(timeText), "%02d-%02d-%02d:%02d", hours, minutes, seconds, hundredths); + + if (timer == 1) + { + mqttSetup.mqtt.publish(MQTT_TIMER1_TOPIC + String("value"), timeText); + } + else if (timer == 2) + { + mqttSetup.mqtt.publish(MQTT_TIMER2_TOPIC + String("value"), timeText); + } +} \ No newline at end of file diff --git a/src/TimerButtons.cpp b/src/TimerButtons.cpp index 7cb6633..c3ac012 100644 --- a/src/TimerButtons.cpp +++ b/src/TimerButtons.cpp @@ -63,18 +63,24 @@ void monitorTimerSwitches() // Check active timer and timer state, then perform appropriate action if (activeTimer == 1 && !timer1Started) { + mqttSetup.mqtt.publish(MQTT_TIMER1_TOPIC + String("started"), "true"); + mqttSetup.mqtt.publish(MQTT_TIMER1_TOPIC + String("paused"), "false"); startTimer1(); } else if (activeTimer == 1) { + mqttSetup.mqtt.publish(MQTT_TIMER1_TOPIC + String("paused"), "true"); pauseTimer(1); } else if (activeTimer == 2 && !timer2Started) { + mqttSetup.mqtt.publish(MQTT_TIMER2_TOPIC + String("started"), "true"); + mqttSetup.mqtt.publish(MQTT_TIMER2_TOPIC + String("paused"), "false"); startTimer2(); } else if (activeTimer == 2) { + mqttSetup.mqtt.publish(MQTT_TIMER2_TOPIC + String("paused"), "true"); pauseTimer(2); } } @@ -87,10 +93,16 @@ void monitorTimerSwitches() if (activeTimer == 1) { resetTimer(1); + mqttSetup.mqtt.publish(MQTT_TIMER1_TOPIC + String("started"), "false"); + mqttSetup.mqtt.publish(MQTT_TIMER1_TOPIC + String("paused"), "false"); + mqttSetup.mqtt.publish(MQTT_TIMER1_TOPIC + String("value"), "00-00-00:00"); } else if (activeTimer == 2) { resetTimer(2); + mqttSetup.mqtt.publish(MQTT_TIMER2_TOPIC + String("started"), "false"); + mqttSetup.mqtt.publish(MQTT_TIMER2_TOPIC + String("paused"), "false"); + mqttSetup.mqtt.publish(MQTT_TIMER2_TOPIC + String("value"), "00-00-00:00"); } } @@ -220,6 +232,10 @@ void pauseTimer(int timerNr) // Stop timer and update status xTimerStop(timer1Handle, 0); timer1Started = false; + // send exact stop value to MQTT + int hours, minutes, seconds, hundredths; + convertTimerToTime(timer1Value, hours, minutes, seconds, hundredths); + setTimeToMqtt(1, hours, minutes, seconds, hundredths); } break; case 2: @@ -228,6 +244,10 @@ void pauseTimer(int timerNr) // Stop timer and update status xTimerStop(timer2Handle, 0); timer2Started = false; + // send exact stop value to MQTT + int hours, minutes, seconds, hundredths; + convertTimerToTime(timer2Value, hours, minutes, seconds, hundredths); + setTimeToMqtt(2, hours, minutes, seconds, hundredths); } break; }