Skip to content

Commit b900a73

Browse files
committed
Updating import and various in examples.
1 parent cfa0053 commit b900a73

38 files changed

+246
-244
lines changed

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import time
2-
from adafruit_circuitplayground.express import cpx
2+
from adafruit_circuitplayground import cp
33

44
while True:
5-
x, y, z = cpx.acceleration
5+
x, y, z = cp.acceleration
66
print(x, y, z)
77

88
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
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 uses the IR receiver found near the center of the board. Works with another Circuit
5+
Playground running the circuitplayground_ir_transmit.py example. The NeoPixels will light up when
6+
the buttons on the TRANSMITTING Circuit Playground are pressed!"""
77
import pulseio
88
import board
99
import adafruit_irremote
10-
from adafruit_circuitplayground.express import cpx
10+
from adafruit_circuitplayground import cp
1111

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

1717
while True:
18-
cpx.red_led = True
18+
cp.red_led = True
1919
pulses = decoder.read_pulses(pulsein)
2020
try:
2121
# Attempt to convert received pulses into numbers
22-
received_code = decoder.decode_bits(pulses, debug=False)
22+
received_code = decoder.decode_bits(pulses)
2323
except adafruit_irremote.IRNECRepeatException:
2424
# We got an unusual short code, probably a 'repeat' signal
2525
continue
@@ -30,7 +30,7 @@
3030
print("Infrared code received: ", received_code)
3131
if received_code == [66, 84, 78, 65]:
3232
print("Button A signal")
33-
cpx.pixels.fill((100, 0, 155))
33+
cp.pixels.fill((100, 0, 155))
3434
if received_code == [66, 84, 78, 64]:
3535
print("Button B Signal")
36-
cpx.pixels.fill((210, 45, 0))
36+
cp.pixels.fill((210, 45, 0))
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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 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!"""
4+
This example uses the IR transmitter found near the center of the board. Works with another Circuit
5+
Playground running the circuitplayground_ir_receive.py example. Press the buttons to light up the
6+
NeoPixels on the RECEIVING Circuit Playground!"""
77
import time
88
import pulseio
99
import board
1010
import adafruit_irremote
11-
from adafruit_circuitplayground.express import cpx
11+
from adafruit_circuitplayground import cp
1212

1313
# Create a 'pulseio' output, to send infrared signals from the IR transmitter
1414
pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15)
@@ -18,16 +18,16 @@
1818
zero=[550, 1700], trail=0)
1919

2020
while True:
21-
if cpx.button_a:
21+
if cp.button_a:
2222
print("Button A pressed! \n")
23-
cpx.red_led = True
23+
cp.red_led = True
2424
encoder.transmit(pulseout, [66, 84, 78, 65])
25-
cpx.red_led = False
25+
cp.red_led = False
2626
# wait so the receiver can get the full message
2727
time.sleep(0.2)
28-
if cpx.button_b:
28+
if cp.button_b:
2929
print("Button B pressed! \n")
30-
cpx.red_led = True
30+
cp.red_led = True
3131
encoder.transmit(pulseout, [66, 84, 78, 64])
32-
cpx.red_led = False
32+
cp.red_led = False
3333
time.sleep(0.2)

examples/circuitplayground_light.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +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."""
1+
"""This example uses the light sensor on your Circuit Playground, located next to the picture of
2+
the eye. Try shining a flashlight on your Circuit Playground, or covering the light sensor with
3+
your finger to see the values increase and decrease."""
44
import time
5-
from adafruit_circuitplayground.express import cpx
5+
from adafruit_circuitplayground import cp
66

77
while True:
8-
print("Light:", cpx.light)
9-
time.sleep(1)
8+
print("Light:", cp.light)
9+
time.sleep(0.2)

0 commit comments

Comments
 (0)