Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events callback #24

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions DCF77.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void DCF77::int0handler() {
// If the detected pulse is too short it will be an
// incorrect pulse that we shall reject as well
if ((flankTime-leadingEdge)<DCFRejectPulseWidth) {
LogLn("rPW");
LogLn("rPW");
return;
}

Expand Down Expand Up @@ -137,36 +137,39 @@ void DCF77::int0handler() {
* Add new bit to buffer
*/
inline void DCF77::appendSignal(unsigned char signal) {
Log(signal, DEC);
runningBuffer = runningBuffer | ((unsigned long long) signal << bufferPosition);
bufferPosition++;
if (bufferPosition > 59) {
// Buffer is full before at end of time-sequence
// this may be due to noise giving additional peaks
LogLn("EoB");
finalizeBuffer();
}
Log(signal, DEC);
if (cb != nullptr) cb->onSignal(signal);
runningBuffer = runningBuffer | ((unsigned long long)signal << bufferPosition);
bufferPosition++;
if (bufferPosition > 59) {
// Buffer is full before at end of time-sequence
// this may be due to noise giving additional peaks
LogLn("EoB");
finalizeBuffer();
}
}

/**
* Finalize filled buffer
*/
inline void DCF77::finalizeBuffer(void) {
if (bufferPosition == 59) {
// Buffer is full
LogLn("BF");
// Prepare filled buffer and time stamp for main loop
filledBuffer = runningBuffer;
filledTimestamp = now();
// Reset running buffer
bufferinit();
FilledBufferAvailable = true;
} else {
// Buffer is not yet full at end of time-sequence
LogLn("EoM");
// Reset running buffer
bufferinit();
}
// Buffer is full
LogLn("BF");
// Prepare filled buffer and time stamp for main loop
filledBuffer = runningBuffer;
filledTimestamp = now();
// Reset running buffer
bufferinit();
FilledBufferAvailable = true;
if (cb != nullptr) cb->onBufferMsg("BF");
} else {
// Buffer is not yet full at end of time-sequence
LogLn("EoM");
if (cb != nullptr) cb->onBufferMsg("EoM");
// Reset running buffer
bufferinit();
}
}

/**
Expand All @@ -181,14 +184,15 @@ bool DCF77::receivedTimeUpdate(void) {
// if buffer is filled, we will process it and see if this results in valid parity
if (!processBuffer()) {
LogLn("Invalid parity");
if (cb != nullptr) cb->onParityError();
return false;
}

// Since the received signal is error-prone, and the parity check is not very strong,
// we will do some sanity checks on the time
time_t processedTime = latestupdatedTime + (now() - processingTimestamp);
if (processedTime<MIN_TIME || processedTime>MAX_TIME) {
LogLn("Time outside of bounds");
if (cb != nullptr) cb->onParityError();
return false;
}

Expand All @@ -197,6 +201,7 @@ bool DCF77::receivedTimeUpdate(void) {
if(difference < 2*SECS_PER_MIN) {
LogLn("close to internal clock");
storePreviousTime();
if (cb != nullptr) cb->onTimeUpdateMsg("Close to internal clock");
return true;
}

Expand All @@ -208,8 +213,10 @@ bool DCF77::receivedTimeUpdate(void) {
storePreviousTime();
if(shiftDifference < 2*SECS_PER_MIN) {
LogLn("time lag consistent");
if (cb != nullptr) cb->onTimeUpdateMsg("Time lag consistent");
return true;
} else {
if (cb != nullptr) cb->onTimeUpdateMsg("Time lag inconsistent");
LogLn("time lag inconsistent");
}

Expand Down Expand Up @@ -270,7 +277,6 @@ bool DCF77::processBuffer(void) {
// Calculate parities for checking buffer
calculateBufferParities();
tmElements_t time;
bool proccessedSucces;

struct DCF77Buffer *rx_buffer;
rx_buffer = (struct DCF77Buffer *)(unsigned long long)&processingBuffer;
Expand Down Expand Up @@ -333,7 +339,11 @@ time_t DCF77::getUTCTime(void)
int DCF77::getSummerTime(void)
{
return (CEST)?1:0;
}
}

void DCF77::setCallBack(DCF77EventsCallback *pCallBack) {
cb = pCallBack;
}

/**
* Initialize parameters
Expand Down Expand Up @@ -366,5 +376,6 @@ time_t DCF77::processingTimestamp= 0;
time_t DCF77::previousProcessingTimestamp=0;
unsigned char DCF77::CEST=0;
DCF77::ParityFlags DCF77::flags = {0,0,0,0};
DCF77EventsCallback *DCF77::cb = nullptr;


15 changes: 14 additions & 1 deletion DCF77.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define DCFSplitTime 180 // Specifications distinguishes pulse width 100 ms and 200 ms. In practice we see 130 ms and 230
#define DCFSyncTime 1500 // Specifications defines 2000 ms pulse for end of sequence

class DCF77EventsCallback;
class DCF77 {
private:

Expand Down Expand Up @@ -93,8 +94,20 @@ class DCF77 {
static void Start(void);
static void Stop(void);
static void int0handler();
static int getSummerTime();
static int getSummerTime();
static void setCallBack(DCF77EventsCallback *pCallBack);

static DCF77EventsCallback *cb;
};

class DCF77EventsCallback {
public:
virtual ~DCF77EventsCallback () {};
virtual void onParityError();
virtual void onBufferMsg(const char * msg);
virtual void onTimeUpdateMsg(const char * msg);
virtual void onSignal(unsigned char signal);
};

#endif

6 changes: 4 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
[
{
"name": "Time",
"frameworks": "arduino"
"frameworks": "arduino",
"versions" : "1.6.1"
},
{
"name": "Timezone",
"frameworks": "arduino"
"frameworks": "arduino",
"versions" : "1.2.4"
}
],
"frameworks": "arduino"
Expand Down
4 changes: 2 additions & 2 deletions utility/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace Utils {
#define DEBUG_BLINK_PIN 13 // Connected to debug led
//#define VERBOSE_DEBUG 1 // Verbose

void LogLn(char*s)
void LogLn(const char*s)
{
#ifdef VERBOSE_DEBUG
Serial.println(s);
#endif
}

void Log(char*s)
void Log(const char*s)
{
#ifdef VERBOSE_DEBUG
Serial.print(s);
Expand Down
4 changes: 2 additions & 2 deletions utility/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#define intRestore(sreg) SREG = sreg

namespace Utils {
void Log(char*s);
void LogLn(char*s);
void Log(const char*s);
void LogLn(const char*s);
void Log(int i,char format);
void LogLn(int i,char format);
void Log(int i);
Expand Down