Skip to content

Commit

Permalink
Add parity check
Browse files Browse the repository at this point in the history
Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
  • Loading branch information
SRGDamia1 committed Aug 6, 2024
1 parent d284004 commit d3b9d19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/SDI12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ void SDI12::setState(SDI12_STATES state) {
pinMode(_dataPin, INPUT); // Turn off the pull-up resistor
pinMode(_dataPin, OUTPUT); // Pin mode = output
setPinInterrupts(false); // Interrupts disabled on data pin
#ifdef SDI12_CHECK_PARITY;
_parityFailure = false; // reset the parity failure flag
#endif
break;
}
case SDI12_LISTENING:
Expand Down Expand Up @@ -790,10 +793,18 @@ void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::receiveISR() {
}

// If this was the 8th or more bit then the character and parity are complete.
// The stop bit may still be outstanding
if (rxState > 7) {
#ifdef SDI12_CHECK_PARITY;
uint8_t rxParity = bitRead(rxValue, 7); // pull out the parity bit
#endif
rxValue &= 0x7F; // Throw away the parity bit (and with 0b01111111)
charToBuffer(rxValue); // Put the finished character into the buffer

#ifdef SDI12_CHECK_PARITY;
uint8_t checkParity =
parity_even_bit(rxValue); // Calculate the parity bit from character w/o parity
if (rxParity != checkParity) { _parityFailure = true; }
#endif

// if this is LOW, or we haven't exceeded the number of bits in a
// character (but have gotten all the data bits) then this should be a
Expand Down
10 changes: 10 additions & 0 deletions src/SDI12.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ typedef const __FlashStringHelper* FlashString;
/// a char not found in a valid ASCII numeric field
#define NO_IGNORE_CHAR '\x01'

#ifndef SDI12_IGNORE_PARITY
/**
* @brief Check the value of the parity bit on reception
*/
#define SDI12_CHECK_PARITY
#endif

#ifndef SDI12_WAKE_DELAY
/**
* @brief The amount of additional time in milliseconds that the sensor takes to wake
Expand Down Expand Up @@ -599,6 +606,9 @@ class SDI12 : public Stream {
* @param dataPin The data pin's digital pin number
*/
void setDataPin(int8_t dataPin);
#ifdef SDI12_CHECK_PARITY;
bool _parityFailure;
#endif
/**@}*/


Expand Down

0 comments on commit d3b9d19

Please sign in to comment.