greeter

Morse code raspberry pi pico

Oct 15th, 2023
1,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. /**
  2.  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
  3.  *
  4.  * SPDX-License-Identifier: BSD-3-Clause
  5.  */
  6.  
  7. #include "pico/stdlib.h"
  8. #include "hardware/pwm.h"
  9. #include <string.h>
  10.  
  11. #ifndef PICO_DEFAULT_LED_PIN
  12. #warning blink example requires a board with a regular LED
  13. #else
  14. #define LED 25
  15. #define SND 15
  16.  
  17. #define BASE 100
  18. //above is base speed of single . unit of morse in milliseconds
  19.  
  20. void blink_dit() {
  21.     gpio_put(LED, 1);
  22.     pwm_set_gpio_level(SND, 15000);
  23.     sleep_ms(BASE);
  24.     gpio_put(LED, 0);
  25.     pwm_set_gpio_level(SND, 0);
  26.     sleep_ms(BASE);
  27.     return;
  28. }
  29.  
  30. void blink_dat() {
  31.     gpio_put(LED, 1);
  32.     pwm_set_gpio_level(SND, 15000);
  33.     sleep_ms(BASE * 3);
  34.     gpio_put(LED, 0);
  35.     pwm_set_gpio_level(SND, 0);
  36.     sleep_ms(BASE);
  37.     return;
  38. }
  39.  
  40. void output(char *a) {
  41.     int i = 0;
  42.     while (a[i] != '\0') {
  43.         if (a[i] == '.') blink_dit();
  44.         if (a[i] == '-') blink_dat();
  45.         if (a[i] == 'S') sleep_ms(BASE); //represents silence one base unit long, or one .
  46.         i++;
  47.     }
  48.     sleep_ms(BASE * 3);
  49.     return;
  50. }
  51.  
  52. void morse(char *a) {
  53.     int i = 0, j = strlen(a);
  54.     for (i = 0; i != j; i++) {
  55.         if (a[i] == 'A' || a[i] == 'a') output(".-");
  56.         if (a[i] == 'B' || a[i] == 'b') output("-...");
  57.         if (a[i] == 'C' || a[i] == 'c') output("-.-.");
  58.         if (a[i] == 'D' || a[i] == 'd') output("-..");
  59.         if (a[i] == 'E' || a[i] == 'e') output(".");
  60.         if (a[i] == 'F' || a[i] == 'f') output("..-.");
  61.         if (a[i] == 'G' || a[i] == 'g') output("--.");
  62.         if (a[i] == 'H' || a[i] == 'h') output("....");
  63.         if (a[i] == 'I' || a[i] == 'i') output("..");
  64.         if (a[i] == 'J' || a[i] == 'j') output(".---");
  65.         if (a[i] == 'K' || a[i] == 'k') output("-.-");
  66.         if (a[i] == 'L' || a[i] == 'l') output(".-..");
  67.         if (a[i] == 'M' || a[i] == 'm') output("--");
  68.         if (a[i] == 'N' || a[i] == 'n') output("-.");
  69.         if (a[i] == 'O' || a[i] == 'o') output("---");
  70.         if (a[i] == 'P' || a[i] == 'p') output(".--.");
  71.         if (a[i] == 'Q' || a[i] == 'q') output("--.-");
  72.         if (a[i] == 'R' || a[i] == 'r') output(".-.");
  73.         if (a[i] == 'S' || a[i] == 's') output("...");
  74.         if (a[i] == 'T' || a[i] == 't') output("-");
  75.         if (a[i] == 'U' || a[i] == 'u') output("..-");
  76.         if (a[i] == 'V' || a[i] == 'v') output("...-");
  77.         if (a[i] == 'W' || a[i] == 'w') output(".--");
  78.         if (a[i] == 'X' || a[i] == 'x') output("-..-");
  79.         if (a[i] == 'Y' || a[i] == 'y') output("-.--");
  80.         if (a[i] == 'Z' || a[i] == 'z') output("--..");
  81.         if (a[i] == '0') output("-----");
  82.         if (a[i] == '1') output(".----");
  83.         if (a[i] == '2') output("..---");
  84.         if (a[i] == '3') output("...--");
  85.         if (a[i] == '4') output("....-");
  86.         if (a[i] == '5') output(".....");
  87.         if (a[i] == '6') output("-....");
  88.         if (a[i] == '7') output("--...");
  89.         if (a[i] == '8') output("---..");
  90.         if (a[i] == '9') output("----.");
  91.         if (a[i] == '.') output(".-.-.-SSSSSSS");
  92.         if (a[i] == '?') output("..--..SSSSSSS");
  93.         if (a[i] == ' ') output("SSS");
  94.     }
  95.     return;
  96. }
  97.  
  98. int main() {
  99.     int i = 0;
  100.     uint slice_snd;
  101.  
  102.     sleep_ms(5000);
  103.  
  104.     gpio_init(LED);
  105.     gpio_init(SND);
  106.     gpio_set_function(SND, GPIO_FUNC_PWM);
  107.     gpio_set_dir(LED, GPIO_OUT);
  108.  
  109.     pwm_config config = pwm_get_default_config();
  110.     slice_snd = pwm_gpio_to_slice_num(SND);
  111.     pwm_init(slice_snd, &config, true);
  112.  
  113.     morse("HAVING FUN WITH PULSE WIDTH MODULATION");
  114.  
  115. #endif
  116.     return 0;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment