Skip to content

Commit

Permalink
Clang format
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 21, 2023
1 parent 7abc730 commit ee41b8b
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 130 deletions.
234 changes: 117 additions & 117 deletions src/SDI12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,34 +356,37 @@ void SDI12::setPinInterrupts(bool enable) {
// sets the state of the SDI-12 object.
void SDI12::setState(SDI12_STATES state) {
switch (state) {
case SDI12_HOLDING: {
pinMode(_dataPin, INPUT); // Turn off the pull-up resistor
pinMode(_dataPin, OUTPUT); // Pin mode = output
digitalWrite(_dataPin, LOW); // Pin state = low - marking
setPinInterrupts(false); // Interrupts disabled on data pin
break;
}
case SDI12_TRANSMITTING: {
pinMode(_dataPin, INPUT); // Turn off the pull-up resistor
pinMode(_dataPin, OUTPUT); // Pin mode = output
setPinInterrupts(false); // Interrupts disabled on data pin
break;
}
case SDI12_LISTENING: {
digitalWrite(_dataPin, LOW); // Pin state = low (turns off pull-up)
pinMode(_dataPin, INPUT); // Pin mode = input, pull-up resistor off
interrupts(); // Enable general interrupts
setPinInterrupts(true); // Enable Rx interrupts on data pin
rxState = WAITING_FOR_START_BIT;
break;
}
case SDI12_HOLDING:
{
pinMode(_dataPin, INPUT); // Turn off the pull-up resistor
pinMode(_dataPin, OUTPUT); // Pin mode = output
digitalWrite(_dataPin, LOW); // Pin state = low - marking
setPinInterrupts(false); // Interrupts disabled on data pin
break;
}
case SDI12_TRANSMITTING:
{
pinMode(_dataPin, INPUT); // Turn off the pull-up resistor
pinMode(_dataPin, OUTPUT); // Pin mode = output
setPinInterrupts(false); // Interrupts disabled on data pin
break;
}
case SDI12_LISTENING:
{
digitalWrite(_dataPin, LOW); // Pin state = low (turns off pull-up)
pinMode(_dataPin, INPUT); // Pin mode = input, pull-up resistor off
interrupts(); // Enable general interrupts
setPinInterrupts(true); // Enable Rx interrupts on data pin
rxState = WAITING_FOR_START_BIT;
break;
}
default: // SDI12_DISABLED or SDI12_ENABLED
{
digitalWrite(_dataPin, LOW); // Pin state = low (turns off pull-up)
pinMode(_dataPin, INPUT); // Pin mode = input, pull-up resistor off
setPinInterrupts(false); // Interrupts disabled on data pin
break;
}
{
digitalWrite(_dataPin, LOW); // Pin state = low (turns off pull-up)
pinMode(_dataPin, INPUT); // Pin mode = input, pull-up resistor off
setPinInterrupts(false); // Interrupts disabled on data pin
break;
}
}
}

Expand Down Expand Up @@ -545,127 +548,124 @@ void SDI12::sendResponse(FlashString resp) {
#ifdef ENVIRODIY_SDI12_USE_CRC

#define POLY 0xa001
String SDI12::addCRCResponse(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

String SDI12::addCRCResponse(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));
return (resp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2]));
}


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

for(int i = 0; i < strlen(resp); 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));
return (resp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2]));
}

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

for (int i = 0; i < strlen(resp); 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[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F));
crcStr[2] = (char)( 0x0040 | (crc & 0x003F));
return (strncat(resp, crcStr,3));
crcStr[1] = (char)(0x0040 | ((crc >> 6) & 0x003F));
crcStr[2] = (char)(0x0040 | (crc & 0x003F));
return (strncat(resp, crcStr, 3));
}

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 crc = 0;
int i = 0;
char responsechar ;


for(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
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
}
char crcStr[3] = {0};
char respBuffer[SDI12_BUFFER_SIZE - 5]; // don't need space for the CRC or CR/LF
uint16_t crc = 0;
int i = 0;
char responsechar;


for (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
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
}
}
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]));
}

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
}
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]));
}

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));
return (String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2]));
}
}
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

#endif // ENVIRODIY_SDI12_USE_CRC


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

