It's no secret I think the NeoTrinkey is wonderful. It's small, it's cheap and it runs CircuitPython! Who could ask for more??
As I played with it, one thing I found handy was making some helper modules to assist in building programs - three that are particularly handy are:
-
morse.py - translate text to Morse code, also defines touch pads for input
- def docode(text): # display given text in Morse code
- def blinknum(num,color): #count out a number in a color
- def compthink(): #blink out all the colors when computer "thinking"
-
prt.py - provides a function to print text to the REPL *OR* using HID to send it as typed text.
- def prt(text, REPL): #prints text to REPL if REPL=True; otherwise uses HID to send as typed text
-
ncount.py - blinks numbers, or displays 0-15 in binary.
- def docolor(color): #briefly set all pixels to color
- def blinknum(num,color): #blinks num times in color
- def binnum(num,color): #display num%16 in binary on pixels with color
Using The Modules
Here's a very short program that blinks out, in Morse code some Bible verse fragments (in Klingon - my "nickname" is mrklingon, after all):
from morse import *
John316 = "vaD joH'a' vaj loved the qo'"
Isaiah263 = "Duvoqchugh, vaj rojna'Daq Da'av, DuHarmo'"
Psalm231 = "DevwI'wI' ghaH joH'a''e' jIneHbe'."
while True:
docode(John316)
docode(Isaiah263)
docode(Psalm231)
Pretty simple - it just blinks the code over and over. You could plug it into a power source and let it run - and you could choose your own text. Plugged into a computer running Mu, you would see the text being printed over and over as well.
Using the prt.py module, I wrote magicquest.py. This uses morse.py for the touch pad initialization, and the "compthink()" blinking. Touching pad 1 creates a fantasy character (race, class and name), and pad 2 generates a short story about the character's adventure. I used fantasynamegenerators.com to come up with place names and more.
If you change the value of REPL in the code you can decide if the text gets printed in the REPL using Mu or Thonny, or if it is sent as if typed using a computer.
REPL = False #"printed" text from prt() goes as if typed
REPL = True #"printed" text goes to REPL
I used the ncount.py module's binnum() function in two progams, cards.py and orbit.py.
For the cards.py program, the neotrinkey shuffles a deck of cards when you touch pad1, and pad2 draws a card and prints it in the REPL but displays it as a binary number (1-13) and color
suits = ["diamonds","clubs","hearts","spades"]
scolor = [gold,blue,red,green]
Touching 1 and 2 together shuffles and draws three cards.
orbit.py blinks a purple NeoPixel around and around at a speed (sort-of) appropriate to the planet the ship is at - from Mercury to Pluto. This uses binnum() to light up the number 1,2,4, or 8 to move the position around and around the four NeoPixels. Touching pad 1 makes all NeoPixels flash gold (closer to the sun) and shifts to the planet that is next closer to the sun. Touching pad 2 makes all NeoPixels flash blue (farther from the sun) and shifts to the planet next farther out.
Pads 1&2 together make jump the position either to Mercury or Pluto depending on whether you are closer than Mars or at Mar or farther out.
The name of the target planet is printed to the REPL and the orbit speed speeds up or slows down depending on which direction you have moved. (Note: the "speed" is just a factor based the position (planet 1-9) divided by ten. It is not really correct for the real planets).