From d3b9d19e9fac7e348d6f54893350a163d42e2709 Mon Sep 17 00:00:00 2001 From: Sara Damiano Date: Tue, 6 Aug 2024 12:31:38 -0400 Subject: [PATCH] Add parity check Signed-off-by: Sara Damiano --- src/SDI12.cpp | 13 ++++++++++++- src/SDI12.h | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/SDI12.cpp b/src/SDI12.cpp index f494d9d..bc3ca02 100644 --- a/src/SDI12.cpp +++ b/src/SDI12.cpp @@ -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: @@ -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 diff --git a/src/SDI12.h b/src/SDI12.h index d149275..2bddc34 100644 --- a/src/SDI12.h +++ b/src/SDI12.h @@ -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 @@ -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 /**@}*/