-
Notifications
You must be signed in to change notification settings - Fork 0
/
itsy-bitsy-circuitpython-demo.py
135 lines (108 loc) · 3.53 KB
/
itsy-bitsy-circuitpython-demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Itsy Bitsy M0 Express IO demo
# Welcome to CircuitPython 2.2 :)
# https://forums.pimoroni.com/t/itsy-bitsy-m0-express-main-py/7530
import board
import gc
import time
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn
import audioio
import touchio
import pulseio
import neopixel
import adafruit_dotstar
from adafruit_motor import servo
gc.collect() # make some rooooom
# HID keyboard support
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
# One pixel connected internally!
dot = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.5)
# Built in red LED
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
# Analog audio output on A0, using two audio files
audiofiles = ["rimshot.wav", "laugh.wav"]
# Analog input on A1
analog1in = AnalogIn(board.A1)
# Capacitive touch on A2
touch = touchio.TouchIn(board.A2)
# Digital input with pullup on D7, D9, and D10
buttons = []
for p in [board.D7, board.D9, board.D10]:
button = DigitalInOut(p)
button.direction = Direction.INPUT
button.pull = Pull.UP
buttons.append(button)
# Servo on D12
servo_pwm = pulseio.PWMOut(board.D12, frequency=50)
servo = servo.Servo(servo_pwm)
# NeoPixel strip (of 16 LEDs) connected on D5
NUMPIXELS = 16
neopixels = neopixel.NeoPixel(board.D5, NUMPIXELS, brightness=0.2, auto_write=False)
# Used if we do HID output, see below
kbd = Keyboard()
######################### HELPERS ##############################
# Helper to convert analog input to voltage
def getVoltage(pin):
return (pin.value * 3.3) / 65536
# Helper to give us a nice color swirl
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):
return [0, 0, 0]
if (pos > 255):
return [0, 0, 0]
if (pos < 85):
return [int(pos * 3), int(255 - (pos*3)), 0]
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 play_file(filename):
print("")
print("----------------------------------")
print("playing file "+filename)
f = open(filename, "rb")
a = audioio.AudioOut(board.A0, f)
a.play()
while a.playing:
pass
print("finished")
print("----------------------------------")
######################### MAIN LOOP ##############################
i = 0
while True:
# spin internal LED around! autoshow is on
dot[0] = wheel(i & 255)
# also make the neopixels swirl around
for p in range(NUMPIXELS):
idx = int ((p * 256 / NUMPIXELS) + i)
neopixels[p] = wheel(idx & 255)
neopixels.show()
# Read analog voltage on A1
print("A1: %0.2f" % getVoltage(analog1in), end="\t")
# use A2 as capacitive touch to turn on internal LED
print("A2 touch: %d" % touch.raw_value, end="\t")
if touch.value:
print("A2 touched!", end ="\t")
led.value = touch.value
if not buttons[0].value:
print("Button D7 pressed!", end ="\t")
# optional! uncomment below & save to have it sent a keypress
#kbd.press(Keycode.A)
#kbd.release_all()
if not buttons[1].value:
print("Button D9 pressed!", end ="\t")
play_file(audiofiles[0])
if not buttons[2].value:
print("Button D10 pressed!", end ="\t")
play_file(audiofiles[1])
# sweep a servo from 0-180 degrees (map from 0-255)
servo.angle = i / 255 * 180
i = (i+1) % 256 # run from 0 to 255
#time.sleep(0.01) # make bigger to slow down
print("")