commit e86628da6bc1703aa6fbd5beb096f641abe17a28 from: Tracey Emery date: Wed Aug 31 20:29:11 2022 UTC Initial import commit - /dev/null commit + e86628da6bc1703aa6fbd5beb096f641abe17a28 blob - /dev/null blob + 2cd894f75acda6b6877dd308f19bb24622fc4ffd (mode 644) --- /dev/null +++ LICENSE.md @@ -0,0 +1,25 @@ +BSD 2-Clause License + +Copyright (c) 2021, 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 + e04b367485d953e5e43cbf236716a71e146b97f3 (mode 644) --- /dev/null +++ Morse.cpp @@ -0,0 +1,295 @@ +/* + * Copyright (c) 2021, 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. + */ + +#include "Morse.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 15 + +#define MAX_RAD 127.5 + +#define UNIT_T(x) (60.0 / (50.0 * (float)x)) * 1000.0 + +volatile uint8_t gpio_wpm; + +volatile uint8_t gpio_tx_pin; + +volatile float gpio_unit_t; + +volatile uint8_t gpio_tx_sending = 0; +uint8_t gpio_tx_len, gpio_tx_enc; + +String gpio_tx_str; + +uint8_t gpio_this_index = 0, gpio_next_index = 0; +uint8_t gpio_handle_unit, gpio_unit_handled, gpio_bit; +uint8_t gpio_digraph = 0, gpio_inited = 0; + +unsigned long gpio_tx_start_millis, gpio_tx_current_millis; +unsigned long gpio_handle_unit_millis; + +uint8_t ctob(uint8_t c); + +void gpio_stop(void); +void gpio_handle_chars(void); +void gpio_handle_units(uint8_t c); + +Morse::Morse(uint8_t type, uint8_t the_pin) +{ + Morse(type, the_pin, D_WPM); +} + +Morse::Morse(uint8_t type, uint8_t the_pin, uint8_t the_wpm) +{ + switch(type) { + case M_GPIO: + if (!gpio_inited) { + gpio_wpm = the_wpm; + gpio_tx_pin = the_pin; + gpio_unit_t = UNIT_T(gpio_wpm); + gpio_inited = 1; + } + break; + default: + exit(1); + break; + } +} + +// gpio functions + +uint8_t +Morse::gpio_transmitting(void) +{ + if (gpio_tx_sending) + return 1; + + return 0; +} + +void +Morse::gpio_tx_stop(void) +{ + gpio_stop(); +} + +void +Morse::gpio_watchdog(void) +{ + if (gpio_inited) + gpio_handle_chars(); +} + +void +Morse::gpio_tx(String tx) +{ + // if we are sending, return + if (gpio_tx_sending) + return; + + // start by setting our tx pin low + digitalWrite(gpio_tx_pin, LOW); + + // gather our starting variables + gpio_tx_sending = 1; + gpio_tx_len = tx.length(); + tx.toUpperCase(); + gpio_tx_str = tx; + + gpio_this_index = 0; + gpio_next_index = 1; + + gpio_tx_start_millis = millis(); + gpio_tx_current_millis = millis(); + gpio_handle_unit_millis = millis(); +} + +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_tx_start_millis = gpio_tx_current_millis; + 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; + } 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); +} + +void +gpio_handle_units(uint8_t 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 + + (gpio_tx_current_millis - gpio_tx_start_millis); + } else { + if (bitRead(c, gpio_bit)) { + gpio_handle_unit_millis = DAH * gpio_unit_t + + (gpio_tx_current_millis - + gpio_tx_start_millis); + digitalWrite(gpio_tx_pin, HIGH); + } else { + gpio_handle_unit_millis = DIT * gpio_unit_t + + (gpio_tx_current_millis - + gpio_tx_start_millis); + digitalWrite(gpio_tx_pin, HIGH); + } + } + gpio_handle_unit = 1; + gpio_tx_start_millis = gpio_tx_current_millis; + } + + // set led off, handle IC_SP, or handle C_SP + if (gpio_handle_unit && !gpio_unit_handled && !gpio_next_index && + gpio_tx_sending && + (millis() - gpio_tx_start_millis) >= gpio_handle_unit_millis) { + gpio_tx_start_millis = gpio_tx_current_millis; + digitalWrite(gpio_tx_pin, LOW); + gpio_bit++; + + if (c >> (gpio_bit + 1) || gpio_digraph) + gpio_handle_unit_millis = IC_SP * gpio_unit_t + + (gpio_tx_current_millis - gpio_tx_start_millis); + else + gpio_handle_unit_millis = C_SP * gpio_unit_t + + (gpio_tx_current_millis - gpio_tx_start_millis); + + gpio_unit_handled = 1; + } + + // we're done, start next + if (gpio_unit_handled && gpio_tx_sending && + (millis() - gpio_tx_start_millis) >= gpio_handle_unit_millis) { + gpio_tx_start_millis = gpio_tx_current_millis; + gpio_unit_handled = 0; + gpio_handle_unit = 0; + + // hit the end of that byte + if (!(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(); + return; + } + } +} + +void +gpio_stop(void) +{ + gpio_digraph = 0; + gpio_tx_sending = 0; + gpio_this_index = 0; + gpio_next_index = 0; +} + +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 + f9459dab3489ba5e96eab01c3eafdfba6e1ceae8 (mode 644) --- /dev/null +++ Morse.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021, 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. + */ + +#ifndef _MORSE_H +#define _MORSE_H + +#include "Arduino.h" + +class +Morse +{ + public: + Morse(uint8_t type, uint8_t the_pin); + Morse(uint8_t type, uint8_t the_pin, uint8_t the_wpm); + + uint8_t gpio_transmitting(void); + + void gpio_tx(String tx); + void gpio_tx_stop(void); + void gpio_watchdog(void); + +}; + +enum +{ + M_GPIO, + M_ADC +}; + +#endif // _MORSE_H blob - /dev/null blob + 2a8f07fd12ab179a443420dadc811ba538498a68 (mode 644) --- /dev/null +++ README.md @@ -0,0 +1,50 @@ +# Arduino Morse Code + +Arduino 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/Arduino-Morse-Code.git libraries/Morse + +Two class instantiation methods are in place: + + Morse(TYPE, PIN); + Morse(TYPE, PIN, WPM); + +Types available are: + + M_GPIO // GPIO Pin Type + +Pro-sign Morse Code can be generated using backticks: + + String tx = "ab2cd de a2bcd `sos`"; + +To enable the Morse Code, a watchdog is available: + + morse.gpio_watchdog(); + +Example +------- + +See the [LED Example](examples/LED/LED.ino) file for more information. + +Notes +----- + +I don't pretend to know anything about writing Arduino libraries. This style is +OpenBSD style and may not look right in the Arduino IDE, since I don't use it. + +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 + 1eb4c80c3f9b8fd76fc607f9e025e227ba07ebad (mode 644) --- /dev/null +++ examples/LED/LED.ino @@ -0,0 +1,49 @@ +#include "Morse.h" + +#define WPM 17 +#define LEDP 13 // most arduino LED pins +#define GSECS 2000 // seconds between transmissions +#define DSECS 4000 + +unsigned long current_gpio_millis, start_gpio_millis; +void setup(void); +void loop(void); + + +Morse morse(M_GPIO, LEDP, WPM); + +// backticks will toggle digraph sending + +String tx = "ab7cd de a7bcd k `ar`"; + +void +setup(void) +{ + Serial.begin(115200); + + pinMode(LEDP, OUTPUT); + digitalWrite(LEDP, LOW); + + // setup millis + start_gpio_millis = millis(); + current_gpio_millis = millis(); + + delay(2000); + Serial.println("LED setup"); +} + +void +loop(void) +{ + current_gpio_millis = millis(); + + morse.gpio_watchdog(); + + // it's up to the programmer to properly space transmissions + if (!morse.gpio_transmitting() && + (current_gpio_millis - start_gpio_millis >= GSECS)) { + Serial.println("Transmit via LED"); + morse.gpio_tx(tx); + } else if (morse.gpio_transmitting()) + start_gpio_millis = current_gpio_millis; +} blob - /dev/null blob + bbf950e464acaaf7feb29e3534a03cfb6e429433 (mode 644) --- /dev/null +++ library.properties @@ -0,0 +1,9 @@ +name=Arduino Morse Code Library +version=1.0.0 +author=Tracey Emery K7TLE +maintainer=Tracey Emery +sentence=GPIO Morse Code Generator +paragraph=Send Morse code a GPIO pins +category=Communication +url=https://github.com/basepr1me/Arduino-Morse-Code +architectures=* blob - /dev/null blob + 3d5453248510836471a5dd35d50eb9e43c69967a (mode 644) --- /dev/null +++ license.txt @@ -0,0 +1,13 @@ +Copyright (c) 2021, 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.