Skip to content

Commit 620b22c

Browse files
committed
Adding examples and content.
1 parent 0cc2e97 commit 620b22c

36 files changed

+470
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""This example lights up the first and second NeoPixel, red and blue respectively."""
2+
from adafruit_circuitplayground.express import cpx
3+
4+
cpx.pixels.brightness = 0.3
5+
6+
while True:
7+
cpx.pixels[0] = (255, 0, 0)
8+
cpx.pixels[1] = (0, 0, 255)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""If the switch is to the right, it will appear that nothing is happening. Move the switch to the
2+
left to see the NeoPixels light up in colors related to the accelerometer! The CPX has an
3+
accelerometer in the center that returns (x, y, z) acceleration values. This program uses those
4+
values to light up the NeoPixels based on those acceleration values."""
5+
from adafruit_circuitplayground.express import cpx
6+
7+
# Main loop gets x, y and z axis acceleration, prints the values, and turns on
8+
# red, green and blue, at levels related to the x, y and z values.
9+
while True:
10+
if not cpx.switch:
11+
# If the switch is to the right, it returns False!
12+
print("Slide switch off!")
13+
cpx.pixels.fill((0, 0, 0))
14+
continue
15+
else:
16+
R = 0
17+
G = 0
18+
B = 0
19+
x, y, z = cpx.acceleration
20+
print((x, y, z))
21+
if x:
22+
R = R + abs(int(x))
23+
if y:
24+
G = G + abs(int(y))
25+
if z:
26+
B = B + abs(int(z))
27+
cpx.pixels.fill((R, G, B))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""This example turns on the little red LED when button A is pressed."""
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
if cpx.button_a:
6+
print("Button A pressed!")
7+
cpx.red_led = True
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""This example turns the little red LED on only while button B is currently being pressed."""
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
if cpx.button_b:
6+
cpx.red_led = True
7+
else:
8+
cpx.red_led = False
9+
10+
# Can also be written as:
11+
# cpx.red_led = cpx.button_b
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""This example lights up the third NeoPixel while button A is being pressed, and lights up the
2+
eighth NeoPixel while button B is being pressed."""
3+
from adafruit_circuitplayground.express import cpx
4+
5+
cpx.pixels.brightness = 0.3
6+
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
7+
8+
while True:
9+
if cpx.button_a:
10+
cpx.pixels[2] = (0, 255, 0)
11+
else:
12+
cpx.pixels[2] = (0, 0, 0)
13+
14+
if cpx.button_b:
15+
cpx.pixels[7] = (0, 0, 255)
16+
else:
17+
cpx.pixels[7] = (0, 0, 0)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""This example lights up half the NeoPixels red while button A is being pressed, and half the
2+
NeoPixels green while button B is being pressed."""
3+
from adafruit_circuitplayground.express import cpx
4+
5+
cpx.pixels.brightness = 0.3
6+
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
7+
8+
while True:
9+
if cpx.button_a:
10+
cpx.pixels[0:5] = [(255, 0, 0)] * 5
11+
else:
12+
cpx.pixels[0:5] = [(0, 0, 0)] * 5
13+
14+
if cpx.button_b:
15+
cpx.pixels[5:10] = [(0, 255, 0)] * 5
16+
else:
17+
cpx.pixels[5:10] = [(0, 0, 0)] * 5
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
2+
This example requires the adafruit_irremote.mpy library.
3+
4+
This example uses the IR receiver found near the center of the board. Works with another CPX
5+
running the cpx_ir_transmit.py example. The NeoPixels will light up when the buttons on the
6+
TRANSMITTING CPX are pressed!"""
7+
import pulseio
8+
import board
9+
import adafruit_irremote
10+
from adafruit_circuitplayground.express import cpx
11+
12+
# Create a 'pulseio' input, to listen to infrared signals on the IR receiver
13+
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
14+
# Create a decoder that will take pulses and turn them into numbers
15+
decoder = adafruit_irremote.GenericDecode()
16+
17+
while True:
18+
cpx.red_led = True
19+
pulses = decoder.read_pulses(pulsein)
20+
try:
21+
# Attempt to convert received pulses into numbers
22+
received_code = decoder.decode_bits(pulses, debug=False)
23+
except adafruit_irremote.IRNECRepeatException:
24+
# We got an unusual short code, probably a 'repeat' signal
25+
continue
26+
except adafruit_irremote.IRDecodeException:
27+
# Something got distorted
28+
continue
29+
30+
print("Infrared code received: ", received_code)
31+
if received_code == [66, 84, 78, 65]:
32+
print("Button A signal")
33+
cpx.pixels.fill((100, 0, 155))
34+
if received_code == [66, 84, 78, 64]:
35+
print("Button B Signal")
36+
cpx.pixels.fill((210, 45, 0))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
2+
This example requires the adafruit_irremote.mpy library.
3+
4+
This example uses the IR transmitter found near the center of the board. Works with another CPX
5+
running the cpx_ir_receive.py example. Press the buttons to light up the NeoPixels on the RECEIVING
6+
CPX!"""
7+
import time
8+
import pulseio
9+
import board
10+
import adafruit_irremote
11+
from adafruit_circuitplayground.express import cpx
12+
13+
# Create a 'pulseio' output, to send infrared signals from the IR transmitter
14+
pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15)
15+
pulseout = pulseio.PulseOut(pwm)
16+
# Create an encoder that will take numbers and turn them into NEC IR pulses
17+
encoder = adafruit_irremote.GenericTransmit(header=[9500, 4500], one=[550, 550],
18+
zero=[550, 1700], trail=0)
19+
20+
while True:
21+
if cpx.button_a:
22+
print("Button A pressed! \n")
23+
cpx.red_led = True
24+
encoder.transmit(pulseout, [66, 84, 78, 65])
25+
cpx.red_led = False
26+
# wait so the receiver can get the full message
27+
time.sleep(0.2)
28+
if cpx.button_b:
29+
print("Button B pressed! \n")
30+
cpx.red_led = True
31+
encoder.transmit(pulseout, [66, 84, 78, 64])
32+
cpx.red_led = False
33+
time.sleep(0.2)

examples/circuitplayground_light.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""This example uses the light sensor on your CPX, located next to the picture of the eye. Try
2+
shining a flashlight on your CPX, or covering the light sensor with your finger to see the values
3+
increase and decrease."""
4+
import time
5+
from adafruit_circuitplayground.express import cpx
6+
7+
while True:
8+
print("Light:", cpx.light)
9+
time.sleep(1)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
2+
This example requires the simpleio.mpy library.
3+
4+
This example uses the light sensor on the CPX, located net to the picture of the eye on the board.
5+
Once you have the library loaded, try shining a flashlight on your CPX to watch the number of
6+
NeoPixels lit up increase, or try covering up the light sensor to watch the number decrease."""
7+
import time
8+
from adafruit_circuitplayground.express import cpx
9+
import simpleio
10+
11+
cpx.pixels.auto_write = False
12+
cpx.pixels.brightness = 0.3
13+
14+
while True:
15+
# light value remapped to pixel position
16+
peak = simpleio.map_range(cpx.light, 0, 320, 0, 10)
17+
print(cpx.light)
18+
print(int(peak))
19+
20+
for i in range(0, 10, 1):
21+
if i <= peak:
22+
cpx.pixels[i] = (0, 255, 255)
23+
else:
24+
cpx.pixels[i] = (0, 0, 0)
25+
cpx.pixels.show()
26+
time.sleep(0.05)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""If you're using Mu, this example will plot the light levels from the light sensor (located next
2+
to the eye) on your CPX. Try shining a flashlight on your CPX, or covering the light sensor to see
3+
the plot increase and decrease."""
4+
import time
5+
from adafruit_circuitplayground.express import cpx
6+
7+
while True:
8+
print("Light:", cpx.light)
9+
print((cpx.light,))
10+
time.sleep(0.1)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""THIS EXAMPLE REQUIRES A WAV FILE FROM THE examples FOLDER IN THE
2+
Adafruit_CircuitPython_CircuitPlayground REPO found at:
3+
https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground/tree/master/examples
4+
5+
Copy the "dip.wav" file to your CIRCUITPY drive.
6+
7+
Once the file is copied, this example plays a wav file!"""
8+
from adafruit_circuitplayground.express import cpx
9+
10+
cpx.play_file("dip.wav")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""THIS EXAMPLE REQUIRES A WAV FILE FROM THE examples FOLDER IN THE
2+
Adafruit_CircuitPython_CircuitPlayground REPO found at:
3+
https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground/tree/master/examples
4+
5+
Copy the "dip.wav" and "rise.wav" files to your CIRCUITPY drive.
6+
7+
Once the files are copied, this example plays a different wav file for each button pressed!"""
8+
from adafruit_circuitplayground.express import cpx
9+
10+
while True:
11+
if cpx.button_a:
12+
cpx.play_file("dip.wav")
13+
if cpx.button_b:
14+
cpx.play_file("rise.wav")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""This example plays two tones for 1 second each. Note that the tones are not in a loop - this is
2+
to prevent them from playing indefinitely!"""
3+
from adafruit_circuitplayground.express import cpx
4+
5+
cpx.play_tone(262, 1)
6+
cpx.play_tone(294, 1)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""This example plays a different tone for a duration of 1 second for each button pressed."""
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
if cpx.button_a:
6+
cpx.play_tone(262, 1)
7+
if cpx.button_b:
8+
cpx.play_tone(294, 1)

examples/circuitplayground_red_led.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""This example turns on the little red LED."""
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
cpx.red_led = True
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""This is the "Hello, world!" of CircuitPython: Blinky! This example blinks the little red LED on
2+
and off!"""
3+
import time
4+
from adafruit_circuitplayground.express import cpx
5+
6+
while True:
7+
cpx.red_led = True
8+
time.sleep(0.5)
9+
cpx.red_led = False
10+
time.sleep(0.5)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""This is the "Hello, world!" of CircuitPython: Blinky! This example blinks the little red LED on
2+
and off! It's a shorter version of the other Blinky example."""
3+
import time
4+
from adafruit_circuitplayground.express import cpx
5+
6+
while True:
7+
cpx.red_led = not cpx.red_led
8+
time.sleep(0.5)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""This example prints the status of the slide switch. Try moving the switch back and forth to see
2+
what's printed to the serial console!"""
3+
import time
4+
from adafruit_circuitplayground.express import cpx
5+
6+
while True:
7+
print("Slide switch:", cpx.switch)
8+
time.sleep(0.1)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""This example uses the slide switch to control the little red LED."""
2+
from adafruit_circuitplayground.express import cpx
3+
4+
while True:
5+
if cpx.switch:
6+
cpx.red_led = True
7+
else:
8+
cpx.red_led = False
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""This example uses the slide switch to control the little red LED. When the switch is to the
2+
right it returns False, and when it's to the left, it returns True."""
3+
from adafruit_circuitplayground.express import cpx
4+
5+
while True:
6+
cpx.red_led = cpx.switch
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""This example uses the sound sensor, located next to the picture of the ear on your board, to
2+
light up the NeoPixels as a sound meter. Try talking to your CPX or clapping, etc, to see the
3+
NeoPixels light up!"""
4+
import array
5+
import math
6+
import audiobusio
7+
import board
8+
from adafruit_circuitplayground.express import cpx
9+
10+
11+
def constrain(value, floor, ceiling):
12+
return max(floor, min(value, ceiling))
13+
14+
15+
def log_scale(input_value, input_min, input_max, output_min, output_max):
16+
normalized_input_value = (input_value - input_min) / (input_max - input_min)
17+
return output_min + math.pow(normalized_input_value, 0.630957) * (output_max - output_min)
18+
19+
20+
def normalized_rms(values):
21+
minbuf = int(sum(values) / len(values))
22+
return math.sqrt(sum(float(sample - minbuf) *
23+
(sample - minbuf) for sample in values) / len(values))
24+
25+
26+
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
27+
sample_rate=16000, bit_depth=16)
28+
29+
samples = array.array('H', [0] * 160)
30+
mic.record(samples, len(samples))
31+
input_floor = normalized_rms(samples) + 10
32+
33+
# Lower number means more sensitive - more LEDs will light up with less sound.
34+
sensitivity = 500
35+
input_ceiling = input_floor + sensitivity
36+
37+
peak = 0
38+
while True:
39+
mic.record(samples, len(samples))
40+
magnitude = normalized_rms(samples)
41+
print((magnitude,))
42+
43+
c = log_scale(constrain(magnitude, input_floor, input_ceiling),
44+
input_floor, input_ceiling, 0, 10)
45+
46+
cpx.pixels.fill((0, 0, 0))
47+
for i in range(10):
48+
if i < c:
49+
cpx.pixels[i] = (i * (255 // 10), 50, 0)
50+
if c >= peak:
51+
peak = min(c, 10 - 1)
52+
elif peak > 0:
53+
peak = peak - 1
54+
if peak > 0:
55+
cpx.pixels[int(peak)] = (80, 0, 255)
56+
cpx.pixels.show()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""This example turns on the little red LED and prints to the serial console when you double-tap
2+
the CPX!"""
3+
import time
4+
from adafruit_circuitplayground.express import cpx
5+
6+
# Change to 1 for detecting a single-tap!
7+
cpx.detect_taps = 2
8+
9+
while True:
10+
if cpx.tapped:
11+
print("Tapped!")
12+
cpx.red_led = True
13+
time.sleep(0.1)
14+
else:
15+
cpx.red_led = False

0 commit comments

Comments
 (0)