Skip to content

Commit

Permalink
Merge pull request #845 from cyberman54/development
Browse files Browse the repository at this point in the history
v3.1.0
  • Loading branch information
cyberman54 authored Jan 24, 2022
2 parents 7e6e160 + 529059e commit 96c5912
Show file tree
Hide file tree
Showing 23 changed files with 288 additions and 358 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,17 +569,17 @@ Send for example `83` `86` as Downlink on Port 2 to get battery status and time/
bytes 1..4 = time/date in UTC epoch seconds (LSB)
byte 5 = time source & status, see below

bits 0..3 last seen time source
bits 0..3 time source
0x00 = GPS
0x01 = RTC
0x02 = LORA
0x03 = unsynched (never synched)
0x03 = unsynched
0x04 = set (source unknown)

bits 4..7 time status
0x00 = timeNotSet (never synched)
0x01 = timeNeedsSync (last sync failed)
0x02 = timeSet (synched)
bits 4..7 esp32 sntp time status
0x00 = SNTP_SYNC_STATUS_RESET
0x01 = SNTP_SYNC_STATUS_COMPLETED
0x02 = SNTP_SYNC_STATUS_IN_PROGRESS

0x87 sync time/date

Expand Down
12 changes: 3 additions & 9 deletions include/dcf77.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@
#include "globals.h"
#include "timekeeper.h"

#define DCF77_FRAME_SIZE (60)
#define DCF77_PULSE_LENGTH (100)
#define set_dcfbit(b) (1ULL << (b))

#ifdef DCF77_ACTIVE_LOW
enum dcf_pinstate { dcf_high, dcf_low };
#else
enum dcf_pinstate { dcf_low, dcf_high };
#endif

enum DCF77_Pulses { dcf_Z, dcf_0, dcf_1 };

void DCF77_Pulse(time_t t, uint8_t const *DCFpulse);
uint8_t *IRAM_ATTR DCF77_Frame(time_t const t);
uint8_t IRAM_ATTR dec2bcd(uint8_t const dec, uint8_t const startpos, uint8_t const endpos,
uint8_t *DCFpulse);
uint8_t IRAM_ATTR setParityBit(uint8_t const p);
void DCF77_Pulse(uint8_t bit);
uint64_t DCF77_Frame(const struct tm t);

#endif
4 changes: 2 additions & 2 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <Arduino.h>

// Time functions
#include <ezTime.h>
#include <RtcDateTime.h>
#include <Ticker.h>

Expand Down Expand Up @@ -111,6 +110,7 @@ typedef struct {
float pm25;
} sdsStatus_t;

extern char clientId[20]; // unique clientID
extern char clientId[20]; // unique clientID
extern time_t _COMPILETIME; // epoch build time

#endif
1 change: 0 additions & 1 deletion include/gpsread.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ bool gps_hasfix();
void gps_storelocation(gpsStatus_t *gps_store);
void gps_loop(void *pvParameters);
time_t get_gpstime(uint16_t *msec);
time_t get_gpstime(void);

#endif
3 changes: 2 additions & 1 deletion include/if482.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

#include "globals.h"
#include "timekeeper.h"
#include "esp_sntp.h"

#define IF482_FRAME_SIZE (17)
#define IF482_SYNC_FIXUP (10) // calibration to fixup processing time [milliseconds]

String IRAM_ATTR IF482_Frame(time_t t);
String IF482_Frame(time_t t);

#endif
17 changes: 0 additions & 17 deletions include/mobaserial.h

This file was deleted.

1 change: 1 addition & 0 deletions include/rcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "sensor.h"
#include "cyclic.h"
#include "timekeeper.h"
#include "esp_sntp.h"
#include "timesync.h"
#include "power.h"
#include "antenna.h"
Expand Down
11 changes: 8 additions & 3 deletions include/timekeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
#include "gpsread.h"
#include "if482.h"
#include "dcf77.h"
#include "esp_sntp.h"

#define SECS_YR_2000 (946684800UL) // the time at the start of y2k
#define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch
#define LEAP_SECS_SINCE_GPSEPOCH 18UL // state of 2021

enum timesource_t { _gps, _rtc, _lora, _unsynced, _set };

extern const char timeSetSymbols[];
extern Ticker timesyncer;
extern timesource_t timeSource;
extern TaskHandle_t ClockTask;
extern Timezone myTZ;
extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC
extern hw_timer_t *ppsIRQ;

Expand All @@ -25,11 +29,12 @@ void clock_loop(void *pvParameters);
void timepulse_start(void);
void setTimeSyncIRQ(void);
uint8_t timepulse_init(void);
time_t timeIsValid(time_t const t);
bool timeIsValid(time_t const t);
void calibrateTime(void);
void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
timesource_t mytimesource);
time_t compiledUTC(void);
time_t compileTime(const String compile_date);
time_t mkgmtime(const struct tm *ptm);
TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config,
int8_t rxPin, int8_t txPins);

