code.py
  1  # SPDX-FileCopyrightText: 2017 Collin Cunningham for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  # Circuit Playground Express CircuitPython Morse Code Flasher
  6  # This is meant to work with the Circuit Playground Express board:
  7  #   https://www.adafruit.com/product/3333
  8  # Needs the NeoPixel module installed:
  9  #   https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel
 10  # Author: Collin Cunningham
 11  # License: MIT License (https://opensource.org/licenses/MIT)
 12  
 13  import time
 14  
 15  import board
 16  import neopixel
 17  
 18  # Configuration:
 19  # Message to display (capital letters and numbers only)
 20  message = 'SOS'
 21  dot_length = 0.15  # Duration of one Morse dot
 22  dash_length = (dot_length * 3.0)  # Duration of one Morse dash
 23  symbol_gap = dot_length  # Duration of gap between dot or dash
 24  character_gap = (dot_length * 3.0)  # Duration of gap between characters
 25  flash_color = (255, 0, 0)  # Color of the morse display.
 26  brightness = 0.5  # Display brightness (0.0 - 1.0)
 27  morse = [
 28      ('A', '.-'),
 29      ('B', '-...'),
 30      ('C', '-.-.'),
 31      ('D', '-..'),
 32      ('E', '.'),
 33      ('F', '..-.'),
 34      ('G', '--.'),
 35      ('H', '....'),
 36      ('I', '..'),
 37      ('J', '.---'),
 38      ('K', '-.-'),
 39      ('L', '.-..'),
 40      ('M', '--'),
 41      ('N', '-.'),
 42      ('O', '---'),
 43      ('P', '.--.'),
 44      ('Q', '--.-'),
 45      ('R', '.-.'),
 46      ('S', '...'),
 47      ('T', '-'),
 48      ('U', '..-'),
 49      ('V', '...-'),
 50      ('W', '.--'),
 51      ('X', '-..-'),
 52      ('Y', '-.--'),
 53      ('Z', '--..'),
 54      ('0', '-----'),
 55      ('1', '.----'),
 56      ('2', '..---'),
 57      ('3', '...--'),
 58      ('4', '....-'),
 59      ('5', '.....'),
 60      ('6', '-....'),
 61      ('7', '--...'),
 62      ('8', '---..'),
 63      ('9', '----.'),
 64  ]
 65  
 66  
 67  # Define a class that represents the morse flasher.
 68  
 69  
 70  class MorseFlasher:
 71      def __init__(self, color=(255, 255, 255)):
 72          # set the color adjusted for brightness
 73          self._color = (
 74              int(color[0] * brightness),
 75              int(color[1] * brightness),
 76              int(color[2] * brightness)
 77          )
 78  
 79      def light(self, on=True):
 80          if on:
 81              pixels.fill(self._color)
 82          else:
 83              pixels.fill((0, 0, 0))
 84          pixels.show()
 85  
 86      def showDot(self):
 87          self.light(True)
 88          time.sleep(dot_length)
 89          self.light(False)
 90          time.sleep(symbol_gap)
 91  
 92      def showDash(self):
 93          self.light(True)
 94          time.sleep(dash_length)
 95          self.light(False)
 96          time.sleep(symbol_gap)
 97  
 98      def encode(self, string):
 99          output = ""
100          # iterate through string's characters
101          for c in string:
102              # find morse code for a character
103              for x in morse:
104                  if x[0] == c:
105                      # add code to output
106                      output += x[1]
107              # add a space in between characters
108              output += " "
109          # save complete morse code output to display
110          self.display(output)
111  
112      def display(self, code=".-.-.- "):
113          # iterate through morse code symbols
114          for c in code:
115              # show a dot
116              if c == ".":
117                  self.showDot()
118              # show a dash
119              elif c == "-":
120                  self.showDash()
121              # show a gap
122              elif c == " ":
123                  time.sleep(character_gap)
124  
125  
126  # Initialize NeoPixels
127  pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=False)
128  pixels.fill((0, 0, 0))
129  pixels.show()
130  
131  # Create a morse flasher object.
132  flasher = MorseFlasher(flash_color)
133  
134  # Main loop will run forever
135  while True:
136      flasher.encode(message)