From 7dcd35c2ef890ad6244ef2165ccee411485ab276 Mon Sep 17 00:00:00 2001 From: Javier Novoa Date: Fri, 20 Sep 2013 17:07:11 -0500 Subject: [PATCH 1/2] A Morse Code translator example. Taken from '30 Arduino projects for the evil genius'. --- examples/MorseCode.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/MorseCode.py diff --git a/examples/MorseCode.py b/examples/MorseCode.py new file mode 100644 index 0000000..8a9d1d3 --- /dev/null +++ b/examples/MorseCode.py @@ -0,0 +1,39 @@ +from BreakfastSerial import Arduino, Led +from time import sleep + +morsePin = 13 +dotDelay = 0.2 + +letters = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", # A-I + ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", # J-R + "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", # S-Z + ] +numbers = ["-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."] # digits + +if __name__ == "__main__": + board = Arduino() + led = Led(board, morsePin) + + while(True): + message = raw_input("Type your message ('EOF' to finish): ") + if message == 'EOF': + break + + for ch in message.upper(): + if ch.isalpha(): + morsech = letters[ord(ch) - ord('A')] + elif ch.isdigit(): + morsech = numbers[ord(ch) - ord('0')] + elif ch == " ": + sleep(4 * dotDelay) # gab between words + continue + + for dotOrDash in morsech: + led.on() + if dotOrDash == ".": + sleep(dotDelay) # dot duration + else: # must be "-" + sleep(dotDelay * 3) # dash duration + led.off() + sleep(dotDelay) # gap between flashes + sleep(dotDelay * 3) # gap between letters From ff4f3cce097af73e9ff17db7644dd29423f51569 Mon Sep 17 00:00:00 2001 From: Javier Novoa Date: Thu, 31 Oct 2013 22:44:09 -0600 Subject: [PATCH 2/2] Add RGB Led rgb and html methods. rgb receives % of each R,G,B for the LED to emit html receives a HTML color code and translates it to RGB --- BreakfastSerial/components.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/BreakfastSerial/components.py b/BreakfastSerial/components.py index dadfb39..aca7229 100644 --- a/BreakfastSerial/components.py +++ b/BreakfastSerial/components.py @@ -129,6 +129,16 @@ def off(self): self._red.off(); self._green.off(); self._blue.off() return self + def rgb(self, r,g,b): + self._red.brightness(r) + self._green.brightness(g) + self._blue.brightness(b) + + def html(self,html): + self._red.brightness(int("0x"+html[:2],16) * 100 / 255) + self._green.brightness(int("0x"+html[2:4],16) * 100 / 255) + self._blue.brightness(int("0x"+html[4:],16) * 100 / 255) + def red(self): self._red.on(); self._green.off(); self._blue.off() return self