diff --git a/libraries/morse/README.md b/libraries/morse/README.md new file mode 100644 index 000000000..dc1bd5910 --- /dev/null +++ b/libraries/morse/README.md @@ -0,0 +1,90 @@ +# Morse Arduino Library + +A simple Arduino library for decoding Morse code. +It provides a lookup table and functions to translate Morse sequences (`.` and `-`) into letters and numbers. +Reusable across projects for IoT, communication experiments, and custom input devices. + +--- + +## Features + +- Decode Morse sequences (e.g. `".-" → A`). +- Supports **A–Z** and **0–9**. +- Lightweight: uses only a lookup table and string compare. +- Works on all Arduino boards (`architectures=*`). + +--- + +## Installation + +### Method 1: Arduino IDE Library Manager + +_Not yet published to Library Manager – coming soon._ + +### Method 2: Manual Installation + +1. Download or clone this repo: + ```bash + git clone https://github.com/aryankajiwala/Morse.git + ``` +2. Move the folder into your Arduino libraries folder: + ``` + Documents/Arduino/libraries/Morse/ + ``` +3. Restart Arduino IDE. +4. Include the library in your sketch: + ```cpp + #include + ``` + +--- + +## Usage + +### Simple Test + +```cpp +#include + +void setup() { + Serial.begin(9600); + + char test = decodeMorse(".-"); // should return 'A' + Serial.print("Decoded: "); + Serial.println(test); +} + +void loop() { +} +``` + +**Output:** + +``` +Decoded: A +``` + +--- + +## API Reference + +### `char decodeMorse(const char* code)` + +- **Input:** Morse sequence as `const char*` (e.g., `".-"`). +- **Output:** Corresponding ASCII character (`'A'`) or `'?'` if unknown. + +--- + +## Roadmap + +- [ ] Add **encoder function** (`encodeMorse('A') → ".-"`). +- [ ] Add support for **punctuation** (`. , ? !`). +- [ ] Publish to Arduino Library Manager. + +--- + +## Author + +**Aryan Kajiwala** +📧 aryanbkaji124@gmail.com +🔗 [GitHub](https://github.com/AryanKaji) diff --git a/libraries/morse/examples/ButtonMorse/ButtonMorse.ino b/libraries/morse/examples/ButtonMorse/ButtonMorse.ino new file mode 100644 index 000000000..967524c90 --- /dev/null +++ b/libraries/morse/examples/ButtonMorse/ButtonMorse.ino @@ -0,0 +1,34 @@ +#include + +#define DOT_BUTTON 2 +#define DASH_BUTTON 3 + +String currentCode = ""; +unsigned long lastPressTime = 0; +const unsigned long letterGap = 1500; + +void setup() { + pinMode(DOT_BUTTON, INPUT_PULLUP); + pinMode(DASH_BUTTON, INPUT_PULLUP); + Serial.begin(9600); +} + +void loop() { + if (digitalRead(DOT_BUTTON) == LOW) { + currentCode += "."; + delay(300); + lastPressTime = millis(); + } + + if (digitalRead(DASH_BUTTON) == LOW) { + currentCode += "-"; + delay(300); + lastPressTime = millis(); + } + + if (currentCode.length() > 0 && millis() - lastPressTime > letterGap) { + char decoded = decodeMorse(currentCode.c_str()); + Serial.print(decoded); + currentCode = ""; + } +} diff --git a/libraries/morse/examples/HelloMorse/HelloMorse.ino b/libraries/morse/examples/HelloMorse/HelloMorse.ino new file mode 100644 index 000000000..ac7c66502 --- /dev/null +++ b/libraries/morse/examples/HelloMorse/HelloMorse.ino @@ -0,0 +1,29 @@ +#include + +void setup() { + Serial.begin(9600); + Serial.println("Morse Library Example"); + Serial.println("----------------------"); + + // Decode a simple Morse sequence + char letter = decodeMorse(".-"); // should return 'A' + Serial.print("Decoded '.-': "); + Serial.println(letter); + + letter = decodeMorse("..."); // should return 'S' + Serial.print("Decoded '...': "); + Serial.println(letter); + + letter = decodeMorse("----."); // should return '9' + Serial.print("Decoded '----.': "); + Serial.println(letter); + + // Unknown sequence + letter = decodeMorse("..--.."); // not defined + Serial.print("Decoded '..--..': "); + Serial.println(letter); // should print '?' +} + +void loop() { + // Nothing here for now +} diff --git a/libraries/morse/keywords.txt b/libraries/morse/keywords.txt new file mode 100644 index 000000000..f848e853a --- /dev/null +++ b/libraries/morse/keywords.txt @@ -0,0 +1,2 @@ +decodeMorse KEYWORD2 +MorseMap KEYWORD1 diff --git a/libraries/morse/library.json b/libraries/morse/library.json new file mode 100644 index 000000000..742d26cf3 --- /dev/null +++ b/libraries/morse/library.json @@ -0,0 +1,15 @@ +{ + "name": "Morse", + "keywords": ["KEYWORD1", "KEYWORD2"], + "repository": { + "type": "git", + "url": "https://github.com/AryanKaji/Morse.git" + }, + "version": "1.0.0", + "author": "Aryan Kajiwala", + "maintainer": "Aryan Kajiwala ", + "sentence": "A simple Arduino library for decoding Morse code.", + "paragraph": "Provides a lookup table and functions to decode Morse sequences into characters. Can be reused across projects to interpret dot-dash sequences as letters and numbers. Includes functions for single letters and full string decoding.", + "category": "Communication", + "architectures": "*" +} diff --git a/libraries/morse/library.properties b/libraries/morse/library.properties new file mode 100644 index 000000000..96559fd03 --- /dev/null +++ b/libraries/morse/library.properties @@ -0,0 +1,9 @@ +name=Morse +version=1.0.0 +author=Aryan Kajiwala +maintainer=Aryan Kajiwala +sentence=A simple Arduino library for decoding Morse code. +paragraph=paragraph=Provides a lookup table and functions to decode Morse sequences into characters. Can be reused across projects to interpret dot-dash sequences as letters and numbers. Includes functions for single letters and full string decoding. +category=Communication +url=https://github.com/aryankajiwala/Morse +architectures=* diff --git a/libraries/morse/src/Morse.cpp b/libraries/morse/src/Morse.cpp new file mode 100644 index 000000000..e54b61c62 --- /dev/null +++ b/libraries/morse/src/Morse.cpp @@ -0,0 +1,24 @@ +#include "Morse.h" + +// Morse lookup table (A–Z, 0–9) +const MorseMap morseTable[] = { + {".-", 'A'}, {"-...", 'B'}, {"-.-.", 'C'}, {"-..", 'D'}, {".", 'E'}, + {"..-.", 'F'}, {"--.", 'G'}, {"....", 'H'}, {"..", 'I'}, {".---", 'J'}, + {"-.-", 'K'}, {".-..", 'L'}, {"--", 'M'}, {"-.", 'N'}, {"---", 'O'}, + {".--.", 'P'}, {"--.-", 'Q'}, {".-.", 'R'}, {"...", 'S'}, {"-", 'T'}, + {"..-", 'U'}, {"...-", 'V'}, {".--", 'W'}, {"-..-", 'X'}, {"-.--", 'Y'}, + {"--..", 'Z'}, + + {"-----", '0'}, {".----", '1'}, {"..---", '2'}, {"...--", '3'}, {"....-", '4'}, + {".....", '5'}, {"-....", '6'}, {"--...", '7'}, {"---..", '8'}, {"----.", '9'} +}; + +// Decode a Morse sequence into a character +char decodeMorse(const char* code) { + for (unsigned int i = 0; i < sizeof(morseTable) / sizeof(morseTable[0]); i++) { + if (strcmp(code, morseTable[i].code) == 0) { + return morseTable[i].letter; + } + } + return '?'; // Unknown sequence +} diff --git a/libraries/morse/src/Morse.h b/libraries/morse/src/Morse.h new file mode 100644 index 000000000..2b1fd9a6a --- /dev/null +++ b/libraries/morse/src/Morse.h @@ -0,0 +1,15 @@ +#ifndef MORSE_H +#define MORSE_H + +#include + +// Morse lookup table entry +struct MorseMap { + const char* code; + char letter; +}; + +// Function to decode a Morse sequence into a character +char decodeMorse(const char* code); + +#endif