Guest User

Untitled

a guest
May 26th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #define ALPH_LEN 26
  2. #define UNIT 100
  3.  
  4. #include "nrf_delay.h"
  5.  
  6. void morse(char ch, int led)
  7. {
  8. /* even numbers represent a code of a letter
  9. * e.g.: -... -> 0111 -> 1110 -> 0xe
  10. * odd numbers represent the length of a character
  11. */
  12. static char code[ALPH_LEN*2] = {
  13. 0x1,2, 0xe,4, 0xa,4, 0x6,3, 0x1,1, 0xb,4, 0x4,3, 0xf,4, 0x3,2, 0x1,4,
  14. 0x2,3, 0xd,4, 0x0,2, 0x2,2, 0x0,3, 0x9,4, 0x4,4, 0x5,3, 0x7,3, 0x0,1,
  15. 0x3,3, 0x7,4, 0x1,3, 0x6,4, 0x2,4, 0xc,4
  16. };
  17.  
  18. if (ch == ' ') {
  19. nrf_delay_ms(UNIT*4);
  20. return;
  21. }
  22. if (ch < 'a' || ch > 'z') return;
  23.  
  24. char letter = code[2*(ch - 'a')];
  25. char count = code[2*(ch - 'a') + 1];
  26. char signal;
  27. for (char i = 0; i < count; ++i) {
  28. signal = letter & 1;
  29.  
  30. bsp_board_led_invert(led);
  31. nrf_delay_ms(UNIT * (signal ? 1 : 3));
  32. bsp_board_led_invert(led);
  33.  
  34. letter >>= 1;
  35. nrf_delay_ms(UNIT);
  36. }
  37. nrf_delay_ms(UNIT*3);
  38. }
  39.  
  40. #undef UNIT
  41. #undef ALPH_LEN
Add Comment
Please, Sign In to add comment