JTEncode.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * JTEncode.h - JT65/JT9/WSPR/FSQ encoder library for Arduino
  3. *
  4. * Copyright (C) 2015-2016 Jason Milldrum <milldrum@gmail.com>
  5. *
  6. * Based on the algorithms presented in the WSJT software suite.
  7. * Thanks to Andy Talbot G4JNT for the whitepaper on the WSPR encoding
  8. * process that helped me to understand all of this.
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include "int.h"
  24. #include "rs_common.h"
  25. #include "Arduino.h"
  26. #include <stdint.h>
  27. #define JT65_SYMBOL_COUNT 126
  28. #define JT9_SYMBOL_COUNT 85
  29. #define JT4_SYMBOL_COUNT 207
  30. #define WSPR_SYMBOL_COUNT 162
  31. #define JT65_ENCODE_COUNT 63
  32. #define JT9_ENCODE_COUNT 69
  33. #define JT9_BIT_COUNT 206
  34. #define JT4_BIT_COUNT 206
  35. #define WSPR_BIT_COUNT 162
  36. // Define the structure of a varicode table
  37. typedef struct fsq_varicode
  38. {
  39. uint8_t ch;
  40. uint8_t var[2];
  41. } Varicode;
  42. // The FSQ varicode table, based on the FSQ Varicode V3.0
  43. // document provided by Murray Greenman, ZL1BPU
  44. const Varicode fsq_code_table[] PROGMEM =
  45. {
  46. {' ', {00, 00}}, // space
  47. {'!', {11, 30}},
  48. {'"', {12, 30}},
  49. {'#', {13, 30}},
  50. {'$', {14, 30}},
  51. {'%', {15, 30}},
  52. {'&', {16, 30}},
  53. {'\'', {17, 30}},
  54. {'(', {18, 30}},
  55. {')', {19, 30}},
  56. {'*', {20, 30}},
  57. {'+', {21, 30}},
  58. {',', {27, 29}},
  59. {'-', {22, 30}},
  60. {'.', {27, 00}},
  61. {'/', {23, 30}},
  62. {'0', {10, 30}},
  63. {'1', {01, 30}},
  64. {'2', {02, 30}},
  65. {'3', {03, 30}},
  66. {'4', {04, 30}},
  67. {'5', {05, 30}},
  68. {'6', {06, 30}},
  69. {'7', {07, 30}},
  70. {'8', {8, 30}},
  71. {'9', {9, 30}},
  72. {':', {24, 30}},
  73. {';', {25, 30}},
  74. {'<', {26, 30}},
  75. {'=', {00, 31}},
  76. {'>', {27, 30}},
  77. {'?', {28, 29}},
  78. {'@', {00, 29}},
  79. {'A', {01, 29}},
  80. {'B', {02, 29}},
  81. {'C', {03, 29}},
  82. {'D', {04, 29}},
  83. {'E', {05, 29}},
  84. {'F', {06, 29}},
  85. {'G', {07, 29}},
  86. {'H', {8, 29}},
  87. {'I', {9, 29}},
  88. {'J', {10, 29}},
  89. {'K', {11, 29}},
  90. {'L', {12, 29}},
  91. {'M', {13, 29}},
  92. {'N', {14, 29}},
  93. {'O', {15, 29}},
  94. {'P', {16, 29}},
  95. {'Q', {17, 29}},
  96. {'R', {18, 29}},
  97. {'S', {19, 29}},
  98. {'T', {20, 29}},
  99. {'U', {21, 29}},
  100. {'V', {22, 29}},
  101. {'W', {23, 29}},
  102. {'X', {24, 29}},
  103. {'Y', {25, 29}},
  104. {'Z', {26, 29}},
  105. {'[', {01, 31}},
  106. {'\\', {02, 31}},
  107. {']', {03, 31}},
  108. {'^', {04, 31}},
  109. {'_', {05, 31}},
  110. {'`', {9, 31}},
  111. {'a', {01, 00}},
  112. {'b', {02, 00}},
  113. {'c', {03, 00}},
  114. {'d', {04, 00}},
  115. {'e', {05, 00}},
  116. {'f', {06, 00}},
  117. {'g', {07, 00}},
  118. {'h', {8, 00}},
  119. {'i', {9, 00}},
  120. {'j', {10, 00}},
  121. {'k', {11, 00}},
  122. {'l', {12, 00}},
  123. {'m', {13, 00}},
  124. {'n', {14, 00}},
  125. {'o', {15, 00}},
  126. {'p', {16, 00}},
  127. {'q', {17, 00}},
  128. {'r', {18, 00}},
  129. {'s', {19, 00}},
  130. {'t', {20, 00}},
  131. {'u', {21, 00}},
  132. {'v', {22, 00}},
  133. {'w', {23, 00}},
  134. {'x', {24, 00}},
  135. {'y', {25, 00}},
  136. {'z', {26, 00}},
  137. {'{', {06, 31}},
  138. {'|', {07, 31}},
  139. {'}', {8, 31}},
  140. {'~', {00, 30}},
  141. {127, {28, 31}}, // DEL
  142. {13, {28, 00}}, // CR
  143. {10, {28, 00}}, // LF
  144. {0, {28, 30}}, // IDLE
  145. {241, {10, 31}}, // plus/minus
  146. {246, {11, 31}}, // division sign
  147. {248, {12, 31}}, // degrees sign
  148. {158, {13, 31}}, // multiply sign
  149. {156, {14, 31}}, // pound sterling sign
  150. {8, {27, 31}} // BS
  151. };
  152. const uint8_t crc8_table[] PROGMEM = {
  153. 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31,
  154. 0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
  155. 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, 0xe0, 0xe7, 0xee, 0xe9,
  156. 0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
  157. 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1,
  158. 0xb4, 0xb3, 0xba, 0xbd, 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
  159. 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, 0xb7, 0xb0, 0xb9, 0xbe,
  160. 0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
  161. 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16,
  162. 0x03, 0x04, 0x0d, 0x0a, 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
  163. 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, 0x89, 0x8e, 0x87, 0x80,
  164. 0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
  165. 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8,
  166. 0xdd, 0xda, 0xd3, 0xd4, 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
  167. 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, 0x19, 0x1e, 0x17, 0x10,
  168. 0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
  169. 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f,
  170. 0x6a, 0x6d, 0x64, 0x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
  171. 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7,
  172. 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
  173. 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef,
  174. 0xfa, 0xfd, 0xf4, 0xf3
  175. };
  176. const uint8_t jt9i[JT9_BIT_COUNT] PROGMEM = {
  177. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0x10, 0x90, 0x50, 0x30, 0xb0, 0x70,
  178. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0x18, 0x98, 0x58, 0x38, 0xb8, 0x78,
  179. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0x14, 0x94, 0x54, 0x34, 0xb4, 0x74,
  180. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0x1c, 0x9c, 0x5c, 0x3c, 0xbc, 0x7c,
  181. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0x12, 0x92, 0x52, 0x32, 0xb2, 0x72,
  182. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0x1a, 0x9a, 0x5a, 0x3a, 0xba, 0x7a,
  183. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0x16, 0x96, 0x56, 0x36, 0xb6, 0x76,
  184. 0x0e, 0x8e, 0x4e, 0x2e, 0xae, 0x6e, 0x1e, 0x9e, 0x5e, 0x3e, 0xbe, 0x7e, 0x01,
  185. 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0x11, 0x91, 0x51, 0x31, 0xb1, 0x71, 0x09,
  186. 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0x19, 0x99, 0x59, 0x39, 0xb9, 0x79, 0x05,
  187. 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0x15, 0x95, 0x55, 0x35, 0xb5, 0x75, 0x0d,
  188. 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0x1d, 0x9d, 0x5d, 0x3d, 0xbd, 0x7d, 0x03,
  189. 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0x13, 0x93, 0x53, 0x33, 0xb3, 0x73, 0x0b,
  190. 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0x1b, 0x9b, 0x5b, 0x3b, 0xbb, 0x7b, 0x07,
  191. 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0x17, 0x97, 0x57, 0x37, 0xb7, 0x77, 0x0f,
  192. 0x8f, 0x4f, 0x2f, 0xaf, 0x6f, 0x1f, 0x9f, 0x5f, 0x3f, 0xbf, 0x7f
  193. };
  194. class JTEncode
  195. {
  196. public:
  197. JTEncode(void);
  198. void jt65_encode(const char *, uint8_t *);
  199. void jt9_encode(const char *, uint8_t *);
  200. void jt4_encode(const char *, uint8_t *);
  201. void wspr_encode(const char *, const char *, const uint8_t, uint8_t *);
  202. void fsq_encode(const char *, const char *, uint8_t *);
  203. void fsq_dir_encode(const char *, const char *, const char, const char *, uint8_t *);
  204. private:
  205. uint8_t jt_code(char);
  206. uint8_t wspr_code(char);
  207. uint8_t gray_code(uint8_t);
  208. void jt_message_prep(char *);
  209. void wspr_message_prep(char *, char *, uint8_t);
  210. void jt65_bit_packing(char *, uint8_t *);
  211. void jt9_bit_packing(char *, uint8_t *);
  212. void wspr_bit_packing(uint8_t *);
  213. void jt65_interleave(uint8_t *);
  214. void jt9_interleave(uint8_t *);
  215. void wspr_interleave(uint8_t *);
  216. void jt9_packbits(uint8_t *, uint8_t *);
  217. void jt_gray_code(uint8_t *, uint8_t);
  218. void jt65_merge_sync_vector(uint8_t *, uint8_t *);
  219. void jt9_merge_sync_vector(uint8_t *, uint8_t *);
  220. void jt4_merge_sync_vector(uint8_t *, uint8_t *);
  221. void wspr_merge_sync_vector(uint8_t *, uint8_t *);
  222. void convolve(uint8_t *, uint8_t *, uint8_t, uint8_t);
  223. void rs_encode(uint8_t *, uint8_t *);
  224. void encode_rs_int(void *,data_t *, data_t *);
  225. void free_rs_int(void *);
  226. void * init_rs_int(int, int, int, int, int, int);
  227. uint8_t crc8(const char *);
  228. void * rs_inst;
  229. char callsign[7];
  230. char locator[5];
  231. uint8_t power;
  232. };