RadioLib
Universal wireless communication library for Arduino
Pager.h
1 #if !defined(_RADIOLIB_PAGER_H) && !defined(RADIOLIB_EXCLUDE_PAGER)
2 #define _RADIOLIB_PAGER_H
3 
4 #include "../../TypeDef.h"
5 #include "../PhysicalLayer/PhysicalLayer.h"
6 
7 // frequency shift in Hz
8 #define RADIOLIB_PAGER_FREQ_SHIFT_HZ (4500)
9 
10 // supported encoding schemes
11 #define RADIOLIB_PAGER_ASCII (0)
12 #define RADIOLIB_PAGER_BCD (1)
13 
14 // preamble length in 32-bit code words
15 #define RADIOLIB_PAGER_PREAMBLE_LENGTH (18)
16 
17 // protocol-specified code words
18 #define RADIOLIB_PAGER_PREAMBLE_CODE_WORD (0xAAAAAAAA)
19 #define RADIOLIB_PAGER_FRAME_SYNC_CODE_WORD (0x7CD215D8)
20 #define RADIOLIB_PAGER_IDLE_CODE_WORD (0x7A89C197)
21 
22 // code word type identification flags
23 #define RADIOLIB_PAGER_ADDRESS_CODE_WORD (0UL)
24 #define RADIOLIB_PAGER_MESSAGE_CODE_WORD (1UL)
25 
26 // length of code word in bits
27 #define RADIOLIB_PAGER_CODE_WORD_LEN (32)
28 
29 // number of message bits in a single code block
30 #define RADIOLIB_PAGER_ADDRESS_POS (13)
31 #define RADIOLIB_PAGER_FUNC_BITS_POS (11)
32 #define RADIOLIB_PAGER_MESSAGE_BITS_LENGTH (20)
33 #define RADIOLIB_PAGER_MESSAGE_END_POS (11)
34 
35 // number of code words in a batch
36 #define RADIOLIB_PAGER_BATCH_LEN (16)
37 
38 // mask for address bits in a single code word
39 #define RADIOLIB_PAGER_ADDRESS_BITS_MASK (0x7FFFE000UL)
40 
41 // mask for function bits in a single code word
42 #define RADIOLIB_PAGER_FUNCTION_BITS_MASK (0x00001800UL)
43 
44 // mask for BCH bits in a single code word
45 #define RADIOLIB_PAGER_BCH_BITS_MASK (0x000007FFUL)
46 
47 // message type functional bits
48 #define RADIOLIB_PAGER_FUNC_BITS_NUMERIC (0b00UL << RADIOLIB_PAGER_FUNC_BITS_POS)
49 #define RADIOLIB_PAGER_FUNC_BITS_TONE (0b01UL << RADIOLIB_PAGER_FUNC_BITS_POS)
50 #define RADIOLIB_PAGER_FUNC_BITS_ALPHA (0b11UL << RADIOLIB_PAGER_FUNC_BITS_POS)
51 
52 // the maximum allowed address (2^22 - 1)
53 #define RADIOLIB_PAGER_ADDRESS_MAX (2097151)
54 
55 // BCH(31, 21) code constants
56 #define RADIOLIB_PAGER_BCH_M (5)
57 #define RADIOLIB_PAGER_BCH_N (31)
58 #define RADIOLIB_PAGER_BCH_K (21)
59 #define RADIOLIB_PAGER_BCH_D (5)
60 
61  // BCH(31, 21) primitive polynomial x^5 + x^2 + 1
62 #define RADIOLIB_PAGER_BCH_PRIMITIVE_POLY (0x25)
63 
69 class PagerClient {
70  public:
76  explicit PagerClient(PhysicalLayer* phy);
77 
78  // basic methods
79 
89  int16_t begin(float base, uint16_t speed);
90 
98  int16_t sendTone(uint32_t addr);
99 
111  int16_t transmit(String& str, uint32_t addr, uint8_t encoding = RADIOLIB_PAGER_BCD);
112 
124  int16_t transmit(const char* str, uint32_t addr, uint8_t encoding = RADIOLIB_PAGER_BCD);
125 
139  int16_t transmit(uint8_t* data, size_t len, uint32_t addr, uint8_t encoding = RADIOLIB_PAGER_BCD);
140 
152  int16_t startReceive(RADIOLIB_PIN_TYPE pin, uint32_t addr, uint32_t mask = 0xFFFFF);
153 
159  size_t available();
160 
171  int16_t readData(String& str, size_t len = 0);
172 
184  int16_t readData(uint8_t* data, size_t* len);
185 
186 #if !defined(RADIOLIB_GODMODE)
187  private:
188 #endif
189  PhysicalLayer* _phy;
190 
191  float _base;
192  float _speed;
193  uint32_t _baseRaw;
194  uint16_t _shift;
195  uint16_t _bitDuration;
196  uint32_t _readBatchPos;
197  uint32_t _filterAddr;
198  uint32_t _filterMask;
199 
200  // BCH encoder
201  int32_t _bchAlphaTo[RADIOLIB_PAGER_BCH_N + 1];
202  int32_t _bchIndexOf[RADIOLIB_PAGER_BCH_N + 1];
203  int32_t _bchG[RADIOLIB_PAGER_BCH_N - RADIOLIB_PAGER_BCH_K + 1];
204 
205  void write(uint32_t* data, size_t len);
206  void write(uint32_t codeWord);
207  uint32_t read();
208 
209  uint8_t encodeBCD(char c);
210  char decodeBCD(uint8_t b);
211 
212  void encoderInit();
213  uint32_t encodeBCH(uint32_t data);
214 };
215 
216 #endif
PagerClient::startReceive
int16_t startReceive(RADIOLIB_PIN_TYPE pin, uint32_t addr, uint32_t mask=0xFFFFF)
Start reception of POCSAG packets.
Definition: Pager.cpp:214
PagerClient::begin
int16_t begin(float base, uint16_t speed)
Initialization method.
Definition: Pager.cpp:22
PagerClient::transmit
int16_t transmit(String &str, uint32_t addr, uint8_t encoding=RADIOLIB_PAGER_BCD)
Arduino String transmit method.
Definition: Pager.cpp:48
PagerClient::PagerClient
PagerClient(PhysicalLayer *phy)
Default constructor.
Definition: Pager.cpp:17
PagerClient::readData
int16_t readData(String &str, size_t len=0)
Reads data that was received after calling startReceive method.
Definition: Pager.cpp:246
PagerClient
Client for Pager communication.
Definition: Pager.h:69
PagerClient::sendTone
int16_t sendTone(uint32_t addr)
Method to send a tone-only alert to a destination pager.
Definition: Pager.cpp:44
PhysicalLayer
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition: PhysicalLayer.h:14
PagerClient::available
size_t available()
Get the number of POCSAG batches available in buffer. Limited by the size of direct mode buffer!
Definition: Pager.cpp:242