#include <WiFi.h>
#include <TM1637Display.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
//#include <Arduino.h>
#include <SimpleRotary.h>
#define CLK_PIN 4
#define DIO_PIN 5
// Initialize the TM1637 display
TM1637Display display(CLK_PIN, DIO_PIN);
const int led1 = 2;
const byte PinCLK = 19;
const byte PinDT = 18;
const byte PinSW = 15;
const byte PinBuzzer = 23;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* ntpServerName = "pool.ntp.org";
const long utcOffsetInSeconds = 25200; // Define time zone (GMT+7 for Bangkok, Thailand)
// Initialize UDP client for NTP
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServerName, utcOffsetInSeconds);
SimpleRotary rotary(PinCLK, PinDT, PinSW);
TaskHandle_t Task0;
TaskHandle_t Task1;
void timeShow(){
timeClient.update();
// Get the current time
uint8_t hours = timeClient.getHours();
uint8_t minutes = timeClient.getMinutes();
uint8_t seconds = timeClient.getSeconds();
if (hours >= 7 && hours < 18) {
display.setBrightness(0x0f); // Set the brightness to high
} else {
display.setBrightness(0x07); // Set the brightness to medium
}
// Display the time on the TM1637 display
//display.showNumberDecEx(hours * 100 + minutes, 0b11100000, true); //Flash every 2 seconds
Serial.println((String)hours + ":" + minutes + ":" + seconds);
if(seconds % 2 == 0) {
display.showNumberDecEx(hours * 100 + minutes, 0b01000000 , true, 4, 0);
} else {
display.showNumberDecEx(hours * 100 + minutes, 0b00000000 , true, 4, 0);
}
delay(1000); // Update the display every second
}
void onAlarm() {
tone(PinBuzzer, 4978, 100);
delay(130);
noTone(PinBuzzer);
}
void onPush() {
tone(PinBuzzer, 4978, 80);
delay(200);
tone(PinBuzzer, 4978, 100);
delay(130);
noTone(PinBuzzer);
}
void onLongPush() {
tone(PinBuzzer, 4978, 800);
delay(130);
tone(PinBuzzer, 4978, 100);
delay(130);
noTone(PinBuzzer);
}
void detectEncoder(void) {
uint8_t tmp;
uint8_t rotary_event = 0; // 0 = not turning, 1 = CW, 2 = CCW
uint8_t push_event = 0; // 0 = not pushed, 1 = pushed, 2 = long pushed
tmp = rotary.pushType(400);
if ( tmp != 0 ) // only assign the rotation event, never clear the event here
push_event = tmp;
tmp = rotary.rotate();
if ( tmp != 0 ) // only assign the rotation event, never clear the event here
rotary_event = tmp;
// 0 = not pushed, 1 = pushed
if ( push_event == 1 ) {
push_event = 0;
onPush();
//mui.sendSelect();
//mod.sendSel/ct();
//is_redraw = 1;
Serial.println("SW_button is Pushed ");
}
if ( push_event == 2 ) {
push_event = 0;
onLongPush();
//mui.sendSelect();
//mode.sendEnter();
//is_redraw = 1;
Serial.println("SW_button is Long Pushed " );
}
// 0 = not turning, 1 = CW, 2 = CCW
if ( rotary_event == 1 ) {
rotary_event = 0;
onPush();
//mui.nextField();
//is_redraw = 1;
Serial.println("Rotated to CW");
}
if ( rotary_event == 2 ) {
rotary_event = 0;
onPush();
//mui.prevField();
//is_redraw = 1;
Serial.println("Rotated to CCW");
}
}
//#include "loop_T0.h"
//#include "loop_T1.h"
void setup() {
Serial.begin(115200);
pinMode(led1, OUTPUT);
pinMode(PinBuzzer, OUTPUT);
// Initialize the TM1637 display
display.setBrightness(0x0a); // Set the brightness (0x00 to 0x0f)
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Initialize the time client
timeClient.begin();
timeClient.setTimeOffset(utcOffsetInSeconds);
xTaskCreatePinnedToCore(loop_T0, "Task0", 12000, NULL, 8, &Task0, 0);
xTaskCreatePinnedToCore(loop_T1, "Task1", 12000, NULL, 2, &Task1, 1);
}
void loop() {
//timeShow();
// Get the current time
}
void loop_T0(void * parameter) {
for (;;) {
Serial.print("Task0 running on core ");
Serial.println(xPortGetCoreID());
detectEncoder();
//digitalWrite(led1, HIGH);
//delay(1000);
//digitalWrite(led1, LOW);
//delay(1000);
}
}
void loop_T1(void * parameter) {
for (;;) {
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
timeShow();
/*
timeClient.update();
// Get the current time
uint8_t hours = timeClient.getHours();
uint8_t minutes = timeClient.getMinutes();
uint8_t seconds = timeClient.getSeconds();
if (hours >= 7 && hours < 18) {
display.setBrightness(0x0f); // Set the brightness to high
} else {
display.setBrightness(0x07); // Set the brightness to medium
}
// Display the time on the TM1637 display
//display.showNumberDecEx(hours * 100 + minutes, 0b11100000, true); //Flash every 2 seconds
Serial.println((String)hours + ":" + minutes + ":" + seconds);
if(seconds % 2 == 0) {
display.showNumberDecEx(hours * 100 + minutes, 0b01000000 , true, 4, 0);
} else {
display.showNumberDecEx(hours * 100 + minutes, 0b00000000 , true, 4, 0);
}
delay(1000);
*/
}
}