Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Created December 23, 2023 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FoamyGuy/035ad1659a45a9ec82691b625d031c1f to your computer and use it in GitHub Desktop.
Save FoamyGuy/035ad1659a45a9ec82691b625d031c1f to your computer and use it in GitHub Desktop.
import board
from adafruit_dotstar import DotStar
import time
import random
# XMorse Code
"""
RGB LEDs blinking
short blinks will be red
long blinks will be green
in between characters there will 1 short white blink
at the end of the message there will be 3 short white blinks
"""
# Dictionary representing the morse code chart
MORSE_CODE_DICT = {'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': '--..',
'1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....',
'7': '--...', '8': '---..', '9': '----.',
'0': '-----', ', ': '--..--', '.': '.-.-.-',
'?': '..--..', '/': '-..-.', '-': '-....-',
'(': '-.--.', ')': '-.--.-'}
# Message "HI"
class XMorseCodeBlinker:
def __init__(self, pixels, message: str):
self.pixels = pixels
self.pixels.fill(0x0)
self.message = message
self.cur_message_index = 0
self.cur_character_index = 0
self.last_action_time = -1
self.next_action_time = -1
self.SHORT_DURATION = 0.3
self.LONG_DURATION = 0.6
self.INBETWEEN_DURATION = 0.3
def tick(self):
now = time.monotonic()
if now >= self.next_action_time:
# print(self.pixels[0])
if self.pixels[0] != (0, 0, 0, 1.0):
# pixels were on
self.pixels.fill(0x000000)
self.next_action_time = now + self.INBETWEEN_DURATION
return
else:
# pixels were off
cur_character = self.message[self.cur_message_index]
if cur_character == " ":
self.pixels.fill(0x000000)
self.next_action_time = now + self.SHORT_DURATION * 7
self.cur_message_index += 1
return
cur_morse_code = MORSE_CODE_DICT[cur_character]
if self.cur_character_index >= len(cur_morse_code):
self.cur_character_index = 0
self.cur_message_index += 1
if self.cur_message_index >= len(self.message):
# message terminiator character
# 3 short white blinks
self.pixels.fill(0xffffff)
time.sleep(self.SHORT_DURATION)
self.pixels.fill(0x000000)
time.sleep(self.INBETWEEN_DURATION)
self.pixels.fill(0xffffff)
time.sleep(self.SHORT_DURATION)
self.pixels.fill(0x000000)
time.sleep(self.INBETWEEN_DURATION)
self.pixels.fill(0xffffff)
time.sleep(self.SHORT_DURATION)
self.pixels.fill(0x000000)
self.next_action_time = time.monotonic() + (self.SHORT_DURATION * 7)
self.cur_message_index = 0
else:
# show white short blink seperator
self.pixels.fill(0xffffff)
self.next_action_time = now + self.SHORT_DURATION
else:
if cur_character in MORSE_CODE_DICT:
cur_morse_state = cur_morse_code[self.cur_character_index]
print((cur_character, cur_morse_code, cur_morse_state))
if cur_morse_state == ".":
# red
self.pixels.fill(0xff0000)
self.next_action_time = now + self.SHORT_DURATION
elif cur_morse_state == "-":
# green
self.pixels.fill(0x00ff00)
self.next_action_time = now + self.LONG_DURATION
self.cur_character_index += 1
# On-board DotStar for boards including Gemma, Trinket, and ItsyBitsy
dots = DotStar(board.D13, board.D11, 6, brightness=0.01)
dots.fill(0x00ff00)
xmorse_blinker = XMorseCodeBlinker(dots, "MERRY XMAS")
while True:
xmorse_blinker.tick()
time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment