#include <Arduino.h>
#include <TM1637Display.h>
#include <WiFi.h>
#include <esp_sntp.h> //for sntp_sync_status
#define CLK D7// #include <TM1637Display.h>
#define DIO D10
//#define LED_PIN 10
//#define BUTTON_PIN 18
//#define MODE_PIN 14
// put function declarations here:
//const int led = D10; // there is no LED_BUILTIN available for the XIAO ESP32C3.
const char* ssid = "**********";
const char* password = "**********";
const char* ntpServer1 = "ntp.nict.jp";
const char* ntpServer2 = "time.google.com";
const char* ntpServer3 = "ntp.jst.mfeed.ad.jp";
const long gmtOffset_sec = 9 * 3600;
const int daylightOffset_sec = 0;
bool timeset = 0;
hw_timer_t * timer = NULL;
volatile SemaphoreHandle_t timerSemaphore;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
struct tm timeInfo;
// 現在時刻表示
void showLocalTime()
{
char str[256];
static const char *wd[7] = { "日", "月", "火", "水", "木", "金", "土" };
unsigned long m;
// getLocalTimeを使用して現在時刻取得
m = millis();
getLocalTime(&timeInfo);
sprintf(str, "[getLocalTime ] %04d/%02d/%02d(%s) %02d:%02d:%02d : %d (ms)", timeInfo.tm_year+1900, timeInfo.tm_mon+1, timeInfo.tm_mday, wd[timeInfo.tm_wday], timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec, millis()-m);
Serial.println(str);
}
void timeavailable(struct timeval *t)
{
Serial.println("Got time adjustment from NTP!");
timeset = 1;
}
void ARDUINO_ISR_ATTR onTimer(){
// Give a semaphore that we can check in the loop
xSemaphoreGiveFromISR(timerSemaphore, NULL);
// It is safe to use digitalRead/Write here if you want to toggle an output
}
TM1637Display display(CLK, DIO);
void setup() {
// initialize digital pin led as an output
//pinMode(led, OUTPUT);
Serial.begin(115200);
delay(100);
Serial.printf("%s - run\n",__func__);
//pinMode(LED_PIN, OUTPUT);
// Wifi接続処理
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// NTP
sntp_set_time_sync_notification_cb( timeavailable );
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2, ntpServer3);
// 時刻設定後の時刻取得
while( !timeset ) {
delay(1000);
}
Serial.println("<<時刻設定後の時刻取得>>");
for(int i=0;i<1;i++) {
showLocalTime();
delay(1000);
}
//lastIsrAt = millis();
//resetTime = lastIsrAt;
// Create semaphore to inform us when the timer has fired
timerSemaphore = xSemaphoreCreateBinary();
// Use 1st timer of 4 (counted from zero).
// Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more
// info).
timer = timerBegin(0, 80, true);
// Attach onTimer function to our timer.
timerAttachInterrupt(timer, &onTimer, true);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter)
timerAlarmWrite(timer, 1000000, true);
// Start an alarm
timerAlarmEnable(timer);
display.setBrightness(0x0f);
}
void loop() {
// put your main code here, to run repeatedly:
uint32_t countsec;
uint32_t countmin;
uint32_t counthour;
uint8_t sec;
uint8_t min;
uint8_t hour;
if (xSemaphoreTake(timerSemaphore, 0) == pdTRUE){
// Read the interrupt count and time
//tm.tm_hour, tm.tm_min, tm.tm_sec
getLocalTime(&timeInfo);
display.showNumberDecEx((timeInfo.tm_hour*100)+timeInfo.tm_min, (0x80 >> 1), true); //Hour:Min
//showLocalTime();
}
}
// put function definitions here:
コメント
コメントを投稿