Skip to content

Commit 154b74d

Browse files
authored
Merge pull request #59 from kattni/add-examples-content
Adding examples and content.
2 parents 0cc2e97 + f47a89b commit 154b74d

37 files changed

+600
-12
lines changed

docs/examples.rst

Lines changed: 124 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,142 @@ Simple test
33

44
Ensure your device works with this simple test.
55

6-
.. literalinclude:: ../examples/circuitplayground_acceleration_simpletest.py
7-
:caption: examples/circuitplayground_acceleration_simpletest.py
6+
.. literalinclude:: ../examples/circuitplayground_acceleration.py
7+
:caption: examples/circuitplayground_acceleration.py
88
:linenos:
99

1010
.. literalinclude:: ../examples/circuitplayground_pixels_simpletest.py
1111
:caption: examples/circuitplayground_pixels_simpletest.py
1212
:linenos:
1313

14-
.. literalinclude:: ../examples/circuitplayground_shake_simpletest.py
15-
:caption: examples/circuitplayground_shake_simpletest.py
14+
.. literalinclude:: ../examples/circuitplayground_shake.py
15+
:caption: examples/circuitplayground_shake.py
1616
:linenos:
1717

18-
.. literalinclude:: ../examples/circuitplayground_tapdetect_simpletest.py
19-
:caption: examples/circuitplayground_tapdetect_simpletest.py
18+
.. literalinclude:: ../examples/circuitplayground_tapdetect_single_double.py
19+
:caption: examples/circuitplayground_tapdetect_single_double.py
2020
:linenos:
2121

22-
.. literalinclude:: ../examples/circuitplayground_tapdetectsimple_simpletest.py
23-
:caption: examples/circuitplayground_tapdetectsimple_simpletest.py
22+
.. literalinclude:: ../examples/circuitplayground_tapdetect.py
23+
:caption: examples/circuitplayground_tapdetect.py
2424
:linenos:
2525

26-
.. literalinclude:: ../examples/circuitplayground_tone_simpletest.py
27-
:caption: examples/circuitplayground_tone_simpletest.py
26+
.. literalinclude:: ../examples/circuitplayground_tone.py
27+
:caption: examples/circuitplayground_tone.py
2828
:linenos:
2929

