Skip to content

Commit

Permalink
Fix 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 d3b9d19 commit b94f973
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/SDI12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ 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;
#ifdef SDI12_CHECK_PARITY
_parityFailure = false; // reset the parity failure flag
#endif
break;
Expand Down Expand Up @@ -795,12 +795,12 @@ 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;
#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;
#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; }
Expand All @@ -810,7 +810,7 @@ void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::receiveISR() {
// character (but have gotten all the data bits) then this should be a
// stop bit and we can start looking for a new start bit.
if ((pinLevel == LOW) || !nextCharStarted) {
rxState = WAITING_FOR_START_BIT; // DISABLE STOP BIT TIMER
rxState = WAITING_FOR_START_BIT; // reset the rx state, stop waiting for stop bit
} else {
// If we just switched to HIGH, or we've exceeded the total number of
// bits in a character, then the character must have ended with 1's/LOW,
Expand Down
14 changes: 13 additions & 1 deletion src/SDI12.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ class SDI12 : public Stream {
*
* - if 0: indicates that we got a start bit
* - if >0: indicates the number of bits received
*
* 0 - got start bit
* 1 - got data bit 0
* 2 - got data bit 1
* 3 - got data bit 2
* 4 - got data bit 3
* 5 - got data bit 4
* 6 - got data bit 5
* 7 - got data bit 6
* 8 - got data bit 7 (parity)
* 9 - got stop bit
* 255 - waiting for next start bit
*/
static uint8_t rxState;
/**
Expand Down Expand Up @@ -606,7 +618,7 @@ class SDI12 : public Stream {
* @param dataPin The data pin's digital pin number
*/
void setDataPin(int8_t dataPin);
#ifdef SDI12_CHECK_PARITY;
#ifdef SDI12_CHECK_PARITY
bool _parityFailure;
#endif
/**@}*/
Expand Down

2 comments on commit b94f973

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lint_errors

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lint_errors

Please sign in to comment.