ESP32-PaxCounter/src/dcf77.cpp

151 lines
4.0 KiB
C++
Raw Normal View History

2019-02-04 20:02:30 +01:00
/*
// Emulate a DCF77 radio receiver
//
2019-02-05 23:50:05 +01:00
// a nice & free logic test program for DCF77 can be found here:
https://www-user.tu-chemnitz.de/~heha/viewzip.cgi/hs/Funkuhr.zip/
//
*/
#ifdef HAS_DCF77
#include "dcf77.h"
// Local logging tag
static const char TAG[] = "main";
// array of dcf pulses for three minutes
2019-02-16 15:02:07 +01:00
uint8_t DCFpulse[DCF77_FRAME_SIZE];
2019-02-04 20:02:30 +01:00
2019-02-16 15:02:07 +01:00
// called by timepulse interrupt to ticker out DCF signal
void DCF_Pulse(time_t startTime) {
2019-02-04 20:02:30 +01:00
2019-02-16 15:02:07 +01:00
static uint8_t current_second = second(startTime);
2019-02-04 20:02:30 +01:00
static uint8_t pulse = 0;
2019-02-16 15:02:07 +01:00
static bool SecondsPending = false;
if (!SecondsPending) {
// prepare dcf timeframe to send for next minute
DCF77_Frame(now() + DCF77_FRAME_SIZE + 1);
ESP_LOGD(TAG, "DCF77 minute %d", minute(now() + DCF77_FRAME_SIZE + 1));
// begin output of dcf timeframe
SecondsPending = true;
2019-02-04 20:02:30 +01:00
}
2019-02-04 23:42:17 +01:00
// ticker out current DCF frame
2019-02-16 15:02:07 +01:00
if (SecondsPending) {
2019-02-04 20:02:30 +01:00
switch (pulse++) {
2019-02-04 20:02:30 +01:00
case 0: // start of second -> start of timeframe for logic signal
2019-02-16 15:02:07 +01:00
if (DCFpulse[current_second] != dcf_off)
2019-02-05 23:50:05 +01:00
set_DCF77_pin(dcf_low);
2019-02-16 15:02:07 +01:00
ESP_LOGD(TAG, "DCF77 bit %d", current_second);
2019-02-04 23:42:17 +01:00
break;
2019-02-04 20:02:30 +01:00
case 1: // 100ms after start of second -> end of timeframe for logic 0
2019-02-16 15:02:07 +01:00
if (DCFpulse[current_second] == dcf_zero)
2019-02-05 23:50:05 +01:00
set_DCF77_pin(dcf_high);
2019-02-04 23:42:17 +01:00
break;
2019-02-04 20:02:30 +01:00
case 2: // 200ms after start of second -> end of timeframe for logic 1
2019-02-05 23:50:05 +01:00
set_DCF77_pin(dcf_high);
2019-02-04 23:42:17 +01:00
break;
2019-02-07 07:32:55 +01:00
case 9: // 900ms after start -> last pulse before next second starts
2019-02-04 20:02:30 +01:00
pulse = 0;
2019-02-16 15:02:07 +01:00
if (current_second++ >=
(DCF77_FRAME_SIZE - 1)) // end of DCF77 frame (59th second)
2019-02-04 23:42:17 +01:00
{
2019-02-16 15:02:07 +01:00
current_second = 0;
SecondsPending = false;
2019-02-04 20:02:30 +01:00
};
break;
2019-02-04 20:02:30 +01:00
}; // switch
2019-02-04 23:42:17 +01:00
}; // if
2019-02-16 15:02:07 +01:00
} // DCF_Pulse()
2019-02-04 20:02:30 +01:00
2019-02-05 23:50:05 +01:00
// helper function to convert decimal to bcd digit
uint8_t dec2bcd(uint8_t dec, uint8_t startpos, uint8_t endpos,
uint8_t pArray[]) {
uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10);
uint8_t parity = 0;
for (uint8_t n = startpos; n <= endpos; n++) {
pArray[n] = (data & 1) ? dcf_one : dcf_zero;
parity += (data & 1);
data >>= 1;
}
return parity;
}
2019-02-16 15:02:07 +01:00
void DCF77_Frame(time_t tt) {
2019-02-07 20:39:32 +01:00
2019-02-16 15:02:07 +01:00
uint8_t Parity;
2019-02-07 20:39:32 +01:00
time_t t = myTZ.toLocal(tt); // convert to local time
// ENCODE HEAD
// bits 0..19 initialized with zeros
for (int n = 0; n <= 19; n++)
2019-02-16 15:02:07 +01:00
DCFpulse[n] = dcf_zero;
2019-02-07 20:39:32 +01:00
// bits 17..18: adjust for DayLightSaving
2019-02-16 15:02:07 +01:00
DCFpulse[18 - (myTZ.locIsDST(t) ? 1 : 0)] = dcf_one;
2019-02-07 20:39:32 +01:00
// bit 20: must be 1 to indicate time active
2019-02-16 15:02:07 +01:00
DCFpulse[20] = dcf_one;
2019-02-07 20:39:32 +01:00
// ENCODE MINUTE (bits 21..28)
2019-02-16 15:02:07 +01:00
Parity = dec2bcd(minute(t), 21, 27, DCFpulse);
DCFpulse[28] = (Parity & 1) ? dcf_one : dcf_zero;
2019-02-07 20:39:32 +01:00
// ENCODE HOUR (bits 29..35)
2019-02-16 15:02:07 +01:00
Parity = dec2bcd(hour(t), 29, 34, DCFpulse);
DCFpulse[35] = (Parity & 1) ? dcf_one : dcf_zero;
2019-02-07 20:39:32 +01:00
// ENCODE DATE (bits 36..58)
2019-02-16 15:02:07 +01:00
Parity = dec2bcd(day(t), 36, 41, DCFpulse);
Parity += dec2bcd((weekday(t) - 1) ? (weekday(t) - 1) : 7, 42, 44, DCFpulse);
Parity += dec2bcd(month(t), 45, 49, DCFpulse);
Parity += dec2bcd(year(t) - 2000, 50, 57,
DCFpulse); // yes, we have a millenium 3000 bug here ;-)
DCFpulse[58] = (Parity & 1) ? dcf_one : dcf_zero;
2019-02-07 20:39:32 +01:00
// ENCODE TAIL (bit 59)
2019-02-16 15:02:07 +01:00
DCFpulse[59] = dcf_off;
2019-02-07 20:39:32 +01:00
// !! missing code here for leap second !!
/*
// for debug: print the DCF77 frame buffer
char out[DCF77_FRAME_SIZE + 1];
uint8_t i;
for (i = 0; i < DCF77_FRAME_SIZE; i++) {
2019-02-16 15:02:07 +01:00
out[i] = DCFpulse[i] + '0'; // convert int digit to printable ascii
2019-02-07 20:39:32 +01:00
}
out[DCF77_FRAME_SIZE] = '\0'; // string termination char
ESP_LOGD(TAG, "DCF Timeframe = %s", out);
*/
}
2019-02-05 23:50:05 +01:00
// helper function to switch GPIO line with DCF77 signal
void set_DCF77_pin(dcf_pinstate state) {
switch (state) {
case dcf_low:
#ifdef DCF77_ACTIVE_LOW
digitalWrite(HAS_DCF77, HIGH);
#else
digitalWrite(HAS_DCF77, LOW);
#endif
break;
case dcf_high:
#ifdef DCF77_ACTIVE_LOW
digitalWrite(HAS_DCF77, LOW);
#else
digitalWrite(HAS_DCF77, HIGH);
#endif
break;
} // switch
} // DCF77_pulse
2019-02-16 15:02:07 +01:00
// helper function calculates next minute
#endif // HAS_DCF77