Expand Down
2 changes: 0 additions & 2 deletions include/timesync.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#define TIME_SYNC_FIXUP 25 // compensation for processing time [milliseconds]
#define TIME_SYNC_MAX_SEQNO 0xfe // threshold for wrap around time_sync_seqNo
#define TIME_SYNC_END_FLAG (TIME_SYNC_MAX_SEQNO + 1) // end of handshake marker
#define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch
#define LEAP_SECS_SINCE_GPSEPOCH 18UL // state of 2021

enum timesync_t {
timesync_tx,
Expand Down
3 changes: 1 addition & 2 deletions platformio_orig.ini
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ description = Paxcounter is a device for metering passenger flows in realtime. I

[common]
; for release_version use max. 10 chars total, use any decimal format like "a.b.c"
release_version = 3.0.3
release_version = 3.1.0
; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running!
; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose
debug_level = 3
Expand Down Expand Up @@ -82,7 +82,6 @@ lib_deps_basic =
https://github.com/dbSuS/libpax.git @ ^1.0.0
https://github.com/SukkoPera/Arduino-Rokkit-Hash.git
bblanchon/ArduinoJson @ ^6
ropg/ezTime @ ^0.8.3
makuna/RTC @ ^2.3.5
spacehuhn/SimpleButton
lewisxhe/AXP202X_Library @ ^1.1.3
Expand Down
124 changes: 58 additions & 66 deletions src/dcf77.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,104 +17,96 @@
static const char TAG[] = __FILE__;

// triggered by second timepulse to ticker out DCF signal
void DCF77_Pulse(time_t t, uint8_t const *DCFpulse) {
void DCF77_Pulse(uint8_t bit) {

TickType_t startTime = xTaskGetTickCount();
uint8_t sec = myTZ.second(t);

ESP_LOGD(TAG, "[%s] DCF second: %d", myTZ.dateTime("H:i:s.v").c_str(), sec);
TickType_t startTime;

// induce a DCF Pulse
for (uint8_t pulse = 0; pulse <= 2; pulse++) {
for (uint8_t pulseLength = 0; pulseLength <= 2; pulseLength++) {

startTime = xTaskGetTickCount(); // reference time pulse start

switch (pulse) {
switch (pulseLength) {

case 0: // start of second -> start of timeframe for logic signal
if (DCFpulse[sec] != dcf_Z)
digitalWrite(HAS_DCF77, dcf_low);
case 0: // 0ms = start of pulse
digitalWrite(HAS_DCF77, dcf_low);
break;

case 1: // 100ms after start of second -> end of timeframe for logic 0
if (DCFpulse[sec] == dcf_0)
case 1: // 100ms = logic 0
if (bit == 0)
digitalWrite(HAS_DCF77, dcf_high);
break;

case 2: // 200ms after start of second -> end of timeframe for logic 1
case 2: // 200ms = logic 1
digitalWrite(HAS_DCF77, dcf_high);
break;

} // switch

// pulse pause
vTaskDelayUntil(&startTime, pdMS_TO_TICKS(DCF77_PULSE_LENGTH));
// delay to genrate pulseLength
vTaskDelayUntil(&startTime, pdMS_TO_TICKS(100));

} // for
} // DCF77_Pulse()

uint8_t *IRAM_ATTR DCF77_Frame(time_t const t) {

// array of dcf pulses for one minute, secs 0..16 and 20 are never touched, so
// we keep them statically to avoid same recalculation every minute

static uint8_t DCFpulse[DCF77_FRAME_SIZE + 1] = {
dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0,
dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0,
dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_1};

uint8_t Parity;
// helper function to convert decimal to bcd digit
uint64_t dec2bcd(uint8_t const dec, uint8_t const startpos,
uint8_t const endpos, uint8_t *odd_parity) {

// ENCODE DST CHANGE ANNOUNCEMENT (Sec 16)
DCFpulse[16] = dcf_0; // not yet implemented
uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10);
uint64_t bcd = 0;

// ENCODE DAYLIGHTSAVING (secs 17..18)
DCFpulse[17] = myTZ.isDST(t) ? dcf_1 : dcf_0;
DCFpulse[18] = myTZ.isDST(t) ? dcf_0 : dcf_1;
*odd_parity = 0;
for (uint8_t i = startpos; i <= endpos; i++) {
bcd += (data & 1) ? set_dcfbit(i) : 0;
*odd_parity += (data & 1);
data >>= 1;
}
*odd_parity %= 2;

// ENCODE MINUTE (secs 21..28)
Parity = dec2bcd(myTZ.minute(t), 21, 27, DCFpulse);
DCFpulse[28] = setParityBit(Parity);
return bcd;
}

// ENCODE HOUR (secs 29..35)
Parity = dec2bcd(myTZ.hour(t), 29, 34, DCFpulse);
DCFpulse[35] = setParityBit(Parity);
// generates a 1 minute dcf pulse frame for calendar time t
uint64_t DCF77_Frame(const struct tm t) {

// ENCODE DATE (secs 36..58)
Parity = dec2bcd(myTZ.day(t), 36, 41, DCFpulse);
Parity += dec2bcd((myTZ.weekday(t) - 1) ? (myTZ.weekday(t) - 1) : 7, 42, 44,
DCFpulse);
Parity += dec2bcd(myTZ.month(t), 45, 49, DCFpulse);
Parity += dec2bcd(myTZ.year(t) - 2000, 50, 57, DCFpulse);
DCFpulse[58] = setParityBit(Parity);
uint8_t parity = 0, parity_sum = 0;
uint64_t frame = 0; // start with all bits 0

// ENCODE MARK (sec 59)
DCFpulse[59] = dcf_Z; // !! missing code here for leap second !!
// DST CHANGE ANNOUNCEMENT (16)
// -- not implemented --

// timestamp this frame with it's minute
DCFpulse[60] = myTZ.minute(t);
// DAYLIGHTSAVING (17, 18)
// "01" = MEZ / "10" = MESZ
frame += t.tm_isdst > 0 ? set_dcfbit(17) : set_dcfbit(18);

return DCFpulse;
// LEAP SECOND (19)
// -- not implemented --

} // DCF77_Frame()
// BEGIN OF TIME INFORMATION (20)
frame += set_dcfbit(20);

// helper function to convert decimal to bcd digit
uint8_t IRAM_ATTR dec2bcd(uint8_t const dec, uint8_t const startpos,
uint8_t const endpos, uint8_t *DCFpulse) {
// MINUTE (21..28)
frame += dec2bcd(t.tm_min, 21, 27, &parity);
frame += parity ? set_dcfbit(28) : 0;

uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10);
uint8_t parity = 0;
// HOUR (29..35)
frame += dec2bcd(t.tm_hour, 29, 34, &parity);
frame += parity ? set_dcfbit(35) : 0;

for (uint8_t i = startpos; i <= endpos; i++) {
DCFpulse[i] = (data & 1) ? dcf_1 : dcf_0;
parity += (data & 1);
data >>= 1;
}
// DATE (36..58)
frame += dec2bcd(t.tm_mday, 36, 41, &parity);
parity_sum += parity;
frame += dec2bcd((t.tm_wday == 0) ? 7 : t.tm_wday, 42, 44, &parity);
parity_sum += parity;
frame += dec2bcd(t.tm_mon + 1, 45, 49, &parity);
parity_sum += parity;
frame += dec2bcd(t.tm_year + 1900 - 2000, 50, 57, &parity);
parity_sum += parity;
frame += parity_sum % 2 ? set_dcfbit(58) : 0;

return parity;
}
return frame;

// helper function to encode parity
uint8_t IRAM_ATTR setParityBit(uint8_t const p) {
return ((p & 1) ? dcf_1 : dcf_0);
}
} // DCF77_Frame()

#endif // HAS_DCF77
18 changes: 14 additions & 4 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ void dp_drawPage(bool nextpage) {
// nextpage = true -> flip 1 page

static uint8_t DisplayPage = 0;
char timeState;
char timeState, strftime_buf[64];
time_t now;
struct tm timeinfo = {0};

#if (HAS_GPS)
static bool wasnofix = true;
#endif
Expand Down Expand Up @@ -320,11 +323,15 @@ void dp_drawPage(bool nextpage) {
dp_println();

// line 6: time + date
// 27.Feb 2019 20:27:00*
// Wed Jan 12 21:49:08 *

#if (TIME_SYNC_INTERVAL)
timeState = TimePulseTick ? ' ' : timeSetSymbols[timeSource];
TimePulseTick = false;
dp_printf("%s", myTZ.dateTime("d.M Y H:i:s").c_str());
time(&now);
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
dp_printf("%.20s", strftime_buf);

// display inverse timeState if clock controller is enabled
#if (defined HAS_DCF77) || (defined HAS_IF482)
Expand Down Expand Up @@ -442,7 +449,10 @@ void dp_drawPage(bool nextpage) {

dp_setFont(MY_FONT_LARGE);
dp_setTextCursor(0, 4);
dp_printf("%s", myTZ.dateTime("H:i:s").c_str());
time(&now);
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%T", &timeinfo);
dp_printf("%.8s", strftime_buf);
break;

// ---------- page 5: pax graph ----------
Expand Down
Loading

0 comments on commit 96c5912

Please sign in to comment.