morse.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef MORSE_DECODER_H_
  2. #define MORSE_DECODER_H_
  3. #include <stdint.h>
  4. /* Low level morse symbols */
  5. enum morse_marks {
  6. MORSE_DIT = 0, /* . */
  7. MORSE_DAH = 1, /* - */
  8. };
  9. /* A morse symbol.
  10. * bit 0-8: The transmitted marks (dit, dah)
  11. * bit 12-15: The symbol length. In number of marks.
  12. */
  13. typedef uint16_t morse_sym_t;
  14. /* Construct a morse symbol */
  15. #define __MORSE_SYM(marks, size) ((morse_sym_t)((marks) | (size) << 12))
  16. #define MORSE_MARK(type, index) ((morse_sym_t)((type) << (index)))
  17. #define MORSE_MAX_NR_MARKS 9
  18. #define MORSE_SYM_1(m0) __MORSE_SYM(((m0) << 0), 1)
  19. #define MORSE_SYM_2(m0, m1) __MORSE_SYM(((m0) << 0) | ((m1) << 1), 2)
  20. #define MORSE_SYM_3(m0, m1, m2) __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2), 3)
  21. #define MORSE_SYM_4(m0, m1, m2, m3) __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3), 4)
  22. #define MORSE_SYM_5(m0, m1, m2, m3, m4) __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  23. ((m4) << 4), 5)
  24. #define MORSE_SYM_6(m0, m1, m2, m3, m4, m5) \
  25. __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  26. ((m4) << 4) | ((m5) << 5), 6)
  27. #define MORSE_SYM_7(m0, m1, m2, m3, m4, m5, m6) \
  28. __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  29. ((m4) << 4) | ((m5) << 5) | ((m6) << 6), 7)
  30. #define MORSE_SYM_8(m0, m1, m2, m3, m4, m5, m6, m7) \
  31. __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  32. ((m4) << 4) | ((m5) << 5) | ((m6) << 6) | ((m7) << 7), 8)
  33. #define MORSE_SYM_9(m0, m1, m2, m3, m4, m5, m6, m7, m8) \
  34. __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  35. ((m4) << 4) | ((m5) << 5) | ((m6) << 6) | ((m7) << 7) | \
  36. ((m8) << 8), 9)
  37. static inline uint8_t morse_sym_size(morse_sym_t sym)
  38. {
  39. return (sym & 0xF000) >> 12;
  40. }
  41. static inline void morse_sym_set_size(morse_sym_t *sym, uint8_t size)
  42. {
  43. *sym |= size << 12;
  44. }
  45. /* Returns the marks */
  46. #define MORSE_SYM_MARKS(symbol) ((symbol) & 0x1FF)
  47. /* Returns true, if the symbol is a space between words */
  48. #define MORSE_SYM_IS_SPACE(symbol) ((symbol) == 0)
  49. /* The morse alphabet */
  50. enum morse_character {
  51. MORSE_INVALID = 0,
  52. /* Characters */
  53. MORSE_A = 'A',
  54. MORSE_B = 'B',
  55. MORSE_C = 'C',
  56. MORSE_D = 'D',
  57. MORSE_E = 'E',
  58. MORSE_F = 'F',
  59. MORSE_G = 'G',
  60. MORSE_H = 'H',
  61. MORSE_I = 'I',
  62. MORSE_J = 'J',
  63. MORSE_K = 'K',
  64. MORSE_L = 'L',
  65. MORSE_M = 'M',
  66. MORSE_N = 'N',
  67. MORSE_O = 'O',
  68. MORSE_P = 'P',
  69. MORSE_Q = 'Q',
  70. MORSE_R = 'R',
  71. MORSE_S = 'S',
  72. MORSE_T = 'T',
  73. MORSE_U = 'U',
  74. MORSE_V = 'V',
  75. MORSE_W = 'W',
  76. MORSE_X = 'X',
  77. MORSE_Y = 'Y',
  78. MORSE_Z = 'Z',
  79. MORSE_CHARS_START = MORSE_A,
  80. MORSE_CHARS_END = MORSE_Z,
  81. /* Numbers */
  82. MORSE_0 = '0',
  83. MORSE_1 = '1',
  84. MORSE_2 = '2',
  85. MORSE_3 = '3',
  86. MORSE_4 = '4',
  87. MORSE_5 = '5',
  88. MORSE_6 = '6',
  89. MORSE_7 = '7',
  90. MORSE_8 = '8',
  91. MORSE_9 = '9',
  92. MORSE_NUMS_START = MORSE_0,
  93. MORSE_NUMS_END = MORSE_9,
  94. /* Special characters */
  95. MORSE_GACC_A = 128, /* grave accent A */
  96. MORSE_AE, /* Ä */
  97. MORSE_GACC_E, /* grave accent E */
  98. MORSE_AACC_E, /* acute accent E */
  99. MORSE_OE, /* Ö */
  100. MORSE_UE, /* Ü */
  101. MORSE_SZ, /* ß */
  102. MORSE_CH, /* CH */
  103. MORSE_TILDE_N, /* tilde N */
  104. MORSE_PERIOD, /* . */
  105. MORSE_COMMA, /* , */
  106. MORSE_COLON, /* : */
  107. MORSE_SEMICOLON, /* ; */
  108. MORSE_QUESTION, /* ? */
  109. MORSE_DASH, /* - */
  110. MORSE_UNDERSCORE, /* _ */
  111. MORSE_PAREN_OPEN, /* ( */
  112. MORSE_PAREN_CLOSE, /* ) */
  113. MORSE_TICK, /* ' */
  114. MORSE_EQUAL, /* = */
  115. MORSE_PLUS, /* + */
  116. MORSE_SLASH, /* / */
  117. MORSE_AT, /* @ */
  118. MORSE_SPACE, /* */
  119. MORSE_SPEC_START = MORSE_GACC_A,
  120. MORSE_SPEC_END = MORSE_SPACE,
  121. /* Signals */
  122. MORSE_SIG_KA = 192, /* Start */
  123. MORSE_SIG_BT, /* Pause */
  124. MORSE_SIG_AR, /* End */
  125. MORSE_SIG_VE, /* Understood */
  126. MORSE_SIG_SK, /* End of work */
  127. MORSE_SIG_SOS, /* SOS */
  128. MORSE_SIG_ERROR, /* Error */
  129. MORSE_SIG_START = MORSE_SIG_KA,
  130. MORSE_SIG_END = MORSE_SIG_ERROR,
  131. };
  132. /* Morse factors - In multiples of "dit" */
  133. enum morse_factors {
  134. FACTOR_DIT = 1,
  135. FACTOR_DAH = 3,
  136. FACTOR_INTER_MARK = 1,
  137. FACTOR_INTER_CHAR = 3,
  138. FACTOR_INTER_WORD = 7,
  139. };
  140. /* Length of one "dit" in milliseconds at a rate
  141. * of one word per minute. */
  142. #define DIT_LENGTH_1WPM_MS 1200
  143. morse_sym_t morse_encode_character(enum morse_character mchar);
  144. enum morse_character morse_decode_symbol(morse_sym_t sym);
  145. int8_t morse_to_ascii(char *buf, uint8_t buf_size,
  146. enum morse_character mchar);
  147. #endif /* MORSE_DECODER_H_ */