From c093d34c9104570fd9c774bda520e2863ebb8358 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sat, 28 Sep 2019 10:30:50 +0200 Subject: [PATCH] Updated debug macros --- src/Module.cpp | 70 ++++++++-------- src/TypeDef.h | 19 ++++- src/modules/CC1101.cpp | 4 +- src/modules/ESP8266.cpp | 2 +- src/modules/RF69.cpp | 4 +- src/modules/SX1231.cpp | 30 +++---- src/modules/SX126x.cpp | 50 +++++------ src/modules/SX1272.cpp | 12 +-- src/modules/SX1278.cpp | 12 +-- src/modules/SX127x.cpp | 8 +- src/modules/XBee.cpp | 182 ++++++++++++++++++++-------------------- src/modules/nRF24.cpp | 2 +- src/protocols/Morse.cpp | 8 +- 13 files changed, 207 insertions(+), 196 deletions(-) diff --git a/src/Module.cpp b/src/Module.cpp index 8b87cf2c..5ca9a77d 100644 --- a/src/Module.cpp +++ b/src/Module.cpp @@ -120,20 +120,20 @@ bool Module::ATgetResponse() { while (millis() - start < _ATtimeout) { while(ModuleSerial->available() > 0) { char c = ModuleSerial->read(); - DEBUG_PRINT(c); + RADIOLIB_VERBOSE_PRINT(c); data += c; } if(data.indexOf("OK") != -1) { - DEBUG_PRINTLN(); + RADIOLIB_VERBOSE_PRINTLN(); return(true); } else if (data.indexOf("ERROR") != -1) { - DEBUG_PRINTLN(); + RADIOLIB_VERBOSE_PRINTLN(); return(false); } } - DEBUG_PRINTLN(); + RADIOLIB_VERBOSE_PRINTLN(); return(false); } @@ -170,24 +170,24 @@ int16_t Module::SPIsetRegValue(uint8_t reg, uint8_t value, uint8_t msb, uint8_t } // check failed, print debug info - DEBUG_PRINTLN(); - DEBUG_PRINT(F("address:\t0x")); - DEBUG_PRINTLN(reg, HEX); - DEBUG_PRINT(F("bits:\t\t")); - DEBUG_PRINT(msb); - DEBUG_PRINT(' '); - DEBUG_PRINTLN(lsb); - DEBUG_PRINT(F("value:\t\t0b")); - DEBUG_PRINTLN(value, BIN); - DEBUG_PRINT(F("current:\t0b")); - DEBUG_PRINTLN(currentValue, BIN); - DEBUG_PRINT(F("mask:\t\t0b")); - DEBUG_PRINTLN(mask, BIN); - DEBUG_PRINT(F("new:\t\t0b")); - DEBUG_PRINTLN(newValue, BIN); - DEBUG_PRINT(F("read:\t\t0b")); - DEBUG_PRINTLN(readValue, BIN); - DEBUG_PRINTLN(); + RADIOLIB_DEBUG_PRINTLN(); + RADIOLIB_DEBUG_PRINT(F("address:\t0x")); + RADIOLIB_DEBUG_PRINTLN(reg, HEX); + RADIOLIB_DEBUG_PRINT(F("bits:\t\t")); + RADIOLIB_DEBUG_PRINT(msb); + RADIOLIB_DEBUG_PRINT(' '); + RADIOLIB_DEBUG_PRINTLN(lsb); + RADIOLIB_DEBUG_PRINT(F("value:\t\t0b")); + RADIOLIB_DEBUG_PRINTLN(value, BIN); + RADIOLIB_DEBUG_PRINT(F("current:\t0b")); + RADIOLIB_DEBUG_PRINTLN(currentValue, BIN); + RADIOLIB_DEBUG_PRINT(F("mask:\t\t0b")); + RADIOLIB_DEBUG_PRINTLN(mask, BIN); + RADIOLIB_DEBUG_PRINT(F("new:\t\t0b")); + RADIOLIB_DEBUG_PRINTLN(newValue, BIN); + RADIOLIB_DEBUG_PRINT(F("read:\t\t0b")); + RADIOLIB_DEBUG_PRINTLN(readValue, BIN); + RADIOLIB_DEBUG_PRINTLN(); return(ERR_SPI_WRITE_FAILED); } @@ -219,30 +219,30 @@ void Module::SPItransfer(uint8_t cmd, uint8_t reg, uint8_t* dataOut, uint8_t* da // send SPI register address with access command _spi->transfer(reg | cmd); - DEBUG_PRINT(reg | cmd, HEX); - DEBUG_PRINT('\t'); - DEBUG_PRINT(reg | cmd, BIN); - DEBUG_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(reg | cmd, HEX); + RADIOLIB_VERBOSE_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(reg | cmd, BIN); + RADIOLIB_VERBOSE_PRINT('\t'); // send data or get response if(cmd == SPIwriteCommand) { for(size_t n = 0; n < numBytes; n++) { _spi->transfer(dataOut[n]); - DEBUG_PRINT(dataOut[n], HEX); - DEBUG_PRINT('\t'); - DEBUG_PRINT(dataOut[n], BIN); - DEBUG_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(dataOut[n], HEX); + RADIOLIB_VERBOSE_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(dataOut[n], BIN); + RADIOLIB_VERBOSE_PRINT('\t'); } } else if (cmd == SPIreadCommand) { for(size_t n = 0; n < numBytes; n++) { dataIn[n] = _spi->transfer(0x00); - DEBUG_PRINT(dataIn[n], HEX); - DEBUG_PRINT('\t'); - DEBUG_PRINT(dataIn[n], BIN); - DEBUG_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(dataIn[n], HEX); + RADIOLIB_VERBOSE_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(dataIn[n], BIN); + RADIOLIB_VERBOSE_PRINT('\t'); } } - DEBUG_PRINTLN(); + RADIOLIB_VERBOSE_PRINTLN(); // release CS digitalWrite(_cs, HIGH); diff --git a/src/TypeDef.h b/src/TypeDef.h index 5359cb82..a3f89aed 100644 --- a/src/TypeDef.h +++ b/src/TypeDef.h @@ -7,14 +7,25 @@ #error "Unsupported Arduino version (< 1.0.0)" #endif +#define RADIOLIB_DEBUG_PORT Serial + //#define RADIOLIB_DEBUG +//#define RADIOLIB_VERBOSE #ifdef RADIOLIB_DEBUG - #define DEBUG_PRINT(...) { Serial.print(__VA_ARGS__); } - #define DEBUG_PRINTLN(...) { Serial.println(__VA_ARGS__); } + #define RADIOLIB_DEBUG_PRINT(...) { RADIOLIB_DEBUG_PORT.print(__VA_ARGS__); } + #define RADIOLIB_DEBUG_PRINTLN(...) { RADIOLIB_DEBUG_PORT.println(__VA_ARGS__); } #else - #define DEBUG_PRINT(...) {} - #define DEBUG_PRINTLN(...) {} + #define RADIOLIB_DEBUG_PRINT(...) {} + #define RADIOLIB_DEBUG_PRINTLN(...) {} +#endif + +#ifdef RADIOLIB_VERBOSE + #define RADIOLIB_VERBOSE_PRINT(...) { RADIOLIB_DEBUG_PORT.print(__VA_ARGS__); } + #define RADIOLIB_VERBOSE_PRINTLN(...) { RADIOLIB_DEBUG_PORT.println(__VA_ARGS__); } +#else + #define RADIOLIB_VERBOSE_PRINT(...) {} + #define RADIOLIB_VERBOSE_PRINTLN(...) {} #endif /*! diff --git a/src/modules/CC1101.cpp b/src/modules/CC1101.cpp index 3bf6d4f9..42fe5d7f 100644 --- a/src/modules/CC1101.cpp +++ b/src/modules/CC1101.cpp @@ -36,11 +36,11 @@ int16_t CC1101::begin(float freq, float br, float rxBw, float freqDev, int8_t po } if(!flagFound) { - DEBUG_PRINTLN(F("No CC1101 found!")); + RADIOLIB_DEBUG_PRINTLN(F("No CC1101 found!")); SPI.end(); return(ERR_CHIP_NOT_FOUND); } else { - DEBUG_PRINTLN(F("Found CC1101! (match by CC1101_REG_VERSION == 0x14)")); + RADIOLIB_DEBUG_PRINTLN(F("Found CC1101! (match by CC1101_REG_VERSION == 0x14)")); } // configure settings not accessible by API diff --git a/src/modules/ESP8266.cpp b/src/modules/ESP8266.cpp index d34e0d57..7f7e47b5 100644 --- a/src/modules/ESP8266.cpp +++ b/src/modules/ESP8266.cpp @@ -173,7 +173,7 @@ size_t ESP8266::receive(uint8_t* data, size_t len, uint32_t timeout) { while((millis() - start < timeout) && (i < len)) { while(_mod->ModuleSerial->available() > 0) { uint8_t b = _mod->ModuleSerial->read(); - DEBUG_PRINT(b); + RADIOLIB_DEBUG_PRINT(b); data[i] = b; i++; } diff --git a/src/modules/RF69.cpp b/src/modules/RF69.cpp index 951e6aba..8e7f56b0 100644 --- a/src/modules/RF69.cpp +++ b/src/modules/RF69.cpp @@ -35,11 +35,11 @@ int16_t RF69::begin(float freq, float br, float rxBw, float freqDev, int8_t powe } if(!flagFound) { - DEBUG_PRINTLN(F("No RF69 found!")); + RADIOLIB_DEBUG_PRINTLN(F("No RF69 found!")); SPI.end(); return(ERR_CHIP_NOT_FOUND); } else { - DEBUG_PRINTLN(F("Found RF69! (match by RF69_REG_VERSION == 0x24)")); + RADIOLIB_DEBUG_PRINTLN(F("Found RF69! (match by RF69_REG_VERSION == 0x24)")); } // configure settings not accessible by API diff --git a/src/modules/SX1231.cpp b/src/modules/SX1231.cpp index 23c15cd4..63767987 100644 --- a/src/modules/SX1231.cpp +++ b/src/modules/SX1231.cpp @@ -7,7 +7,7 @@ SX1231::SX1231(Module* mod) : RF69(mod) { int16_t SX1231::begin(float freq, float br, float rxBw, float freqDev, int8_t power) { // set module properties _mod->init(USE_SPI, INT_BOTH); - + // try to find the SX1231 chip uint8_t i = 0; bool flagFound = false; @@ -21,7 +21,7 @@ int16_t SX1231::begin(float freq, float br, float rxBw, float freqDev, int8_t po Serial.print(F("SX127x not found! (")); Serial.print(i + 1); Serial.print(F(" of 10 tries) SX127X_REG_VERSION == ")); - + char buffHex[7]; sprintf(buffHex, "0x%04X", version); Serial.print(buffHex); @@ -32,55 +32,55 @@ int16_t SX1231::begin(float freq, float br, float rxBw, float freqDev, int8_t po i++; } } - + if(!flagFound) { - DEBUG_PRINTLN(F("No SX1231 found!")); + RADIOLIB_DEBUG_PRINTLN(F("No SX1231 found!")); SPI.end(); return(ERR_CHIP_NOT_FOUND); } else { - DEBUG_PRINTLN(F("Found SX1231!")); + RADIOLIB_DEBUG_PRINTLN(F("Found SX1231!")); } - + // configure settings not accessible by API int16_t state = config(); if(state != ERR_NONE) { return(state); } - + // configure publicly accessible settings state = setFrequency(freq); if(state != ERR_NONE) { return(state); } - + _rxBw = 125.0; state = setBitRate(br); if(state != ERR_NONE) { return(state); } - + state = setRxBandwidth(rxBw); if(state != ERR_NONE) { return(state); } - + state = setFrequencyDeviation(freqDev); if(state != ERR_NONE) { return(state); } - + state = setOutputPower(power); if(state != ERR_NONE) { return(state); } - + // default sync word values 0x2D01 is the same as the default in LowPowerLab RFM69 library uint8_t syncWord[] = {0x2D, 0x01}; state = setSyncWord(syncWord, 2); if(state != ERR_NONE) { return(state); } - + // SX1231 V2a only if(_chipRevision == SX1231_CHIP_REVISION_2_A) { // modify default OOK threshold value @@ -88,13 +88,13 @@ int16_t SX1231::begin(float freq, float br, float rxBw, float freqDev, int8_t po if(state != ERR_NONE) { return(state); } - + // enable OCP with 95 mA limit state = _mod->SPIsetRegValue(RF69_REG_OCP, RF69_OCP_ON | RF69_OCP_TRIM, 4, 0); if(state != ERR_NONE) { return(state); } } - + return(ERR_NONE); } diff --git a/src/modules/SX126x.cpp b/src/modules/SX126x.cpp index 6d0e8fce..0d36f05e 100644 --- a/src/modules/SX126x.cpp +++ b/src/modules/SX126x.cpp @@ -172,9 +172,9 @@ int16_t SX126x::transmit(uint8_t* data, size_t len, uint8_t addr) { return(ERR_UNKNOWN); } - DEBUG_PRINT(F("Timeout in ")); - DEBUG_PRINT(timeout); - DEBUG_PRINTLN(F(" us")); + RADIOLIB_DEBUG_PRINT(F("Timeout in ")); + RADIOLIB_DEBUG_PRINT(timeout); + RADIOLIB_DEBUG_PRINTLN(F(" us")); // start transmission state = startTransmit(data, len, addr); @@ -236,9 +236,9 @@ int16_t SX126x::receive(uint8_t* data, size_t len) { return(ERR_UNKNOWN); } - DEBUG_PRINT(F("Timeout in ")); - DEBUG_PRINT(timeout); - DEBUG_PRINTLN(F(" us")); + RADIOLIB_DEBUG_PRINT(F("Timeout in ")); + RADIOLIB_DEBUG_PRINT(timeout); + RADIOLIB_DEBUG_PRINTLN(F(" us")); // start reception uint32_t timeoutValue = (uint32_t)((float)timeout / 15.625); @@ -1107,9 +1107,9 @@ int16_t SX126x::setModulationParams(uint8_t sf, uint8_t bw, uint8_t cr, uint8_t // calculate symbol length and enable low data rate optimization, if needed if(ldro == 0xFF) { float symbolLength = (float)(uint32_t(1) << _sf) / (float)_bwKhz; - DEBUG_PRINT("Symbol length: "); - DEBUG_PRINT(symbolLength); - DEBUG_PRINTLN(" ms"); + RADIOLIB_DEBUG_PRINT("Symbol length: "); + RADIOLIB_DEBUG_PRINT(symbolLength); + RADIOLIB_DEBUG_PRINTLN(" ms"); if(symbolLength >= 16.0) { _ldro = SX126X_LORA_LOW_DATA_RATE_OPTIMIZE_ON; } else { @@ -1277,8 +1277,8 @@ int16_t SX126x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* d // send command byte(s) for(uint8_t n = 0; n < cmdLen; n++) { spi->transfer(cmd[n]); - DEBUG_PRINT(cmd[n], HEX); - DEBUG_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(cmd[n], HEX); + RADIOLIB_VERBOSE_PRINT('\t'); } // variable to save error during SPI transfer @@ -1289,10 +1289,10 @@ int16_t SX126x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* d for(uint8_t n = 0; n < numBytes; n++) { // send byte uint8_t in = spi->transfer(dataOut[n]); - DEBUG_PRINT(dataOut[n], HEX); - DEBUG_PRINT('\t'); - DEBUG_PRINT(in, HEX); - DEBUG_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(dataOut[n], HEX); + RADIOLIB_VERBOSE_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(in, HEX); + RADIOLIB_VERBOSE_PRINT('\t'); // check status if(((in & 0b00001110) == SX126X_STATUS_CMD_TIMEOUT) || @@ -1302,14 +1302,14 @@ int16_t SX126x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* d status = in; } } - DEBUG_PRINTLN(); + RADIOLIB_VERBOSE_PRINT(); } else { // skip the first byte for read-type commands (status-only) uint8_t in = spi->transfer(SX126X_CMD_NOP); - DEBUG_PRINT(SX126X_CMD_NOP, HEX); - DEBUG_PRINT('\t'); - DEBUG_PRINT(in, HEX); - DEBUG_PRINT('\t') + RADIOLIB_VERBOSE_PRINT(SX126X_CMD_NOP, HEX); + RADIOLIB_VERBOSE_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(in, HEX); + RADIOLIB_VERBOSE_PRINT('\t') // check status if(((in & 0b00001110) == SX126X_STATUS_CMD_TIMEOUT) || @@ -1320,12 +1320,12 @@ int16_t SX126x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* d } for(uint8_t n = 0; n < numBytes; n++) { dataIn[n] = spi->transfer(SX126X_CMD_NOP); - DEBUG_PRINT(SX126X_CMD_NOP, HEX); - DEBUG_PRINT('\t'); - DEBUG_PRINT(dataIn[n], HEX); - DEBUG_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(SX126X_CMD_NOP, HEX); + RADIOLIB_VERBOSE_PRINT('\t'); + RADIOLIB_VERBOSE_PRINT(dataIn[n], HEX); + RADIOLIB_VERBOSE_PRINT('\t'); } - DEBUG_PRINTLN(); + RADIOLIB_VERBOSE_PRINT(); } // stop transfer diff --git a/src/modules/SX1272.cpp b/src/modules/SX1272.cpp index f592cff9..86d058d7 100644 --- a/src/modules/SX1272.cpp +++ b/src/modules/SX1272.cpp @@ -125,9 +125,9 @@ int16_t SX1272::setBandwidth(float bw) { // calculate symbol length and set low data rate optimization, if needed float symbolLength = (float)(uint32_t(1) << SX127x::_sf) / (float)SX127x::_bw; - DEBUG_PRINT("Symbol length: "); - DEBUG_PRINT(symbolLength); - DEBUG_PRINTLN(" ms"); + RADIOLIB_DEBUG_PRINT("Symbol length: "); + RADIOLIB_DEBUG_PRINT(symbolLength); + RADIOLIB_DEBUG_PRINTLN(" ms"); if(symbolLength >= 16.0) { state = _mod->SPIsetRegValue(SX127X_REG_MODEM_CONFIG_1, SX1272_LOW_DATA_RATE_OPT_ON, 0, 0); } else { @@ -179,9 +179,9 @@ int16_t SX1272::setSpreadingFactor(uint8_t sf) { // calculate symbol length and set low data rate optimization, if needed float symbolLength = (float)(uint32_t(1) << SX127x::_sf) / (float)SX127x::_bw; - DEBUG_PRINT("Symbol length: "); - DEBUG_PRINT(symbolLength); - DEBUG_PRINTLN(" ms"); + RADIOLIB_DEBUG_PRINT("Symbol length: "); + RADIOLIB_DEBUG_PRINT(symbolLength); + RADIOLIB_DEBUG_PRINTLN(" ms"); if(symbolLength >= 16.0) { state = _mod->SPIsetRegValue(SX127X_REG_MODEM_CONFIG_1, SX1272_LOW_DATA_RATE_OPT_ON, 0, 0); } else { diff --git a/src/modules/SX1278.cpp b/src/modules/SX1278.cpp index 7f6ad28a..64f2245a 100644 --- a/src/modules/SX1278.cpp +++ b/src/modules/SX1278.cpp @@ -195,9 +195,9 @@ int16_t SX1278::setBandwidth(float bw) { // calculate symbol length and set low data rate optimization, if needed float symbolLength = (float)(uint32_t(1) << SX127x::_sf) / (float)SX127x::_bw; - DEBUG_PRINT("Symbol length: "); - DEBUG_PRINT(symbolLength); - DEBUG_PRINTLN(" ms"); + RADIOLIB_DEBUG_PRINT("Symbol length: "); + RADIOLIB_DEBUG_PRINT(symbolLength); + RADIOLIB_DEBUG_PRINTLN(" ms"); if(symbolLength >= 16.0) { state = _mod->SPIsetRegValue(SX1278_REG_MODEM_CONFIG_3, SX1278_LOW_DATA_RATE_OPT_ON, 3, 3); } else { @@ -249,9 +249,9 @@ int16_t SX1278::setSpreadingFactor(uint8_t sf) { // calculate symbol length and set low data rate optimization, if needed float symbolLength = (float)(uint32_t(1) << SX127x::_sf) / (float)SX127x::_bw; - DEBUG_PRINT("Symbol length: "); - DEBUG_PRINT(symbolLength); - DEBUG_PRINTLN(" ms"); + RADIOLIB_DEBUG_PRINT("Symbol length: "); + RADIOLIB_DEBUG_PRINT(symbolLength); + RADIOLIB_DEBUG_PRINTLN(" ms"); if(symbolLength >= 16.0) { state = _mod->SPIsetRegValue(SX1278_REG_MODEM_CONFIG_3, SX1278_LOW_DATA_RATE_OPT_ON, 3, 3); } else { diff --git a/src/modules/SX127x.cpp b/src/modules/SX127x.cpp index 31d63ec1..c0739db9 100644 --- a/src/modules/SX127x.cpp +++ b/src/modules/SX127x.cpp @@ -11,11 +11,11 @@ int16_t SX127x::begin(uint8_t chipVersion, uint8_t syncWord, uint8_t currentLimi // try to find the SX127x chip if(!SX127x::findChip(chipVersion)) { - DEBUG_PRINTLN(F("No SX127x found!")); + RADIOLIB_DEBUG_PRINTLN(F("No SX127x found!")); _mod->term(); return(ERR_CHIP_NOT_FOUND); } else { - DEBUG_PRINTLN(F("Found SX127x!")); + RADIOLIB_DEBUG_PRINTLN(F("Found SX127x!")); } // check active modem @@ -55,11 +55,11 @@ int16_t SX127x::beginFSK(uint8_t chipVersion, float br, float freqDev, float rxB // try to find the SX127x chip if(!SX127x::findChip(chipVersion)) { - DEBUG_PRINTLN(F("No SX127x found!")); + RADIOLIB_DEBUG_PRINTLN(F("No SX127x found!")); _mod->term(); return(ERR_CHIP_NOT_FOUND); } else { - DEBUG_PRINTLN(F("Found SX127x!")); + RADIOLIB_DEBUG_PRINTLN(F("Found SX127x!")); } // check currently active modem diff --git a/src/modules/XBee.cpp b/src/modules/XBee.cpp index 7c31a588..9be17456 100644 --- a/src/modules/XBee.cpp +++ b/src/modules/XBee.cpp @@ -12,13 +12,13 @@ int16_t XBee::begin(long speed) { // set module properties _mod->baudrate = speed; _mod->init(USE_UART, INT_1); - + // reset module reset(); - + // empty UART buffer (garbage data) _mod->ATemptyBuffer(); - + // try to find the XBee bool flagFound = false; uint8_t i = 0; @@ -26,29 +26,29 @@ int16_t XBee::begin(long speed) { // hardware reset should return 2 modem status frames - 1st status 0x00, second status 0x06 int16_t state = readApiFrame(0x00, 1, 2000); readApiFrame(0x00, 1, 2000); - + if(state == ERR_NONE) { flagFound = true; } else { - DEBUG_PRINT(F("XBee not found! (")); - DEBUG_PRINT(i + 1); - DEBUG_PRINT(F(" of 10 tries) STATE == ")); - DEBUG_PRINTLN(state); - DEBUG_PRINTLN(F("Resetting ...")); + RADIOLIB_DEBUG_PRINT(F("XBee not found! (")); + RADIOLIB_DEBUG_PRINT(i + 1); + RADIOLIB_DEBUG_PRINT(F(" of 10 tries) STATE == ")); + RADIOLIB_DEBUG_PRINTLN(state); + RADIOLIB_DEBUG_PRINTLN(F("Resetting ...")); reset(); delay(1000); _mod->ATemptyBuffer(); i++; } } - + if(!flagFound) { - DEBUG_PRINTLN(F("No XBee found!")); + RADIOLIB_DEBUG_PRINTLN(F("No XBee found!")); return(ERR_CMD_MODE_FAILED); } else { - DEBUG_PRINTLN(F("Found XBee!")); + RADIOLIB_DEBUG_PRINTLN(F("Found XBee!")); } - + return(ERR_NONE); } @@ -75,12 +75,12 @@ int16_t XBee::transmit(uint8_t* dest, uint8_t* destNetwork, const char* payload, cmd[10] = radius; cmd[11] = 0x01; // options: no retries memcpy(cmd + 12, payload, payloadLen); - + // send frame uint8_t frameID = _frameID++; sendApiFrame(XBEE_API_FRAME_ZIGBEE_TRANSMIT_REQUEST, frameID, cmd, dataLen); delete[] cmd; - + // get response code return(readApiFrame(frameID, 5)); } @@ -90,35 +90,35 @@ size_t XBee::available() { size_t serialBytes = _mod->ModuleSerial->available(); if(serialBytes < 3) { return(0); - } - + } + uint8_t header[3]; if(!_frameHeaderProcessed) { // read frame header for(uint8_t i = 0; i < 3; i++) { header[i] = _mod->ModuleSerial->read(); } - + // check if we received API frame if(header[0] != XBEE_API_START) { return(0); } - + // get expected frame length _frameLength = ((header[1] << 8) | header[2]) + 1; _frameHeaderProcessed = true; } - + // check if the header is complete if(serialBytes < _frameLength) { return(0); } - + uint8_t* frame = new uint8_t[_frameLength]; //24 for(size_t i = 0; i < _frameLength; i++) { frame[i] = _mod->ModuleSerial->read(); } - + // save packet source and data size_t payloadLength = _frameLength - 12; delete[] _packetData; @@ -126,11 +126,11 @@ size_t XBee::available() { memcpy(_packetData, frame + 12, payloadLength - 1); _packetData[payloadLength - 1] = '\0'; memcpy(_packetSource, frame + 1, 8); - + delete[] frame; _frameLength = 0; _frameHeaderProcessed = false; - + // return number of bytes in payload return(payloadLength); } @@ -153,23 +153,23 @@ int16_t XBee::setPanId(uint8_t* panId) { uint8_t cmd[10]; memcpy(cmd, "ID", 2); memcpy(cmd + 2, panId, 8); - + // send frame uint8_t frameID = _frameID++; sendApiFrame(XBEE_API_FRAME_AT_COMMAND_QUEUE, frameID, cmd, 10); - + // get response code int16_t state = readApiFrame(frameID, 4); if(state != ERR_NONE) { return(state); } - + // confirm changes return(confirmChanges()); } XBeeSerial::XBeeSerial(Module* mod) : ISerial(mod) { - + } int16_t XBeeSerial::begin(long speed) { @@ -177,24 +177,24 @@ int16_t XBeeSerial::begin(long speed) { _mod->AtLineFeed = "\r"; _mod->baudrate = speed; _mod->init(USE_UART, INT_NONE); - + // empty UART buffer (garbage data) _mod->ATemptyBuffer(); - + // enter command mode - DEBUG_PRINTLN(F("Entering command mode ...")); + RADIOLIB_DEBUG_PRINTLN(F("Entering command mode ...")); if(!enterCmdMode()) { return(ERR_CMD_MODE_FAILED); } - + // test AT setup - DEBUG_PRINTLN(F("Sending test command ...")); + RADIOLIB_DEBUG_PRINTLN(F("Sending test command ...")); if(!_mod->ATsendCommand("AT")) { return(ERR_AT_FAILED); } - + // exit command mode - DEBUG_PRINTLN(F("Exiting command mode ...")); + RADIOLIB_DEBUG_PRINTLN(F("Exiting command mode ...")); if(!_mod->ATsendCommand("ATCN")) { return(ERR_AT_FAILED); } @@ -212,13 +212,13 @@ void XBeeSerial::reset() { int16_t XBeeSerial::setDestinationAddress(const char* destinationAddressHigh, const char* destinationAddressLow) { // enter command mode - DEBUG_PRINTLN(F("Entering command mode ...")); + RADIOLIB_DEBUG_PRINTLN(F("Entering command mode ...")); if(!enterCmdMode()) { return(ERR_CMD_MODE_FAILED); } - + // set higher address bytes - DEBUG_PRINTLN(F("Setting address (high) ...")); + RADIOLIB_DEBUG_PRINTLN(F("Setting address (high) ...")); char* addressHigh = new char[strlen(destinationAddressHigh) + 4]; strcpy(addressHigh, "ATDH"); strcat(addressHigh, destinationAddressHigh); @@ -227,9 +227,9 @@ int16_t XBeeSerial::setDestinationAddress(const char* destinationAddressHigh, co if(!res) { return(ERR_AT_FAILED); } - + // set lower address bytes - DEBUG_PRINTLN(F("Setting address (low) ...")); + RADIOLIB_DEBUG_PRINTLN(F("Setting address (low) ...")); char* addressLow = new char[strlen(destinationAddressLow) + 4]; strcpy(addressLow, "ATDL"); strcat(addressLow, destinationAddressLow); @@ -238,25 +238,25 @@ int16_t XBeeSerial::setDestinationAddress(const char* destinationAddressHigh, co if(!res) { return(ERR_AT_FAILED); } - + // exit command mode - DEBUG_PRINTLN(F("Exiting command mode ...")); + RADIOLIB_DEBUG_PRINTLN(F("Exiting command mode ...")); if(!_mod->ATsendCommand("ATCN")) { return(ERR_AT_FAILED); } - + return(ERR_NONE); } int16_t XBeeSerial::setPanId(const char* panId) { // enter command mode - DEBUG_PRINTLN(F("Entering command mode ...")); + RADIOLIB_DEBUG_PRINTLN(F("Entering command mode ...")); if(!enterCmdMode()) { return(ERR_CMD_MODE_FAILED); } - + // set PAN ID - DEBUG_PRINTLN(F("Setting PAN ID ...")); + RADIOLIB_DEBUG_PRINTLN(F("Setting PAN ID ...")); char* cmd = new char[strlen(panId) + 4]; strcpy(cmd, "ATID"); strcat(cmd, panId); @@ -265,40 +265,40 @@ int16_t XBeeSerial::setPanId(const char* panId) { if(!res) { return(ERR_AT_FAILED); } - + // exit command mode - DEBUG_PRINTLN(F("Exiting command mode ...")); + RADIOLIB_DEBUG_PRINTLN(F("Exiting command mode ...")); if(!_mod->ATsendCommand("ATCN")) { return(ERR_AT_FAILED); } - + return(ERR_NONE); } bool XBeeSerial::enterCmdMode() { for(uint8_t i = 0; i < 10; i++) { delay(1000); - + _mod->ModuleSerial->write('+'); _mod->ModuleSerial->write('+'); _mod->ModuleSerial->write('+'); - + delay(1000); - + if(_mod->ATgetResponse()) { return(true); } else { - DEBUG_PRINT(F("Unable to enter command mode! (")); - DEBUG_PRINT(i + 1); - DEBUG_PRINTLN(F(" of 10 tries)")); - + RADIOLIB_DEBUG_PRINT(F("Unable to enter command mode! (")); + RADIOLIB_DEBUG_PRINT(i + 1); + RADIOLIB_DEBUG_PRINTLN(F(" of 10 tries)")); + reset(); - + _mod->ATsendCommand("ATCN"); } } - - DEBUG_PRINTLN(F("Terminated, check your wiring. Is AT FW uploaded?")); + + RADIOLIB_DEBUG_PRINTLN(F("Terminated, check your wiring. Is AT FW uploaded?")); return(false); } @@ -306,23 +306,23 @@ int16_t XBee::confirmChanges() { // save changes to non-volatile memory uint8_t frameID = _frameID++; sendApiFrame(XBEE_API_FRAME_AT_COMMAND_QUEUE, frameID, "WR"); - + // get response code int16_t state = readApiFrame(frameID, 4); if(state != ERR_NONE) { return(state); } - + // apply changes frameID = _frameID++; sendApiFrame(XBEE_API_FRAME_AT_COMMAND_QUEUE, frameID, "AC"); - + // get response code state = readApiFrame(frameID, 4); if(state != ERR_NONE) { return(state); } - + return(state); } @@ -334,42 +334,42 @@ void XBee::sendApiFrame(uint8_t type, uint8_t id, uint8_t* data, uint16_t length // build the API frame size_t frameLength = 1 + 2 + length + 1 + 2; uint8_t* frame = new uint8_t[frameLength]; - + frame[0] = 0x7E; // start delimiter frame[1] = ((length + 2) & 0xFF00) >> 8; // length MSB frame[2] = (length + 2) & 0x00FF; // length LSB frame[3] = type; // frame type frame[4] = id; // frame ID memcpy(frame + 5, data, length); // data - + // calculate the checksum uint8_t checksum = 0; for(uint16_t i = 3; i < frameLength - 1; i++) { checksum += frame[i]; } frame[5 + length] = 0xFF - checksum; - + // send the frame for(uint16_t i = 0; i < frameLength; i++) { _mod->ModuleSerial->write(frame[i]); } - + // deallocate memory delete[] frame; } int16_t XBee::readApiFrame(uint8_t frameID, uint8_t codePos, uint16_t timeout) { // TODO: modemStatus frames may be sent at any time, interfering with frame parsing. Add check to make sure this does not happen. - + // get number of bytes in response (must be enough to read the length field uint16_t numBytes = getNumBytes(timeout/2, 3); if(numBytes == 0) { return(ERR_FRAME_NO_RESPONSE); } - + // checksum byte is not included in length field numBytes++; - + // wait until all response bytes are available (5s timeout) uint32_t start = millis(); while(_mod->ModuleSerial->available() < (int16_t)numBytes) { @@ -377,37 +377,37 @@ int16_t XBee::readApiFrame(uint8_t frameID, uint8_t codePos, uint16_t timeout) { return(ERR_FRAME_MALFORMED); } } - DEBUG_PRINT(F("frame data field length: ")); - DEBUG_PRINTLN(numBytes); - + RADIOLIB_DEBUG_PRINT(F("frame data field length: ")); + RADIOLIB_DEBUG_PRINTLN(numBytes); + // read the response uint8_t* resp = new uint8_t[numBytes]; for(uint16_t i = 0; i < numBytes; i++) { resp[i] = _mod->ModuleSerial->read(); } - - // verify checksum + + // verify checksum uint8_t checksum = 0; for(uint16_t i = 0; i < numBytes; i++) { - DEBUG_PRINT(resp[i], HEX); - DEBUG_PRINT('\t'); + RADIOLIB_DEBUG_PRINT(resp[i], HEX); + RADIOLIB_DEBUG_PRINT('\t'); checksum += resp[i]; } - DEBUG_PRINTLN(); + RADIOLIB_DEBUG_PRINTLN(); if(checksum != 0xFF) { - DEBUG_PRINTLN(checksum, HEX); + RADIOLIB_DEBUG_PRINTLN(checksum, HEX); return(ERR_FRAME_INCORRECT_CHECKSUM); } - + // check frame ID if(resp[1] != frameID) { - DEBUG_PRINT(F("received frame ID: ")); - DEBUG_PRINTLN(resp[1]); - DEBUG_PRINT(F("expected frame ID: ")); - DEBUG_PRINTLN(frameID); + RADIOLIB_DEBUG_PRINT(F("received frame ID: ")); + RADIOLIB_DEBUG_PRINTLN(resp[1]); + RADIOLIB_DEBUG_PRINT(F("expected frame ID: ")); + RADIOLIB_DEBUG_PRINTLN(frameID); return(ERR_FRAME_UNEXPECTED_ID); } - + // codePos does not include start delimiter and frame ID uint8_t code = resp[codePos]; delete[] resp; @@ -422,21 +422,21 @@ uint16_t XBee::getNumBytes(uint32_t timeout, size_t minBytes) { return(0); } } - + // read response uint8_t resp[3]; uint8_t i = 0; - DEBUG_PRINT(F("reading frame length: ")); + RADIOLIB_DEBUG_PRINT(F("reading frame length: ")); while(_mod->ModuleSerial->available() > 0) { uint8_t b = _mod->ModuleSerial->read(); - DEBUG_PRINT(b, HEX); - DEBUG_PRINT('\t'); + RADIOLIB_DEBUG_PRINT(b, HEX); + RADIOLIB_DEBUG_PRINT('\t'); resp[i++] = b; if(i == 3) { break; } } - DEBUG_PRINTLN(); - + RADIOLIB_DEBUG_PRINTLN(); + return((resp[1] << 8) | resp[2]); } diff --git a/src/modules/nRF24.cpp b/src/modules/nRF24.cpp index 0c776ce1..55c47d73 100644 --- a/src/modules/nRF24.cpp +++ b/src/modules/nRF24.cpp @@ -20,7 +20,7 @@ int16_t nRF24::begin(int16_t freq, int16_t dataRate, int8_t power, uint8_t addrW // check SPI connection int16_t val = _mod->SPIgetRegValue(NRF24_REG_SETUP_AW); if(!((val >= 1) && (val <= 3))) { - DEBUG_PRINTLN(F("No nRF24 found!")); + RADIOLIB_DEBUG_PRINTLN(F("No nRF24 found!")); _mod->term(); return(ERR_CHIP_NOT_FOUND); } diff --git a/src/protocols/Morse.cpp b/src/protocols/Morse.cpp index e874c722..83137abf 100644 --- a/src/protocols/Morse.cpp +++ b/src/protocols/Morse.cpp @@ -119,9 +119,9 @@ size_t MorseClient::write(uint8_t b) { // check if the requested code was found in the array if(found) { - DEBUG_PRINT(mc.c); - DEBUG_PRINT('\t'); - DEBUG_PRINTLN(mc.m); + RADIOLIB_DEBUG_PRINT(mc.c); + RADIOLIB_DEBUG_PRINT('\t'); + RADIOLIB_DEBUG_PRINTLN(mc.m); // iterate over Morse code representation and output appropriate tones for(uint8_t i = 0; i < strlen(mc.m); i++) { @@ -145,7 +145,7 @@ size_t MorseClient::write(uint8_t b) { } // letter space - DEBUG_PRINTLN(); + RADIOLIB_DEBUG_PRINTLN(); delay(_dotLength * 3); return(1);