commit 2c35f57cb5f767632443388b8d43631df74c13a9 from: Tracey Emery date: Thu Sep 15 21:34:04 2022 UTC initial import commit - /dev/null commit + 2c35f57cb5f767632443388b8d43631df74c13a9 blob - /dev/null blob + c6b3846233176ce97bfe42ceb9ceaa6a70c4dd71 (mode 644) --- /dev/null +++ CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.13) + +include(/usr/local/raspberrypi-pico-sdk/pico_sdk_init.cmake) + +project(morse) + +# initialize the Raspberry Pi Pico SDK +pico_sdk_init() + +add_executable(morse + morse.cpp + libmorse.cpp + ) + +# pull in common dependencies +target_link_libraries(morse + pico_stdlib + ) + +pico_enable_stdio_usb(morse 1) +pico_enable_stdio_uart(morse 0) + +# create map/bin/hex/uf2 file etc. +pico_add_extra_outputs(morse) blob - /dev/null blob + c563e44eaaf4220ffd3d5698a440b47cc0157543 (mode 644) --- /dev/null +++ LICENSE.md @@ -0,0 +1,25 @@ +BSD 2-Clause License + +Copyright (c) 2022, Tracey Emery K7TLE +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. blob - /dev/null blob + 20aa50925d2dc68bd835f4ac927db5df2e3bdeb8 (mode 644) --- /dev/null +++ README.md @@ -0,0 +1,45 @@ +# Raspberry Pi Pico Morse Code + +Raspberry Pi Morse Code Library + +This Morse Code library allows the programmer to generate Morse Code via a GPIO +pin. + +Usage +----- + +Clone the library to your project: + + git clone https://github.com/basepr1me/Raspberry-Pi-Morse-Code.git + +Two class instantiation methods are in place: + + Morse(TYPE, PIN, TX_PAUSE); + Morse(TYPE, PIN, TX_PAUSE, WPM); + +Types available are: + + M_GPIO // GPIO Pin Type + +Pro-sign Morse Code can be generated using backticks: + + char led_morse[] = "Hello giant world `ar`"; + +Example +------- + +See the [LED Example](morse.cpp) file for more information. + +Notes +----- + +The make.sh file is designed for OpenBSD ksh. + +Author +------ + +[Tracey Emery](https://github.com/basepr1me/) + +If you like this software, consider [donating](https://k7tle.com/?donate=1). + +See the [License](LICENSE.md) file for more information. blob - /dev/null blob + bf70cd85440adcb813070fb2d630591cdb1d113b (mode 644) --- /dev/null +++ libmorse.cpp @@ -0,0 +1,329 @@ +/* + * Copyright (c) 2022 Tracey Emery + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include "pico/stdlib.h" +#include "libmorse.h" + +#define DIT 1.0 +#define DAH 3.0 +#define IC_SP 1.0 +#define C_SP 3.0 +#define W_SP 7.0 + +#define D_WPM 10 + +#define UNIT_T(x) (60.0 / (50.0 * (float)x)) * 1000.0 +#define bit_read(value, bit) (((value) >> (bit)) & 0x01) + +static volatile uint8_t gpio_wpm; +static volatile uint8_t gpio_tx_pin; +static volatile uint64_t gpio_pause; + +static volatile float gpio_unit_t; + +static volatile uint8_t gpio_tx_sending = 0; +static volatile uint8_t gpio_unit_handled; + +static uint8_t gpio_tx_len, gpio_tx_enc; +static uint8_t ctob(uint8_t); +static uint8_t gpio_this_index = 0, gpio_next_index = 0; +static uint8_t gpio_handle_unit, gpio_bit; +static uint8_t gpio_digraph = 0, gpio_inited = 0; + +static unsigned long gpio_handle_unit_millis; + +static char *gpio_tx_str = NULL; + +void gpio_stop(void); +void gpio_handle_chars(void); +void gpio_handle_units(uint8_t c); + +int64_t gpio_unit_handled_cb(alarm_id_t, void *); +int64_t gpio_pause_handled_cb(alarm_id_t, void *); +int64_t gpio_tx_handled_cb(alarm_id_t, void *); + +Morse::Morse(uint8_t type, uint8_t the_pin, unsigned long the_pause) +{ + Morse(type, the_pin, the_pause, D_WPM); +} + +Morse::Morse(uint8_t type, uint8_t the_pin, unsigned long the_pause, + uint8_t the_wpm) +{ + switch(type) { + case M_GPIO: + if (!gpio_inited) { + gpio_wpm = the_wpm; + gpio_pause = the_pause * 1000; + gpio_tx_pin = the_pin; + gpio_init(gpio_tx_pin); + gpio_set_dir(gpio_tx_pin, GPIO_OUT); + gpio_unit_t = UNIT_T(gpio_wpm); + gpio_inited = 1; + } + break; + case M_DAC: + // we don't have a dac, but we will setup pwm + break; + default: + exit(1); + break; + } +} + +void +Morse::gpio_set_transmitting(void) +{ + gpio_tx_sending = 1; +} + +uint8_t +Morse::gpio_get_transmitting(void) +{ + return gpio_tx_sending; +} + +void +Morse::gpio_tx_stop(void) +{ + gpio_stop(); +} + +void +Morse::gpio_tx(const char tx[]) +{ + int i; + + if (gpio_tx_sending) + return; + else { + gpio_put(gpio_tx_pin, 0); + gpio_tx_sending = 1; + + gpio_tx_len = strlen(tx); + + 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); + } +} + +void +gpio_stop(void) +{ + gpio_digraph = 0; + 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 +gpio_tx_handled_cb(alarm_id_t id, void *data) +{ + gpio_this_index = 0; + gpio_next_index = 1; + + gpio_handle_chars(); + + return 0; +} + +int64_t +gpio_unit_handled_cb(alarm_id_t id, void *data) +{ + gpio_put(gpio_tx_pin, 0); + gpio_bit++; + gpio_unit_handled = 1; + + gpio_handle_chars(); + + return 0; +} + +struct pause_cb { + uint8_t c; +}; + +int64_t +gpio_pause_handled_cb(alarm_id_t id, void *data) +{ + struct pause_cb *pcb = (struct pause_cb *) data; + + gpio_unit_handled = 0; + gpio_handle_unit = 0; + + // hit the end of that byte + if (!(pcb->c >> (gpio_bit + 1))) { + gpio_bit = 0; + gpio_next_index = 1; + gpio_this_index++; + } + + // hit the end of the tx + if (gpio_this_index == gpio_tx_len) + gpio_stop(); + + gpio_handle_chars(); + + return 0; +} + +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) { + gpio_handle_unit_millis = W_SP * gpio_unit_t; + } else { + 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) { + case 32: return 0b1; // ' ' + case 33: return 0b1110101; // '!' + case 34: return 0b1010010; // '"' + case 36: return 0b11001000; // '$' + case 38: return 0b100010; // '&' + case 39: return 0b1011110; // '\'' + case 40: return 0b101101; // '(' + case 41: return 0b1101101; // ')' + case 43: return 0b101010; // '+' 'AR' + case 44: return 0b1110011; // ',' + case 45: return 0b1100001; // '-' + case 46: return 0b1101010; // '.' + case 47: return 0b101001; // '/' + + case 48: return 0b111111; // '0' + case 49: return 0b111110; // '1' + case 50: return 0b111100; // '2' + case 51: return 0b111000; // '3' + case 52: return 0b110000; // '4' + case 53: return 0b100000; // '5' + case 54: return 0b100001; // '6' + case 55: return 0b100011; // '7' + case 56: return 0b100111; // '8' + case 57: return 0b101111; // '9' + + case 58: return 0b1000111; // ':' + case 59: return 0b1010101; // ';' + case 61: return 0b110001; // '=' 'BT' + case 63: return 0b1001100; // '?' + case 64: return 0b1010110; // '@' + + case 65: return 0b110; // 'A' + case 66: return 0b10001; // 'B' + case 67: return 0b10101; // 'C' + case 68: return 0b1001; // 'D' + case 69: return 0b10; // 'E' + case 70: return 0b10100; // 'F' + case 71: return 0b1011; // 'G' + case 72: return 0b10000; // 'H' + case 73: return 0b100; // 'I' + case 74: return 0b11110; // 'J' + case 75: return 0b1101; // 'K' + case 76: return 0b10010; // 'L' + case 77: return 0b111; // 'M' + case 78: return 0b101; // 'N' + case 79: return 0b1111; // 'O' + case 80: return 0b10110; // 'P' + case 81: return 0b11011; // 'Q' + case 82: return 0b1010; // 'R' + case 83: return 0b1000; // 'S' + case 84: return 0b11; // 'T' + case 85: return 0b1100; // 'U' + case 86: return 0b11000; // 'V' + case 87: return 0b1110; // 'W' + case 88: return 0b11001; // 'X' + case 89: return 0b11101; // 'Y' + case 90: return 0b10011; // 'Z' + + case 95: return 0b1101100; // '_' + + default: return 0b11000000; // non (126) + } +}; blob - /dev/null blob + c710b6ca4779d1026ef486c4e9741d39be7e5fed (mode 644) --- /dev/null +++ libmorse.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 Tracey Emery + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _LIBMORSE_H +#define _LIBMORSE_H + +class +Morse +{ + public: + Morse(uint8_t type, uint8_t the_pin, + unsigned long the_pause); + Morse(uint8_t type, uint8_t the_pin, + unsigned long the_pause, uint8_t the_wpm); + + uint8_t gpio_get_transmitting(void); + void gpio_set_transmitting(void); + + void gpio_tx(const char[]); + void gpio_tx_stop(void); + void gpio_watchdog(void); +}; + +enum +{ + M_GPIO, + M_DAC, + M_ADC // in the event we ever want to translate incoming morse +}; + +#endif // _LIBMORSE_H blob - /dev/null blob + 53083494aae683f7c4aec7c813583596adcfaa9d (mode 644) --- /dev/null +++ license.txt @@ -0,0 +1,13 @@ +Copyright (c) 2022 Tracey Emery K7TLE + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. blob - /dev/null blob + 7395a32ce9b3be3ab120794d9dd03a38f6d6c432 (mode 755) --- /dev/null +++ make.sh @@ -0,0 +1,23 @@ +#!/bin/ksh +clear +printf "\nRUNNING THIS WILL DESTROY YOUR BUILD DIRECTORY!!!" +printf "\nDo you wish to proceed? [Y|n]: " + +read proceed + +if [[ $proceed != "y" && $proceed != "Y" && $proceed != "" ]]; then + printf "\nExitting!\n" + exit +fi + +printf "\nBuilding project...\n\n"; + +rm -rf build +mkdir build && cd build +cmake -DPICO_TOOLCHAIN_PATH=/usr/local/arm-none-eabi/bin .. +make + +printf "\nFiles built:\n\n" +ls -lah . + +printf "\nDone\n\n"; blob - /dev/null blob + cbc34d9c1a33d9342c38d5f68fc5a46b545e92ca (mode 644) --- /dev/null +++ morse.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Tracey Emery + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include "pico/stdlib.h" +#include "libmorse.h" + +#define WPM 10 +#define TX_PAUSE 3 // pause before transmitting and between TXs + +const uint pin = PICO_DEFAULT_LED_PIN; + +Morse morse_led(M_GPIO, pin, TX_PAUSE, WPM); + +int main(void); + +int +main() +{ + char led_morse[] = "Hello giant world `ar`"; + + stdio_init_all(); + + while (1) { + morse_led.gpio_tx(led_morse); + } + + return 0; +}