commit d5071333fd7e00f134090b1e04c80a71aa88511a from: Tracey Emery date: Fri Sep 16 22:08:18 2022 UTC fix the first botch job commit - a21caa7dd27dbc82080dbab79457ae6fdafbf77b commit + d5071333fd7e00f134090b1e04c80a71aa88511a blob - c6b3846233176ce97bfe42ceb9ceaa6a70c4dd71 blob + 3afaead3b1ab8960c6e9c4e8fb673857ebaff134 --- CMakeLists.txt +++ CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(morse # pull in common dependencies target_link_libraries(morse pico_stdlib + pico_malloc ) pico_enable_stdio_usb(morse 1) blob - 6b762b223ca76e75079313a0b4bee29f549ebbee blob + 5922345fb5f9da14800d8e0b50e15f03ff61a2ef --- README.md +++ README.md @@ -1,7 +1,5 @@ # Raspberry Pi Pico Morse Code -XXX: a recently added feature broke this. Check back - Raspberry Pi Pico Morse Code Library This Morse Code library allows the programmer to generate Morse Code via a GPIO blob - bf70cd85440adcb813070fb2d630591cdb1d113b blob + 9a9d15378dac63fe6872ef48ec60d7fe693aa0ed --- libmorse.cpp +++ libmorse.cpp @@ -20,6 +20,8 @@ #include #include "pico/stdlib.h" +#include "pico/malloc.h" + #include "libmorse.h" #define DIT 1.0 @@ -108,7 +110,7 @@ Morse::gpio_tx_stop(void) } void -Morse::gpio_tx(const char tx[]) +Morse::gpio_tx(const char *morse) { int i; @@ -118,16 +120,9 @@ Morse::gpio_tx(const char tx[]) gpio_put(gpio_tx_pin, 0); gpio_tx_sending = 1; - gpio_tx_len = strlen(tx); + gpio_tx_len = strlen(morse); + gpio_tx_str = strndup(morse, gpio_tx_len); - char tx2[gpio_tx_len]; - for (i = 0; i < gpio_tx_len; i++) - tx2[i] = toupper(tx[i]); - - free(gpio_tx_str); - gpio_tx_str = NULL; - gpio_tx_str = strndup(tx2, gpio_tx_len); - add_alarm_in_ms(gpio_pause, gpio_tx_handled_cb, NULL, false); } } @@ -139,35 +134,7 @@ gpio_stop(void) gpio_tx_sending = 0; gpio_this_index = 0; gpio_next_index = 0; -} -void -gpio_handle_chars(void) -{ - /* gpio_tx_current_millis = millis(); */ - if (gpio_next_index) { - if (gpio_this_index == gpio_tx_len) { - gpio_stop(); - return; - } - gpio_next_index = 0; - gpio_handle_unit = 0; - gpio_unit_handled = 0; - if (gpio_tx_str[gpio_this_index] == 126) { - gpio_this_index++; - gpio_next_index = 1; - } else if (gpio_tx_str[gpio_this_index] == 96) { - gpio_digraph = !gpio_digraph; - gpio_this_index++; - gpio_next_index = 1; - gpio_handle_chars(); - } else { - gpio_tx_enc = ctob(gpio_tx_str[gpio_this_index]); - gpio_handle_units(gpio_tx_enc); - gpio_bit = 0; - } - } else if (gpio_tx_sending) - gpio_handle_units(gpio_tx_enc); } int64_t @@ -181,22 +148,32 @@ gpio_tx_handled_cb(alarm_id_t id, void *data) return 0; } +struct pause_cb { + uint8_t c; +}; +struct pause_cb pcb; + int64_t gpio_unit_handled_cb(alarm_id_t id, void *data) { + struct pause_cb *pcb = (struct pause_cb *) data; + gpio_put(gpio_tx_pin, 0); - gpio_bit++; gpio_unit_handled = 1; + gpio_bit++; - gpio_handle_chars(); + // handle IC_SP, or handle C_SP + if (pcb->c >> (gpio_bit + 1) || gpio_digraph) + gpio_handle_unit_millis = IC_SP * gpio_unit_t; + else + gpio_handle_unit_millis = C_SP * gpio_unit_t; + add_alarm_in_ms(gpio_handle_unit_millis, gpio_pause_handled_cb, pcb, + false); + return 0; } -struct pause_cb { - uint8_t c; -}; - int64_t gpio_pause_handled_cb(alarm_id_t id, void *data) { @@ -209,61 +186,79 @@ gpio_pause_handled_cb(alarm_id_t id, void *data) if (!(pcb->c >> (gpio_bit + 1))) { gpio_bit = 0; gpio_next_index = 1; + gpio_tx_str++; gpio_this_index++; - } + gpio_handle_chars(); + } else + gpio_handle_units(pcb->c); - // hit the end of the tx if (gpio_this_index == gpio_tx_len) gpio_stop(); - gpio_handle_chars(); - return 0; } void +gpio_handle_chars(void) +{ + if (gpio_this_index == gpio_tx_len) + gpio_stop(); + if (gpio_next_index) { + gpio_next_index = 0; + gpio_handle_unit = 0; + gpio_unit_handled = 0; + if (*gpio_tx_str == 126) { + gpio_tx_str++; + gpio_this_index++; + gpio_next_index = 1; + } else if (*gpio_tx_str == 96) { + gpio_digraph = !gpio_digraph; + gpio_tx_str++; + gpio_this_index++; + gpio_next_index = 1; + gpio_handle_chars(); + } else { + gpio_tx_enc = ctob(*gpio_tx_str); + gpio_handle_units(gpio_tx_enc); + gpio_bit = 0; + } + } else if (gpio_tx_sending) + gpio_handle_units(gpio_tx_enc); + +} + +void gpio_handle_units(uint8_t c) { - struct pause_cb pcb; - memset(&pcb, 0, sizeof(pcb)); pcb.c = c; // set led on and space off if (!gpio_next_index && !gpio_handle_unit && !gpio_unit_handled && gpio_tx_sending) { - if (c == 1) { + if (c == 1) gpio_handle_unit_millis = W_SP * gpio_unit_t; - } else { + else { + gpio_put(gpio_tx_pin, 1); if (bit_read(c, gpio_bit)) gpio_handle_unit_millis = DAH * gpio_unit_t; else gpio_handle_unit_millis = DIT * gpio_unit_t; - gpio_put(gpio_tx_pin, 1); } gpio_handle_unit = 1; add_alarm_in_ms(gpio_handle_unit_millis, gpio_unit_handled_cb, - NULL, false); - } - - // handle IC_SP, or handle C_SP - if (gpio_handle_unit && gpio_unit_handled && !gpio_next_index && - gpio_tx_sending) { - if (c >> (gpio_bit + 1) || gpio_digraph) - gpio_handle_unit_millis = IC_SP * gpio_unit_t; - else - gpio_handle_unit_millis = C_SP * gpio_unit_t; - - add_alarm_in_ms(gpio_handle_unit_millis, gpio_pause_handled_cb, &pcb, false); } + } static uint8_t ctob(uint8_t c) { - switch(c) { + uint8_t uc = toupper(c); + + switch(uc) { case 32: return 0b1; // ' ' case 33: return 0b1110101; // '!' case 34: return 0b1010010; // '"' blob - c710b6ca4779d1026ef486c4e9741d39be7e5fed blob + 5e78539a2a00f5c5c25f094acb19f832be476293 --- libmorse.h +++ libmorse.h @@ -29,7 +29,7 @@ Morse uint8_t gpio_get_transmitting(void); void gpio_set_transmitting(void); - void gpio_tx(const char[]); + void gpio_tx(const char *); void gpio_tx_stop(void); void gpio_watchdog(void); }; blob - cbc34d9c1a33d9342c38d5f68fc5a46b545e92ca blob + 53528edcf69b0c6b73541550242226f29206be01 --- morse.cpp +++ morse.cpp @@ -15,11 +15,14 @@ */ #include +#include #include "pico/stdlib.h" #include "libmorse.h" -#define WPM 10 +#define CALLS 3 + +#define WPM 15 #define TX_PAUSE 3 // pause before transmitting and between TXs const uint pin = PICO_DEFAULT_LED_PIN; @@ -31,12 +34,25 @@ int main(void); int main() { - char led_morse[] = "Hello giant world `ar`"; + uint8_t n = 0; + const char *led_morse[][CALLS] = { + { + "de k7tle `ar`", + "Bk", + "Hello", + } + }; + stdio_init_all(); while (1) { - morse_led.gpio_tx(led_morse); + if (!morse_led.gpio_get_transmitting()) { + n = 0 + (rand() % CALLS); + printf("Sending transmission %d: %s\n", n, + led_morse[0][n]); + morse_led.gpio_tx(led_morse[0][n]); + } } return 0;