#include <TM1638plus.h>
#include "WiFi.h"
#include "time.h"
// GPIO I/O pins on the Arduino connected to strobe, clock, data,
//pick on any I/O you want.
#define STROBE_TM 4
#define CLOCK_TM 16
#define DIO_TM 17
TaskHandle_t hDisplayTask;
struct tm timeinfo;
SemaphoreHandle_t xMutex;
float poolFlowRateGallons = 0;
float spaFlowRateGallons = 0;
float poolTotalGallons = 0;
float spaTotalGallons = 0;
float poolDailyGallons = 0;
float spaDailyGallons = 0;
float poolGallonsSinceReset = 0;
float spaGallonsSinceReset = 0;
bool spaFillEnabled = false;
bool spaFilling = false;
bool spaFull = false;
bool systemError = false;
bool poolFillEnabled = false;
bool poolFilling = false;
bool poolFull = false;
bool wifiConnected = false;
int currentDisplay = 1;
bool high_freq = true; //default false,, If using a high freq CPU > ~100 MHZ set to true.
const char* ssid = "";
const char* password = "";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -25200;
const int daylightOffset_sec = 3600;
//Constructor object (GPIO STB , GPIO CLOCK , GPIO DIO, use high freq MCU default false)
TM1638plus tm1638(STROBE_TM, CLOCK_TM , DIO_TM, high_freq);
int LED0 = 0;
int LED1 = 0;
int LED2 = 0;
int LED3 = 0;
int LED4 = 0;
int LED5 = 0;
int LED6 = 0;
int LED7 = 0;
void setup()
{
Serial.begin(115200);
tm1638.displayBegin();
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
tm1638.setLED(7, TM_GREEN_LED);
wifiConnected = true;
Serial.println(" CONNECTED");
xMutex = xSemaphoreCreateMutex();
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
if(!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain time");
}
Serial.println(timeinfo.tm_sec);
tm1638.displayBegin();
delay(1000);
Serial.println(xPortGetCoreID());
//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
DisplayHandler, /* Task function. */
"DisplayHandler", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&hDisplayTask, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(50);
}
void DisplayHandler(void *pvParameters) {
Serial.print("DisplayHandler running on core ");
Serial.println(xPortGetCoreID());
char workStr[11];
char strDate[4], strMonth[6], strYear[8];
while (1) {
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
}
if (xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE) {
switch (currentDisplay) {
case 1:
sprintf(workStr, " %02d.%02d.%02d ", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
break;
case 2:
strftime(strDate, sizeof strDate, "%d", &timeinfo);
strftime(strMonth, sizeof strMonth, "%m", &timeinfo);
strftime(strYear, sizeof strYear, "%y", &timeinfo);
sprintf(workStr, "%s-%s-%s", strMonth, strDate, strYear);
break;
case 3:
strcpy(workStr, "Total ");
break;
case 4:
strcpy(workStr, "Today ");
break;
case 5:
strcpy(workStr, "Reset ");
break;
case 6:
strcpy(workStr, "Rate ");
break;
}
tm1638.displayText(workStr);
tm1638.setLED(0, spaFillEnabled ? TM_GREEN_LED : TM_RED_LED);
tm1638.setLED(1, spaFilling ? TM_GREEN_LED : TM_RED_LED);
tm1638.setLED(2, spaFull ? TM_GREEN_LED : TM_RED_LED);
tm1638.setLED(3, systemError ? TM_RED_LED : TM_GREEN_LED);
tm1638.setLED(4, poolFillEnabled ? TM_GREEN_LED : TM_RED_LED);
tm1638.setLED(5, poolFilling ? TM_GREEN_LED : TM_RED_LED);
tm1638.setLED(6, poolFull ? TM_GREEN_LED : TM_RED_LED);
tm1638.setLED(7, wifiConnected ? TM_GREEN_LED : TM_RED_LED);
xSemaphoreGive(xMutex);
}
delay(1000);
}
}
void loop()
{
uint8_t buttons;
if (xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE)
{
buttons = tm1638.readButtons();
switch (buttons) //testcount)
{
//currentDisplay = timeDisplay;
case 1: if(spaFillEnabled == true) spaFillEnabled=false; else spaFillEnabled=true; delay(250); break;
case 2: if(LED1 == 0) LED1=1; else LED1=0; tm1638.setLED(1, LED1); delay(250); currentDisplay = 2; break;
case 4: if(LED2 == 0) LED2=1; else LED2=0; tm1638.setLED(2, LED2); delay(250); break;
case 8:if(LED7 == 0) LED7=1; else LED7=0; tm1638.setLED(7, LED7); delay(250); break;
case 16: if(poolFillEnabled == true) poolFillEnabled=false; else poolFillEnabled=true; delay(250); break;
case 32: if(LED5 == 0) LED5=1; else LED5=0; tm1638.setLED(5, LED5); delay(250); break;
case 64: if(LED6 == 0) LED6=1; else LED6=0; tm1638.setLED(6, LED6); delay(250); break;
case 128: if(currentDisplay == 6) currentDisplay = 1; else currentDisplay++; delay(250); break;
}
xSemaphoreGive(xMutex);
}
delay(125);
}