Skip to content

Commit

Permalink
Refactor addCRCResponse into part of sendResponse
Browse files Browse the repository at this point in the history
Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
  • Loading branch information
SRGDamia1 committed Sep 22, 2023
1 parent ee41b8b commit 9389a7b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 64 deletions.
92 changes: 38 additions & 54 deletions src/SDI12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,44 +514,62 @@ void SDI12::sendCommand(FlashString cmd, int8_t extraWakeTime) {
// marking and then sending out the characters of resp one by one (for slave-side use,
// that is, when the Arduino itself is acting as an SDI-12 device rather than a
// recorder).
void SDI12::sendResponse(String& resp) {
void SDI12::sendResponse(String& resp, bool addCRC) {
setState(SDI12_TRANSMITTING); // Get ready to send data to the recorder
digitalWrite(_dataPin, LOW); // marking is LOW
delayMicroseconds(marking_micros); // 8.33 ms marking before response
for (int unsigned i = 0; i < resp.length(); i++) {
writeChar(resp[i]); // write each character
}
// tack on the CRC if requested
if (addCRC) {
String crc = crcToString(calculateCRC(resp));
for (int unsigned i = 0; i < 3; i++) {
writeChar(crc[i]); // write each character
}
}
setState(SDI12_LISTENING); // return to listening state
}

void SDI12::sendResponse(const char* resp) {
void SDI12::sendResponse(const char* resp, bool addCRC) {
setState(SDI12_TRANSMITTING); // Get ready to send data to the recorder
digitalWrite(_dataPin, LOW); // marking is LOW
delayMicroseconds(marking_micros); // 8.33 ms marking before response
for (int unsigned i = 0; i < strlen(resp); i++) {
writeChar(resp[i]); // write each character
}
// tack on the CRC if requested
if (addCRC) {
String crc = crcToString(calculateCRC(resp));
for (int unsigned i = 0; i < 3; i++) {
writeChar(crc[i]); // write each character
}
}
setState(SDI12_LISTENING); // return to listening state
}

void SDI12::sendResponse(FlashString resp) {
void SDI12::sendResponse(FlashString resp, bool addCRC) {
setState(SDI12_TRANSMITTING); // Get ready to send data to the recorder
digitalWrite(_dataPin, LOW); // marking is LOW
delayMicroseconds(marking_micros); // 8.33 ms marking before response
for (int unsigned i = 0; i < strlen_P((PGM_P)resp); i++) {
// write each character
writeChar(static_cast<char>(pgm_read_byte((const char*)resp + i)));
}
// tack on the CRC if requested
if (addCRC) {
String crc = crcToString(calculateCRC(resp));
for (int unsigned i = 0; i < 3; i++) {
writeChar(crc[i]); // write each character
}
}
setState(SDI12_LISTENING); // return to listening state
}

#ifdef ENVIRODIY_SDI12_USE_CRC

#define POLY 0xa001

String SDI12::addCRCResponse(String& resp) {
char crcStr[3] = {0};
uint16_t crc = 0;
uint16_t SDI12::calculateCRC(String& resp) {
uint16_t crc = 0;

for (int i = 0; i < resp.length(); i++) {
crc ^= (uint16_t)
Expand All @@ -565,15 +583,11 @@ String SDI12::addCRCResponse(String& resp) {
}
}
}
crcStr[0] = (char)(0x0040 | (crc >> 12));
crcStr[1] = (char)(0x0040 | ((crc >> 6) & 0x003F));
crcStr[2] = (char)(0x0040 | (crc & 0x003F));
return (resp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2]));
return crc;
}

char* SDI12::addCRCResponse(char* resp) {
char crcStr[3] = {0};
uint16_t crc = 0;
uint16_t SDI12::calculateCRC(const char* resp) {
uint16_t crc = 0;

for (int i = 0; i < strlen(resp); i++) {
crc ^= (uint16_t)
Expand All @@ -587,21 +601,14 @@ char* SDI12::addCRCResponse(char* resp) {
}
}
}

crcStr[1] = (char)(0x0040 | ((crc >> 6) & 0x003F));
crcStr[2] = (char)(0x0040 | (crc & 0x003F));
return (strncat(resp, crcStr, 3));
return crc;
}

String SDI12::addCRCResponse(FlashString resp) {
char crcStr[3] = {0};
char respBuffer[SDI12_BUFFER_SIZE - 5]; // don't need space for the CRC or CR/LF
uint16_t SDI12::calculateCRC(FlashString resp) {
uint16_t crc = 0;
int i = 0;
char responsechar;


for (i = 0; i < strlen_P((PGM_P)resp); i++) {
for (int i = 0; i < strlen_P((PGM_P)resp); i++) {
responsechar = (char)pgm_read_byte((const char*)resp + i);
crc ^= (uint16_t)responsechar; // Set the CRC equal to the exclusive OR of the
// character and itself
Expand All @@ -613,41 +620,18 @@ String SDI12::addCRCResponse(FlashString resp) {
crc >>= 1; // right shift the CRC one bit
}
}
respBuffer[i] = responsechar;
}
respBuffer[++i] = '\0';
String outResp = respBuffer;
crcStr[0] = (char)(0x0040 | (crc >> 12));
crcStr[1] = (char)(0x0040 | ((crc >> 6) & 0x003F));
crcStr[2] = (char)(0x0040 | (crc & 0x003F));
return (outResp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2]));
return crc;
}

String SDI12::calculateCRC(String& resp) {
char crcStr[3] = {0};
uint16_t crc = 0;

for (int i = 0; i < resp.length(); i++) {
crc ^= (uint16_t)
resp[i]; // Set the CRC equal to the exclusive OR of the character and itself
for (int j = 0; j < 8; j++) { // count = 1 to 8
if (crc & 0x0001) { // if the least significant bit of the CRC is one
crc >>= 1; // right shift the CRC one bit
crc ^= POLY; // set CRC equal to the exclusive OR of POLY and itself
} else {
crc >>= 1; // right shift the CRC one bit
}
}
}
crcStr[0] = (char)(0x0040 | (crc >> 12));
crcStr[1] = (char)(0x0040 | ((crc >> 6) & 0x003F));
crcStr[2] = (char)(0x0040 | (crc & 0x003F));
String SDI12::crcToString(uint16_t crc) {
char crcStr[3] = {0};
crcStr[0] = (char)(0x0040 | (crc >> 12));
crcStr[1] = (char)(0x0040 | ((crc >> 6) & 0x003F));
crcStr[2] = (char)(0x0040 | (crc & 0x003F));
return (String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2]));
}

