RadioLib
Universal wireless communication library for Arduino
FEC.h
1 #if !defined(_RADIOLIB_FEC_H)
2 #define _RADIOLIB_FEC_H
3 
4 #include "../TypeDef.h"
5 #include "../Module.h"
6 #if defined(RADIOLIB_BUILD_ARDUINO)
7 #include "../ArduinoHal.h"
8 #endif
9 
10 // BCH(31, 21) code constants
11 #define RADIOLIB_PAGER_BCH_N (31)
12 #define RADIOLIB_PAGER_BCH_K (21)
13 #define RADIOLIB_PAGER_BCH_PRIMITIVE_POLY (0x25)
14 
15 #if RADIOLIB_STATIC_ONLY
16 #define RADIOLIB_BCH_MAX_N (63)
17 #define RADIOLIB_BCH_MAX_K (31)
18 #endif
19 
26 class RadioLibBCH {
27  public:
31  RadioLibBCH();
32 
36  ~RadioLibBCH();
37 
44  void begin(uint8_t n, uint8_t k, uint32_t poly);
45 
52  uint32_t encode(uint32_t dataword);
53 
54  private:
55  uint8_t n = 0;
56  uint8_t k = 0;
57  uint32_t poly = 0;
58  uint8_t m = 0;
59 
60  #if RADIOLIB_STATIC_ONLY
61  int32_t alphaTo[RADIOLIB_BCH_MAX_N + 1] = { 0 };
62  int32_t indexOf[RADIOLIB_BCH_MAX_N + 1] = { 0 };
63  int32_t generator[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1] = { 0 };
64  #else
65  int32_t* alphaTo = nullptr;
66  int32_t* indexOf = nullptr;
67  int32_t* generator = nullptr;
68  #endif
69 };
70 
71 // the global singleton
72 extern RadioLibBCH RadioLibBCHInstance;
73 
74 // macros to access bits in byte array, from http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html
75 #define SET_BIT_IN_ARRAY_MSB(A, k) ( A[((k)/8)] |= (1 << ((k)%8)) )
76 #define CLEAR_BIT_IN_ARRAY_MSB(A, k) ( A[((k)/8)] &= ~(1 << ((k)%8)) )
77 #define TEST_BIT_IN_ARRAY_MSB(A, k) ( A[((k)/8)] & (1 << ((k)%8)) )
78 #define GET_BIT_IN_ARRAY_MSB(A, k) ( (A[((k)/8)] & (1 << ((k)%8))) ? 1 : 0 )
79 #define SET_BIT_IN_ARRAY_LSB(A, k) ( A[((k)/8)] |= (1 << (7 - ((k)%8))) )
80 #define CLEAR_BIT_IN_ARRAY_LSB(A, k) ( A[((k)/8)] &= ~(1 << (7 - ((k)%8))) )
81 #define TEST_BIT_IN_ARRAY_LSB(A, k) ( A[((k)/8)] & (1 << (7 - ((k)%8))) )
82 #define GET_BIT_IN_ARRAY_LSB(A, k) ( (A[((k)/8)] & (1 << (7 - ((k)%8)))) ? 1 : 0 )
83 
124  public:
129 
134  void begin(uint8_t rt);
135 
146  int16_t encode(const uint8_t* in, size_t in_bits, uint8_t* out, size_t* out_bits = NULL);
147 
148  private:
149  uint8_t enc_state = 0;
150  uint8_t rate = 0;
151 };
152 
153 // each 32-bit word stores 8 values, one per each nibble
154 static const uint32_t ConvCodeTable1_3[16] = {
155  0x07347043, 0x61521625, 0x16256152, 0x70430734,
156  0x43703407, 0x25165261, 0x52612516, 0x34074370,
157  0x70430734, 0x16256152, 0x61521625, 0x07347043,
158  0x34074370, 0x52612516, 0x25165261, 0x43703407,
159 };
160 
161 static const uint32_t ConvCodeTable1_2[4] = {
162  0x03122130, 0x21300312, 0x30211203, 0x12033021,
163 };
164 
165 extern RadioLibConvCode RadioLibConvCodeInstance;
166 
167 #endif
Class to calculate Bose–Chaudhuri–Hocquenghem (BCH) class of forward error correction codes....
Definition: FEC.h:26
RadioLibBCH()
Default constructor.
Definition: FEC.cpp:4
void begin(uint8_t n, uint8_t k, uint32_t poly)
Initialization method.
Definition: FEC.cpp:21
~RadioLibBCH()
Default destructor.
Definition: FEC.cpp:8
uint32_t encode(uint32_t dataword)
Encoding method - encodes one data word (without check bits) into a code word (with check bits).
Definition: FEC.cpp:200
Class to perform convolutional coding wtih variable rates. Only 1/2 and 1/3 rate is currently support...
Definition: FEC.h:123
RadioLibConvCode()
Default constructor.
Definition: FEC.cpp:311
int16_t encode(const uint8_t *in, size_t in_bits, uint8_t *out, size_t *out_bits=NULL)
Encoding method.
Definition: FEC.cpp:320
void begin(uint8_t rt)
Initialization method.
Definition: FEC.cpp:315