// 7.1 - Passes off responsibility for the interrupt to the active object.
void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::handleInterrupt(){
void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::handleInterrupt() {
if (_activeObject) _activeObject->receiveISR();
}

// 7.2 - Creates a blank slate of bits for an incoming character
void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::startChar()
{
rxState = 0; // got a start bit
void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::startChar() {
rxState = 0; // got a start bit
rxMask = 0x01; // 0b00000001, bit mask, lsb first
rxValue = 0x00; // 0b00000000, RX character to be, a blank slate
} // startChar

// 7.3 - The actual interrupt service routine
void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::receiveISR()
{

sdi12timer_t thisBitTCNT = READTIME; // time of this data transition (plus ISR latency)
void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::receiveISR() {
sdi12timer_t thisBitTCNT =
READTIME; // time of this data transition (plus ISR latency)

uint8_t pinLevel = digitalRead(_dataPin); // current RX data level

Expand Down
14 changes: 7 additions & 7 deletions src/SDI12.h
Original file line number Diff line number Diff line change
Expand Up @@ -914,12 +914,13 @@ 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
#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 Send a response out on the data line (for slave use)
Expand Down Expand Up @@ -986,7 +987,6 @@ class SDI12 : public Stream {
/** on AVR boards, uncomment to use your own PCINT ISRs */
// #define SDI12_EXTERNAL_PCINT
/**@}*/

};

#endif // SRC_SDI12_H_
22 changes: 16 additions & 6 deletions src/SDI12_boards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void SDI12Timer::configSDI12TimerPrescale(void) {
TCCR2B = 0x07; // TCCR2B = 0x07 = 0b00000111 - Clock Select bits 22, 21, & 20 on -
// prescaler set to CK/1024
}

void SDI12Timer::resetSDI12TimerPrescale(void) {
TCCR2A = preSDI12_TCCR2A;
TCCR2B = preSDI12_TCCR2B;
Expand All @@ -59,6 +60,7 @@ void SDI12Timer::configSDI12TimerPrescale(void) {
TCCR2B = 0x07; // TCCR2B = 0x07 = 0b00000111 - Clock Select bits 22, 21, & 20 on -
// prescaler set to CK/1024
}

void SDI12Timer::resetSDI12TimerPrescale(void) {
TCCR2A = preSDI12_TCCR2A;
TCCR2B = preSDI12_TCCR2B;
Expand All @@ -74,6 +76,7 @@ void SDI12Timer::configSDI12TimerPrescale(void) {
TCCR2B = 0x06; // TCCR2B = 0x06 = 0b00000110 - Clock Select bits 22 & 20 on -
// prescaler set to CK/256
}

void SDI12Timer::resetSDI12TimerPrescale(void) {
TCCR2A = preSDI12_TCCR2A;
TCCR2B = preSDI12_TCCR2B;
Expand Down Expand Up @@ -108,8 +111,9 @@ static uint8_t preSDI12_TCCR1A;

void SDI12Timer::configSDI12TimerPrescale(void) {
preSDI12_TCCR1A = TCCR1;
TCCR1 = 0b00001011; // Set the prescaler to 1024
TCCR1 = 0b00001011; // Set the prescaler to 1024
}

void SDI12Timer::resetSDI12TimerPrescale(void) {
TCCR1 = preSDI12_TCCR1A;
}
Expand All @@ -119,8 +123,9 @@ void SDI12Timer::resetSDI12TimerPrescale(void) {

void SDI12Timer::configSDI12TimerPrescale(void) {
preSDI12_TCCR1A = TCCR1;
TCCR1 = 0b00001010; // Set the prescaler to 512
TCCR1 = 0b00001010; // Set the prescaler to 512
}

void SDI12Timer::resetSDI12TimerPrescale(void) {
TCCR1 = preSDI12_TCCR1A;
}
Expand Down Expand Up @@ -170,6 +175,7 @@ void SDI12Timer::configSDI12TimerPrescale(void) {
TCCR4D = 0x00; // TCCR4D = 0x00 = No fault protection
TCCR4E = 0x00; // TCCR4E = 0x00 = No register locks or overrides
}

void SDI12Timer::resetSDI12TimerPrescale(void) {
TCCR4A = preSDI12_TCCR4A;
TCCR4B = preSDI12_TCCR4B;
Expand All @@ -194,6 +200,7 @@ void SDI12Timer::configSDI12TimerPrescale(void) {
TCCR4D = 0x00; // TCCR4D = 0x00 = No fault protection
TCCR4E = 0x00; // TCCR4E = 0x00 = No register locks or overrides
}

void SDI12Timer::resetSDI12TimerPrescale(void) {
TCCR4A = preSDI12_TCCR4A;
TCCR4B = preSDI12_TCCR4B;
Expand Down Expand Up @@ -245,6 +252,7 @@ void SDI12Timer::configSDI12TimerPrescale(void) {
TC_CTRLA_ENABLE; // Enable TC3
while (TC3->COUNT16.STATUS.bit.SYNCBUSY) {} // Wait for synchronization
}

// NOT resetting the SAMD timer settings
void SDI12Timer::resetSDI12TimerPrescale(void) {
// Disable TCx
Expand All @@ -267,13 +275,15 @@ void SDI12Timer::resetSDI12TimerPrescale(void) {
#elif defined(ESP32) || defined(ESP8266)

void SDI12Timer::configSDI12TimerPrescale(void) {}

void SDI12Timer::resetSDI12TimerPrescale(void) {}

sdi12timer_t ESPFAMILY_USE_INSTRUCTION_RAM SDI12Timer::SDI12TimerRead(void)
{
// Its a one microsecond clock but we want 64uS ticks so divide by 64 i.e. right shift 6
return ((sdi12timer_t)(micros() >> 6));
sdi12timer_t ESPFAMILY_USE_INSTRUCTION_RAM SDI12Timer::SDI12TimerRead(void) {
// Its a one microsecond clock but we want 64uS ticks so divide by 64 i.e. right shift
// 6
return ((sdi12timer_t)(micros() >> 6));
}

// Unknown board
#else
#error "Please define your board timer and pins"
Expand Down

0 comments on commit ee41b8b

Please sign in to comment.