Skip to content

Instantly share code, notes, and snippets.

@NeoCat
Created July 23, 2020 22:07
Show Gist options
  • Save NeoCat/b8f1a19c93d30ecc47ae9a005b0cad7b to your computer and use it in GitHub Desktop.
Save NeoCat/b8f1a19c93d30ecc47ae9a005b0cad7b to your computer and use it in GitHub Desktop.
JJY Emulator using M5Stick-C
#include <M5StickC.h>
#include <WiFi.h>
#include <Time.h>
const char ssid[] = "<YOUR SSID>";
const char pass[] = "***********";
#define GPIO_PIN 10 /* JJY擬似信号を出力するピン */
#define LEDC_BASE_FREQ 60000.0 /* JJY擬似信号の周波数 */
#define LEDC_CHANNEL_0 0
#define LEDC_TIMER_BIT 8
#define SECS_PER_DAY (24*60*60)
byte timecode[60];
void setup()
{
M5.begin();
Serial.begin(115200);
M5.Axp.ScreenBreath(8);
M5.Lcd.setRotation(3);
WiFi.begin();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 30000) {
Serial.print(".");
delay(1000);
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("failed to connect WiFi.");
delay(1000);
ESP.restart();
}
Serial.println("");
configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
delay(1000);
}
void loop()
{
time_t t;
struct tm *tm;
static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};
time_t wait_start = time(NULL);
while (wait_start == time(NULL)) delay(1); // wait until time is corrected
t = time(NULL);
tm = localtime(&t);
unsigned long startTime = millis();
pinMode(GPIO_PIN, OUTPUT);
ledcAttachPin(GPIO_PIN, LEDC_CHANNEL_0);
ledcWriteTone(LEDC_CHANNEL_0, LEDC_BASE_FREQ);
// print out current time
Serial.printf("%04d/%02d/%02d(%s) %02d:%02d:%02d %d\r\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday],
tm->tm_hour, tm->tm_min, tm->tm_sec, dayOfYear(tm));
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.setCursor(0, 0, 4);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("%04d/%02d/%02d\n%s\n%02d:%02d:%02d %d",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday],
tm->tm_hour, tm->tm_min, tm->tm_sec, dayOfYear(tm));
// calc signal duration (ms)
int ms = calcTimeCodeDuration(tm);
// wait ms and stop PWM
while (millis() - startTime < ms) delay(1);
ledcDetachPin(GPIO_PIN);
pinMode(GPIO_PIN, INPUT);
}
//=========================== JJY ===========================
unsigned int calcTimeCodeDuration(struct tm *tm)
{
int s = tm->tm_sec;
if (s == 0 || !timecode[0])
setupTimeCode(tm);
return timecode[s] * 100;
}
void setupTimeCode(struct tm *tm)
{
int i;
memset(timecode, 8, sizeof(timecode));
setupTimeCode100(tm->tm_min, 0);
timecode[0] = 2;
setupTimeCode100(tm->tm_hour, 10);
int d = dayOfYear(tm);
setupTimeCode100(d/10, 20);
setupTimeCode100(d%10*10, 30);
int parity1 = 0, parity2 = 0;
for (i = 12; i < 20; i++) parity1 ^= timecode[i] == 5;
for (i = 1; i < 10; i++) parity2 ^= timecode[i] == 5;
timecode[36] = parity1 ? 5 : 8;
timecode[37] = parity2 ? 5 : 8;
setupTimeCode100(tm->tm_year % 100, 40);
for (i = 44; i > 40; i--)
timecode[i] = timecode[i-1];
timecode[40] = 8;
int w = tm->tm_wday;
timecode[50] = (w & 4) ? 5 : 8;
timecode[51] = (w & 2) ? 5 : 8;
timecode[52] = (w & 1) ? 5 : 8;
timecode[59] = 2;
/* dump */
for (i = 0; i < 60; i++) {
Serial.print(timecode[i], DEC);
Serial.print(i % 10 == 9 ? "\r\n" : " ");
}
}
void setupTimeCode100(int m, int i)
{
timecode[i+0] = ((m/10) & 8) ? 5 : 8;
timecode[i+1] = ((m/10) & 4) ? 5 : 8;
timecode[i+2] = ((m/10) & 2) ? 5 : 8;
timecode[i+3] = ((m/10) & 1) ? 5 : 8;
timecode[i+4] = 8;
timecode[i+5] = ((m%10) & 8) ? 5 : 8;
timecode[i+6] = ((m%10) & 4) ? 5 : 8;
timecode[i+7] = ((m%10) & 2) ? 5 : 8;
timecode[i+8] = ((m%10) & 1) ? 5 : 8;
timecode[i+9] = 2;
}
int dayOfYear(struct tm *tm)
{
struct tm tm0 = *tm;
tm0.tm_sec = 0;
tm0.tm_min = 0;
tm0.tm_hour = 0;
tm0.tm_mday = 1;
tm0.tm_mon = 0;
time_t t0 = mktime(&tm0);
return (time(NULL) - t0) / SECS_PER_DAY + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment