Skip to content

Commit d853952

Browse files
authored
Merge pull request #1 from kattni/cpx-code
Presentation code
2 parents b1b592c + 8a475b7 commit d853952

17 files changed

+242
-0
lines changed

cpx_accel_neopixels.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from adafruit_circuitplayground.express import cpx
2+
import time
3+
4+
# Main loop gets x, y and z axis acceleration, prints the values, and turns on
5+
# red, green and blue, at levels related to the x, y and z values.
6+
while True:
7+
if cpx.switch:
8+
print("Slide switch off!")
9+
cpx.pixels.fill((0, 0, 0))
10+
continue
11+
else:
12+
R = 0
13+
G = 0
14+
B = 0
15+
x, y, z = cpx.acceleration
16+
print(x, y, z)
17+
if x:
18+
R = R + abs(int(x))
19+
if y:
20+
G = G + abs(int(y))
21+
if z:
22+
B = B + abs(int(z))
23+
cpx.pixels.fill((R, G, B))
24+
time.sleep(0.2)

cpx_accelerometer.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import time
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
x, y, z = cpx.acceleration
6+
print(x, y, z)
7+
time.sleep(0.5)

cpx_blinky.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import time
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
cpx.red_led = True
6+
time.sleep(0.5)
7+
cpx.red_led = False
8+
time.sleep(0.5)

cpx_button_a.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from adafruit_circuitplayground.express import cpx
2+
3+
while True:
4+
if cpx.button_a:
5+
cpx.red_led = True

cpx_button_b.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from adafruit_circuitplayground.express import cpx
2+
3+
while True:
4+
if cpx.button_b:
5+
cpx.red_led = True
6+
else:
7+
cpx.red_led = False

cpx_light_sensor.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import time
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
print("Lux:", cpx.light)
6+
time.sleep(1)

cpx_neopixel_1.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from adafruit_circuitplayground.express import cpx
2+
3+
cpx.pixels.brightness = 0.3
4+
5+
while True:
6+
cpx.pixels[1] = (0, 255, 0)

cpx_neopixels_fill.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from adafruit_circuitplayground.express import cpx
2+
3+
while True:
4+
cpx.pixels.fill((50, 0, 0))

cpx_play_tone.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from adafruit_circuitplayground.express import cpx
2+
3+
while True:
4+
cpx.play_tone(262, 1)

cpx_shake.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from adafruit_circuitplayground.express import cpx
2+
3+
while True:
4+
if cpx.shake():
5+
print("Shake detected!")

cpx_sound_meter.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import array
2+
import audiobusio
3+
import board
4+
import math
5+
import neopixel
6+
7+
CURVE = 2
8+
SCALE_EXPONENT = math.pow(10, CURVE * -0.1)
9+
10+
PEAK_COLOR = (80, 0, 255)
11+
NUM_PIXELS = 10
12+
NUM_SAMPLES = 160
13+
14+
15+
def constrain(value, floor, ceiling):
16+
return max(floor, min(value, ceiling))
17+
18+
19+
def log_scale(input_value, input_min, input_max, output_min, output_max):
20+
normalized_input_value = (input_value - input_min) / (input_max - input_min)
21+
return output_min + math.pow(normalized_input_value, SCALE_EXPONENT) * (output_max - output_min)
22+
23+
24+
def normalized_rms(values):
25+
minbuf = int(mean(values))
26+
return math.sqrt(sum(float(sample - minbuf) * (sample - minbuf) for sample in values) / len(values))
27+
28+
29+
def mean(values):
30+
return sum(values) / len(values)
31+
32+
33+
def volume_color(i):
34+
return i * (255 // NUM_PIXELS), 50, 0
35+
36+
37+
pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1, auto_write=False)
38+
pixels.fill(0)
39+
pixels.show()
40+
41+
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, frequency=16000, bit_depth=16)
42+
samples = array.array('H', [0] * NUM_SAMPLES)
43+
mic.record(samples, len(samples))
44+
input_floor = normalized_rms(samples) + 10
45+
46+
# Lower number means more sensitive - more LEDs will light up with less sound.
47+
sensitivity = 500
48+
input_ceiling = input_floor + sensitivity
49+
50+
peak = 0
51+
while True:
52+
mic.record(samples, len(samples))
53+
magnitude = normalized_rms(samples)
54+
print(magnitude)
55+
56+
c = log_scale(constrain(magnitude, input_floor, input_ceiling),
57+
input_floor, input_ceiling, 0, NUM_PIXELS)
58+
59+
pixels.fill(0)
60+
for i in range(NUM_PIXELS):
61+
if i < c:
62+
pixels[i] = volume_color(i)
63+
if c >= peak:
64+
peak = min(c, NUM_PIXELS - 1)
65+
elif peak > 0:
66+
peak = peak - 1
67+
if peak > 0:
68+
pixels[int(peak)] = PEAK_COLOR
69+
pixels.show()

