Skip to content

Adding examples and content. #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 124 additions & 12 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,142 @@ Simple test

Ensure your device works with this simple test.

.. literalinclude:: ../examples/circuitplayground_acceleration_simpletest.py
:caption: examples/circuitplayground_acceleration_simpletest.py
.. literalinclude:: ../examples/circuitplayground_acceleration.py
:caption: examples/circuitplayground_acceleration.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_pixels_simpletest.py
:caption: examples/circuitplayground_pixels_simpletest.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_shake_simpletest.py
:caption: examples/circuitplayground_shake_simpletest.py
.. literalinclude:: ../examples/circuitplayground_shake.py
:caption: examples/circuitplayground_shake.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_tapdetect_simpletest.py
:caption: examples/circuitplayground_tapdetect_simpletest.py
.. literalinclude:: ../examples/circuitplayground_tapdetect_single_double.py
:caption: examples/circuitplayground_tapdetect_single_double.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_tapdetectsimple_simpletest.py
:caption: examples/circuitplayground_tapdetectsimple_simpletest.py
.. literalinclude:: ../examples/circuitplayground_tapdetect.py
:caption: examples/circuitplayground_tapdetect.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_tone_simpletest.py
:caption: examples/circuitplayground_tone_simpletest.py
.. literalinclude:: ../examples/circuitplayground_tone.py
:caption: examples/circuitplayground_tone.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_touched_simpletest.py
:caption: examples/circuitplayground_touched_simpletest.py
.. literalinclude:: ../examples/circuitplayground_touched.py
:caption: examples/circuitplayground_touched.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_acceleration_neopixels.py
:caption: examples/circuitplayground_acceleration_neopixels.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_button_a.py
:caption: examples/circuitplayground_button_a.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_button_b.py
:caption: examples/circuitplayground_button_b.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_buttons_1_neopixel.py
:caption: examples/circuitplayground_buttons_1_neopixel.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_buttons_neopixels.py
:caption: examples/circuitplayground_buttons_neopixels.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_ir_receive.py
:caption: examples/circuitplayground_ir_receive.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_ir_transmit.py
:caption: examples/circuitplayground_ir_transmit.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_light_neopixels.py
:caption: examples/circuitplayground_light_neopixels.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_light.py
:caption: examples/circuitplayground_light.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_neopixel_0_1.py
:caption: examples/circuitplayground_neopixel_0_1.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_light_plotter.py
:caption: examples/circuitplayground_light_plotter.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_play_file_buttons.py
:caption: examples/circuitplayground_play_file_buttons.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_play_file.py
:caption: examples/circuitplayground_play_file.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_play_tone_buttons.py
:caption: examples/circuitplayground_play_tone_buttons.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_play_tone.py
:caption: examples/circuitplayground_play_tone.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_red_led_blinky.py
:caption: examples/circuitplayground_red_led_blinky.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_red_led_blnky_short.py
:caption: examples/circuitplayground_red_led_blnky_short.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_red_led.py
:caption: examples/circuitplayground_red_led.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_slide_switch_red_led.py
:caption: examples/circuitplayground_slide_switch_red_led.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_slide_switch_red_led_short.py
:caption: examples/circuitplayground_slide_switch_red_led_short.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_slide_switch.py
:caption: examples/circuitplayground_slide_switch.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_sound_meter.py
:caption: examples/circuitplayground_sound_meter.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_tap_red_led.py
:caption: examples/circuitplayground_tap_red_led.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_temperature_neopixels.py
:caption: examples/circuitplayground_temperature_neopixels.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_temperture_plotter.py
:caption: examples/circuitplayground_temperture_plotter.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_temperture.py
:caption: examples/circuitplayground_temperture.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_touch_pixel_fill_rainbow.py
:caption: examples/circuitplayground_touch_pixel_fill_rainbow.py
:linenos:

