C3P0 can talk. R2D2 expresses what he's thinking with beeps and boops. What can you do with a Neo Trinkey?
How about.....Morse code? It's simple, and four neopixels is more than enough to say anything! Here's how I set about doing it.
The great thing about Circuit Python is you can just plug in a device and use any editor on the code, but I like to use thonny as my IDE. Thonny gives you easy access to the REPL and you can have plenty of print() statements for debugging.
Using Thonny, I created a support module, morse.py that provides a framework for displaying morse code text:
- encryption(text) - returns text in morse code
- dot() - displays "dot" in green
- dash() - displays "dash" in blue
- space() - separates code letters in purple
- blinkcode(code) - blinks out morse text
- docode(text) - encrypts in morse, then blinks text
- In addition, I defined a set of colors [pink,gold,blue,orange,green,red,paleblue,white,purple] and some fun routines:
- blinknum(number, color) - blinks color number time
- docolor (color) - briefly sets all pixels to color
- compthink(number) - cycles through all colors number times (it's supposed to represent the computer "thinking")
Finally, morse.py sets up the touch pads for use as input.
It's *NOT* a program - if you run this code in Thonny, it just completes and returns to the REPL prompt. But then you can test the functions:
Here you can see
- Thonny loaded with morse.py,
- Executed the code, and returned to the prompt,
- entered "docode("hello")"
- output of encryption (I included diagnostic print of the encryption for use in Thonny). What you don't see is the NeoTrinkey blinking out the text.
Now... how to make that into a program? So I wrote DoMorse.py to take advantage of morse.py and give you a "menu" of four message.
from morse import * message1 = "abcdefghijklmnopqrstuvwxyz" message2 = "Live long and prosper!" message3 = "May the force be with you!" message4 = "Never give up, never surrender!" texts = [message1,message2,message3,message4] Done = False count = 0 compthink() #computer thinking docode("hello") #say hello blinknum(1,blue) while not Done: val = 0 if touch1.value: val = val + 1 if touch2.value: val = val + 2 if val == 1: #advance through messages touching "1" count = count+1 if count == 4: count = 0 blinknum (count+1,blue) #display index+1 if val == 2: #display message in Morse when touching "2" docode(texts[count]) compthink() #indicates end of message time.sleep(.2)
And there you have it - your Neo Trinkey can "talk!" With this routine renamed as "code.py", your Neo Trinkey can be plugged into a computer, a battery - any USB power source, and it is ready to blink out one of four messages.
Touch "1" to step through 1,2,3,4 (it will blink blue lights 1,2,3 or 4 times to tell you which message it is on).
Touch "2' to display the message in Morse code.
I chose the alphabet as the first message since it's a nice way to familiarize yourself with the full Morse alphabet. You can change the other messages to your choice - even add more than four, if you update the code to cycle through more than four messages.