cpx_start_stop_tone.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from adafruit_circuitplayground.express import cpx
2+
3+
while True:
4+
if cpx.button_a:
5+
cpx.start_tone(262)
6+
elif cpx.button_b:
7+
cpx.start_tone(294)
8+
else:
9+
cpx.stop_tone()

cpx_temperture_sensor.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import time
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
print("Temperature C:", cpx.temperature)
6+
time.sleep(1)

cpx_touch_a1.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import time
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
if cpx.touch_A1:
6+
print("Touched A1!")
7+
time.sleep(0.1)

cpx_touch_all.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import time
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
if cpx.touch_A1:
6+
print("Touched A1!")
7+
if cpx.touch_A2:
8+
print("Touched A2!")
9+
if cpx.touch_A3:
10+
print("Touched A3!")
11+
if cpx.touch_A4:
12+
print("Touched A4!")
13+
if cpx.touch_A5:
14+
print("Touched A5!")
15+
if cpx.touch_A6:
16+
print("Touched A6!")
17+
if cpx.touch_A7:
18+
print("Touched A7!")
19+
time.sleep(0.1)

cpx_touch_fill_rainbow.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
from adafruit_circuitplayground.express import cpx
3+
4+
cpx.pixels.brightness = 0.3
5+
6+
while True:
7+
if cpx.touch_A1:
8+
print("Touched A1!")
9+
cpx.pixels.fill((255, 0, 0))
10+
if cpx.touch_A2:
11+
print("Touched A2!")
12+
cpx.pixels.fill((210, 45, 0))
13+
if cpx.touch_A3:
14+
print("Touched A3!")
15+
cpx.pixels.fill((155, 100, 0))
16+
if cpx.touch_A4:
17+
print("Touched A4!")
18+
cpx.pixels.fill((0, 255, 0))
19+
if cpx.touch_A5:
20+
print("Touched A5!")
21+
cpx.pixels.fill((0, 135, 125))
22+
if cpx.touch_A6:
23+
print("Touched A6!")
24+
cpx.pixels.fill((0, 0, 255))
25+
if cpx.touch_A7:
26+
print("Touched A7!")
27+
cpx.pixels.fill((100, 0, 155))
28+
time.sleep(0.1)

cpx_touch_pixel_rainbow.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
from adafruit_circuitplayground.express import cpx
3+
4+
cpx.pixels.brightness = 0.3
5+
6+
while True:
7+
if cpx.touch_A1:
8+
print("Touched A1!")
9+
cpx.pixels[6] = (255, 0, 0)
10+
if cpx.touch_A2:
11+
print("Touched A2!")
12+
cpx.pixels[8] = (210, 45, 0)
13+
if cpx.touch_A3:
14+
print("Touched A3!")
15+
cpx.pixels[9] = (155, 100, 0)
16+
if cpx.touch_A4:
17+
print("Touched A4!")
18+
cpx.pixels[0] = (0, 255, 0)
19+
if cpx.touch_A5:
20+
print("Touched A5!")
21+
cpx.pixels[1] = (0, 135, 125)
22+
if cpx.touch_A6:
23+
print("Touched A6!")
24+
cpx.pixels[3] = (0, 0, 255)
25+
if cpx.touch_A7:
26+
print("Touched A7!")
27+
cpx.pixels[4] = (100, 0, 155)
28+
time.sleep(0.1)

0 commit comments

Comments
 (0)