morse_encoder.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef MORSE_ENCODER_H_
  2. #define MORSE_ENCODER_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_SYM_1(m0) __MORSE_SYM(((m0) << 0), 1)
  17. #define MORSE_SYM_2(m0, m1) __MORSE_SYM(((m0) << 0) | ((m1) << 1), 2)
  18. #define MORSE_SYM_3(m0, m1, m2) __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2), 3)
  19. #define MORSE_SYM_4(m0, m1, m2, m3) __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3), 4)
  20. #define MORSE_SYM_5(m0, m1, m2, m3, m4) __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  21. ((m4) << 4), 5)
  22. #define MORSE_SYM_6(m0, m1, m2, m3, m4, m5) \
  23. __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  24. ((m4) << 4) | ((m5) << 5), 6)
  25. #define MORSE_SYM_7(m0, m1, m2, m3, m4, m5, m6) \
  26. __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  27. ((m4) << 4) | ((m5) << 5) | ((m6) << 6), 7)
  28. #define MORSE_SYM_8(m0, m1, m2, m3, m4, m5, m6, m7) \
  29. __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  30. ((m4) << 4) | ((m5) << 5) | ((m6) << 6) | ((m7) << 7), 8)
  31. #define MORSE_SYM_9(m0, m1, m2, m3, m4, m5, m6, m7, m8) \
  32. __MORSE_SYM(((m0) << 0) | ((m1) << 1) | ((m2) << 2) | ((m3) << 3) | \
  33. ((m4) << 4) | ((m5) << 5) | ((m6) << 6) | ((m7) << 7) | \
  34. ((m8) << 8), 9)
  35. /* Get the size (in number of marks) of a morse symbol */
  36. #define MORSE_SYM_SIZE(symbol) (((uint16_t)(symbol) & 0xF000) >> 12)
  37. /* Returns the marks */
  38. #define MORSE_SYM_MARKS(symbol) ((symbol) & 0x1FF)
  39. /* Returns true, if the symbol is a space between words */
  40. #define MORSE_SYM_IS_SPACE(symbol) ((symbol) == 0)
  41. #endif /* MORSE_ENCODER_H_ */