Skip to content

Commit

Permalink
Update BF-018A.ino
Browse files Browse the repository at this point in the history
Corrected leap year calculation: Wrong date in multiples of 400 years and January-February
  • Loading branch information
uehara00 committed May 17, 2021
1 parent a461322 commit 6ee441e
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions BF-018A/BF-018A.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
WiFiManager wm;

// for NTP and time
const long gmt_offset = 3600 * 9; // JST-9
const int gmt_offset = 3600 * 9; // JST-9
const int daylight = 3600 * 0; // No daylight time
const char* ntp_server = "pool.ntp.org";
struct tm td; // time of day .. year, month, day, hour, minute, second
Expand Down Expand Up @@ -76,7 +76,7 @@ const uint32_t ledc_duty_off = 0; // 0
// Ticker for TCO(Time Code Output) generation
const int ticker_period = 100; // 100ms
const int marker = 0xff; // marker code which TcoValue() returns
long last_usec; // last time in microsecond for statistics of Ticker
int last_usec; // last time in microsecond for statistics of Ticker
Ticker tk;

// main task of TCO
Expand All @@ -90,7 +90,7 @@ void TcoGen()
ReportStatus(kNtp, kOn);

gettimeofday(&tv, NULL);
long tv_100ms = tv.tv_usec / 100000L;
int tv_100ms = tv.tv_usec / 100000;
switch (tv_100ms) {
case 0: Tco000ms(); break;
case 2: Tco200ms(); break;
Expand All @@ -100,15 +100,15 @@ void TcoGen()
}

// statistics of tv_usec
long now_usec = tv.tv_usec;
if (now_usec < 100000L)
now_usec += 1000000L; // 0..99999 --> 100000..199999
long tk_deviation = now_usec - last_usec - 100000L; // ticker deviation in micro second
int now_usec = tv.tv_usec;
if (now_usec < 100000)
now_usec += 1000000; // 0..99999 --> 100000..199999
int tk_deviation = now_usec - last_usec - 100000; // ticker deviation in micro second
last_usec = tv.tv_usec;

static long tk_count = 0L;
static long tk_max = 0L;
static long tk_min = 0L;
static int tk_count = 0;
static int tk_max = 0;
static int tk_min = 0;
static double tk_sum = 0.0;
static double tk_sq_sum = 0.0;
++tk_count;
Expand All @@ -119,7 +119,7 @@ void TcoGen()
tk_sum += (double)tk_deviation;
tk_sq_sum += (double)tk_deviation * (double)tk_deviation;

static long tk_distribution[9] = {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L};
static int tk_distribution[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
if (tk_deviation < -50000) ++tk_distribution[0]; // ~ -50ms
else if (tk_deviation < -5000) ++tk_distribution[1]; // ~ -5ms
else if (tk_deviation < -500) ++tk_distribution[2]; // ~ -0.5ms
Expand Down Expand Up @@ -213,7 +213,7 @@ int TcoValue()
int days = td.tm_mday;
for (int i = 0; i < td.tm_mon; ++i) // td.tm_mon starts from 0
days += days_of_month[i];
if ((td.tm_mon >= 2) && ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
if ((td.tm_mon >= 2) && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
++days;
int bcd_days = Int3bcd(days);

Expand Down Expand Up @@ -338,7 +338,7 @@ void setup()

// wait until middle of 100ms timing. ex. 50ms, 150ms, 250ms,..
gettimeofday(&tv, NULL);
delayMicroseconds(150000L - tv.tv_usec % 100000L);
delayMicroseconds(150000 - tv.tv_usec % 100000);

// for the first sample of statistics
gettimeofday(&tv, NULL);
Expand Down

0 comments on commit 6ee441e

Please sign in to comment.