30-
.. literalinclude:: ../examples/circuitplayground_touched_simpletest.py
31-
:caption: examples/circuitplayground_touched_simpletest.py
30+
.. literalinclude:: ../examples/circuitplayground_touched.py
31+
:caption: examples/circuitplayground_touched.py
32+
:linenos:
33+
34+
.. literalinclude:: ../examples/circuitplayground_acceleration_neopixels.py
35+
:caption: examples/circuitplayground_acceleration_neopixels.py
36+
:linenos:
37+
38+
.. literalinclude:: ../examples/circuitplayground_button_a.py
39+
:caption: examples/circuitplayground_button_a.py
40+
:linenos:
41+
42+
.. literalinclude:: ../examples/circuitplayground_button_b.py
43+
:caption: examples/circuitplayground_button_b.py
44+
:linenos:
45+
46+
.. literalinclude:: ../examples/circuitplayground_buttons_1_neopixel.py
47+
:caption: examples/circuitplayground_buttons_1_neopixel.py
48+
:linenos:
49+
50+
.. literalinclude:: ../examples/circuitplayground_buttons_neopixels.py
51+
:caption: examples/circuitplayground_buttons_neopixels.py
52+
:linenos:
53+
54+
.. literalinclude:: ../examples/circuitplayground_ir_receive.py
55+
:caption: examples/circuitplayground_ir_receive.py
56+
:linenos:
57+
58+
.. literalinclude:: ../examples/circuitplayground_ir_transmit.py
59+
:caption: examples/circuitplayground_ir_transmit.py
60+
:linenos:
61+
62+
.. literalinclude:: ../examples/circuitplayground_light_neopixels.py
63+
:caption: examples/circuitplayground_light_neopixels.py
64+
:linenos:
65+
66+
.. literalinclude:: ../examples/circuitplayground_light.py
67+
:caption: examples/circuitplayground_light.py
68+
:linenos:
69+
70+
.. literalinclude:: ../examples/circuitplayground_neopixel_0_1.py
71+
:caption: examples/circuitplayground_neopixel_0_1.py
72+
:linenos:
73+
74+
.. literalinclude:: ../examples/circuitplayground_light_plotter.py
75+
:caption: examples/circuitplayground_light_plotter.py
76+
:linenos:
77+
78+
.. literalinclude:: ../examples/circuitplayground_play_file_buttons.py
79+
:caption: examples/circuitplayground_play_file_buttons.py
80+
:linenos:
81+
82+
.. literalinclude:: ../examples/circuitplayground_play_file.py
83+
:caption: examples/circuitplayground_play_file.py
84+
:linenos:
85+
86+
.. literalinclude:: ../examples/circuitplayground_play_tone_buttons.py
87+
:caption: examples/circuitplayground_play_tone_buttons.py
88+
:linenos:
89+
90+
.. literalinclude:: ../examples/circuitplayground_play_tone.py
91+
:caption: examples/circuitplayground_play_tone.py
92+
:linenos:
93+
94+
.. literalinclude:: ../examples/circuitplayground_red_led_blinky.py
95+
:caption: examples/circuitplayground_red_led_blinky.py
96+
:linenos:
97+
98+
.. literalinclude:: ../examples/circuitplayground_red_led_blnky_short.py
99+
:caption: examples/circuitplayground_red_led_blnky_short.py
100+
:linenos:
101+
102+
.. literalinclude:: ../examples/circuitplayground_red_led.py
103+
:caption: examples/circuitplayground_red_led.py
104+
:linenos:
105+
106+
.. literalinclude:: ../examples/circuitplayground_slide_switch_red_led.py
107+
:caption: examples/circuitplayground_slide_switch_red_led.py
108+
:linenos:
109+
110+
.. literalinclude:: ../examples/circuitplayground_slide_switch_red_led_short.py
111+
:caption: examples/circuitplayground_slide_switch_red_led_short.py
112+
:linenos:
113+
114+
.. literalinclude:: ../examples/circuitplayground_slide_switch.py
115+
:caption: examples/circuitplayground_slide_switch.py
116+
:linenos:
117+
118+
.. literalinclude:: ../examples/circuitplayground_sound_meter.py
119+
:caption: examples/circuitplayground_sound_meter.py
120+
:linenos:
121+
122+
.. literalinclude:: ../examples/circuitplayground_tap_red_led.py
123+
:caption: examples/circuitplayground_tap_red_led.py
124+
:linenos:
125+
126+
.. literalinclude:: ../examples/circuitplayground_temperature_neopixels.py
127+
:caption: examples/circuitplayground_temperature_neopixels.py
128+
:linenos:
129+
130+
.. literalinclude:: ../examples/circuitplayground_temperture_plotter.py
131+
:caption: examples/circuitplayground_temperture_plotter.py
132+
:linenos:
133+
134+
.. literalinclude:: ../examples/circuitplayground_temperture.py
135+
:caption: examples/circuitplayground_temperture.py
136+
:linenos:
137+
138+
.. literalinclude:: ../examples/circuitplayground_touch_pixel_fill_rainbow.py
139+
:caption: examples/circuitplayground_touch_pixel_fill_rainbow.py
140+
:linenos:
141+
142+
.. literalinclude:: ../examples/circuitplayground_touch_pixel_rainbow.py
143+
:caption: examples/circuitplayground_touch_pixel_rainbow.py
32144
:linenos:
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
# This code is written to be readable versus being Pylint compliant.
5+
# pylint: disable=simplifiable-if-statement
6+
7+
while True:
8+
if cpx.button_b:
9+
cpx.red_led = True
10+
else:
11+
cpx.red_led = False
12+
13+
# Can also be written as:
14+
# 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: 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: 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")

0 commit comments

Comments
 (0)