.. literalinclude:: ../examples/circuitplayground_touch_pixel_rainbow.py
:caption: examples/circuitplayground_touch_pixel_rainbow.py
:linenos:
27 changes: 27 additions & 0 deletions examples/circuitplayground_acceleration_neopixels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""If the switch is to the right, it will appear that nothing is happening. Move the switch to the
left to see the NeoPixels light up in colors related to the accelerometer! The CPX has an
accelerometer in the center that returns (x, y, z) acceleration values. This program uses those
values to light up the NeoPixels based on those acceleration values."""
from adafruit_circuitplayground.express import cpx

# Main loop gets x, y and z axis acceleration, prints the values, and turns on
# red, green and blue, at levels related to the x, y and z values.
while True:
if not cpx.switch:
# If the switch is to the right, it returns False!
print("Slide switch off!")
cpx.pixels.fill((0, 0, 0))
continue
else:
R = 0
G = 0
B = 0
x, y, z = cpx.acceleration
print((x, y, z))
if x:
R = R + abs(int(x))
if y:
G = G + abs(int(y))
if z:
B = B + abs(int(z))
cpx.pixels.fill((R, G, B))
7 changes: 7 additions & 0 deletions examples/circuitplayground_button_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""This example turns on the little red LED when button A is pressed."""
from adafruit_circuitplayground.express import cpx

while True:
if cpx.button_a:
print("Button A pressed!")
cpx.red_led = True
14 changes: 14 additions & 0 deletions examples/circuitplayground_button_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""This example turns the little red LED on only while button B is currently being pressed."""
from adafruit_circuitplayground.express import cpx

# This code is written to be readable versus being Pylint compliant.
# pylint: disable=simplifiable-if-statement

while True:
if cpx.button_b:
cpx.red_led = True
else:
cpx.red_led = False

# Can also be written as:
# cpx.red_led = cpx.button_b
17 changes: 17 additions & 0 deletions examples/circuitplayground_buttons_1_neopixel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""This example lights up the third NeoPixel while button A is being pressed, and lights up the
eighth NeoPixel while button B is being pressed."""
from adafruit_circuitplayground.express import cpx

cpx.pixels.brightness = 0.3
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!

while True:
if cpx.button_a:
cpx.pixels[2] = (0, 255, 0)
else:
cpx.pixels[2] = (0, 0, 0)

if cpx.button_b:
cpx.pixels[7] = (0, 0, 255)
else:
cpx.pixels[7] = (0, 0, 0)
17 changes: 17 additions & 0 deletions examples/circuitplayground_buttons_neopixels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""This example lights up half the NeoPixels red while button A is being pressed, and half the
NeoPixels green while button B is being pressed."""
from adafruit_circuitplayground.express import cpx

cpx.pixels.brightness = 0.3
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!

while True:
if cpx.button_a:
cpx.pixels[0:5] = [(255, 0, 0)] * 5
else:
cpx.pixels[0:5] = [(0, 0, 0)] * 5

if cpx.button_b:
cpx.pixels[5:10] = [(0, 255, 0)] * 5
else:
cpx.pixels[5:10] = [(0, 0, 0)] * 5
36 changes: 36 additions & 0 deletions examples/circuitplayground_ir_receive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
This example requires the adafruit_irremote.mpy library.

This example uses the IR receiver found near the center of the board. Works with another CPX
running the cpx_ir_transmit.py example. The NeoPixels will light up when the buttons on the
TRANSMITTING CPX are pressed!"""
import pulseio
import board
import adafruit_irremote
from adafruit_circuitplayground.express import cpx

# Create a 'pulseio' input, to listen to infrared signals on the IR receiver
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
# Create a decoder that will take pulses and turn them into numbers
decoder = adafruit_irremote.GenericDecode()

while True:
cpx.red_led = True
pulses = decoder.read_pulses(pulsein)
try:
# Attempt to convert received pulses into numbers
received_code = decoder.decode_bits(pulses, debug=False)
except adafruit_irremote.IRNECRepeatException:
# We got an unusual short code, probably a 'repeat' signal
continue
except adafruit_irremote.IRDecodeException:
# Something got distorted
continue

print("Infrared code received: ", received_code)
if received_code == [66, 84, 78, 65]:
print("Button A signal")
cpx.pixels.fill((100, 0, 155))
if received_code == [66, 84, 78, 64]:
print("Button B Signal")
cpx.pixels.fill((210, 45, 0))
33 changes: 33 additions & 0 deletions examples/circuitplayground_ir_transmit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
This example requires the adafruit_irremote.mpy library.