#endif // ENVIRODIY_SDI12_USE_CRC


/* ================ Interrupt Service Routine =======================================*/

// 7.1 - Passes off responsibility for the interrupt to the active object.
Expand Down
33 changes: 23 additions & 10 deletions src/SDI12.h
Original file line number Diff line number Diff line change
Expand Up @@ -914,28 +914,41 @@ class SDI12 : public Stream {
/// @copydoc SDI12::sendCommand(String&, int8_t)
void sendCommand(FlashString cmd, int8_t extraWakeTime = SDI12_WAKE_DELAY);

#ifdef ENVIRODIY_SDI12_USE_CRC
String addCRCResponse(String& resp); // Add CRC to the resp string (for slave use)
char* addCRCResponse(char* resp); // Add CRC to the resp string (for slave use)
String
addCRCResponse(FlashString resp); // Add CRC to the resp string (for slave use)
String calculateCRC(String& resp); // Calculate the CRC for a response
#endif
/**
* @brief Calculates the 16-bit Cyclic Redundancy Check (CRC) for an SDI-12 message.
*
* @param resp The message to calculate the CRC for.
* @return *uint16_t* The calculated CRC
*/
uint16_t calculateCRC(String& resp);
/// @copydoc SDI12::calculateCRC(String&)
uint16_t calculateCRC(const char* resp);
/// @copydoc SDI12::calculateCRC(String&)
uint16_t calculateCRC(FlashString resp);

/**
* @brief Converts a numeric 16-bit CRC to an ASCII String.
*
* @param crc The 16-bit CRC
* @return *String* An ASCII string for the CRC
*/
String crcToString(uint16_t crc);

/**
* @brief Send a response out on the data line (for slave use)
*
* @param resp the response to send
* @param addCRC True to append a CRC to the outgoing response
*
* A publicly accessible function that sends out an 8.33 ms marking and a response
* byte by byte on the data line. This is needed if the Arduino is acting as an
* SDI-12 device itself, not as a recorder for another SDI-12 device.
*/
void sendResponse(String& resp);
void sendResponse(String& resp, bool addCRC = false);
/// @copydoc SDI12::sendResponse(String& resp)
void sendResponse(const char* resp);
void sendResponse(const char* resp, bool addCRC = false);
/// @copydoc SDI12::sendResponse(String& resp)
void sendResponse(FlashString resp);
void sendResponse(FlashString resp, bool addCRC = false);
///@}

/**
Expand Down

0 comments on commit 9389a7b

Please sign in to comment.