void DCF77::appendSignal(unsigned char signal) { #ifdef DCF_DEBUG Serial.print(", appending value "); Serial.print(signal); Serial.print(" at position "); Serial.println(bufferPosition); #endif dcf_rx_buffer = dcf_rx_buffer | ((unsigned long long) signal << bufferPosition); bufferPosition++; if (bufferPosition > 59) { finalizeBuffer(); } }
/* * * Append a signal to the dcf_rx_buffer. Argument can be 1 or 0. An internal * counter shifts the writing position within the buffer. If position > 59, * a new minute begins -> time to call finalizeBuffer(). */ void appendSignal(uint8_t signal) { dcf_rx_buffer = dcf_rx_buffer | ((unsigned long long) signal << bufferPosition); // Update the parity bits. First: Reset when minute, hour or date starts. if (bufferPosition == 21 || bufferPosition == 29 || bufferPosition == 36) { flags.parity_flag = 0; } // Save the parity when the corresponding segment ends if (bufferPosition == 28) { flags.parity_min = flags.parity_flag; }; if (bufferPosition == 35) { flags.parity_hour = flags.parity_flag; }; if (bufferPosition == 58) { flags.parity_date = flags.parity_flag; }; // When we received a 1, toggle the parity flag if (signal == 1) { flags.parity_flag = flags.parity_flag ^ 1; } bufferPosition++; if (bufferPosition > 59) { finalizeBuffer(); } }
/** * Evaluates the signal as it is received. */ void scanSignal(void) { if (DCFSignalState == 1) { int thisFlankTime = millis(); if (thisFlankTime - previousFlankTime > DCF_sync_millis) { finalizeBuffer(); } else if (thisFlankTime - previousFlankTime < 300) { bufferPosition--; if (bufferPosition < 0) bufferPosition = 0; } if (thisFlankTime - previousFlankTime > 300) { previousFlankTime = thisFlankTime; } } else { int difference = millis() - previousFlankTime; if (difference < DCF_split_millis) { appendSignal(0); } else { appendSignal(1); } } }
int DCF77::scanSignal(void){ unsigned char DCFsignal = digitalRead(DCF77Pin); if (DCFsignal != previousSignalState) { if (DCFsignal == 1) { /* We detected a raising flank, increase per one second! */ addSecond(); #ifdef DCF_DEBUG serialDumpTime(); #endif int thisFlankTime=millis(); if (thisFlankTime - previousFlankTime > DCF_sync_millis) { #ifdef DCF_DEBUG Serial.println("####"); Serial.println("#### Begin of new Minute!!!"); Serial.println("####"); #endif finalizeBuffer(); } previousFlankTime=thisFlankTime; #ifdef DCF_DEBUG Serial.print(previousFlankTime); Serial.print(": DCF77 Signal detected, "); #endif } else { /* or a falling flank */ int difference=millis() - previousFlankTime; #ifdef DCF_DEBUG Serial.print("duration: "); Serial.print(difference); #endif if (difference < DCF_split_millis) { appendSignal(0); } else { appendSignal(1); } } previousSignalState = DCFsignal; } return DCFsignal; }