This example uses the IR transmitter found near the center of the board. Works with another CPX
running the cpx_ir_receive.py example. Press the buttons to light up the NeoPixels on the RECEIVING
CPX!"""
import time
import pulseio
import board
import adafruit_irremote
from adafruit_circuitplayground.express import cpx

# Create a 'pulseio' output, to send infrared signals from the IR transmitter
pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15)
pulseout = pulseio.PulseOut(pwm)
# Create an encoder that will take numbers and turn them into NEC IR pulses
encoder = adafruit_irremote.GenericTransmit(header=[9500, 4500], one=[550, 550],
zero=[550, 1700], trail=0)

while True:
if cpx.button_a:
print("Button A pressed! \n")
cpx.red_led = True
encoder.transmit(pulseout, [66, 84, 78, 65])
cpx.red_led = False
# wait so the receiver can get the full message
time.sleep(0.2)
if cpx.button_b:
print("Button B pressed! \n")
cpx.red_led = True
encoder.transmit(pulseout, [66, 84, 78, 64])
cpx.red_led = False
time.sleep(0.2)
9 changes: 9 additions & 0 deletions examples/circuitplayground_light.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""This example uses the light sensor on your CPX, located next to the picture of the eye. Try
shining a flashlight on your CPX, or covering the light sensor with your finger to see the values
increase and decrease."""
import time
from adafruit_circuitplayground.express import cpx

while True:
print("Light:", cpx.light)
time.sleep(1)
26 changes: 26 additions & 0 deletions examples/circuitplayground_light_neopixels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
This example requires the simpleio.mpy library.

This example uses the light sensor on the CPX, located net to the picture of the eye on the board.
Once you have the library loaded, try shining a flashlight on your CPX to watch the number of
NeoPixels lit up increase, or try covering up the light sensor to watch the number decrease."""
import time
from adafruit_circuitplayground.express import cpx
import simpleio

cpx.pixels.auto_write = False
cpx.pixels.brightness = 0.3

while True:
# light value remapped to pixel position
peak = simpleio.map_range(cpx.light, 0, 320, 0, 10)
print(cpx.light)
print(int(peak))

for i in range(0, 10, 1):
if i <= peak:
cpx.pixels[i] = (0, 255, 255)
else:
cpx.pixels[i] = (0, 0, 0)
cpx.pixels.show()
time.sleep(0.05)
10 changes: 10 additions & 0 deletions examples/circuitplayground_light_plotter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""If you're using Mu, this example will plot the light levels from the light sensor (located next
to the eye) on your CPX. Try shining a flashlight on your CPX, or covering the light sensor to see
the plot increase and decrease."""
import time
from adafruit_circuitplayground.express import cpx

while True:
print("Light:", cpx.light)
print((cpx.light,))
time.sleep(0.1)
8 changes: 8 additions & 0 deletions examples/circuitplayground_neopixel_0_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""This example lights up the first and second NeoPixel, red and blue respectively."""
from adafruit_circuitplayground.express import cpx

cpx.pixels.brightness = 0.3

while True:
cpx.pixels[0] = (255, 0, 0)
cpx.pixels[1] = (0, 0, 255)
10 changes: 10 additions & 0 deletions examples/circuitplayground_play_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""THIS EXAMPLE REQUIRES A WAV FILE FROM THE examples FOLDER IN THE
Adafruit_CircuitPython_CircuitPlayground REPO found at:
https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground/tree/master/examples

Copy the "dip.wav" file to your CIRCUITPY drive.

Once the file is copied, this example plays a wav file!"""
from adafruit_circuitplayground.express import cpx

cpx.play_file("dip.wav")
14 changes: 14 additions & 0 deletions examples/circuitplayground_play_file_buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""THIS EXAMPLE REQUIRES A WAV FILE FROM THE examples FOLDER IN THE
Adafruit_CircuitPython_CircuitPlayground REPO found at:
https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground/tree/master/examples

Copy the "dip.wav" and "rise.wav" files to your CIRCUITPY drive.

Once the files are copied, this example plays a different wav file for each button pressed!"""
from adafruit_circuitplayground.express import cpx

while True:
if cpx.button_a:
cpx.play_file("dip.wav")
if cpx.button_b:
cpx.play_file("rise.wav")
Loading