diff --git a/Debug.h b/Debug.h index 6404fba..25993a3 100644 --- a/Debug.h +++ b/Debug.h @@ -75,12 +75,16 @@ extern "C" { #endif #include "esp_log.h" - #define debug(str) ESP_LOGI("", str); - #define debugMod(module, str) ESP_LOGI(module, str); - #define debugMod1(module, str, val) ESP_LOGI(module, str, val); - #define debugMod2(module, str, v1, v2) ESP_LOGI(module, str, v1, v2); - #define debugMod3(module, str, v1, v2, v3) ESP_LOGI(module, str, v1, v2, v3); - #define debugMod4(module, str, v1, v2, v3, v4) ESP_LOGI(module, str, v1, v2, v3, v4); + #define debug(str) printf(str); + #define debugMod(module, str) printf("i[%-10s] ", module); printf(str); printf("\n"); + #define debugMod1(module, str, v1) printf("i[%-10s] ", module); printf(str, v1); printf("\n"); + #define debugMod2(module, str, v1, v2) printf("i[%-10s] ", module); printf(str, v1, v2); printf("\n"); + #define debugMod3(module, str, v1, v2, v3) printf("i[%-10s] ", module); printf(str, v1, v2, v3); printf("\n"); + #define debugMod4(module, str, v1, v2, v3, v4) printf("i[%-10s] ", module); printf(str, v1, v2, v3, v4); printf("\n"); + + #define errorMod(module, str) printf("e[%-10s] ", module); printf(str); printf("\n"); esp_restart(); + #define errorMod1(module, str, v1) printf("e[%-10s] ", module); printf(str, v1); printf("\n"); esp_restart(); + #define IF_DEBUG(a) a #define debugShow(buf, len) hexdump(buf,len) diff --git a/ext/lcd/Draw.h b/ext/lcd/Draw.h index 6636e08..6f8789c 100644 --- a/ext/lcd/Draw.h +++ b/ext/lcd/Draw.h @@ -172,6 +172,13 @@ public: drawLineVer(y+1, y+h-2, x); // left 2 pixels already included above drawLineVer(y+1, y+h-2, x+w-1); // right 2 pixels already included above } + + void drawRect(const Scalar x, const Scalar y, const Scalar w, const Scalar h, Color ct, Color cl, Color cb, Color cr) { + setColor(ct); drawLineHor(x, x+w-1, y); // top + setColor(cb); drawLineHor(x, x+w-1, y+h-1); // bottom + setColor(cl); drawLineVer(y+1, y+h-2, x); // left 2 pixels already included above + setColor(cr); drawLineVer(y+1, y+h-2, x+w-1); // right 2 pixels already included above + } // void drawRect(const Scalar x1, const Scalar y1, const Scalar x2, const Scalar y2) { diff --git a/ext/rf/SX1276.h b/ext/rf/SX1276.h index 6148690..bf0df4a 100644 --- a/ext/rf/SX1276.h +++ b/ext/rf/SX1276.h @@ -111,8 +111,8 @@ template class SX // PA config static constexpr const uint8_t PA_BOOST = 0x80; - static constexpr const uint8_t PA_OUTPUT_RFO_PIN = 0; - static constexpr const uint8_t PA_OUTPUT_PA_BOOST_PIN = 1; + static constexpr const uint8_t PA_OUTPUT_RFO_PIN = 0; // max output: +14 dB + static constexpr const uint8_t PA_OUTPUT_PA_BOOST_PIN = 1; // max output: +20 dB // IRQ marks static constexpr const uint8_t IRQ_TX_DONE_MASK = 0x08; @@ -125,12 +125,12 @@ template class SX static constexpr const char* NAME = "LoRa"; uint32_t curFreq = 0; - + uint8_t curTXP = 17; public: struct PacketDetails { - int8_t rssi; + int16_t rssi; // can be smaller than -128! float snr; }; @@ -159,10 +159,10 @@ public: writeRegister(REG_FIFO_RX_BASE_ADDR, 0); // set LNA boost - writeRegister(REG_LNA, readRegister(REG_LNA) | 0x03); + writeRegister(REG_LNA, readRegister(REG_LNA) | 0x03); // 0b11 = boost on, 150% LNA current // set auto AGC - writeRegister(REG_MODEM_CONFIG_3, 0x04); + writeRegister(REG_MODEM_CONFIG_3, 0x04); // LNA-Gain set by the internal AGC loop // set output power to 17 dBm setTxPower(17); @@ -262,6 +262,67 @@ public: return -1; } + /** + * configure the TX power (in dB) + * two output pins are possible: + * RFO (max +14dB) + * PA_BOOST (max +20dB) + */ + void setTxPower(int level, int outputPin = PA_OUTPUT_PA_BOOST_PIN) { + + debugMod1(NAME, "setTx(%d dB)", level); + + // the +14 dB pin // RFO + if (PA_OUTPUT_RFO_PIN == outputPin) { + + if (level < 0 || level > 14) {errorMod1(NAME, "setTxPower(RFO, %d) TX Power out of range", level); return;} + curTXP = level; // cache + + writeRegister(REG_PA_CONFIG, 0x70 | level); + + // the +20dbPin PA BOOST + } else if (PA_OUTPUT_PA_BOOST_PIN == outputPin) { + + if (level < 2 || level > 20) {errorMod1(NAME, "setTxPower(PA_BOOST, %d) TX Power out of range", level); return;} + curTXP = level; // cache + + if (level > 17) { + + // subtract 3 from level, so 18 - 20 maps to 15 - 17 + level -= 3; + + // High Power +20 dBm Operation (Semtech SX1276/77/78/79 5.4.3.) + writeRegister(REG_PA_DAC, 0x87); + setOCP(150); + + } else { + + //Default value PA_HF/LF or +17dBm + writeRegister(REG_PA_DAC, 0x84); + setOCP(100); + + } + + // enable PA_BOOST, and set (level-3-2) // a level of 15 is MAX and equals +20dB with PABoost + writeRegister(REG_PA_CONFIG, PA_BOOST | (level - 2)); + + } else { + debugMod(NAME, "failed to set TX Power"); + } + + + + + } + + /** get the current transmit power (from cache) */ + uint8_t getTxPower() { + return curTXP; + } + + + + /** switch to idle mode */ void idle() { debugMod(NAME, "idle()"); @@ -418,43 +479,7 @@ private: writeRegister(REG_FRF_LSB, (uint8_t)(frf >> 0)); } - /** configure the TX power (in dB). but what is the pin for??? */ - void setTxPower(int level, int outputPin = PA_OUTPUT_PA_BOOST_PIN) { - - debugMod1(NAME, "setTx(%d dB)", level); - - if (PA_OUTPUT_RFO_PIN == outputPin) { - - // RFO - if (level < 0) {level = 0;} - if (level > 14) {level = 14;} - writeRegister(REG_PA_CONFIG, 0x70 | level); - - } else { - - // PA BOOST - if (level > 17) { - - if (level > 20) {level = 20;} - // subtract 3 from level, so 18 - 20 maps to 15 - 17 - level -= 3; - - // High Power +20 dBm Operation (Semtech SX1276/77/78/79 5.4.3.) - writeRegister(REG_PA_DAC, 0x87); - setOCP(140); - - } else { - if (level < 2) {level = 2;} - //Default value PA_HF/LF or +17dBm - writeRegister(REG_PA_DAC, 0x84); - setOCP(100); - } - - writeRegister(REG_PA_CONFIG, PA_BOOST | (level - 2)); - - } - } /** perform hard reset */ void reset() { @@ -477,12 +502,12 @@ private: sleep(); } - /** what exactly is this? */ + /** over-current protection */ void setOCP(uint8_t mA) { uint8_t ocpTrim = 27; if (mA <= 120) {ocpTrim = (mA - 45) / 5;} else if (mA <= 240) {ocpTrim = (mA + 30) / 10;} - writeRegister(REG_OCP, 0x20 | (0x1F & ocpTrim)); + writeRegister(REG_OCP, 0x20 | (0b11111 & ocpTrim)); } /** what exactly is this? Low-DataRate-Optimizer??? */