From f51229ef9a0be16079360641843edf219e0ad112 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 27 Nov 2022 20:47:01 +0100 Subject: [PATCH] [Pager] Added option to invert frequencies (#7) --- src/protocols/Pager/Pager.cpp | 22 ++++++++++++++++------ src/protocols/Pager/Pager.h | 7 ++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/protocols/Pager/Pager.cpp b/src/protocols/Pager/Pager.cpp index 90ddaa59..144903a2 100644 --- a/src/protocols/Pager/Pager.cpp +++ b/src/protocols/Pager/Pager.cpp @@ -19,7 +19,7 @@ PagerClient::PagerClient(PhysicalLayer* phy) { _readBitInstance = _phy; } -int16_t PagerClient::begin(float base, uint16_t speed, uint16_t shift) { +int16_t PagerClient::begin(float base, uint16_t speed, bool invert, uint16_t shift) { // calculate duration of 1 bit in us _speed = (float)speed/1000.0f; _bitDuration = (uint32_t)1000000/speed; @@ -34,6 +34,7 @@ int16_t PagerClient::begin(float base, uint16_t speed, uint16_t shift) { // calculate raw frequency shift _shiftHz = shift; _shift = _shiftHz/step; + inv = invert; // initialize BCH encoder encoderInit(); @@ -433,14 +434,23 @@ void PagerClient::write(uint32_t codeWord) { for(int8_t i = 31; i >= 0; i--) { uint32_t mask = (uint32_t)0x01 << i; uint32_t start = mod->micros(); + + // figure out the shift direction - start by assuming the bit is 0 + int16_t change = _shift; + + // now check if it's actually 1 if(codeWord & mask) { - // send 1 - _phy->transmitDirect(_baseRaw + _shift); - } else { - // send 0 - _phy->transmitDirect(_baseRaw - _shift); + change = -_shift; } + // finally, check if inversion is enabled + if(inv) { + change = -change; + } + + // now transmit the shifted frequency + _phy->transmitDirect(_baseRaw + change); + // this is pretty silly, while(mod->micros() ... ) would be enough // but for some reason, MegaCore throws a linker error on it // "relocation truncated to fit: R_AVR_7_PCREL against `no symbol'" diff --git a/src/protocols/Pager/Pager.h b/src/protocols/Pager/Pager.h index 6a831b9e..eb828fbb 100644 --- a/src/protocols/Pager/Pager.h +++ b/src/protocols/Pager/Pager.h @@ -84,9 +84,13 @@ class PagerClient { \param speed Bit rate to use in bps. Common POCSAG decoders can receive 512, 1200 and 2400 bps. + \param invert Enable frequency inversion. Disabled by default (high frequency is digital 0). + + \param shift Set custom frequency shift, defaults to 4500 Hz. + \returns \ref status_codes */ - int16_t begin(float base, uint16_t speed, uint16_t shift = RADIOLIB_PAGER_FREQ_SHIFT_HZ); + int16_t begin(float base, uint16_t speed, bool invert = false, uint16_t shift = RADIOLIB_PAGER_FREQ_SHIFT_HZ); /*! \brief Method to send a tone-only alert to a destination pager. @@ -201,6 +205,7 @@ class PagerClient { uint32_t _readBatchPos; uint32_t _filterAddr; uint32_t _filterMask; + bool inv = false; // BCH encoder int32_t _bchAlphaTo[RADIOLIB_PAGER_BCH_N + 1];