Skip to content

Commit

Permalink
MQTT timer publish added + bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
askrejans committed Jan 22, 2024
1 parent 5ba5648 commit 36729e2
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 11 deletions.
2 changes: 2 additions & 0 deletions include/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions include/SecondaryLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <MD_UISwitch.h>
#include "LedController.hpp"
#include "MqttSetup.h"

#define DIN 5
#define CS 4
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions include/TimerButtons.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define TIMER_BUTTON_H

#include <MD_UISwitch.h> // Physical switch lib https://github.com/MajicDesigns/MD_UISwitch
#include "MqttSetup.h"

// External declarations for global variables used in TimerButtons.cpp
extern int activeTimer;
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 3 additions & 7 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
60 changes: 56 additions & 4 deletions src/SecondaryLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(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++;
}
}

/**
Expand All @@ -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<char *>(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++;
}
}

/**
Expand Down Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions src/TimerButtons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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:
Expand All @@ -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;
}
Expand Down

0 comments on commit 36729e2

Please sign in to comment.