M5Stack Core2: RTC を使った時計

![IMG_20210903_155614.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/afd4d6cb-e3c7-b387-cf41-4facb67e2c10.jpeg)

こちらのプログラムを改造して、RTC の割り込みを使うようにしました。
省エネの為、画面下部のボタンを押して 10秒間、時刻を表示するようにしました。
[M5Stack Core 2: NTP で時刻を合わせる](https://qiita.com/ekzemplaro/items/1b9bb452af58eb48cfb8)

フォルダー構造

```text
$ tree clock01/
clock01/
├── clock01.ino
└── ntp.ino
```

```text:clock01.ino
// ---------------------------------------------------------------
/*
	clock01.ino

					Sep/03/2021

*/
// ---------------------------------------------------------------
#include <M5Core2.h>
#include <WiFiMulti.h>
#include <time.h>

// for WiFi
char ssid[] = "some-ssid";
char password[] = "some-password";
WiFiMulti wifiMulti;

RTC_DateTypeDef RTC_DateStruct; // Data
RTC_TimeTypeDef RTC_TimeStruct; // Time

hw_timer_t *timer1 = NULL;  

boolean flag_display = false;
int sec_display = 10;
// ---------------------------------------------------------------
void IRAM_ATTR onTimer_proc()
{
	int iix = millis() / 100;

	Serial.println("*** onTimer_proc *** " + String(iix));

	if (0 < sec_display)
		{
		flag_display = true;
		sec_display--;
		}
}

// ---------------------------------------------------------------
void setup()
{
	M5.begin();
	M5.Lcd.fillScreen(BLACK);
	M5.Lcd.setTextColor(WHITE,BLACK);

	wifiMulti.addAP(ssid, password);

	timer1 = timerBegin(0, 80, true); 

	timerAttachInterrupt(timer1, &onTimer_proc, true); 

	timerAlarmWrite(timer1, 1000000, true); 

	timerAlarmEnable(timer1);

	Serial.println("*** setup *** end ***");
}

// ---------------------------------------------------------------
void loop()
{
	// for Touch
	TouchPoint_t pos= M5.Touch.getPressPoint();

	if(240 < pos.y)
		{
		sec_display = 10;
		if(pos.x < 109)
			M5.Lcd.setTextColor(RED,BLACK);
		else if(109 <= pos.x && pos.x <= 218)
			M5.Lcd.setTextColor(GREEN,BLACK);
		else if(218 < pos.x)
			M5.Lcd.setTextColor(WHITE,BLACK);
	}

	if ((flag_display) && (0 < sec_display))
		{
		display_time_proc();
		delay(100);

		flag_display = false;
		}
	else if (sec_display == 0)
		{
		M5.Lcd.fillScreen(BLACK);
		}
}

// ---------------------------------------------------------------
void display_time_proc()
{
	const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};

	M5.Rtc.GetDate(&RTC_DateStruct);
	M5.Rtc.GetTime(&RTC_TimeStruct);

	M5.Lcd.setTextSize(3);
	M5.Lcd.setCursor(10, 60, 1);
	M5.Lcd.printf("%04d.%02d.%02d %s\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, wd[RTC_DateStruct.WeekDay]);
 M5.Lcd.setTextSize(5);
	M5.Lcd.setCursor(30, 140, 1);
	M5.Lcd.printf("%02d:%02d:%02d", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);

}

// ---------------------------------------------------------------
```

```text:ntp.ino
// ---------------------------------------------------------------
/*
	ntp.ino

					Sep/03/2021

*/
// ---------------------------------------------------------------
void setNTP2RTC()
{
	const char* ntpServer = "ntp.jst.mfeed.ad.jp";
	const long  gmtOffset_sec = 9 * 3600;
	const int   daylightOffset_sec = 0;

	struct tm timeinfo;

	configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
	while (!getLocalTime(&timeinfo))
		{
		delay(1000);
		}

	getLocalTime(&timeinfo);
	// read RTC
	M5.Rtc.GetTime(&RTC_TimeStruct);
	M5.Rtc.GetDate(&RTC_DateStruct);

	// --- to over write date&time
	RTC_DateStruct.Year = timeinfo.tm_year + 1900;
	RTC_DateStruct.Month = timeinfo.tm_mon + 1;
	RTC_DateStruct.Date = timeinfo.tm_mday;
	RTC_DateStruct.WeekDay = timeinfo.tm_wday;
	M5.Rtc.SetDate(&RTC_DateStruct);
	RTC_TimeStruct.Hours = timeinfo.tm_hour;
	RTC_TimeStruct.Minutes = timeinfo.tm_min;
	RTC_TimeStruct.Seconds = timeinfo.tm_sec;
	M5.Rtc.SetTime(&RTC_TimeStruct);
}

// ---------------------------------------------------------------
```