Skip to content

Instantly share code, notes, and snippets.

@atotto
Created April 28, 2021 14:15
Show Gist options
  • Save atotto/ad298b6c0f134db2a8a57bea77b833b7 to your computer and use it in GitHub Desktop.
Save atotto/ad298b6c0f134db2a8a57bea77b833b7 to your computer and use it in GitHub Desktop.
#include <M5Stack.h>
#include <WiFi.h>
#include "lwip/apps/sntp.h"
#define TFT_GREY 0x5AEB
const char *ssid = "ssid";
const char *password = "password";
byte clock_ss_render_xpos;
byte unixtime_render_xpos;
struct tm timeInfoUTC;
struct tm timeInfoJST;
struct timeval tv;
uint8_t old_ss = 60;
const long JST = 9 * 3600L;
const long UTC = 0;
#define GFXFF 1
// https://github.com/m5stack/M5Stack/tree/master/src/Fonts/GFXFF
#define FONT_24 &FreeMonoBold24pt7b
#define FONT_18 &FreeMonoBold18pt7b
void setup(void) {
M5.begin();
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.setBrightness(3);
M5.Lcd.drawString("connecting", 20, 20, GFXFF);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
if (WiFi.begin(ssid, password) != WL_DISCONNECTED) {
ESP.restart();
}
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
M5.Lcd.fillScreen(TFT_BLACK);
setupTime();
initializeTime();
M5.Lcd.setFreeFont(FONT_24);
clock_ss_render_xpos = M5.Lcd.textWidth("00:00", GFXFF);
M5.Lcd.setFreeFont(FONT_18);
unixtime_render_xpos = M5.Lcd.textWidth("00000000", GFXFF);
}
void setupTime() {
tcpip_adapter_init();
if (sntp_enabled()) {
sntp_stop();
}
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "ntp.nict.jp");
sntp_setservername(1, "time.google.com");
sntp_init();
// UTC
setenv("TZ", "UTC", 1);
tzset();
}
void initializeTime() {
uint32_t start = millis();
time_t now;
time(&now);
struct tm info;
localtime_r(&now, &info);
while ((millis() - start) <= 1000) {
if (info.tm_year > (2016 - 1900)) {
break;
}
delay(10);
}
}
void getTime(struct tm* info_utc, struct tm* info_jst)
{
time_t now;
time(&now);
localtime_r(&now, info_utc);
time_t jst_now = now + (3600 * 9); // JST
localtime_r(&jst_now , info_jst);
}
void rendarClock(struct tm* timeInfo, int ypos) {
int xpos = 70;
// int ysecs = ypos + 36;
int ysecs = ypos + 10;
uint8_t hh = timeInfo->tm_hour;
uint8_t mm = timeInfo->tm_min;
uint8_t ss = timeInfo->tm_sec;
// rendar hh:mm
if (ss == 0 || old_ss == 60) {
char hhmmStr[6];
sprintf(hhmmStr, "%02d:%02d", hh, mm);
M5.Lcd.setFreeFont(FONT_24);
M5.Lcd.drawString(hhmmStr, xpos, ypos, GFXFF);
}
// rendar ss
char ssStr[4];
sprintf(ssStr, ":%02d", ss);
M5.Lcd.setFreeFont(FONT_18);
M5.Lcd.drawString(ssStr, clock_ss_render_xpos + xpos, ysecs, GFXFF);
}
void rendarUnixTime(timeval* tv, int ypos) {
int xpos = 60;
int64_t unixtime_sec = tv->tv_sec;
M5.Lcd.setFreeFont(FONT_18);
if (unixtime_sec % 100 == 0 || old_ss == 60) {
M5.Lcd.drawNumber(unixtime_sec / 100, xpos, ypos, GFXFF);
}
char ssStr[3];
sprintf(ssStr, "%02d", unixtime_sec % 100);
M5.Lcd.drawString(ssStr, unixtime_render_xpos + xpos + 3, ypos, GFXFF);
}
void loop() {
getTime(&timeInfoUTC, &timeInfoJST);
uint8_t ss = timeInfoUTC.tm_sec;
gettimeofday(&tv, NULL);
int64_t unixtime_sec = tv.tv_sec;
if (old_ss != ss) {
rendarClock(&timeInfoUTC, 0);
rendarClock(&timeInfoJST, 95);
rendarUnixTime(&tv, 190);
old_ss = ss;
}
}
@atotto
Copy link
Author

atotto commented Apr 28, 2021

m5-clock

clock with:

  • UTC
  • JST
  • UnixTime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment