Skip to content

Commit 82ba9e4

Browse files
authored
Merge pull request #79 from kattni/board-handling
Update to import options and examples
2 parents f042c59 + cfdca44 commit 82ba9e4

File tree

40 files changed

+304
-256
lines changed

40 files changed

+304
-256
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019 Kattni Rembor for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
23+
"""Verifies which board is being used and imports the appropriate module."""
24+
25+
import sys
26+
if sys.platform == 'nRF52840':
27+
from .bluefruit import cpb as cp
28+
elif sys.platform == 'Atmel SAMD21':
29+
from .express import cpx as cp

adafruit_circuitplayground/circuit_playground_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
import math
4040
import array
4141
import time
42-
import audiocore
42+
try:
43+
import audiocore
44+
except ImportError:
45+
import audioio as audiocore
4346
import adafruit_lis3dh
4447
import adafruit_thermistor
4548
import analogio

examples/advanced_examples/circuitplayground_acceleration_mapping_neopixels.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
x, y, and z acceleration components map to red, green and blue,
44
respectively.
55
6-
When the CPX is level, the lights are blue because there is no acceleration
6+
When the Circuit Playground is level, the lights are blue because there is no acceleration
77
on x or y, but on z, gravity pulls at 9.81 meters per second per second (m/s²).
88
When banking, the vertical (z) axis is no longer directly aligned with gravity,
99
so the blue decreases, and red increases because gravity is now pulling more
@@ -14,9 +14,9 @@
1414
"""
1515

1616
import time
17-
from adafruit_circuitplayground.express import cpx
17+
from adafruit_circuitplayground import cp
1818

19-
cpx.pixels.brightness = 0.2 # Adjust overall brightness as desired, between 0 and 1
19+
cp.pixels.brightness = 0.2 # Adjust overall brightness as desired, between 0 and 1
2020

2121

2222
def color_amount(accel_component):
@@ -41,8 +41,8 @@ def log_values():
4141

4242

4343
while True:
44-
acceleration = cpx.acceleration
44+
acceleration = cp.acceleration
4545
rgb_amounts = [color_amount(axis_value) for axis_value in acceleration]
46-
cpx.pixels.fill(rgb_amounts)
46+
cp.pixels.fill(rgb_amounts)
4747
log_values()
4848
time.sleep(0.1)

examples/advanced_examples/circuitplayground_gravity_pulls_pixel.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Gravity Pulls Pixel
22
3-
This program uses the Circuit Playground Express’s accelerometer to position
3+
This program uses the Circuit Playground's accelerometer to position
44
a white pixel as if gravity were pulling it.
55
66
Flip the switch left (toward the notes) to turn on debugging messages and
77
slow down the action. See a code walkthrough here: https://youtu.be/sZ4tNOUKRpw
88
"""
99
import time
1010
import math
11-
from adafruit_circuitplayground.express import cpx
11+
from adafruit_circuitplayground import cp
1212

1313
PIXEL_SPACING_ANGLE = 30
1414
STANDARD_GRAVITY = 9.81
@@ -21,12 +21,12 @@
2121
def compute_pixel_angles():
2222
"""Return a list of rotation angles of the ten NeoPixels.
2323
24-
On the CPX there are ten pixels arranged like the 12 hours of a clock, except that positions
25-
6 and 12 are empty. The numbers in the list are the angles from the (x, y) accelerometer
26-
values for each pixel when the CPX is rotated with that pixel at the bottom. For example,
27-
with pixel 0 at the bottom (and pixel 5 at the top), the accelerometer’s (x, y) values
28-
give an angle of 300°. Rotated clockwise 1/12 turn (30°), so that pixel 1 is at the bottom,
29-
the angle is 330°.
24+
On the Circuit Playground there are ten pixels arranged like the 12 hours of a clock, except
25+
that positions 6 and 12 are empty. The numbers in the list are the angles from the (x, y)
26+
accelerometer values for each pixel when the Circuit Playground is rotated with that pixel at
27+
the bottom. For example, with pixel 0 at the bottom (and pixel 5 at the top), the
28+
accelerometer’s (x, y) values give an angle of 300°. Rotated clockwise 1/12 turn (30°), so
29+
that pixel 1 is at the bottom, the angle is 330°.
3030
"""
3131
return [(300 + PIXEL_SPACING_ANGLE * n) % 360 for n in range(12) if n not in (5, 11)]
3232

@@ -60,12 +60,12 @@ def positive_degrees(angle):
6060
return (angle + 360) % 360
6161

6262

63-
cpx.pixels.brightness = 0.1 # Adjust overall brightness as desired, between 0 and 1
63+
cp.pixels.brightness = 0.1 # Adjust overall brightness as desired, between 0 and 1
6464
pixel_positions = compute_pixel_angles()
6565

6666
while True:
67-
debug = cpx.switch # True is toward the left
68-
accel_x, accel_y = cpx.acceleration[:2] # Ignore z
67+
debug = cp.switch # True is toward the left
68+
accel_x, accel_y = cp.acceleration[:2] # Ignore z
6969
down_angle = positive_degrees(angle_in_degrees(accel_x, accel_y))
7070
magnitude_limit = STANDARD_GRAVITY
7171
normalized_magnitude = min(math.sqrt(accel_x * accel_x + accel_y * accel_y),
@@ -74,7 +74,7 @@ def positive_degrees(angle):
7474
pixels_lit = []
7575
for i, pixel_position in enumerate(pixel_positions):
7676
pe = pixel_brightness(degrees_between(pixel_position, down_angle), normalized_magnitude)
77-
cpx.pixels[i] = (pe, pe, pe) if pe else BACKGROUND_COLOR
77+
cp.pixels[i] = (pe, pe, pe) if pe else BACKGROUND_COLOR
7878
if pe:
7979
pixels_lit.append((i, pe))
8080

examples/advanced_examples/circuitplayground_tilting_arpeggios.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
Buttons A and B advance forward and backward through the circle. The switch selects
55
the type of arpeggio, either dominant seventh or blues.
66
7-
You can ignore the FrequencyProvider class if you’re just interested in the CPX interface.
7+
You can ignore the FrequencyProvider class if you’re just interested in the Circuit Playground
8+
interface.
89
910
See a code walkthrough here: https://www.youtube.com/watch?v=cDhqyT3ZN0g
1011
"""
1112

1213
# pylint: disable=R0903
1314
import time
14-
from adafruit_circuitplayground.express import cpx
15+
from adafruit_circuitplayground import cp
1516

1617
HS_OCT = 12 # Half-steps per octave
1718
HS_4TH = 5 # Half-steps in a fourth
@@ -65,11 +66,11 @@ def freq(self, normalized_position, selected_arpeg):
6566
class ButtonDetector:
6667
def __init__(self):
6768
self.next_press_allowed_at = time.monotonic()
68-
self.buttons_on = (cpx.button_a, cpx.button_b)
69+
self.buttons_on = (cp.button_a, cp.button_b)
6970

7071
def pressed(self, index):
7172
"""Return whether the specified button (0=A, 1=B) was pressed, limiting the repeat rate"""
72-
pressed = cpx.button_b if index else cpx.button_a
73+
pressed = cp.button_b if index else cp.button_a
7374
if pressed:
7475
now = time.monotonic()
7576
if now >= self.next_press_allowed_at:
@@ -80,7 +81,7 @@ def pressed(self, index):
8081

8182
class TiltingArpeggios:
8283
def __init__(self):
83-
cpx.pixels.brightness = 0.2
84+
cp.pixels.brightness = 0.2
8485
self.freq_maker = FrequencyMaker()
8586
TiltingArpeggios.update_pixel(self.freq_maker.circle_pos)
8687
self.button = ButtonDetector()
@@ -97,18 +98,18 @@ def run(self):
9798
@staticmethod
9899
def update_pixel(circle_pos):
99100
"""Manage the display on the NeoPixels of the current circle position"""
100-
cpx.pixels.fill((0, 0, 0))
101+
cp.pixels.fill((0, 0, 0))
101102
# Light the pixels clockwise from “1 o’clock” with the USB connector on the bottom
102103
pixel_index = (4 - circle_pos) % 10
103104
# Use a different color after all ten LEDs used
104105
color = (0, 255, 0) if circle_pos <= 9 else (255, 255, 0)
105-
cpx.pixels[pixel_index] = color
106+
cp.pixels[pixel_index] = color
106107

107108
@staticmethod
108109
def tilt():
109110
"""Normalize the Y-Axis Tilt"""
110111
standard_gravity = 9.81 # Acceleration (m/s²) due to gravity at the earth’s surface
111-
constrained_accel = min(max(0.0, -cpx.acceleration[1]), standard_gravity)
112+
constrained_accel = min(max(0.0, -cp.acceleration[1]), standard_gravity)
112113
return constrained_accel / standard_gravity
113114

114115
def process_button_presses(self):
@@ -120,12 +121,12 @@ def process_button_presses(self):
120121

121122
def change_tone_if_needed(self):
122123
"""Find the frequency for the current arpeggio and tilt, and restart the tone if changed"""
123-
arpeggio_index = 0 if cpx.switch else 1
124+
arpeggio_index = 0 if cp.switch else 1
124125
freq = self.freq_maker.freq(TiltingArpeggios.tilt(), arpeggio_index)
125126
if freq != self.last_freq:
126127
self.last_freq = freq
127-
cpx.stop_tone()
128-
cpx.start_tone(freq)
128+
cp.stop_tone()
129+
cp.start_tone(freq)
129130

130131

131132
TiltingArpeggios().run()
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
"""This example uses the accelerometer on the Circuit Playground. It prints the values. Try moving
2+
the board to see the values change."""
13
import time
2-
from adafruit_circuitplayground.express import cpx
4+
from adafruit_circuitplayground import cp
35

46
while True:
5-
x, y, z = cpx.acceleration
7+
x, y, z = cp.acceleration
68
print(x, y, z)
79

810
time.sleep(0.1)
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
"""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
2+
left to see the NeoPixels light up in colors related to the accelerometer! The Circuit Playground
3+
has an accelerometer in the center that returns (x, y, z) acceleration values. This program uses
4+
those values to light up the NeoPixels based on those acceleration values."""
5+
from adafruit_circuitplayground import cp
66

77
# Main loop gets x, y and z axis acceleration, prints the values, and turns on
88
# red, green and blue, at levels related to the x, y and z values.
99
while True:
10-
if not cpx.switch:
10+
if not cp.switch:
1111
# If the switch is to the right, it returns False!
1212
print("Slide switch off!")
13-
cpx.pixels.fill((0, 0, 0))
13+
cp.pixels.fill((0, 0, 0))
1414
continue
1515
else:
1616
R = 0
1717
G = 0
1818
B = 0
19-
x, y, z = cpx.acceleration
19+
x, y, z = cp.acceleration
2020
print((x, y, z))
21-
cpx.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z)))))
21+
cp.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z)))))
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""This example turns on the little red LED when button A is pressed."""
2-
from adafruit_circuitplayground.express import cpx
2+
from adafruit_circuitplayground import cp
33

44
while True:
5-
if cpx.button_a:
5+
if cp.button_a:
66
print("Button A pressed!")
7-
cpx.red_led = True
7+
cp.red_led = True
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""This example turns the little red LED on only while button B is currently being pressed."""
2-
from adafruit_circuitplayground.express import cpx
2+
from adafruit_circuitplayground import cp
33

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

77
while True:
8-
if cpx.button_b:
9-
cpx.red_led = True
8+
if cp.button_b:
9+
cp.red_led = True
1010
else:
11-
cpx.red_led = False
11+
cp.red_led = False
1212

1313
# Can also be written as:
14-
# cpx.red_led = cpx.button_b
14+
# cp.red_led = cp.button_b
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
"""This example lights up the third NeoPixel while button A is being pressed, and lights up the
1+
"""This example lights up the third NeoPixel while button A is being pressed, and lights up the
22
eighth NeoPixel while button B is being pressed."""
3-
from adafruit_circuitplayground.express import cpx
3+
from adafruit_circuitplayground import cp
44

5-
cpx.pixels.brightness = 0.3
6-
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
5+
cp.pixels.brightness = 0.3
6+
cp.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
77

88
while True:
9-
if cpx.button_a:
10-
cpx.pixels[2] = (0, 255, 0)
9+
if cp.button_a:
10+
cp.pixels[2] = (0, 255, 0)
1111
else:
12-
cpx.pixels[2] = (0, 0, 0)
12+
cp.pixels[2] = (0, 0, 0)
1313

14-
if cpx.button_b:
15-
cpx.pixels[7] = (0, 0, 255)
14+
if cp.button_b:
15+
cp.pixels[7] = (0, 0, 255)
1616
else:
17-
cpx.pixels[7] = (0, 0, 0)
17+
cp.pixels[7] = (0, 0, 0)
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
"""This example lights up half the NeoPixels red while button A is being pressed, and half the
1+
"""This example lights up half the NeoPixels red while button A is being pressed, and half the
22
NeoPixels green while button B is being pressed."""
3-
from adafruit_circuitplayground.express import cpx
3+
from adafruit_circuitplayground import cp
44

5-
cpx.pixels.brightness = 0.3
6-
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
5+
cp.pixels.brightness = 0.3
6+
cp.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
77

88
while True:
9-
if cpx.button_a:
10-
cpx.pixels[0:5] = [(255, 0, 0)] * 5
9+
if cp.button_a:
10+
cp.pixels[0:5] = [(255, 0, 0)] * 5
1111
else:
12-
cpx.pixels[0:5] = [(0, 0, 0)] * 5
12+
cp.pixels[0:5] = [(0, 0, 0)] * 5
1313

14-
if cpx.button_b:
15-
cpx.pixels[5:10] = [(0, 255, 0)] * 5
14+
if cp.button_b:
15+
cp.pixels[5:10] = [(0, 255, 0)] * 5
1616
else:
17-
cpx.pixels[5:10] = [(0, 0, 0)] * 5
17+
cp.pixels[5:10] = [(0, 0, 0)] * 5
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
"""THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
22
This example requires the adafruit_irremote.mpy library.
33
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!"""
4+
THIS EXAMPLE WORKS WITH CIRCUIT PLAYGROUND EXPRESS ONLY.
5+
6+
This example uses the IR receiver found near the center of the board. Works with another Circuit
7+
Playground Express running the circuitplayground_ir_transmit.py example. The NeoPixels will light
8+
up when the buttons on the TRANSMITTING Circuit Playground Express are pressed!"""
79
import pulseio
810
import board
911
import adafruit_irremote
10-
from adafruit_circuitplayground.express import cpx
12+
from adafruit_circuitplayground import cp
1113

1214
# 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)
15+
try:
16+
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
17+
except AttributeError:
18+
raise NotImplementedError("This example does not work with Circuit Playground Bluefruti!")
1419
# Create a decoder that will take pulses and turn them into numbers
1520
decoder = adafruit_irremote.GenericDecode()
1621

1722
while True:
18-
cpx.red_led = True
23+
cp.red_led = True
1924
pulses = decoder.read_pulses(pulsein)
2025
try:
2126
# Attempt to convert received pulses into numbers
22-
received_code = decoder.decode_bits(pulses, debug=False)
27+
received_code = decoder.decode_bits(pulses)
2328
except adafruit_irremote.IRNECRepeatException:
2429
# We got an unusual short code, probably a 'repeat' signal
2530
continue
@@ -30,7 +35,7 @@
3035
print("Infrared code received: ", received_code)
3136
if received_code == [66, 84, 78, 65]:
3237
print("Button A signal")
33-
cpx.pixels.fill((100, 0, 155))
38+
cp.pixels.fill((100, 0, 155))
3439
if received_code == [66, 84, 78, 64]:
3540
print("Button B Signal")
36-
cpx.pixels.fill((210, 45, 0))
41+
cp.pixels.fill((210, 45, 0))

0 commit comments

Comments
 (0)