Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <MD_Parola.h>
- #include <MD_MAX72xx.h>
- #include <Arduino.h>
- #include <time.h>
- #include <stdlib.h>
- #include <MBED_RPi_Pico_TimerInterrupt.h>
- #include <NTPClient.h>
- #include <WiFiEspAT.h>
- #include <WiFiUdp.h>
- #include <sys/time.h>
- #include "main.h"
- #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
- #define MAX_DEVICES 8
- #define CS1 27
- #define CS2 26
- #define SEC2PS(x) ((x) * 1000000)
- #define SEC2MS(x) ((x) * 1000)
- const char ssid[] = "Tuldok-House";
- const char pass[] = "HelloWorld";
- MD_Parola* line1;
- MD_Parola* line2;
- MBED_RPI_PICO_Timer clockTimer(0);
- MBED_RPI_PICO_Timer updateTimer(1);
- WiFiUDP ntpUdp;
- NTPClient timeClient(ntpUdp, "time.google.com", 0, SEC2MS(64));
- void setup() {
- Serial.begin(115200); // USB UART
- Serial1.begin(115200); // Serial line to the ESP-01 WiFi module
- initWiFi();
- initSpi();
- initDisplay();
- initTime();
- initTimer();
- }
- void loop() {
- timeClient.update();
- sleep_ms(500);
- }
- void clockTick(uint alarmNum)
- {
- time_t now;
- char buf[16];
- time(&now);
- auto tm = localtime(&now);
- strftime(buf, 16, "%m/%d/%Y", tm);
- line1->setTextAlignment(PA_CENTER);
- line1->print(buf);
- strftime(buf, 16, "%I:%M:%S %P", tm);
- line2->setTextAlignment(PA_CENTER);
- line2->print(buf);
- }
- void syncRtcTick(uint alarmNum)
- {
- timeval tv {.tv_sec = timeClient.getEpochTime()};
- settimeofday(&tv, NULL);
- }
- void initWiFi()
- {
- WiFi.init(Serial1);
- WiFi.begin(ssid, pass);
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(500);
- }
- Serial.println("WiFi connected.");
- }
- void initDisplay()
- {
- line1 = new MD_Parola(HARDWARE_TYPE, SPI, CS1, 8);
- line2 = new MD_Parola(HARDWARE_TYPE, SPI, CS2, 8);
- line1->begin();
- line2->begin();
- line1->setIntensity(15);
- line2->setIntensity(15);
- line1->displayClear();
- line2->displayClear();
- }
- void initTimer()
- {
- // clock tick interrupt
- clockTimer.attachInterruptInterval(SEC2PS(1), clockTick);
- // rtc sync interrupt
- updateTimer.attachInterrupt(SEC2PS(5*60), syncRtcTick);
- }
- void initTime()
- {
- setenv("TZ", "Asia/Manila", 1);
- tzset();
- timeClient.update();
- timeval tv {.tv_sec = timeClient.getEpochTime()};
- settimeofday(&tv, NULL);
- }
- void initSpi()
- {
- SPI.begin();
- pinMode(CS1, OUTPUT);
- pinMode(CS2, OUTPUT);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement