Merge branch 'pylint'

This commit is contained in:
Craig Richardson 2018-05-15 16:49:39 +01:00
commit 00d491c4e3
192 changed files with 3579 additions and 2889 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
*~
Hue_Controller/secrets.h
.idea

View file

@ -1,9 +1,10 @@
# 3D_Printed_Guardian_Sword
# https://learn.adafruit.com/breath-of-the-wild-guardian-sword-led-3d-printed
import time
import board
import neopixel
import time
pin = board.D4 # DIGITAL IO pin for NeoPixel OUTPUT from GEMMA
pixel_count = 93 # number of neopixels

View file

@ -26,7 +26,9 @@
# Written by Paul Badger 2007
# Modified fromhere code by Greg Shakar
# Ported to Circuit Python by Mikey Sklar
import time
import board
import neopixel
from analogio import AnalogIn
@ -38,8 +40,9 @@ sample_window = .1 # Sample window for average level
peak_hang = 24 # Time of pause before peak dot falls
peak_fall = 4 # Rate of falling peak dot
input_floor = 10 # Lower range of analogRead input
input_ceiling = 300 # Max range of analogRead input, the lower
# the value the more sensitive (1023 = max)
# Max range of analogRead input, the lower the value the more sensitive
# (1023 = max)
input_ceiling = 300
peak = 16 # Peak level of column; used for falling dots
sample = 0
@ -49,6 +52,7 @@ dothangcount = 0 # Frame counter for holding peak dot
strip = neopixel.NeoPixel(led_pin, n_pixels, brightness=1, auto_write=False)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
@ -63,6 +67,7 @@ def wheel(pos):
pos -= 170
return (0, int(pos * 3), int(255 - pos * 3))
def remapRange(value, leftMin, leftMax, rightMin, rightMax):
# this remaps a value fromhere original (left) range to new (right) range
# Figure out how 'wide' each range is
@ -75,12 +80,8 @@ def remapRange(value, leftMin, leftMax, rightMin, rightMax):
# Convert the 0-1 range into a value in the right range.
return int(rightMin + (valueScaled * rightSpan))
def fscale(originalmin, originalmax, newbegin, newend, inputvalue, curve):
originalrange = 0
newrange = 0
zerorefcurval = 0
normalizedcurval = 0
rangedvalue = 0
invflag = 0
# condition curve parameter
@ -94,7 +95,8 @@ def fscale(originalmin, originalmax, newbegin, newend, inputvalue, curve):
# this seems more intuitive
# postive numbers give more weight to high end on output
curve = (curve * -.1)
curve = pow(10, curve) # convert linear scale into lograthimic exponent for other pow function
# convert linear scale into lograthimic exponent for other pow function
curve = pow(10, curve)
# Check for out of range inputValues
if inputvalue < originalmin:
@ -113,7 +115,8 @@ def fscale(originalmin, originalmax, newbegin, newend, inputvalue, curve):
invflag = 1
zerorefcurval = inputvalue - originalmin
normalizedcurval = zerorefcurval / originalrange # normalize to 0 - 1 float
# normalize to 0 - 1 float
normalizedcurval = zerorefcurval / originalrange
# Check for originalMin > originalMax
# -the math for all other cases
@ -128,17 +131,16 @@ def fscale(originalmin, originalmax, newbegin, newend, inputvalue, curve):
return rangedvalue
def drawLine(fromhere, to):
fromheretemp = 0
if fromhere > to:
fromheretemp = fromhere
fromhere = to
to = fromheretemp
for n in range(fromhere, to):
strip[n] = (0, 0, 0)
for index in range(fromhere, to):
strip[index] = (0, 0, 0)
while True:
@ -152,7 +154,8 @@ while True:
# collect data for length of sample window (in seconds)
while (time.monotonic() - time_start) < sample_window:
sample = mic_pin.value / 64 # convert to arduino 10-bit [1024] fromhere 16-bit [65536]
# convert to arduino 10-bit [1024] fromhere 16-bit [65536]
sample = mic_pin.value / 64
if sample < 1024: # toss out spurious readings
@ -184,7 +187,7 @@ while True:
# Frame based peak dot animation
if dothangcount > peak_hang: # Peak pause length
dotcount = dotcount + 1
dotcount += 1
if dotcount >= peak_fall: # Fall rate
peak += 1
dotcount = 0

View file

@ -1,6 +1,8 @@
import time
import board
import neopixel
try:
import urandom as random # for v1.0 API support
except ImportError:

View file

@ -9,6 +9,7 @@
# major bug, you don't need tochange anything here
#
import time
import board
import neopixel
@ -30,28 +31,32 @@ ACT_SPARKLING_RING = 0x20 # sparkling effect
numpix = 16 # total number of NeoPixels
pixel_output = board.D0 # pin where NeoPixels are connected
analog_input = board.A0 # needed to seed the random generator
strip = neopixel.NeoPixel(pixel_output, numpix, brightness=.3, auto_write=False)
strip = neopixel.NeoPixel(pixel_output, numpix,
brightness=.3, auto_write=False)
# available color generation methods
COL_RANDOM = 0x40 # colors will be generated randomly
COL_SPECTRUM = 0x80 # colors will be set as cyclic spectral wipe
# specifiyng the action list
action_duration = 0 # the action's overall duration in milliseconds (be careful not
# the action's overall duration in milliseconds (be careful not
action_duration = 0
# to use values > 2^16-1 - roughly one minute :-)
action_and_color_gen = 1 # the color generation method
action_step_duration = 2 # the duration of each action step rsp. the delay of the main
# the duration of each action step rsp. the delay of the main
action_step_duration = 2
# loop in milliseconds - thus, controls the action speed (be
# careful not to use values > 2^16-1 - roughly one minute :-)
color_granularity = 3 # controls the increment of the R, G, and B portions of the
# rsp. color. 1 means the increment is 0,1,2,3,..., 10 means
# the increment is 0,10,20,... don't use values > 255, and note
# that even values > 127 wouldn't make much sense...
color_granularity = 3 # controls the increment of the R, G, and B
# portions of the rsp. color. 1 means the increment is 0,1,2,3,...,
# 10 means the increment is 0,10,20,... don't use values > 255, and
# note that even values > 127 wouldn't make much sense...
color_interval = 4 # controls the speed of color changing independently from action
# controls the speed of color changing independently from action
color_interval = 4
# general global variables
color = 0
@ -94,34 +99,60 @@ spectrum_part = 0
# duration color generation method duration granularity interval
theactionlist = [
[5, ACT_SPARKLING_RING | COL_RANDOM, 0.01, 25, 1],
[2, ACT_CYCLING_RING_CLKW | COL_RANDOM, 0.02, 1, 0.005 ],
[2, ACT_CYCLING_RING_CLKW | COL_RANDOM,
0.02, 1, 0.005],
[5, ACT_SPARKLING_RING | COL_RANDOM, 0.01, 25, 1],
[2, ACT_CYCLING_RING_ACLK | COL_RANDOM, 0.02, 1, 0.005 ],
[2, ACT_CYCLING_RING_ACLK | COL_RANDOM,
0.02, 1, 0.005],
[5, ACT_SPARKLING_RING | COL_RANDOM, 0.01, 25, 1],
[2.5, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.25, 20, 0.020 ],
[1, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.50, 1, 0.020 ],
[.750, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.075, 1, 0.020 ],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.100, 1, 0.020 ],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.125, 1, 0.020 ],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.150, 1, 0.050 ],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.175, 1, 0.100 ],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.200, 1, 0.200 ],
[.750, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.225, 1, 0.250 ],
[1, ACT_CYCLING_RING_CLKW | COL_SPECTRUM, 0.250, 1, 0.350 ],
[30, ACT_SIMPLE_RING | COL_SPECTRUM, 0.050, 1, 0.010 ],
[2.5, ACT_WHEEL_ACLK | COL_SPECTRUM, 0.010, 1, 0.010 ],
[2.5, ACT_WHEEL_ACLK | COL_SPECTRUM, 0.015, 1, 0.020 ],
[2, ACT_WHEEL_ACLK | COL_SPECTRUM, 0.025, 1, 0.030 ],
[1, ACT_WHEEL_ACLK | COL_SPECTRUM, 0.050, 1, 0.040 ],
[1, ACT_WHEEL_ACLK | COL_SPECTRUM, 0.075, 1, 0.040 ],
[1, ACT_WHEEL_ACLK | COL_SPECTRUM, 0.100, 1, 0.050 ],
[.500, ACT_WHEEL_ACLK | COL_SPECTRUM, 0.125, 1, 0.060 ],
[.500, ACT_WHEEL_CLKW | COL_SPECTRUM, 0.125, 5, 0.050 ],
[1, ACT_WHEEL_CLKW | COL_SPECTRUM, 0.100, 10, 0.040 ],
[1.5, ACT_WHEEL_CLKW | COL_SPECTRUM, 0.075, 15, 0.030 ],
[2, ACT_WHEEL_CLKW | COL_SPECTRUM, 0.050, 20, 0.020 ],
[2.5, ACT_WHEEL_CLKW | COL_SPECTRUM, 0.025, 25, 0.010 ],
[3, ACT_WHEEL_CLKW | COL_SPECTRUM, 0.010, 30, 0.005 ],
[2.5, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.25, 20, 0.020],
[1, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.50, 1, 0.020],
[.750, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.075, 1, 0.020],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.100, 1, 0.020],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.125, 1, 0.020],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.150, 1, 0.050],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.175, 1, 0.100],
[.500, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.200, 1, 0.200],
[.750, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.225, 1, 0.250],
[1, ACT_CYCLING_RING_CLKW | COL_SPECTRUM,
0.250, 1, 0.350],
[30, ACT_SIMPLE_RING | COL_SPECTRUM,
0.050, 1, 0.010],
[2.5, ACT_WHEEL_ACLK | COL_SPECTRUM,
0.010, 1, 0.010],
[2.5, ACT_WHEEL_ACLK | COL_SPECTRUM,
0.015, 1, 0.020],
[2, ACT_WHEEL_ACLK | COL_SPECTRUM,
0.025, 1, 0.030],
[1, ACT_WHEEL_ACLK | COL_SPECTRUM,
0.050, 1, 0.040],
[1, ACT_WHEEL_ACLK | COL_SPECTRUM,
0.075, 1, 0.040],
[1, ACT_WHEEL_ACLK | COL_SPECTRUM,
0.100, 1, 0.050],
[.500, ACT_WHEEL_ACLK | COL_SPECTRUM,
0.125, 1, 0.060],
[.500, ACT_WHEEL_CLKW | COL_SPECTRUM,
0.125, 5, 0.050],
[1, ACT_WHEEL_CLKW | COL_SPECTRUM,
0.100, 10, 0.040],
[1.5, ACT_WHEEL_CLKW | COL_SPECTRUM,
0.075, 15, 0.030],
[2, ACT_WHEEL_CLKW | COL_SPECTRUM,
0.050, 20, 0.020],
[2.5, ACT_WHEEL_CLKW | COL_SPECTRUM,
0.025, 25, 0.010],
[3, ACT_WHEEL_CLKW | COL_SPECTRUM,
0.010, 30, 0.005],
[5, ACT_SPARKLING_RING | COL_RANDOM, 0.010, 25, 1],
[5, ACT_NOP, 0, 0, 0]
]
@ -129,6 +160,7 @@ theactionlist = [
def nextspectrumcolor():
global spectrum_part, color_idx, curr_color_granularity, color
# spectral wipe from green to red
if spectrum_part == 2:
color = (color_idx, 0, 255-color_idx)
@ -153,6 +185,7 @@ def nextspectrumcolor():
spectrum_part = 1
color_idx = 0
def nextrandomcolor():
global color
@ -170,6 +203,7 @@ def nextrandomcolor():
color = (random_red, random_green, random_blue)
def nextcolor():
# save some RAM for more animation actions
if curr_color_gen & COL_RANDOM:
@ -177,8 +211,8 @@ def nextcolor():
else:
nextspectrumcolor()
def setup():
def setup():
# fingers corssed, the seeding makes sense to really get random colors...
apin = AnalogIn(analog_input)
random.seed(apin.value)
@ -188,18 +222,21 @@ def setup():
nextcolor()
strip.write()
setup()
while True: # Loop forever...
# do we need to load the next action?
if (time.monotonic() - action_timer) > curr_action_duration:
curr_action_duration = theactionlist[curr_action_idx][action_duration]
curr_action = theactionlist[curr_action_idx][action_and_color_gen] & 0x3F
curr_action_step_duration = theactionlist[curr_action_idx][action_step_duration]
curr_color_gen = theactionlist[curr_action_idx][action_and_color_gen] & 0xC0
curr_color_granularity = theactionlist[curr_action_idx][color_granularity]
curr_color_interval = theactionlist[curr_action_idx][color_interval]
current_action = theactionlist[curr_action_idx]
curr_action_duration = current_action[action_duration]
curr_action = current_action[action_and_color_gen] & 0x3F
curr_action_step_duration = current_action[action_step_duration]
curr_color_gen = current_action[action_and_color_gen] & 0xC0
curr_color_granularity = current_action[color_granularity]
curr_color_interval = current_action[color_interval]
curr_action_idx += 1
# take care to rotate the action list!
@ -216,6 +253,8 @@ while True: # Loop forever...
if curr_action:
is_act_cycling = (ACT_CYCLING_RING_ACLK or ACT_CYCLING_RING_CLKW)
if curr_action == ACT_NOP:
# rather trivial even tho this will be repeated as long as the
# NOP continues - i could have prevented it from repeating
@ -229,7 +268,7 @@ while True: # Loop forever...
for i in range(0, numpix):
strip[i] = color
elif curr_action == (ACT_CYCLING_RING_ACLK or ACT_CYCLING_RING_CLKW):
elif curr_action == is_act_cycling:
# spin the ring clockwise or anti clockwise
if curr_action == ACT_CYCLING_RING_ACLK:
idx += 1
@ -242,7 +281,7 @@ while True: # Loop forever...
# set the new color, if there is one
strip[idx] = color
elif (curr_action == ACT_WHEEL_ACLK or ACT_WHEEL_CLKW):
elif curr_action == ACT_WHEEL_ACLK or ACT_WHEEL_CLKW:
# switch on / off the appropriate pixels according to
# the current offset
for idx in range(0, numpix):

View file

@ -1,7 +1,8 @@
import time
from digitalio import DigitalInOut, Direction
import board
import neopixel
from digitalio import DigitalInOut, Direction
pixpin = board.D1
numpix = 8
@ -11,6 +12,7 @@ led.direction = Direction.OUTPUT
strip = neopixel.NeoPixel(pixpin, numpix, brightness=1, auto_write=True)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
@ -25,6 +27,7 @@ def wheel(pos):
pos -= 170
return (0, int(pos * 3), int(255 - pos * 3))
def rainbow_cycle(wait):
for j in range(255 * 5):
for i in range(len(strip)):
@ -32,11 +35,14 @@ def rainbow_cycle(wait):
strip[i] = wheel(idx & 255)
time.sleep(wait)
def rainbow(wait):
for j in range(255):
for i in range(len(strip)):
idx = int(i + j)
strip[i] = wheel(idx & 255)
time.sleep(wait)
while True:
rainbow_cycle(0.05)

View file

@ -1,4 +1,5 @@
import time
import board
import pulseio
from digitalio import DigitalInOut, Direction
@ -10,7 +11,6 @@ pwm = pulseio.PWMOut(pwm_leds, frequency=1000, duty_cycle=0)
# digital LEDs connected on D2
digital_leds = DigitalInOut(board.D2)
digital_leds.direction = Direction.OUTPUT
brightness = 0 # how bright the LED is
fade_amount = 1285 # 2% steping of 2^16
counter = 0 # counter to keep track of cycles

View file

@ -1,8 +1,7 @@
import adafruit_fancyled.adafruit_fancyled as fancy
import board
import neopixel
import time
from digitalio import DigitalInOut, Direction, Pull
import adafruit_fancyled.adafruit_fancyled as fancy
led_pin = board.D1 # which pin your pixels are connected to
num_leds = 78 # how many LEDs you have
@ -65,38 +64,41 @@ heat_colors = [ 0x330000, 0x660000, 0x990000, 0xCC0000, 0xFF0000,
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255):
return (0, 0, 0)
if (pos < 85):
if pos < 85:
return (int(pos * 3), int(255 - (pos * 3)), 0)
elif (pos < 170):
elif pos < 170:
pos -= 85
return (int(255 - pos * 3), 0, int(pos * 3))
else:
pos -= 170
return (0, int(pos * 3), int(255 - pos * 3))
def remapRange(value, leftMin, leftMax, rightMin, rightMax):
# this remaps a value from original (left) range to new (right) range
def remapRange(value, leftMin, leftMax, rightMin, rightMax):
# this remaps a value fromhere original (left) range to new (right) range
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (int)
valueScaled = int(value - leftMin) / int(leftSpan)
# Convert the 0-1 range into a value in the right range.
return int(rightMin + (valueScaled * rightSpan))
def shortkeypress(color_palette):
def shortkeypress(color_palette):
color_palette += 1
if ( color_palette > 6):
if color_palette > 6:
color_palette = 1
return ( color_palette )
return color_palette
while True:
@ -104,38 +106,38 @@ while True:
currkeystate = button.value
# button press, move to next pattern
if ( ( prevkeystate == False ) and ( currkeystate == True ) ):
if (prevkeystate is not True) and currkeystate:
ledmode = shortkeypress(ledmode)
# save button press state
prevkeystate = currkeystate
# Fire Colors [ HEAT ]
if ( ledmode == 1 ):
if ledmode == 1:
palette = heat_colors
# Forest
elif ( ledmode == 2 ):
elif ledmode == 2:
palette = forest
# Ocean
elif ( ledmode == 3 ):
elif ledmode == 3:
palette = ocean
# Purple Lovers
elif ( ledmode == 4 ):
elif ledmode == 4:
palette = purple
# All the colors!
elif ( ledmode == 5 ):
elif ledmode == 5:
palette = rainbow
# Rainbow stripes
elif ( ledmode == 6 ):
elif ledmode == 6:
palette = rainbow_stripe
# All the colors except the greens, washed out
elif ( ledmode == 7 ):
elif ledmode == 7:
palette = washed_out
for i in range(num_leds):
@ -144,11 +146,11 @@ while True:
strip[i] = color.pack()
strip.show()
if ( fadeup ):
if fadeup:
offset += steps
if ( offset >= 1 ):
if offset >= 1:
fadeup = False
else:
offset -= steps
if ( offset <= 0 ):
if offset <= 0:
fadeup = True

View file

@ -1,9 +1,10 @@
# Annoy-O-Matic Sound Prank Device
# choose from a variety of sounds and timings to drive your victim bonkers
import pulseio
import board
import time
import board
import pulseio
piezo = pulseio.PWMOut(board.D0, duty_cycle=0, frequency=440,
variable_frequency=True)
@ -33,7 +34,7 @@ ringtone_tempo = 1.6 # suggested Nokia 0.9 , iPhone 1.3 , Rickroll 2.0
def annoy_beep(frequency, length, repeat, rest, interval):
for r in range(repeat):
for _ in range(repeat):
piezo.frequency = frequency # 2600 is a nice choice
piezo.duty_cycle = 65536 // 2 # on 50%
time.sleep(length) # sound on
@ -41,6 +42,7 @@ def annoy_beep(frequency, length, repeat, rest, interval):
time.sleep(rest)
time.sleep(interval) # wait time until next beep
def annoy_doorbell(interval):
piezo.frequency = 740
piezo.duty_cycle = 65536 // 2
@ -53,6 +55,7 @@ def annoy_doorbell(interval):
piezo.duty_cycle = 0
time.sleep(interval)
def annoy_ringtone(ringtone, tempo, interval):
# ringtone 1: Nokia
# ringtone 2: Apple iPhone
@ -61,94 +64,94 @@ def annoy_ringtone(ringtone, tempo, interval):
# tempo is length of whole note in seconds, e.g. 1.5
# set up time signature
whole_note = tempo # adjust this to change tempo of everything
dotted_whole_note = whole_note * 1.5
# dotted_whole_note = whole_note * 1.5
# these notes are fractions of the whole note
half_note = whole_note / 2
dotted_half_note = half_note * 1.5
# half_note = whole_note / 2
# dotted_half_note = half_note * 1.5
quarter_note = whole_note / 4
dotted_quarter_note = quarter_note * 1.5
# dotted_quarter_note = quarter_note * 1.5
eighth_note = whole_note / 8
dotted_eighth_note = eighth_note * 1.5
sixteenth_note = whole_note / 16
# set up note values
A2 = 110
As2 = 117 # 's' stands for sharp: A#2
Bb2 = 117
B2 = 123
# A2 = 110
# As2 = 117 # 's' stands for sharp: A#2
# Bb2 = 117
# B2 = 123
C3 = 131
Cs3 = 139
Db3 = 139
D3 = 147
Ds3 = 156
Eb3 = 156
E3 = 165
F3 = 175
Fs3 = 185
Gb3 = 185
G3 = 196
Gs3 = 208
Ab3 = 208
# C3 = 131
# Cs3 = 139
# Db3 = 139
# D3 = 147
# Ds3 = 156
# Eb3 = 156
# E3 = 165
# F3 = 175
# Fs3 = 185
# Gb3 = 185
# G3 = 196
# Gs3 = 208
# Ab3 = 208
A3 = 220
As3 = 233
Bb3 = 233
# As3 = 233
# Bb3 = 233
B3 = 247
C4 = 262
# C4 = 262
Cs4 = 277
Db4 = 277
# Db4 = 277
D4 = 294
Ds4 = 311
Eb4 = 311
# Ds4 = 311
# Eb4 = 311
E4 = 330
F4 = 349
# F4 = 349
Fs4 = 370
Gb4 = 370
# Gb4 = 370
G4 = 392
Gs4 = 415
Ab4 = 415
A4 = 440
As4 = 466
Bb4 = 466
# Gs4 = 415
# Ab4 = 415
# A4 = 440
# As4 = 466
# Bb4 = 466
B4 = 494
C5 = 523
# C5 = 523
Cs5 = 554
Db5 = 554
# Db5 = 554
D5 = 587
Ds5 = 622
Eb5 = 622
# Ds5 = 622
# Eb5 = 622
E5 = 659
F5 = 698
# F5 = 698
Fs5 = 740
Gb5 = 740
G5 = 784
# Gb5 = 740
# G5 = 784
Gs5 = 831
Ab5 = 831
# Ab5 = 831
A5 = 880
As5 = 932
Bb5 = 932
# As5 = 932
# Bb5 = 932
B5 = 987
# here's another way to express the note pitch, double the previous octave
C6 = C5 * 2
# C6 = C5 * 2
Cs6 = Cs5 * 2
Db6 = Db5 * 2
# Db6 = Db5 * 2
D6 = D5 * 2
Ds6 = Ds5 * 2
Eb6 = Eb5 * 2
# Ds6 = Ds5 * 2
# Eb6 = Eb5 * 2
E6 = E5 * 2
F6 = F5 * 2
Fs6 = Fs5 * 2
Gb6 = Gb5 * 2
G6 = G5 * 2
Gs6 = Gs5 * 2
Ab6 = Ab5 * 2
A6 = A5 * 2
As6 = As5 * 2
Bb6 = Bb5 * 2
B6 = B5 * 2
# F6 = F5 * 2
# Fs6 = Fs5 * 2
# Gb6 = Gb5 * 2
# G6 = G5 * 2
# Gs6 = Gs5 * 2
# Ab6 = Ab5 * 2
# A6 = A5 * 2
# As6 = As5 * 2
# Bb6 = Bb5 * 2
# B6 = B5 * 2
if ringtone == 1:
# Nokia
@ -206,9 +209,10 @@ def annoy_ringtone(ringtone, tempo, interval):
time.sleep(interval)
def annoy_crickets(repeat, interval):
for i in range(repeat):
for r in range(6):
for _ in range(repeat):
for _ in range(6):
piezo.frequency = 8000 # 2600 is a nice choice
piezo.duty_cycle = 65536 // 2 # on 50%
time.sleep(0.02) # sound on
@ -217,6 +221,7 @@ def annoy_crickets(repeat, interval):
time.sleep(0.2)
time.sleep(interval) # wait time until next beep
def annoy_teen_tone(interval):
piezo.frequency = 17400
piezo.duty_cycle = 65536 // 2 # on 50%
@ -224,18 +229,19 @@ def annoy_teen_tone(interval):
piezo.duty_cycle = 0
time.sleep(interval)
while True:
if annoy_mode is 1:
if annoy_mode == 1:
annoy_beep(frequency, length, repeat, rest, interval)
elif annoy_mode is 2:
elif annoy_mode == 2:
annoy_doorbell(interval)
elif annoy_mode is 3:
elif annoy_mode == 3:
annoy_ringtone(ringtone, ringtone_tempo, interval)
elif annoy_mode is 4:
elif annoy_mode == 4:
annoy_crickets(repeat, interval)
elif annoy_mode is 5:
elif annoy_mode == 5:
annoy_teen_tone(interval)
elif annoy_mode is 6:
elif annoy_mode == 6:
annoy_beep(5000, 0.2, 2, 0.05, 3)
annoy_doorbell(3)
annoy_ringtone(1, 0.9, 3)

View file

@ -1,8 +1,9 @@
import digitalio
from board import *
import time
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from board import D13, D12, D11, D10, D9, D6, D5, A0, A1, A2, A3, A4, A5
# A simple neat keyboard demo in circuitpython

View file

@ -1,8 +1,8 @@
import time
import adafruit_CCS811
import board
import busio
import adafruit_CCS811
import neopixel
# i2c interface for the gas sensor
@ -25,7 +25,10 @@ while not ccs.data_ready:
temp = ccs.temperature
ccs.temp_offset = temp - 25.0
# clear all LEDs for breathing effect
def clear_pix(delay):
for i in range(0, num_leds):
temperature_pix[i] = (0, 0, 0)
@ -33,7 +36,10 @@ def clear_pix(delay):
tvoc_pix[i] = (0, 0, 0, 0)
time.sleep(delay)
# Show Carbon Dioxide on a NeoPixel Strip
def co2_led_meter():
co2_floor = 400
co2_ceiling = 8192
@ -48,7 +54,10 @@ def co2_led_meter():
co2_pix[i] = (255, 0, 255, 0)
time.sleep(led_draw)
# Show Total Volatile Organic Compounds on a NeoPixel Strip
def tvoc_led_meter():
tvoc_floor = 0
tvoc_ceiling = 1187
@ -63,7 +72,10 @@ def tvoc_led_meter():
tvoc_pix[i] = (0, 0, 255, 0)
time.sleep(led_draw)
# Show Temperature on Circuit Playground built-in NeoPixels
def temp_led_meter():
temp_floor = 23
temp_ceiling = 36
@ -78,6 +90,7 @@ def temp_led_meter():
temperature_pix[i] = (255, 255, 0)
time.sleep(led_draw)
while True:
# print to console
# - co2

View file

@ -1,7 +1,8 @@
import time
import adafruit_sgp30
import board
import busio
import adafruit_sgp30
i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)
@ -13,26 +14,29 @@ sgp30.set_iaq_baseline(0x8973, 0x8aae)
# highest tVOC recorded in 30 seconds
highest_breath_result = 0
def warmup_message():
def warmup_message():
warmup_time = 20
warmup_counter = 0
co2eq, tvoc = sgp30.iaq_measure() # initial read required to get sensor going
# initial read required to get sensor going
sgp30.iaq_measure()
print()
print("Warming Up [%d seconds]..." % warmup_time)
while ( warmup_counter <= 20 ):
while warmup_counter <= 20:
print('.', end='')
time.sleep(1)
warmup_counter += 1
def get_breath_reading():
def get_breath_reading():
breath_time = 30 # seconds to record breath reading
breath_counter = 0 # one second count up to breath_time value
breath_saves = [0] * ( breath_time + 1 ) # initialize list with empty values
# one second count up to breath_time value
breath_counter = 0
# initialize list with empty values
breath_saves = [0] * (breath_time + 1)
print()
print("We will collect breath samples for 30 seconds.")
@ -40,27 +44,31 @@ def get_breath_reading():
input(" *** Press a key when ready. *** ")
print()
while ( breath_counter <= breath_time ):
co2eq, tvoc = sgp30.iaq_measure()
while breath_counter <= breath_time:
_, tvoc = sgp30.iaq_measure()
breath_saves[breath_counter] = tvoc
print(tvoc, ', ', end='')
time.sleep(1)
breath_counter += 1
breath_saves = sorted(breath_saves)
highest_breath_result = breath_saves[breath_counter - 1]
result = breath_saves[breath_counter - 1]
return result
return(highest_breath_result)
# show the highest reading recorded
def show_results(highest_breath_result):
def show_results(breath_result):
print()
print()
print("peak VOC reading:", highest_breath_result)
print("peak VOC reading:", breath_result)
print()
input("Press any key to test again")
print()
# main
while True:
warmup_message()

View file

@ -1,9 +1,10 @@
# Chilled Drinkibot
from digitalio import DigitalInOut, Direction, Pull
import board
import time
import board
from digitalio import DigitalInOut, Direction, Pull
led = DigitalInOut(board.D2) # Button LED
led.direction = Direction.OUTPUT
@ -17,7 +18,6 @@ chiller.direction = Direction.OUTPUT
pump = DigitalInOut(board.D4) # Pin to control the pump
pump.direction = Direction.OUTPUT
chillTime = 5 # How many _minutes_ of cooling
pumpTime = 35 # How many seconds of pumping

View file

@ -1,15 +1,15 @@
import time
import machine
import adafruit_ssd1306
import bitbangio as io
import board
import network
import ntptime
import uhashlib
import ubinascii
import board
import bitbangio as io
import adafruit_ssd1306
import uhashlib
totp = [("Discord ", 'JBSWY3DPEHPK3PXP'), # https://github.com/pyotp/pyotp exmple
# https://github.com/pyotp/pyotp example
totp = [("Discord ", 'JBSWY3DPEHPK3PXP'),
("Gmail ", 'abcdefghijklmnopqrstuvwxyz234567'),
("Accounts", 'asfdkwefoaiwejfa323nfjkl')]
ssid = 'my_wifi_ssid'
@ -52,6 +52,7 @@ def HMAC(k, m):
outer_message = KEY_OUTER + SHA1(inner_message).digest()
return SHA1(outer_message)
if TEST:
KEY = b'abcd'
MESSAGE = b'efgh'
@ -59,7 +60,10 @@ if TEST:
print("HMAC test: ", ubinascii.hexlify(HMAC(KEY, MESSAGE).digest()))
# should be e5dbcf9263188f9fce90df572afeb39b66b27198
# Base32 decoder, since base64 lib wouldnt fit
def base32_decode(encoded):
missing_padding = len(encoded) % 8
if missing_padding != 0:
@ -93,12 +97,16 @@ def base32_decode(encoded):
out.append(byte) # store what we got
return out
if TEST:
print("===========================================")
print("Base32 test: ", bytes(base32_decode("IFSGCZTSOVUXIIJB")))
# should be "Adafruit!!"
# Turns an integer into a padded-with-0x0 bytestr
def int_to_bytestring(i, padding=8):
result = []
while i != 0:
@ -107,12 +115,18 @@ def int_to_bytestring(i, padding=8):
result = [0] * (padding - len(result)) + result
return bytes(result)
# HMAC -> OTP generator, pretty much same as
# https://github.com/pyotp/pyotp/blob/master/src/pyotp/otp.py
def generate_otp(input, secret, digits=6):
if input < 0:
def generate_otp(int_input, secret_key, digits=6):
if int_input < 0:
raise ValueError('input must be positive integer')
hmac_hash = bytearray(HMAC(bytes(base32_decode(secret)), int_to_bytestring(input)).digest())
hmac_hash = bytearray(
HMAC(bytes(base32_decode(secret_key)),
int_to_bytestring(int_input)).digest()
)
offset = hmac_hash[-1] & 0xf
code = ((hmac_hash[offset] & 0x7f) << 24 |
(hmac_hash[offset + 1] & 0xff) << 16 |
@ -124,6 +138,7 @@ def generate_otp(input, secret, digits=6):
return str_code
print("===========================================")
# Set up networking
@ -152,7 +167,7 @@ t = None
while not t:
try:
t = ntptime.time()
except:
except Exception:
pass
time.sleep(0.1)
@ -183,7 +198,7 @@ while ALWAYS_ON or (countdown > 0):
print(name + " OTP output: ", otp) # serial debugging output
oled.text(name + ": " + str(otp), 0, y) # display name & OTP on OLED
y += 10 # Go to next line on OLED
# We'll display a little bar that 'counts down' how many seconds you have left
# Display a little bar that 'counts down' how many seconds you have left
oled.framebuf.line(0, 31, 128 - (unix_time % 30) * 4, 31, True)
oled.show()
# We'll update every 1/4 second, we can hash very fast so its no biggie!

View file

@ -1,8 +1,9 @@
# CircuitPython AnalogIn Demo
import time
from analogio import AnalogIn
import board
from analogio import AnalogIn
analog_in = AnalogIn(board.A1)

View file

@ -1,7 +1,7 @@
# CircuitPython IO demo - analog output
from analogio import AnalogOut
import board
from analogio import AnalogOut
analog_out = AnalogOut(board.A0)

View file

@ -1,4 +1,5 @@
import time
import board
import touchio

View file

@ -2,6 +2,7 @@
# Example does NOT work with Trinket M0!
import time
import board
import touchio

View file

@ -1,19 +1,20 @@
# CircuitPython IO demo #1 - General Purpose I/O
import time
from digitalio import DigitalInOut, Direction, Pull
import board
from digitalio import DigitalInOut, Direction, Pull
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
switch = DigitalInOut(board.D2) # For Gemma M0, Trinket M0, Metro M0 Express, ItsyBitsy M0 Express
# For Gemma M0, Trinket M0, Metro M0 Express, ItsyBitsy M0 Express
switch = DigitalInOut(board.D2)
# switch = DigitalInOut(board.D5) # For Feather M0 Express
# switch = DigitalInOut(board.D7) # For Circuit Playground Express
switch.direction = Direction.INPUT
switch.pull = Pull.UP
while True:
# We could also just do "led.value = not switch.value"!
if switch.value:

View file

@ -1,11 +1,13 @@
# CircuitPython demo - Dotstar
import time
import board
import adafruit_dotstar
import board
num_pixels = 30
pixels = adafruit_dotstar.DotStar(board.A1, board.A2, num_pixels, brightness=0.1, auto_write=False)
pixels = adafruit_dotstar.DotStar(
board.A1, board.A2, num_pixels, brightness=0.1, auto_write=False)
def wheel(pos):
@ -103,7 +105,8 @@ MAGENTA = (255, 0, 20)
WHITE = (255, 255, 255)
while True:
color_fill(RED, 0.5) # Change this number to change how long it stays on each solid color.
# Change this number to change how long it stays on each solid color.
color_fill(RED, 0.5)
color_fill(YELLOW, 0.5)
color_fill(ORANGE, 0.5)
color_fill(GREEN, 0.5)
@ -114,12 +117,15 @@ while True:
color_fill(MAGENTA, 0.5)
color_fill(WHITE, 0.5)
slice_alternating(0.1) # Increase or decrease this to speed up or slow down the animation.
# Increase or decrease this to speed up or slow down the animation.
slice_alternating(0.1)
color_fill(WHITE, 0.5)
slice_rainbow(0.1) # Increase or decrease this to speed up or slow down the animation.
# Increase or decrease this to speed up or slow down the animation.
slice_rainbow(0.1)
time.sleep(0.5)
rainbow_cycle(0) # Increase this number to slow down the rainbow animation.
# Increase this number to slow down the rainbow animation.
rainbow_cycle(0)

View file

@ -1,11 +1,12 @@
# CircuitPython demo - Keyboard emulator
import time
import digitalio
import board
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
# A simple neat keyboard demo in CircuitPython

View file

@ -1,4 +1,5 @@
import time
import analogio
import board
import digitalio
@ -21,7 +22,8 @@ def get_voltage(pin):
return (pin.value * 3.3) / 65536
def steps(axis): # Maps the potentiometer voltage range to 0-20 in whole numbers
def steps(axis):
""" Maps the potentiometer voltage range to 0-20 """
return round((axis - pot_min) / step)

View file

@ -1,8 +1,9 @@
# CircuitPython demo - I2C scan
import time
import board
import busio
import time
i2c = busio.I2C(board.SCL, board.SDA)
@ -10,5 +11,6 @@ while not i2c.try_lock():
pass
while True:
print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()])
print("I2C addresses found:", [hex(device_address)
for device_address in i2c.scan()])
time.sleep(2)

View file

@ -1,9 +1,10 @@
# CircuitPython Demo - I2C sensor
import time
import adafruit_tsl2561
import board
import busio
import adafruit_tsl2561
import time
i2c = busio.I2C(board.SCL, board.SDA)
@ -11,7 +12,8 @@ i2c = busio.I2C(board.SCL, board.SDA)
while not i2c.try_lock():
pass
# Print the addresses found once
print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()])
print("I2C addresses found:", [hex(device_address)
for device_address in i2c.scan()])
# Unlock I2C now that we're done scanning.
i2c.unlock()

View file

@ -1,11 +1,12 @@
import time
import board
import neopixel
import adafruit_dotstar
import board
# For Trinket M0, Gemma M0, and ItsyBitsy M0 Express
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
# For Feather M0 Express, Metro M0 Express, Metro M4 Express, and Circuit Playground Express
# For Feather M0 Express, Metro M0 Express, Metro M4 Express, and Circuit
# Playground Express
# led = neopixel.NeoPixel(board.NEOPIXEL, 1)
led.brightness = 0.3

View file

@ -1,11 +1,14 @@
import time
import board
import neopixel
import adafruit_dotstar
import board
# For Trinket M0, Gemma M0, and ItsyBitsy M0 Express
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
# For Feather M0 Express, Metro M0 Express, Metro M4 Express and Circuit Playground Express
# For Feather M0 Express, Metro M0 Express, Metro M4 Express and Circuit
# Playground Express
# led = neopixel.NeoPixel(board.NEOPIXEL, 1)

View file

@ -1,7 +1,8 @@
import time
import board
import digitalio
import microcontroller
import time
led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()

View file

@ -1,5 +1,5 @@
import digitalio
import board
import digitalio
import storage
# For Gemma M0, Trinket M0, Metro M0 Express, ItsyBitsy M0 Express

View file

@ -1,13 +1,15 @@
# CircuitPython demo - NeoPixel
import time
import board
import neopixel
pixel_pin = board.A1
num_pixels = 8
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)
pixels = neopixel.NeoPixel(pixel_pin, num_pixels,
brightness=0.3, auto_write=False)
def wheel(pos):
@ -51,7 +53,8 @@ PURPLE = (180, 0, 255)
while True:
pixels.fill(RED)
pixels.show()
time.sleep(1) # Increase or decrease to change the speed of the solid color change.
# Increase or decrease to change the speed of the solid color change.
time.sleep(1)
pixels.fill(GREEN)
pixels.show()
time.sleep(1)

View file

@ -1,6 +1,7 @@
# CircuitPython demo - NeoPixel RGBW
import time
import board
import neopixel
@ -8,7 +9,8 @@ pixel_pin = board.A1
num_pixels = 8
pixels = neopixel.NeoPixel(pixel_pin, num_pixels,
brightness=0.3, auto_write=False, pixel_order=(1, 0, 2, 3))
brightness=0.3, auto_write=False,
pixel_order=(1, 0, 2, 3))
def wheel(pos):
@ -52,7 +54,8 @@ PURPLE = (180, 0, 255, 0)
while True:
pixels.fill(RED)
pixels.show()
time.sleep(1) # Increase or decrease to change the speed of the solid color change.
# Increase or decrease to change the speed of the solid color change.
time.sleep(1)
pixels.fill(GREEN)
pixels.show()
time.sleep(1)

View file

@ -1,6 +1,7 @@
import time
import pulseio
import board
import pulseio
led = pulseio.PWMOut(board.D13, frequency=5000, duty_cycle=0)

View file

@ -1,11 +1,15 @@
import time
import pulseio
import board
import pulseio
# For the M0 boards:
piezo = pulseio.PWMOut(board.A2, duty_cycle=0, frequency=440, variable_frequency=True)
piezo = pulseio.PWMOut(board.A2, duty_cycle=0,
frequency=440, variable_frequency=True)
# For Metro M4 Express:
# piezo = pulseio.PWMOut(board.A1, duty_cycle=0, frequency=440, variable_frequency=True)
# piezo = pulseio.PWMOut(
# board.A1, duty_cycle=0, frequency=440, variable_frequency=True
# )
while True:
for f in (262, 294, 330, 349, 392, 440, 494, 523):

View file

@ -1,4 +1,5 @@
import time
import board
import simpleio

View file

@ -1,6 +1,7 @@
import time
import simpleio
import board
import simpleio
# For the M0 boards:
servo = simpleio.Servo(board.A2)

View file

@ -1,8 +1,8 @@
# CircuitPython Demo - USB/Serial echo
import digitalio
import board
import busio
import digitalio
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
@ -16,7 +16,8 @@ while True:
if data is not None:
led.value = True
data_string = ''.join([chr(b) for b in data]) # convert bytearray to string
# convert bytearray to string
data_string = ''.join([chr(b) for b in data])
print(data_string, end="")
led.value = False

View file

@ -9,5 +9,5 @@ for pin_name in dir(board):
print("PWM on:", pin_name) # Prints the valid, PWM-capable pins!
except ValueError: # This is the error returned when the pin is invalid.
print("No PWM on:", pin_name) # Prints the invalid pins.
except RuntimeError: # This is the error returned when there is a timer conflict.
except RuntimeError: # Timer conflict error.
print("Timers in use:", pin_name) # Prints the timer conflict pins.

View file

@ -2,7 +2,7 @@ import board
import busio
def is_hardware_SPI(clock_pin, data_pin):
def is_hardware_spi(clock_pin, data_pin):
try:
p = busio.SPI(clock_pin, data_pin)
p.deinit()
@ -11,7 +11,8 @@ def is_hardware_SPI(clock_pin, data_pin):
return False
if is_hardware_SPI(board.A1, board.A2): # Provide the two pins you intend to use.
# Provide the two pins you intend to use.
if is_hardware_spi(board.A1, board.A2):
print("This pin combination is hardware SPI!")
else:
print("This pin combination isn't hardware SPI.")

View file

@ -2,7 +2,7 @@ import board
import busio
def is_hardware_UART(tx, rx):
def is_hardware_uart(tx, rx):
try:
p = busio.UART(tx, rx)
p.deinit()
@ -32,7 +32,7 @@ for tx_pin in get_unique_pins():
if rx_pin is tx_pin:
continue
else:
if is_hardware_UART(tx_pin, rx_pin):
if is_hardware_uart(tx_pin, rx_pin):
print("RX pin:", rx_pin, "\t TX pin:", tx_pin)
else:
pass

View file

@ -1,2 +1 @@
print('Hello, World!')

View file

@ -1,9 +1,11 @@
import sys
import adafruit_sdcard
import board
import busio
import digitalio
import board
import storage
import sys
# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.SD_CS)

View file

@ -1,9 +1,11 @@
import sys
import adafruit_sdcard
import board
import busio
import digitalio
import board
import storage
import sys
# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.SD_CS)

View file

@ -1,10 +1,11 @@
# Dotstar painter! Can handle up to ~2300 pixel size image (e.g. 36 x 64)
import board
import digitalio
import time
import gc
import time
import board
import busio
import digitalio
FILENAME = "blinka.bmp"
IMAGE_DELAY = 0.2
@ -23,6 +24,7 @@ databuf = bytearray(0)
led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()
def read_le(s):
# as of this writting, int.from_bytes does not have LE support, DIY!
result = 0
@ -32,6 +34,7 @@ def read_le(s):
shift += 8
return result
class BMPError(Exception):
pass
@ -68,13 +71,14 @@ try:
rowSize = (bmpWidth * 3 + 3) & ~3 # 32-bit line boundary
databuf = bytearray(bmpWidth * bmpHeight * 4) # its huge! but its also fast :)
# its huge! but its also fast :)
databuf = bytearray(bmpWidth * bmpHeight * 4)
for row in range(bmpHeight): # For each scanline...
if(flip): # Bitmap is stored bottom-to-top order (normal BMP)
if flip: # Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize
else: # Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;
pos = bmpImageoffset + row * rowSize
# print ("seek to %d" % pos)
f.seek(pos)
@ -86,14 +90,15 @@ try:
databuf[idx] = 0xFF # first byte is 'brightness'
idx += 1
for color in order:
databuf[idx] = int(pow((color * BRIGHTNESS) / 255, 2.7) * 255 + 0.5)
databuf[idx] = int(
pow((color * BRIGHTNESS) / 255, 2.7) * 255 + 0.5)
idx += 1
except OSError as e:
if e.args[0] == 28:
halt("OS Error 28", 0.25)
raise OSError("OS Error 28 0.25")
else:
halt("OS Error ", 0.5)
raise OSError("OS Error 0.5")
except BMPError as e:
print("Failed to parse BMP: " + e.args[0])

View file

@ -1,8 +1,9 @@
# CircuitPython AnalogIn Demo
import time
from analogio import AnalogIn
import board
from analogio import AnalogIn
analog_in = AnalogIn(board.A1)

View file

@ -1,7 +1,7 @@
# CircuitPython IO demo - analog output
from analogio import AnalogOut
import board
from analogio import AnalogOut
analog_out = AnalogOut(board.A0)

View file

@ -1,4 +1,5 @@
import time
import board
import touchio

View file

@ -2,6 +2,7 @@
# Example does NOT work with Trinket M0!
import time
import board
import touchio

View file

@ -1,19 +1,20 @@
# CircuitPython IO demo #1 - General Purpose I/O
import time
from digitalio import DigitalInOut, Direction, Pull
import board
from digitalio import DigitalInOut, Direction, Pull
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
switch = DigitalInOut(board.D2) # For Gemma M0, Trinket M0, Metro M0 Express, ItsyBitsy M0 Express
# For Gemma M0, Trinket M0, Metro M0 Express, ItsyBitsy M0 Express
switch = DigitalInOut(board.D2)
# switch = DigitalInOut(board.D5) # For Feather M0 Express
# switch = DigitalInOut(board.D7) # For Circuit Playground Express
switch.direction = Direction.INPUT
switch.pull = Pull.UP
while True:
# We could also just do "led.value = not switch.value"!
if switch.value:

View file

@ -1,11 +1,13 @@
# CircuitPython demo - Dotstar
import time
import board
import adafruit_dotstar
import board
num_pixels = 30
pixels = adafruit_dotstar.DotStar(board.A1, board.A2, num_pixels, brightness=0.1, auto_write=False)
pixels = adafruit_dotstar.DotStar(
board.A1, board.A2, num_pixels, brightness=0.1, auto_write=False)
def wheel(pos):
@ -103,7 +105,8 @@ MAGENTA = (255, 0, 20)
WHITE = (255, 255, 255)
while True:
color_fill(RED, 0.5) # Change this number to change how long it stays on each solid color.
# Change this number to change how long it stays on each solid color.
color_fill(RED, 0.5)
color_fill(YELLOW, 0.5)
color_fill(ORANGE, 0.5)
color_fill(GREEN, 0.5)
@ -114,12 +117,15 @@ while True:
color_fill(MAGENTA, 0.5)
color_fill(WHITE, 0.5)
slice_alternating(0.1) # Increase or decrease this to speed up or slow down the animation.
# Increase or decrease this to speed up or slow down the animation.
slice_alternating(0.1)
color_fill(WHITE, 0.5)
slice_rainbow(0.1) # Increase or decrease this to speed up or slow down the animation.
# Increase or decrease this to speed up or slow down the animation.
slice_rainbow(0.1)
time.sleep(0.5)
rainbow_cycle(0) # Increase this number to slow down the rainbow animation.
# Increase this number to slow down the rainbow animation.
rainbow_cycle(0)

View file

@ -1,7 +1,7 @@
import time
import board
import neopixel
import adafruit_dotstar
import board
# For Trinket M0, Gemma M0, and ItsyBitsy M0 Express
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)

View file

@ -1,10 +1,12 @@
import time
import board
import neopixel
import adafruit_dotstar
import board
# For Trinket M0, Gemma M0, and ItsyBitsy M0 Express
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
# For Feather M0 Express, Metro M0 Express, and Circuit Playground Express
# led = neopixel.NeoPixel(board.NEOPIXEL, 1)

View file

@ -1,13 +1,15 @@
# CircuitPython demo - NeoPixel
import time
import board
import neopixel
pixel_pin = board.A1
num_pixels = 8
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)
pixels = neopixel.NeoPixel(pixel_pin, num_pixels,
brightness=0.3, auto_write=False)
def wheel(pos):
@ -51,7 +53,8 @@ PURPLE = (180, 0, 255)
while True:
pixels.fill(RED)
pixels.show()
time.sleep(1) # Increase or decrease to change the speed of the solid color change.
# Increase or decrease to change the speed of the solid color change.
time.sleep(1)
pixels.fill(GREEN)
pixels.show()
time.sleep(1)

View file

@ -1,6 +1,7 @@
# CircuitPython demo - NeoPixel RGBW
import time
import board
import neopixel
@ -8,7 +9,8 @@ pixel_pin = board.A1
num_pixels = 8
pixels = neopixel.NeoPixel(pixel_pin, num_pixels,
brightness=0.3, auto_write=False, pixel_order=(1, 0, 2, 3))
brightness=0.3, auto_write=False,
pixel_order=(1, 0, 2, 3))
def wheel(pos):
@ -52,7 +54,8 @@ PURPLE = (180, 0, 255, 0)
while True:
pixels.fill(RED)
pixels.show()
time.sleep(1) # Increase or decrease to change the speed of the solid color change.
# Increase or decrease to change the speed of the solid color change.
time.sleep(1)
pixels.fill(GREEN)
pixels.show()
time.sleep(1)

View file

@ -1,6 +1,7 @@
import time
import pulseio
import board
import pulseio
led = pulseio.PWMOut(board.D13, frequency=5000, duty_cycle=0)

View file

@ -1,8 +1,10 @@
import time
import pulseio
import board
piezo = pulseio.PWMOut(board.A2, duty_cycle=0, frequency=440, variable_frequency=True)
import board
import pulseio
piezo = pulseio.PWMOut(board.A2, duty_cycle=0,
frequency=440, variable_frequency=True)
while True:
for f in (262, 294, 330, 349, 392, 440, 494, 523):

View file

@ -1,4 +1,5 @@
import time
import board
import simpleio

View file

@ -1,6 +1,7 @@
import time
import simpleio
import board
import simpleio
servo = simpleio.Servo(board.A2)

View file

@ -1,8 +1,8 @@
# CircuitPython Demo - USB/Serial echo
import digitalio
import board
import busio
import digitalio
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
@ -16,7 +16,8 @@ while True:
if data is not None:
led.value = True
data_string = ''.join([chr(b) for b in data]) # convert bytearray to string
# convert bytearray to string
data_string = ''.join([chr(b) for b in data])
print(data_string, end="")
led.value = False

View file

@ -9,5 +9,5 @@ for pin_name in dir(board):
print("PWM on:", pin_name) # Prints the valid, PWM-capable pins!
except ValueError: # This is the error returned when the pin is invalid.
print("No PWM on:", pin_name) # Prints the invalid pins.
except RuntimeError: # This is the error returned when there is a timer conflict.
except RuntimeError: # Timer conflict error.
print("Timers in use:", pin_name) # Prints the timer conflict pins.

View file

@ -11,7 +11,8 @@ def is_hardware_SPI(clock_pin, data_pin):
return False
if is_hardware_SPI(board.A1, board.A2): # Provide the two pins you intend to use.
# Provide the two pins you intend to use.
if is_hardware_SPI(board.A1, board.A2):
print("This pin combination is hardware SPI!")
else:
print("This pin combination isn't hardware SPI.")

View file

@ -1,10 +1,11 @@
import board
import array
import time
from digitalio import DigitalInOut, Direction, Pull
import pulseio
############## Switch to select 'stealth-mode'
import board
import pulseio
from digitalio import DigitalInOut, Direction, Pull
# Switch to select 'stealth-mode'
switch = DigitalInOut(board.SLIDE_SWITCH)
switch.direction = Direction.INPUT
switch.pull = Pull.UP
@ -12,14 +13,14 @@ switch.pull = Pull.UP
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
############## Speaker as haptic feedback
# Speaker as haptic feedback
spkr_en = DigitalInOut(board.SPEAKER_ENABLE)
spkr_en.direction = Direction.OUTPUT
spkr_en.value = True
spkr = DigitalInOut(board.SPEAKER)
spkr.direction = Direction.OUTPUT
############## Allow any button to trigger activity!
# Allow any button to trigger activity!
button_a = DigitalInOut(board.BUTTON_A)
button_a.direction = Direction.INPUT
button_a.pull = Pull.DOWN
@ -27,8 +28,8 @@ button_b = DigitalInOut(board.BUTTON_B)
button_b.direction = Direction.INPUT
button_b.pull = Pull.DOWN
pwm = pulseio.PWMOut(board.REMOTEOUT, frequency=38000, duty_cycle=2 ** 15, variable_frequency=True)
pwm = pulseio.PWMOut(board.REMOTEOUT, frequency=38000,
duty_cycle=2 ** 15, variable_frequency=True)
pulse = pulseio.PulseOut(pwm)
while True:

View file

@ -1,22 +1,24 @@
# Gemma M0 version of TVBgone!
import board
import array
import time
from digitalio import DigitalInOut, Direction, Pull
import pulseio
import adafruit_dotstar
pixel = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
import adafruit_dotstar
import board
import pulseio
from digitalio import DigitalInOut, Direction
pixel = adafruit_dotstar.DotStar(
board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
pixel.fill((0, 0, 0))
# Button to see output debug
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
pwm = pulseio.PWMOut(board.A1, frequency=38000, duty_cycle=2 ** 15, variable_frequency=True)
pwm = pulseio.PWMOut(board.A1, frequency=38000,
duty_cycle=2 ** 15, variable_frequency=True)
pulse = pulseio.PulseOut(pwm)
time.sleep(0.5) # Give a half second before starting
# gooooo!

View file

@ -4,9 +4,10 @@
# Photocell voltage divider center wire to GPIO #2 (analog 1)
# and output tone to GPIO #0 (digital 0)
import board
import analogio
import time
import analogio
import board
import neopixel
import simpleio
@ -26,6 +27,7 @@ tonec = 262
toneC = 130
toneg = 392
def alien():
strip[8] = (255, 255, 0) # yellow front
strip[3] = (255, 255, 0) # yellow back
@ -82,10 +84,11 @@ def alien():
time.sleep(.1)
# Loop forever...
while True:
# turn lights and audio on when dark
# (less than 50% light on analog pin)
if ( photocell.value > darkness_min ):
if photocell.value > darkness_min:
alien() # close Encounters Loop

View file

@ -2,22 +2,26 @@
# for Adafruit Circuit Playground express
# with CircuitPython
from adafruit_circuitplayground.express import cpx
import time
import board
import simpleio
from adafruit_circuitplayground.express import cpx
# plug red servo wire to VOUT, brown to GND, yellow to A3
servo = simpleio.Servo(board.A3)
cpx.pixels.brightness = 0.05 # set brightness value
def unlock_servo():
servo.angle = 180
def lock_servo():
servo.angle = 90
correct_combo = ['B', 'D', 'C'] # this is where to set the combo
entered_combo = [] # this will be used to store attempts
current_dial_position = 'X'
@ -74,7 +78,8 @@ while True:
if cpx.button_a: # this means the button has been pressed
# grab the current_dial_position value and add to the list
entered_combo.append(current_dial_position)
dial_msg = 'Dial Position: ' + str(entered_combo[(len(entered_combo)-1)])
dial_msg = 'Dial Position: ' + \
str(entered_combo[(len(entered_combo) - 1)])
print(dial_msg)
cpx.play_tone(320, 0.3) # beep
time.sleep(1) # slow down button checks

View file

@ -1,11 +1,12 @@
# CircuitPython 3.0 CRICKIT demo
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.pwmout import PWMOut
from adafruit_motor import servo, motor
from busio import I2C
import board
import time
import board
from adafruit_motor import servo, motor
from adafruit_seesaw.pwmout import PWMOut
from adafruit_seesaw.seesaw import Seesaw
from busio import I2C
i2c = I2C(board.SCL, board.SDA)
ss = Seesaw(i2c)
@ -14,7 +15,7 @@ print("Bubble machine!")
SERVOS = True
DCMOTORS = True
#################### Create 4 Servos
# Create 4 Servos
servos = []
if SERVOS:
for ss_pin in (17, 16, 15, 14):
@ -24,7 +25,7 @@ if SERVOS:
_servo.angle = 90 # starting angle, middle
servos.append(_servo)
#################### Create 2 DC motors
# Create 2 DC motors
motors = []
if DCMOTORS:
for ss_pin in ((18, 19), (22, 23)):

View file

@ -1,43 +1,45 @@
# CircuitPython 3.0 CRICKIT demo
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.pwmout import PWMOut
from adafruit_motor import servo, motor
import audioio
from busio import I2C
import random
import board
import time
import gc
import time
import audioio
import board
from adafruit_motor import servo
from adafruit_seesaw.pwmout import PWMOut
from adafruit_seesaw.seesaw import Seesaw
from busio import I2C
i2c = I2C(board.SCL, board.SDA)
ss = Seesaw(i2c)
print("Feynbot demo!")
#################### 1 Servo
# 1 Servo
pwm = PWMOut(ss, 17)
pwm.frequency = 50
myservo = servo.Servo(pwm)
myservo.angle = 180 # starting angle, highest
#################### 2 Drivers
# 2 Drivers
drives = []
for ss_pin in (13, 12):
_pwm = PWMOut(ss, ss_pin)
_pwm.frequency = 1000
drives.append(_pwm)
#################### Audio files
# Audio files
wavfiles = ["01.wav", "02.wav", "03.wav", "04.wav", "05.wav"]
a = audioio.AudioOut(board.A0)
# Start playing the file (in the background)
def play_file(wavfile):
f = open(wavfile, "rb")
wav = audioio.WaveFile(f)
a.play(wav)
# Tap the solenoids back and forth
def bongo(t):
for _ in range(t):
@ -50,6 +52,7 @@ def bongo(t):
drives[1].duty_cycle = 0
time.sleep(0.1)
# Move mouth back and forth
def talk(t):
for _ in range(t):

View file

@ -25,8 +25,10 @@
# calibrated with your body touching it (making it less accurate).
#
# Also note this depends on two external modules to be loaded on the Gemma M0:
# - Adafruit CircuitPython DotStar: https://github.com/adafruit/Adafruit_CircuitPython_DotStar
# - Adafruit CircuitPython FancyLED: https://github.com/adafruit/Adafruit_CircuitPython_FancyLED
# - Adafruit CircuitPython DotStar:
# https://github.com/adafruit/Adafruit_CircuitPython_DotStar
# - Adafruit CircuitPython FancyLED:
# https://github.com/adafruit/Adafruit_CircuitPython_FancyLED
#
# You _must_ have both adafruit_dotstar.mpy and the adafruit_fancyled folder
# and files within it on your board for this code to work! If you run into
@ -38,14 +40,12 @@
import math
import time
import adafruit_dotstar
import adafruit_fancyled.adafruit_fancyled as fancy
import board
import digitalio
import touchio
import adafruit_dotstar
import adafruit_fancyled.adafruit_fancyled as fancy
# Variables that control the code. Try changing these to modify speed, color,
# etc.
START_DELAY = 5.0 # How many seconds to wait after power up before
@ -111,6 +111,8 @@ beat_quarter_period = beat_period/4.0 # Quarter period controls the speed of
# the heartbeat drop-off (using an
# exponential decay function).
beat_phase = beat_period / 5.0 # Phase controls how long in-between
# the two parts of the heart beat
# (the 'ba-boom' of the beat).
@ -119,9 +121,12 @@ beat_phase = beat_period/5.0 # Phase controls how long in-between
# y that's proportionally within y0...y1 based on x within x0...x1. Handy for
# transforming a value in one range to a value in another (like Arduino's map
# function).
def lerp(x, x0, x1, y0, y1):
return y0 + (x - x0) * ((y1 - y0) / (x1 - x0))
# Main loop below will run forever:
while True:
# Get the current time at the start of the animation update.

View file

@ -34,7 +34,6 @@ import busio
import digitalio
import touchio
# Variables that control the code. Try changing these to modify speed, color,
# etc.
START_DELAY = 5.0 # How many seconds to wait after power up before
@ -78,7 +77,11 @@ dotstar_spi = busio.SPI(clock=board.APA102_SCK, MOSI=board.APA102_MOSI)
# pixel data, followed by bytes of 0xFF tail (just one for 1 pixel).
dotstar_data = bytearray([0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
0xFF])
# Define a function to simplify setting dotstar color.
def dotstar_color(rgb_color):
# Set the color of the dot star LED. This is barebones dotstar driving
# code for simplicity and less dependency on other libraries. We're only
@ -93,6 +96,8 @@ def dotstar_color(rgb_color):
dotstar_spi.write(dotstar_data)
finally:
dotstar_spi.unlock()
# Call the function above to turn off the dotstar initially (set it to all 0).
dotstar_color((0, 0, 0))
@ -133,6 +138,7 @@ gamma8 = bytearray(256)
for i in range(len(gamma8)):
gamma8[i] = int(math.pow(i / 255.0, 2.8) * 255.0 + 0.5) & 0xFF
# Define a function to convert from HSV (hue, saturation, value) color to
# RGB colors that DotStar LEDs speak. The HSV color space is a nicer for
# animations because you can easily change the hue and value (brightness)
@ -140,10 +146,9 @@ for i in range(len(gamma8)):
# value that range from 0 to 1.0. This will also use the gamma correction
# table above to get the most accurate color. Adapted from C/C++ code here:
# https://www.cs.rit.edu/~ncs/color/t_convert.html
def HSV_to_RGB(h, s, v):
r = 0
g = 0
b = 0
if s == 0.0:
r = v
g = v
@ -184,14 +189,18 @@ def HSV_to_RGB(h, s, v):
b = gamma8[int(255.0 * b)]
return (r, g, b)
# Another handy function for linear interpolation of a value. Pass in a value
# x that's within the range x0...x1 and a range y0...y1 to get an output value
# y that's proportionally within y0...y1 based on x within x0...x1. Handy for
# transforming a value in one range to a value in another (like Arduino's map
# function).
def lerp(x, x0, x1, y0, y1):
return y0 + (x - x0) * ((y1 - y0) / (x1 - x0))
# Main loop below will run forever:
while True:
# Get the current time at the start of the animation update.

View file

@ -1,13 +1,14 @@
from digitalio import DigitalInOut, Direction
import time
import board
import neopixel
import time
from digitalio import DigitalInOut, Direction
try:
import urandom as random
except ImportError:
import random
pixpin = board.D1
numpix = 16
@ -22,19 +23,27 @@ colors = [
[30, 200, 200], # Blue
]
# Fill the dots one after the other with a color
def colorWipe(color, wait):
for j in range(len(strip)):
strip[j] = (color)
time.sleep(wait)
def rainbow(wait):
for j in range(255):
for i in range(len(strip)):
idx = int(i + j)
strip[i] = wheel(idx & 255)
time.sleep(wait)
# Slightly different, this makes the rainbow equally distributed throughout
def rainbow_cycle(wait):
for j in range(255 * 5):
for i in range(len(strip)):
@ -42,16 +51,19 @@ def rainbow_cycle(wait):
strip[i] = wheel(idx & 255)
time.sleep(wait)
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255):
return (0, 0, 0)
if (pos < 85):
if pos < 85:
return (int(pos * 3), int(255 - (pos * 3)), 0)
elif (pos < 170):
elif pos < 170:
pos -= 85
return (int(255 - pos * 3), 0, int(pos * 3))
else:
@ -60,8 +72,7 @@ def wheel(pos):
def flash_random(wait, howmany):
for k in range(howmany):
for _ in range(howmany):
c = random.randint(0, len(colors) - 1) # Choose random color index
j = random.randint(0, numpix - 1) # Choose random pixel
@ -71,7 +82,6 @@ def flash_random(wait,howmany):
strip.brightness = i / 5.0 # Ramp up brightness
time.sleep(wait)
for i in range(5, 0, -1):
strip.brightness = i / 5.0 # Ramp down brightness
strip[j] = [0, 0, 0] # Set pixel to 'off'
@ -79,8 +89,10 @@ def flash_random(wait,howmany):
while True:
flash_random(.01, 8) # first number is 'wait' delay, shorter num == shorter twinkle
flash_random(.01, 5) # second number is how many neopixels to simultaneously light up
# first number is 'wait' delay, shorter num == shorter twinkle
flash_random(.01, 8)
# second number is how many neopixels to simultaneously light up
flash_random(.01, 5)
flash_random(.01, 11)
colorWipe((232, 100, 255), .1)

View file

@ -1,9 +1,10 @@
import time
import adafruit_sdcard
import adafruit_am2320
import adafruit_sdcard
import analogio
import board
import busio
import analogio
import digitalio
import storage
@ -41,8 +42,10 @@ while True:
print("Humidity:", humidity)
print("VBat voltage: {:.2f}".format(battery_voltage))
print()
sdc.write("{}, {}, {}, {:.2f}\n".format(int(time_stamp), temperature,
humidity, battery_voltage))
sdc.write("{}, {}, {}, {:.2f}\n".format(
int(time_stamp), temperature,
humidity, battery_voltage)
)
time.sleep(3)
except OSError:
pass

View file

@ -29,12 +29,11 @@ Explainatory comments are used verbatim from that code.
import math
import random
import adafruit_dotstar
import adafruit_lsm303
import board
import busio
import adafruit_lsm303
import adafruit_dotstar
N_GRAINS = 10 # Number of grains of sand
WIDTH = 12 # Display width in pixels
HEIGHT = 6 # Display height in pixels
@ -44,21 +43,24 @@ GRAIN_COLOR = (0, 0, 16)
MAX_X = WIDTH * 256 - 1
MAX_Y = HEIGHT * 256 - 1
class Grain:
"""A simple struct to hold position and velocity information for a single grain."""
"""A simple struct to hold position and velocity information
for a single grain."""
def __init__(self):
"""Initialize grain position and velocity."""
x = 0
y = 0
vx = 0
vy = 0
self.x = 0
self.y = 0
self.vx = 0
self.vy = 0
grains = [Grain() for _ in range(N_GRAINS)]
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_lsm303.LSM303(i2c)
wing = adafruit_dotstar.DotStar(board.D13, board.D11, WIDTH * HEIGHT, 0.25, False)
wing = adafruit_dotstar.DotStar(
board.D13, board.D11, WIDTH * HEIGHT, 0.25, False)
oldidx = 0
newidx = 0
@ -68,19 +70,23 @@ newy = 0
occupied_bits = [False for _ in range(WIDTH * HEIGHT)]
def index_of_xy(x, y):
"""Convert an x/column and y/row into an index into a linear pixel array.
"""Convert an x/column and y/row into an index into
a linear pixel array.
:param int x: column value
:param int y: row value
"""
return (y >> 8) * WIDTH + (x >> 8)
def already_present(limit, x, y):
"""Check if a pixel is already used.
:param int limit: the index into the grain array of the grain being assigned a pixel
Only grains already allocated need to be checks against.
:param int limit: the index into the grain array of
the grain being assigned a pixel Only grains already
allocated need to be checks against.
:param int x: proposed clumn value for the new grain
:param int y: proposed row valuse for the new grain
"""
@ -89,6 +95,7 @@ def already_present(limit, x, y):
return True
return False
for g in grains:
placed = False
while not placed:
@ -165,30 +172,38 @@ while True:
oldidx = index_of_xy(g.x, g.y) # prior pixel
newidx = index_of_xy(newx, newy) # new pixel
if oldidx != newidx and occupied_bits[newidx]: # If grain is moving to a new pixel...
# If grain is moving to a new pixel...
if oldidx != newidx and occupied_bits[newidx]:
# but if that pixel is already occupied...
delta = abs(newidx - oldidx) # What direction when blocked?
# What direction when blocked?
delta = abs(newidx - oldidx)
if delta == 1: # 1 pixel left or right
newx = g.x # cancel x motion
g.vx //= -2 # and bounce X velocity (Y is ok)
# and bounce X velocity (Y is ok)
g.vx //= -2
newidx = oldidx # no pixel change
elif delta == WIDTH: # 1 pixel up or down
newy = g.y # cancel Y motion
g.vy //= -2 # and bounce Y velocity (X is ok)
# and bounce Y velocity (X is ok)
g.vy //= -2
newidx = oldidx # no pixel change
else: # Diagonal intersection is more tricky...
# Try skidding along just one axis of motion if possible (start w/
# faster axis). Because we've already established that diagonal
# (both-axis) motion is occurring, moving on either axis alone WILL
# change the pixel index, no need to check that again.
# Try skidding along just one axis of motion if
# possible (start w/ faster axis). Because we've
# already established that diagonal (both-axis)
# motion is occurring, moving on either axis alone
# WILL change the pixel index, no need to check
# that again.
if abs(g.vx) > abs(g.vy): # x axis is faster
newidx = index_of_xy(newx, g.y)
if not occupied_bits[newidx]: # that pixel is free, take it! But...
# that pixel is free, take it! But...
if not occupied_bits[newidx]:
newy = g.y # cancel Y motion
g.vy //= -2 # and bounce Y velocity
else: # X pixel is taken, so try Y...
newidx = index_of_xy(g.x, newy)
if not occupied_bits[newidx]: # Pixel is free, take it, but first...
# Pixel is free, take it, but first...
if not occupied_bits[newidx]:
newx = g.x # Cancel X motion
g.vx //= -2 # Bounce X velocity
else: # both spots are occupied
@ -199,12 +214,14 @@ while True:
newidx = oldidx # Not moving
else: # y axis is faster. start there
newidx = index_of_xy(g.x, newy)
if not occupied_bits[newidx]: # Pixel's free! Take it! But...
# Pixel's free! Take it! But...
if not occupied_bits[newidx]:
newx = g.x # Cancel X motion
g.vx //= -2 # Bounce X velocity
else: # Y pixel is taken, so try X...
newidx = index_of_xy(newx, g.y)
if not occupied_bits[newidx]: # Pixel is free, take it, but first...
# Pixel is free, take it, but first...
if not occupied_bits[newidx]:
newy = g.y # cancel Y motion
g.vy //= -2 # and bounce Y velocity
else: # both spots are occupied

View file

@ -26,8 +26,10 @@ Debounce an input pin.
"""
import time
import digitalio
class Debouncer(object):
"""Debounce an input pin"""
@ -35,45 +37,43 @@ class Debouncer(object):
UNSTABLE_STATE = 0x02
CHANGED_STATE = 0x04
def __init__(self, pin, mode=None, interval=0.010):
"""Make am instance.
:param int pin: the pin (from board) to debounce
:param int mode: digitalio.Pull.UP or .DOWN (default is no pull up/down)
:param int interval: bounce threshold in seconds (default is 0.010, i.e. 10 milliseconds)
:param int mode: digitalio.Pull.UP or .DOWN (default is no
pull up/down)
:param int interval: bounce threshold in seconds (default is 0.010,
i.e. 10 milliseconds)
"""
self.state = 0x00
self.pin = digitalio.DigitalInOut(pin)
self.pin.direction = digitalio.Direction.INPUT
if mode != None:
if mode is not None:
self.pin.pull = mode
if self.pin.value:
self.__set_state(Debouncer.DEBOUNCED_STATE | Debouncer.UNSTABLE_STATE)
self.__set_state(Debouncer.DEBOUNCED_STATE |
Debouncer.UNSTABLE_STATE)
self.previous_time = 0
if interval is None:
self.interval = 0.010
else:
self.interval = interval
def __set_state(self, bits):
self.state |= bits
def __unset_state(self, bits):
self.state &= ~bits
def __toggle_state(self, bits):
self.state ^= bits
def __get_state(self, bits):
return (self.state & bits) != 0
def update(self):
"""Update the debouncer state. Must be called before using any of the properties below"""
"""Update the debouncer state. Must be called before using any of
the properties below"""
self.__unset_state(Debouncer.CHANGED_STATE)
current_state = self.pin.value
if current_state != self.__get_state(Debouncer.UNSTABLE_STATE):
@ -81,25 +81,27 @@ class Debouncer(object):
self.__toggle_state(Debouncer.UNSTABLE_STATE)
else:
if time.monotonic() - self.previous_time >= self.interval:
if current_state != self.__get_state(Debouncer.DEBOUNCED_STATE):
debounced_state = self.__get_state(Debouncer.DEBOUNCED_STATE)
if current_state != debounced_state:
self.previous_time = time.monotonic()
self.__toggle_state(Debouncer.DEBOUNCED_STATE)
self.__set_state(Debouncer.CHANGED_STATE)
@property
def value(self):
"""Return the current debounced value of the input."""
return self.__get_state(Debouncer.DEBOUNCED_STATE)
@property
def rose(self):
"""Return whether the debounced input went from low to high at the most recent update."""
return self.__get_state(self.DEBOUNCED_STATE) and self.__get_state(self.CHANGED_STATE)
"""Return whether the debounced input went from low to high at
the most recent update."""
return self.__get_state(self.DEBOUNCED_STATE) \
and self.__get_state(self.CHANGED_STATE)
@property
def fell(self):
"""Return whether the debounced input went from high to low at the most recent update."""
return (not self.__get_state(self.DEBOUNCED_STATE)) and self.__get_state(self.CHANGED_STATE)
"""Return whether the debounced input went from high to low at
the most recent update."""
return (not self.__get_state(self.DEBOUNCED_STATE)) \
and self.__get_state(self.CHANGED_STATE)

View file

@ -28,6 +28,7 @@ Manage a directory in the file system.
import os
class DirectoryNode(object):
"""Display and navigate the SD card contents"""
@ -46,7 +47,6 @@ class DirectoryNode(object):
self.selected_offset = 0
self.old_selected_offset = -1
def __cleanup(self):
"""Dereference things for speedy gc."""
self.display = None
@ -55,7 +55,6 @@ class DirectoryNode(object):
self.files = None
return self
def __is_dir(self, path):
"""Determine whether a path identifies a machine code bin file.
:param string path: path of the file to check
@ -68,7 +67,6 @@ class DirectoryNode(object):
except OSError:
return False
def __sanitize(self, name):
"""Nondestructively strip off a trailing slash, if any, and return the result.
:param string name: the filename
@ -77,7 +75,6 @@ class DirectoryNode(object):
return name[:-1]
return name
def __path(self):
"""Return the result of recursively follow the parent links, building a full
path to this directory."""
@ -85,24 +82,22 @@ class DirectoryNode(object):
return self.parent.__path() + os.sep + self.__sanitize(self.name)
return self.__sanitize(self.name)
def __make_path(self, filename):
"""Return a full path to the specified file in this directory.
:param string filename: the name of the file in this directory
"""
return self.__path() + os.sep + filename
def __number_of_files(self):
"""The number of files in this directory, including the ".." for the parent
directory if this isn't the top directory on the SD card."""
self.__get_files()
return len(self.files)
def __get_files(self):
"""Return a list of the files in this directory.
If this is not the top directory on the SD card, a ".." entry is the first element.
If this is not the top directory on the SD card, a
".." entry is the first element.
Any directories have a slash appended to their name."""
if len(self.files) == 0:
self.files = os.listdir(self.__path())
@ -113,48 +108,48 @@ class DirectoryNode(object):
if self.__is_dir(self.__make_path(name)):
self.files[index] = name + "/"
def __update_display(self):
"""Update the displayed list of files if required."""
if self.top_offset != self.old_top_offset:
self.__get_files()
self.display.fill(0)
for i in range(self.top_offset, min(self.top_offset + 4, self.__number_of_files())):
min_offset = min(self.top_offset + 4, self.__number_of_files())
for i in range(self.top_offset, min_offset):
self.display.text(self.files[i], 10, (i - self.top_offset) * 8)
self.display.show()
self.old_top_offset = self.top_offset
def __update_selection(self):
"""Update the selected file lighlight if required."""
if self.selected_offset != self.old_selected_offset:
if self.old_selected_offset > -1:
self.display.text(">", 0, (self.old_selected_offset - self.top_offset) * 8, 0)
self.display.text(">", 0, (self.selected_offset - self.top_offset) * 8, 1)
old_offset = (self.old_selected_offset - self.top_offset) * 8
self.display.text(">", 0, old_offset, 0)
new_offset = (self.selected_offset - self.top_offset) * 8
self.display.text(">", 0, new_offset, 1)
self.display.show()
self.old_selected_offset = self.selected_offset
def __is_directory_name(self, filename):
"""Is a filename the name of a directory.
:param string filename: the name of the file
"""
return filename[-1] == '/'
@property
def selected_filename(self):
"""The name of the currently selected file in this directory."""
self.__get_files()
return self.files[self.selected_offset]
@property
def selected_filepath(self):
"""The full path of the currently selected file in this directory."""
return self.__make_path(self.selected_filename)
def force_update(self):
"""Force an update of the file list and selected file highlight."""
self.old_selected_offset = -1
@ -162,10 +157,9 @@ class DirectoryNode(object):
self.__update_display()
self.__update_selection()
def down(self):
"""Move down in the file list if possible, adjusting the selected file indicator
and scrolling the display as required."""
"""Move down in the file list if possible, adjusting the selected file
indicator and scrolling the display as required."""
if self.selected_offset < self.__number_of_files() - 1:
self.selected_offset += 1
if self.selected_offset == self.top_offset + 4:
@ -173,7 +167,6 @@ class DirectoryNode(object):
self.__update_display()
self.__update_selection()
def up(self):
"""Move up in the file list if possible, adjusting the selected file indicator
and scrolling the display as required."""
@ -184,10 +177,10 @@ class DirectoryNode(object):
self.__update_display()
self.__update_selection()
def click(self):
"""Handle a selection and return the new current directory.
If the selected file is the parent, i.e. "..", return to the parent directory.
If the selected file is the parent, i.e. "..", return to the parent
directory.
If the selected file is a directory, go into it."""
if self.selected_filename == "..":
if self.parent:
@ -196,7 +189,8 @@ class DirectoryNode(object):
self.__cleanup()
return p
elif self.__is_directory_name(self.selected_filename):
new_node = DirectoryNode(self.display, self, self.selected_filename)
new_node = DirectoryNode(
self.display, self, self.selected_filename)
new_node.force_update()
return new_node
return self

View file

@ -25,8 +25,8 @@ THE SOFTWARE.
Manage the emulator hardware.
"""
import digitalio
import adafruit_mcp230xx
import digitalio
# control pin values
@ -85,47 +85,40 @@ class Emulator(object):
self.led_pin.direction = digitalio.Direction.OUTPUT
self.led_pin.value = False
def __pulse_write(self):
self.write_pin.value = WRITE_ENABLED
self.write_pin.value = WRITE_DISABLED
def __deactivate_ram(self):
self.chip_select_pin.value = CHIP_DISABLED
def __activate_ram(self):
self.chip_select_pin.value = CHIP_ENABLED
def __reset_address_counter(self):
self.clock_reset_pin.value = RESET_ACTIVE
self.clock_reset_pin.value = RESET_INACTIVE
def __advance_address_counter(self):
self.address_clock_pin.value = CLOCK_ACTIVE
self.address_clock_pin.value = CLOCK_INACTIVE
def __output_on_port_a(self, data_byte):
"""A hack to get around the limitation of the 23017 library to use 8-bit ports"""
"""A hack to get around the limitation of the 23017
library to use 8-bit ports"""
self.mcp.gpio = (self.mcp.gpio & 0xFF00) | (data_byte & 0x00FF)
def enter_program_mode(self):
"""Enter program mode, allowing loading of the emulator RAM."""
self.mode_pin.value = PROGRAMMER_USE
self.led_pin.value = LED_OFF
def enter_emulate_mode(self):
"""Enter emulate mode, giving control of the emulator ram to the host."""
"""Enter emulate mode, giving control of the emulator
ram to the host."""
self.mode_pin.value = EMULATE_USE
self.led_pin.value = LED_ON
def load_ram(self, code):
"""Load the emulator RAM. Automatically switched to program mode.
:param [byte] code: the list of bytes to load into the emulator RAM

View file

@ -29,16 +29,15 @@ Targeted for the SAMD51 boards.
by Dave Astels
"""
import digitalio
import adafruit_sdcard
import adafruit_ssd1306
import board
import busio
import adafruit_ssd1306
import digitalio
import storage
import adafruit_sdcard
from debouncer import Debouncer
from directory_node import DirectoryNode
from emulator import Emulator
from debouncer import Debouncer
# --------------------------------------------------------------------------------
# Initialize Rotary encoder
@ -80,7 +79,6 @@ oled.fill(0)
oled.text("Done", 0, 10)
oled.show()
# --------------------------------------------------------------------------------
# Initialize globals

View file

@ -6,12 +6,13 @@
# Use a jumper wire from D2 to GND to prevent injection while programming!
from digitalio import DigitalInOut, Direction, Pull
import board
import time
import board
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from digitalio import DigitalInOut, Direction, Pull
####################################################################
# Select the target operating system for payload:
@ -53,7 +54,10 @@ led.value = True
# Wait a moment
pause = 0.25
# The functions that follow are the various payloads to deliver
def launch_terminal():
if operating_system is 0:
led.value = False
@ -105,7 +109,8 @@ def launch_terminal():
time.sleep(2)
led.value = False
layout.write("echo \'Try to be more careful what you put in your USB port.\'")
layout.write(
"echo \'Try to be more careful what you put in your USB port.\'")
time.sleep(pause)
kbd.press(Keycode.ENTER)
kbd.release_all()
@ -130,7 +135,7 @@ def launch_terminal():
time.sleep(pause)
# type a message a few times
for i in range(3):
for _ in range(3):
layout.write("HELLO FRIEND")
# time.sleep(pause)
kbd.press(Keycode.ENTER)
@ -138,19 +143,34 @@ def launch_terminal():
# time.sleep(pause)
time.sleep(2)
layout.write(" _ _ _____ _ _ ___ _____ ____ ___ _____ _ _ ____")
layout.write(
" _ _ _____ _ _ ___ "
"_____ ____ ___ _____ _ _ ____"
)
kbd.press(Keycode.ENTER)
kbd.release_all()
layout.write("| | | | ____| | | | / _ \ | ___| _ \|_ _| ____| \ | | _ \ ")
layout.write(
"| | | | ____| | | | / _ \ | "
" ___| _ \|_ _| ____| \ | | _ \ "
)
kbd.press(Keycode.ENTER)
kbd.release_all()
layout.write("| |_| | _| | | | | | | | | | |_ | |_) || || _| | \| | | | |")
layout.write(
"| |_| | _| | | | | | | | | | |"
"_ | |_) || || _| | \| | | | |"
)
kbd.press(Keycode.ENTER)
kbd.release_all()
layout.write("| _ | |___| |___| |__| |_| | | _| | _ < | || |___| |\ | |_| |")
layout.write(
"| _ | |___| |___| |__| |_| | | "
" _| | _ < | || |___| |\ | |_| |"
)
kbd.press(Keycode.ENTER)
kbd.release_all()
layout.write("|_| |_|_____|_____|_____\___/ |_| |_| \_\___|_____|_| \_|____/ ")
layout.write(
"|_| |_|_____|_____|_____\___/ |_"
"| |_| \_\___|_____|_| \_|____/ "
)
kbd.press(Keycode.ENTER)
kbd.release_all()
@ -159,6 +179,7 @@ def launch_terminal():
kbd.press(Keycode.ENTER)
kbd.release_all()
def download_image():
led.value = False
# run this after running 'launch_terminal'
@ -178,7 +199,14 @@ def download_image():
# this says where to save image, and where to get it
led.value = False
layout.write('curl -o ~/Desktop/hackimage.jpg https://cdn-learn.adafruit.com/assets/assets/000/051/840/original/hacks_foulFowl.jpg')
url = (
'https://cdn-learn.adafruit.com/assets/assets/000/051/840/'
'original/hacks_foulFowl.jpg'
)
layout.write(
'curl -o ~/Desktop/hackimage.jpg {}'.format(url)
)
time.sleep(pause)
kbd.press(Keycode.ENTER)
led.value = True
@ -200,7 +228,12 @@ def replace_background():
led.value = False
# run this after download_image (which ran after launch_terminal)
# it uses actionscript to change the background
layout.write('osascript -e \'tell application \"System Events\" to set picture of every desktop to (POSIX path of (path to home folder) & \"/Desktop/hackimage.jpg\" as POSIX file as alias)\'')
layout.write(
'osascript -e \'tell application \"System Events\" '
'to set picture of every desktop to (POSIX path of '
'(path to home folder) & \"/Desktop/hackimage.jpg\" '
'as POSIX file as alias)\''
)
time.sleep(pause)
kbd.press(Keycode.ENTER)
kbd.release_all()
@ -216,6 +249,7 @@ def replace_background():
led.value = True
time.sleep(3) # give it a moment to refresh dock and BG
def hide_everything():
led.value = False
# print("Hiding stuff... ")
@ -224,6 +258,7 @@ def hide_everything():
time.sleep(10)
kbd.release_all()
while True:
# check for presence of jumper from GND to D2
if buttons[0].value is False and payload_delivered is 0:
@ -235,7 +270,6 @@ while True:
led.value = False
payload_delivered = 1
if buttons[0].value is True and payload_delivered is 0: # run it
led.value = True
print("Release the water fowl!") # for debugging in screen or putty

View file

@ -2,9 +2,10 @@
# for Adafruit Circuit Playground express
# with CircuitPython
from adafruit_circuitplayground.express import cpx
import time
from adafruit_circuitplayground.express import cpx
# Change this number to adjust touch sensitivity threshold, 0 is default
cpx.adjust_touch_threshold(600)
@ -40,11 +41,12 @@ step_pixel = [9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
# step colors
step_col = [WHITE, RED, YELLOW, GREEN, AQUA, BLUE, PURPLE, BLACK]
def prog_mode(i):
cpx.play_file(audio_files[i])
step_note[step] = i
def prog_mode(index):
cpx.play_file(audio_files[index])
step_note[step] = index
cpx.pixels[step_pixel[step]] = step_col[step_note[step]]
print("playing file " + audio_files[i])
print("playing file " + audio_files[index])
while True:

View file

@ -2,9 +2,10 @@
# uses two 16 NeoPixel rings (RGBW)
# connected to Gemma M0 powered by LiPo battery
import time
import board
import neopixel
import time
pixpinLeft = board.D1 # Data In attached to Gemma pin D1
pixpinRight = board.D0 # Data In attached to Gemma pin D0
@ -15,6 +16,8 @@ stripLeft = neopixel.NeoPixel(pixpinLeft, numpix, bpp=3, brightness=.18,
auto_write=False)
stripRight = neopixel.NeoPixel(pixpinRight, numpix, bpp=3, brightness=.18,
auto_write=False)
# Use these lines for RGBW NeoPixels
# stripLeft = neopixel.NeoPixel(pixpinLeft, numpix, bpp=4, brightness=.18,
# auto_write=False)
@ -22,18 +25,17 @@ stripRight = neopixel.NeoPixel(pixpinRight, numpix, bpp=3, brightness=.18,
# auto_write=False)
def cog(pos):
# Input a value 0 to 255 to get a color value.
# Note: switch the commented lines below if using RGB vs. RGBW NeoPixles
if (pos < 8) or (pos > 250):
# return (120, 0, 0, 0) #first color, red: for RGBW NeoPixels
return (120, 0, 0) # first color, red: for RGB NeoPixels
if (pos < 85):
if pos < 85:
return (int(pos * 3), int(255 - (pos * 3)), 0)
# return (125, 35, 0, 0) #second color, brass: for RGBW NeoPixels
return (125, 35, 0) #second color, brass: for RGB NeoPixels
elif (pos < 170):
# return (125, 35, 0) # second color, brass: for RGB NeoPixels
elif pos < 170:
pos -= 85
# return (int(255 - pos*3), 0, int(pos*3), 0)#: for RGBW NeoPixels
return (int(255 - pos * 3), 0, int(pos * 3)) # : for RGB NeoPixels
@ -42,6 +44,7 @@ def cog(pos):
# return (0, int(pos*3), int(255 - pos*3), 0)#: for RGBW NeoPixels
return (0, int(pos * 3), int(255 - pos * 3)) # : for RGB NeoPixels
def brass_cycle(wait, patternL, patternR):
# patterns do different things, try:
# 1 blink
@ -61,6 +64,7 @@ def brass_cycle(wait, patternL, patternR):
stripRight.show()
time.sleep(wait)
while True:
brass_cycle(0.01, 256, 24) # brass color cycle with 1ms delay per step
# patternL, patternR

View file

@ -1,13 +1,13 @@
# Gemma M0 Password Vault
# press cap touch pads to enter strong passwords over USB
from digitalio import DigitalInOut, Direction
import touchio
import board
import time
import board
import touchio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from digitalio import DigitalInOut, Direction
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

View file

@ -2,11 +2,12 @@
# for fine tuning Software Defined Radio CubicSDR software
# 10k pot hooked to 3v, A2, and D2 acting as GND
from analogio import AnalogIn
import board
import time
import board
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction
d2_ground = DigitalInOut(board.D2)
@ -22,12 +23,15 @@ pot_min = 0.00
step = (pot_max - pot_min) / 10.0
last_knob = 0
def steps(x):
return round((x - pot_min) / step)
def getVoltage(pin):
return (pin.value * 3.3) / 65536
def spamKey(code):
knobkeys = [Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET,
Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET,
@ -35,16 +39,18 @@ def spamKey(code):
Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET,
Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET,
Keycode.LEFT_BRACKET]
spamRate = [0.01, 0.05, 0.125, 0.25, 0.5, 0.5, 0.5, 0.25, 0.125, 0.05, 0.01]
spamRate = [0.01, 0.05, 0.125, 0.25, 0.5,
0.5, 0.5, 0.25, 0.125, 0.05, 0.01]
kbd = Keyboard()
kbd.press(knobkeys[code]) # which keycode is entered
kbd.release_all()
time.sleep(spamRate[code]) # how fast the key is spammed
while True:
knob = (getVoltage(analog2in))
if steps(knob) is 5: # the center position is active
if steps(knob) == 5: # the center position is active
led.value = True
elif steps(knob) is not 5:
elif steps(knob) != 5:
led.value = False
spamKey(steps(knob))

View file

@ -1,19 +1,20 @@
# Motion Sensor Alarm
# uses Gemma M0, vibration sensor on A0/GND, & piezo on D0/GND
import time
import pulseio
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn
import board
import time
piezo = pulseio.PWMOut(board.D0, duty_cycle=0, frequency=440,
variable_frequency=True)
vibrationPin = AnalogIn(board.A0)
def get_voltage(pin):
return (pin.value * 3.3) / 65536
while True:
print((get_voltage(vibrationPin),))
vibration = get_voltage(vibrationPin)

View file

@ -1,6 +1,8 @@
import time
import board
import neopixel
import time
try:
import urandom as random # for v1.0 API support
except ImportError:
@ -35,7 +37,8 @@ while True: # Loop forever...
strip.write() # Refresh LED states
time.sleep(0.08) # 80 millisecond delay
offset += 1 # Shift animation by 1 pixel on next frame
if offset >= 8: offset = 0
if offset >= 8:
offset = 0
# Additional animation modes could be added here!
t = time.monotonic() # Current time in seconds

View file

@ -10,9 +10,9 @@
# * https://learn.adafruit.com/gemma-led-sneakers
import board
import neopixel
import time
import digitalio
import neopixel
try:
import urandom as random
except ImportError:
@ -78,25 +78,26 @@ gammas = [
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 ]
90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110,
112, 114, 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133,
135, 137, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158,
160, 162, 164, 167, 169, 171, 173, 175, 177, 180, 182, 184, 186,
189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213, 215, 218,
220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252,
255
]
def h2rgb(hue):
# return value
ret = 0
hue %= 90
h = hue_table[hue >> 1]
def h2rgb(colour_hue):
colour_hue %= 90
h = hue_table[colour_hue >> 1]
if ( hue & 1 ):
if colour_hue & 1:
ret = h & 15
else:
ret = (h >> 4)
return ( ret * 17 )
return ret * 17
def wave_setup():
@ -107,22 +108,26 @@ def wave_setup():
[0, 7, 30, 0, 0, 0, 0, 0]]
# assign random starting colors to waves
for w in range(n_waves):
wave[w][hue] = wave[w][hue_target] = 90 + random.randint(0,90)
wave[w][red] = h2rgb(wave[w][hue] - 30)
wave[w][green] = h2rgb(wave[w][hue])
wave[w][blue] = h2rgb(wave[w][hue] + 30)
for wave_index in range(n_waves):
current_wave = wave[wave_index]
random_offset = random.randint(0, 90)
current_wave[hue] = current_wave[hue_target] = 90 + random_offset
current_wave[red] = h2rgb(current_wave[hue] - 30)
current_wave[green] = h2rgb(current_wave[hue])
current_wave[blue] = h2rgb(current_wave[hue] + 30)
def vibration_detector():
while ( True ):
while True:
if not pin.value:
return(True)
return True
while ( True ) :
while True:
# wait for vibration sensor to trigger
if ( ramping_up == False ):
if not ramping_up:
ramping_up = vibration_detector()
wave_setup()
@ -135,7 +140,7 @@ while ( True ) :
brightness = int(((brightness * 7) + 207) / 8)
count += 1
if ( count == ( circumference + num_leds + 5) ):
if count == (circumference + num_leds + 5):
ramping_up = False
count = 0
@ -145,23 +150,24 @@ while ( True ) :
wave[w][center] += wave[w][speed]
# Hue not currently changing?
if ( wave[w][hue] == wave[w][hue_target] ):
if wave[w][hue] == wave[w][hue_target]:
# There's a tiny random chance of picking a new hue...
if ( not random.randint(frames_per_second * 4, 255) ):
if not random.randint(frames_per_second * 4, 255):
# Within 1/3 color wheel
wave[w][hue_target] = random.randint(wave[w][hue] - 30, wave[w][hue] + 30)
wave[w][hue_target] = random.randint(
wave[w][hue] - 30, wave[w][hue] + 30)
# This wave's hue is currently shifting...
else:
if ( wave[w][hue] < wave[w][hue_target] ):
if wave[w][hue] < wave[w][hue_target]:
wave[w][hue] += 1 # Move up or
else:
wave[w][hue] -= 1 # down as needed
# Reached destination?
if ( wave[w][hue] == wave[w][hue_target] ):
if wave[w][hue] == wave[w][hue_target]:
wave[w][hue] = 90 + wave[w][hue] % 90 # Clamp to 90-180 range
wave[w][hue_target] = wave[w][hue] # Copy to target
@ -185,19 +191,19 @@ while ( True ) :
r = g = b = 0
# For each item in wave[] array...
for w in range(n_waves):
for w_index in range(n_waves):
# Calculate distance from pixel center to wave
# center point, using both signed and unsigned
# 8-bit integers...
d1 = int( abs(x - wave[w][center]) )
d2 = int( abs(x - wave[w][center]) )
d1 = int(abs(x - wave[w_index][center]))
d2 = int(abs(x - wave[w_index][center]))
# Then take the lesser of the two, resulting in
# a distance (0-128)
# that 'wraps around' the ends of the strip as
# necessary...it's a contiguous ring, and waves
# can move smoothly across the gap.
if ( d2 < d1 ):
if d2 < d1:
d1 = d2 # d1 is pixel-to-wave-center distance
# d2 distance, relative to wave width, is then
@ -205,37 +211,38 @@ while ( True ) :
# pixel (basic linear y=mx+b stuff).
# Is distance within wave's influence?
# d2 is opposite; distance to wave's end
if ( d1 < wave[w][width] ):
d2 = wave[w][width] - d1
y = int ( brightness * d2 / wave[w][width] ) # 0 to 200
if d1 < wave[w_index][width]:
d2 = wave[w_index][width] - d1
y = int(brightness * d2 / wave[w_index][width]) # 0 to 200
# y is a brightness scale value --
# proportional to, but not exactly equal
# to, the resulting RGB value.
if ( y < 128 ): # Fade black to RGB color
if y < 128: # Fade black to RGB color
# In HSV colorspace, this would be
# tweaking 'value'
n = int(y * 2 + 1) # 1-256
r += ( wave[w][red] * n ) >> 8 # More fixed-point math
g += ( wave[w][green] * n ) >> 8 # Wave color is scaled by 'n'
b += ( wave[w][blue] * n ) >> 8 # >>8 is equiv to /256
r += (wave[w_index][red] * n) >> 8 # More fixed-point math
# Wave color is scaled by 'n'
g += (wave[w_index][green] * n) >> 8
b += (wave[w_index][blue] * n) >> 8 # >>8 is equiv to /256
else: # Fade RGB color to white
# In HSV colorspace, this would be tweaking 'saturation'
# In HSV colorspace, this tweaks 'saturation'
n = int((y - 128) * 2) # 0-255 affects white level
m = 256 * n
n = 256 - n # 1-256 affects RGB level
r += (m + wave[w][red] * n) >> 8
g += (m + wave[w][green] * n) >> 8
b += (m + wave[w][blue] * n) >> 8
r += (m + wave[w_index][red] * n) >> 8
g += (m + wave[w_index][green] * n) >> 8
b += (m + wave[w_index][blue] * n) >> 8
# r,g,b are 16-bit types that accumulate brightness
# from all waves that affect this pixel; may exceed
# 255. Now clip to 0-255 range:
if ( r > 255 ):
if r > 255:
r = 255
if ( g > 255 ):
if g > 255:
g = 255
if ( b > 255 ):
if b > 255:
b = 255
# Store resulting RGB value and we're done with

View file

@ -1,9 +1,11 @@
# NeoPixel earrings example. Makes a nice blinky display with just a
# few LEDs on at any time...uses MUCH less juice than rainbow display!
import time
import board
import neopixel
import time
try:
import urandom as random # for v1.0 API support
except ImportError:
@ -13,20 +15,22 @@ numpix = 16 # Number of NeoPixels (e.g. 16-pixel ring)
pixpin = board.D0 # Pin where NeoPixels are connected
strip = neopixel.NeoPixel(pixpin, numpix, brightness=.3)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255):
return [0, 0, 0]
elif (pos < 85):
elif pos < 85:
return [int(pos * 3), int(255 - (pos * 3)), 0]
elif (pos < 170):
elif pos < 170:
pos -= 85
return [int(255 - pos * 3), 0, int(pos * 3)]
else:
pos -= 170
return [0, int(pos * 3), int(255 - pos * 3)]
mode = 0 # Current animation effect
offset = 0 # Position of spinner animation
hue = 0 # Starting hue
@ -53,7 +57,8 @@ while True: # Loop forever...
strip.write() # Refresh LED states
time.sleep(0.04) # 40 millisecond delay
offset += 1 # Shift animation by 1 pixel on next frame
if offset >= 8: offset = 0
if offset >= 8:
offset = 0
# Additional animation modes could be added here!
t = time.monotonic() # Current time in seconds

View file

@ -1,8 +1,9 @@
"""Interactive light show using built-in LED and capacitive touch"""
import time
import adafruit_dotstar
import touchio
import board
import touchio
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
touch_A0 = touchio.TouchIn(board.A0)
@ -33,10 +34,10 @@ def cycle_sequence(seq):
def rainbow_cycle(seq):
"""Rainbow cycle generator"""
rainbow = cycle_sequence(seq)
rainbow_sequence = cycle_sequence(seq)
while True:
# pylint: disable=stop-iteration-return
led[0] = (wheel(next(rainbow)))
led[0] = (wheel(next(rainbow_sequence)))
yield
@ -59,7 +60,6 @@ color_sequences = cycle_sequence(
]
)
cycle_speeds = cycle_sequence([0.1, 0.3, 0.5])
brightness = brightness_cycle()

View file

@ -1,8 +1,9 @@
"""Touch each pad to change red, green, and blue values on the LED"""
import time
import touchio
import adafruit_dotstar
import board
import touchio
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
touch_A0 = touchio.TouchIn(board.A0)

View file

@ -6,13 +6,14 @@
# Author: Collin Cunningham
# License: MIT License (https://opensource.org/licenses/MIT)
from digitalio import DigitalInOut, Direction, Pull
import board
import time
import board
import neopixel
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from digitalio import DigitalInOut, Direction, Pull
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.2)
pixels.fill((0, 0, 0))
@ -56,18 +57,21 @@ statusled.direction = Direction.OUTPUT
print("Waiting for button presses")
def pressbutton(i):
l = leds[i] # find the switch LED
k = buttonkeys[i] # get the corresp. keycode/str
l.value = True # turn on LED
def pressbutton(index):
switch_led = leds[index] # find the switch LED
k = buttonkeys[index] # get the corresp. keycode/str
switch_led.value = True # turn on LED
kbd.press(k) # send keycode
def releasebutton(i):
l = leds[i] # find the switch LED
k = buttonkeys[i] # get the corresp. keycode/str
l.value = False # turn on LED
def releasebutton(index):
switch_led = leds[index] # find the switch LED
k = buttonkeys[index] # get the corresp. keycode/str
switch_led.value = False # turn on LED
kbd.release(k) # send keycode
def lightneopixels():
vals = [0, 0, 0]
# if switch 0 pressed, show blue
@ -94,18 +98,18 @@ while True:
# check each button
for button in buttons:
i = buttons.index(button)
if button.value == False: # button is pressed?
if button.value is False: # button is pressed?
buttonspressed[i] = True # save pressed button
if buttonspressedlast[i] == False: # was button not pressed last time?
# was button not pressed last time?
if buttonspressedlast[i] is False:
print("Pressed #%d" % i)
pressbutton(i)
else:
buttonspressed[i] = False # button was not pressed
if buttonspressedlast[i] == True: # was button pressed last time?
if buttonspressedlast[i] is True: # was button pressed last time?
print("Released #%d" % i)
releasebutton(i)
lightneopixels()
# save pressed buttons as pressed last
buttonspressedlast = list(buttonspressed)
time.sleep(0.01)

View file

@ -1,4 +1,5 @@
import time
from adafruit_circuitplayground.express import cpx
while True:

View file

@ -1,4 +1,5 @@
import time
from adafruit_circuitplayground.express import cpx
blink_speed = 0.5

View file

@ -1,4 +1,5 @@
import time
from adafruit_circuitplayground.express import cpx
while True:

View file

@ -1,14 +1,24 @@
import time
from adafruit_circuitplayground.express import cpx
# pylint: disable=redefined-outer-name
def upright(x, y, z):
return abs(x) < accel_threshold and abs(y) < accel_threshold and abs(9.8 - z) < accel_threshold
x_up = abs(x) < accel_threshold
y_up = abs(y) < accel_threshold
z_up = abs(9.8 - z) < accel_threshold
return x_up and y_up and z_up
def left_side(x, y, z):
return abs(9.8 - x) < accel_threshold and abs(y) < accel_threshold and abs(z) < accel_threshold
x_side = abs(9.8 - x) < accel_threshold
y_side = abs(y) < accel_threshold
z_side = abs(z) < accel_threshold
return x_side and y_side and z_side
state = None

View file

@ -1,5 +1,8 @@
import time
from adafruit_circuitplayground.express import cpx
# pylint: disable=stop-iteration-return

View file

@ -1,4 +1,5 @@
import time
from adafruit_circuitplayground.express import cpx
@ -18,15 +19,23 @@ def wheel(pos):
# pylint: disable=redefined-outer-name
def upright(x, y, z):
return abs(x) < accel_threshold and abs(y) < accel_threshold and abs(9.8 - z) < accel_threshold
return abs(x) < accel_threshold \
and abs(y) < accel_threshold \
and abs(9.8 - z) < accel_threshold
def right_side(x, y, z):
return abs(-9.8 - x) < accel_threshold and abs(y) < accel_threshold and abs(z) < accel_threshold
return abs(-9.8 - x) < accel_threshold \
and abs(y) < accel_threshold \
and abs(z) < accel_threshold
def left_side(x, y, z):
return abs(9.8 - x) < accel_threshold and abs(y) < accel_threshold and abs(z) < accel_threshold
return abs(9.8 - x) < accel_threshold \
and abs(y) < accel_threshold \
and abs(z) < accel_threshold
# pylint: enable=redefined-outer-name

View file

@ -1,7 +1,8 @@
from digitalio import DigitalInOut, Direction, Pull
import board
import time
import board
from digitalio import DigitalInOut, Direction, Pull
button = DigitalInOut(board.D1)
button.direction = Direction.INPUT
button.pull = Pull.UP

View file

@ -3,15 +3,18 @@
# connected to GND, 3.3V, and pin A1
# and prints the results to the REPL
from analogio import AnalogIn
import board
import time
import board
from analogio import AnalogIn
analogin = AnalogIn(board.A1)
def getVoltage(pin): # helper
return (pin.value * 3.3) / 65536
while True:
print("Analog Voltage: %f" % getVoltage(analogin))
time.sleep(0.1)

View file

@ -1,6 +1,6 @@
from digitalio import DigitalInOut, Direction, Pull
import audioio
import board
from digitalio import DigitalInOut, Direction, Pull
# enable the speaker
spkrenable = DigitalInOut(board.SPEAKER_ENABLE)
@ -19,6 +19,7 @@ buttonB.pull = Pull.DOWN
# The two files assigned to buttons A & B
audiofiles = ["rimshot.wav", "laugh.wav"]
def play_file(filename):
print("playing file " + filename)
f = open(filename, "rb")
@ -28,6 +29,7 @@ def play_file(filename):
pass
print("finished")
while True:
if buttonA.value:
play_file(audiofiles[0])

View file

@ -1,9 +1,10 @@
from digitalio import DigitalInOut, Direction
import array
import math
import time
import audioio
import board
import array
import time
import math
from digitalio import DigitalInOut, Direction
FREQUENCY = 440 # 440 Hz middle 'A'
SAMPLERATE = 8000 # 8000 samples/second, recommended!

View file

@ -1,9 +1,10 @@
# CircuitPlaygroundExpress_Blinky
import digitalio
import board
import time
import board
import digitalio
led = digitalio.DigitalInOut(board.D13) # defines the variable 'led'
led.direction = digitalio.Direction.OUTPUT # set the pin as output

View file

@ -1,6 +1,7 @@
from adafruit_circuitplayground.express import cpx
import time
from adafruit_circuitplayground.express import cpx
while True:
cpx.red_led = True
time.sleep(0.5)

View file

@ -1,9 +1,10 @@
# CircuitPlaygroundExpress_CapTouch
import touchio
import board
import time
import board
import touchio
touch1 = touchio.TouchIn(board.A1)
touch2 = touchio.TouchIn(board.A2)
touch3 = touchio.TouchIn(board.A3)
@ -12,7 +13,6 @@ touch5 = touchio.TouchIn(board.A5)
touch6 = touchio.TouchIn(board.A6)
touch7 = touchio.TouchIn(board.A7)
while True:
if touch1.value:
print("A1 touched!")

View file

@ -1,9 +1,10 @@
# CircuitPlaygroundExpress_DigitalIO
from digitalio import DigitalInOut, Direction, Pull
import board
import time
import board
from digitalio import DigitalInOut, Direction, Pull
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
@ -12,7 +13,7 @@ button.direction = Direction.INPUT
button.pull = Pull.DOWN
while True:
if button.value == True: # button is pushed
if button.value: # button is pushed
led.value = True
else:
led.value = False

Some files were not shown because too many files have changed in this diff Show more