A walkthrough of the code architecture and the key components that make it all tick.
| Action | Input |
|---|---|
| Dot (.) | Short left click |
| Dash (−) | Long left click (>200ms) |
| Complete character | 1.5 second pause |
| Next word | 3 second pause |
| Complete message & read aloud | Right click |
| Reset session | Double right click |
| Switch Mouse / Keyboard mode | TAB |
| Exit | ESC or hold both buttons 5s |
Pygame captures MOUSEBUTTONDOWN and MOUSEBUTTONUP events. The time between press and release determines whether it's a dot (<200ms) or a dash (≥200ms).
Each dot or dash is appended to current_morse. A beep plays immediately — 800 Hz for dots, 600 Hz for dashes.
A background thread monitors the time since the last input. After 1.5 seconds of silence, it looks up the Morse buffer in the dictionary and decodes the character.
The decoded letter is shown on the Sense HAT's 8×8 LED matrix using sense.show_letter(). The display is rotated 270° to match the physical orientation.
After 3 seconds of silence, the current word is completed and a new word begins. Words are tracked in a list.
Right-click joins all words into a sentence, scrolls it on the LED, reads it aloud with text-to-speech, and plays the Mario celebration tune.
The heart of the decoder — a Python dictionary mapping Morse patterns to characters:
How the system distinguishes dots from dashes:
Beeps are generated as sine waves, not pre-recorded files:
A background thread watches for pauses to auto-complete characters and words:
The main event loop, timeout monitor, audio playback, and LED display all run concurrently. Thread locks (input_lock, led_lock) prevent race conditions.
The system tracks state across multiple variables — current Morse buffer, current word, word list, press timing — to correctly handle the complex input flow.
If the Sense HAT isn't connected, the system continues without LED display. If TTS fails, it falls back to espeak. Every component has error handling.
Instead of using audio files, sounds are generated mathematically using sine waves with attack/decay envelopes for clean, professional-sounding beeps and tunes.