Skip to content

Update to import options and examples #79

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 6 commits into from
Dec 17, 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
29 changes: 29 additions & 0 deletions adafruit_circuitplayground/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# The MIT License (MIT)
#
# Copyright (c) 2019 Kattni Rembor for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""Verifies which board is being used and imports the appropriate module."""

import sys
if sys.platform == 'nRF52840':
from .bluefruit import cpb as cp
elif sys.platform == 'Atmel SAMD21':
from .express import cpx as cp
5 changes: 4 additions & 1 deletion adafruit_circuitplayground/circuit_playground_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
import math
import array
import time
import audiocore
try:
import audiocore
except ImportError:
import audioio as audiocore
import adafruit_lis3dh
import adafruit_thermistor
import analogio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
x, y, and z acceleration components map to red, green and blue,
respectively.

When the CPX is level, the lights are blue because there is no acceleration
When the Circuit Playground is level, the lights are blue because there is no acceleration
on x or y, but on z, gravity pulls at 9.81 meters per second per second (m/s²).
When banking, the vertical (z) axis is no longer directly aligned with gravity,
so the blue decreases, and red increases because gravity is now pulling more
Expand All @@ -14,9 +14,9 @@
"""

import time
from adafruit_circuitplayground.express import cpx
from adafruit_circuitplayground import cp

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


def color_amount(accel_component):
Expand All @@ -41,8 +41,8 @@ def log_values():


while True:
acceleration = cpx.acceleration
acceleration = cp.acceleration
rgb_amounts = [color_amount(axis_value) for axis_value in acceleration]
cpx.pixels.fill(rgb_amounts)
cp.pixels.fill(rgb_amounts)
log_values()
time.sleep(0.1)
24 changes: 12 additions & 12 deletions examples/advanced_examples/circuitplayground_gravity_pulls_pixel.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Gravity Pulls Pixel

This program uses the Circuit Playground Express’s accelerometer to position
This program uses the Circuit Playground's accelerometer to position
a white pixel as if gravity were pulling it.

Flip the switch left (toward the notes) to turn on debugging messages and
slow down the action. See a code walkthrough here: https://youtu.be/sZ4tNOUKRpw
"""
import time
import math
from adafruit_circuitplayground.express import cpx
from adafruit_circuitplayground import cp

PIXEL_SPACING_ANGLE = 30
STANDARD_GRAVITY = 9.81
Expand All @@ -21,12 +21,12 @@
def compute_pixel_angles():
"""Return a list of rotation angles of the ten NeoPixels.

On the CPX there are ten pixels arranged like the 12 hours of a clock, except that positions
6 and 12 are empty. The numbers in the list are the angles from the (x, y) accelerometer
values for each pixel when the CPX is rotated with that pixel at the bottom. For example,
with pixel 0 at the bottom (and pixel 5 at the top), the accelerometer’s (x, y) values
give an angle of 300°. Rotated clockwise 1/12 turn (30°), so that pixel 1 is at the bottom,
the angle is 330°.
On the Circuit Playground there are ten pixels arranged like the 12 hours of a clock, except
that positions 6 and 12 are empty. The numbers in the list are the angles from the (x, y)
accelerometer values for each pixel when the Circuit Playground is rotated with that pixel at
the bottom. For example, with pixel 0 at the bottom (and pixel 5 at the top), the
accelerometer’s (x, y) values give an angle of 300°. Rotated clockwise 1/12 turn (30°), so
that pixel 1 is at the bottom, the angle is 330°.
"""
return [(300 + PIXEL_SPACING_ANGLE * n) % 360 for n in range(12) if n not in (5, 11)]

Expand Down Expand Up @@ -60,12 +60,12 @@ def positive_degrees(angle):
return (angle + 360) % 360


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

while True:
debug = cpx.switch # True is toward the left
accel_x, accel_y = cpx.acceleration[:2] # Ignore z
debug = cp.switch # True is toward the left
accel_x, accel_y = cp.acceleration[:2] # Ignore z
down_angle = positive_degrees(angle_in_degrees(accel_x, accel_y))
magnitude_limit = STANDARD_GRAVITY
normalized_magnitude = min(math.sqrt(accel_x * accel_x + accel_y * accel_y),
Expand All @@ -74,7 +74,7 @@ def positive_degrees(angle):
pixels_lit = []
for i, pixel_position in enumerate(pixel_positions):
pe = pixel_brightness(degrees_between(pixel_position, down_angle), normalized_magnitude)
cpx.pixels[i] = (pe, pe, pe) if pe else BACKGROUND_COLOR
cp.pixels[i] = (pe, pe, pe) if pe else BACKGROUND_COLOR
if pe:
pixels_lit.append((i, pe))

Expand Down
23 changes: 12 additions & 11 deletions examples/advanced_examples/circuitplayground_tilting_arpeggios.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
Buttons A and B advance forward and backward through the circle. The switch selects
the type of arpeggio, either dominant seventh or blues.

You can ignore the FrequencyProvider class if you’re just interested in the CPX interface.
You can ignore the FrequencyProvider class if you’re just interested in the Circuit Playground
interface.

See a code walkthrough here: https://www.youtube.com/watch?v=cDhqyT3ZN0g
"""

# pylint: disable=R0903
import time
from adafruit_circuitplayground.express import cpx
from adafruit_circuitplayground import cp

HS_OCT = 12 # Half-steps per octave
HS_4TH = 5 # Half-steps in a fourth
Expand Down Expand Up @@ -65,11 +66,11 @@ def freq(self, normalized_position, selected_arpeg):
class ButtonDetector:
def __init__(self):
self.next_press_allowed_at = time.monotonic()
self.buttons_on = (cpx.button_a, cpx.button_b)
self.buttons_on = (cp.button_a, cp.button_b)

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

class TiltingArpeggios:
def __init__(self):
cpx.pixels.brightness = 0.2
cp.pixels.brightness = 0.2
self.freq_maker = FrequencyMaker()
TiltingArpeggios.update_pixel(self.freq_maker.circle_pos)
self.button = ButtonDetector()
Expand All @@ -97,18 +98,18 @@ def run(self):
@staticmethod
def update_pixel(circle_pos):
"""Manage the display on the NeoPixels of the current circle position"""
cpx.pixels.fill((0, 0, 0))
cp.pixels.fill((0, 0, 0))
# Light the pixels clockwise from “1 o’clock” with the USB connector on the bottom
pixel_index = (4 - circle_pos) % 10
# Use a different color after all ten LEDs used
color = (0, 255, 0) if circle_pos <= 9 else (255, 255, 0)
cpx.pixels[pixel_index] = color
cp.pixels[pixel_index] = color

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

def process_button_presses(self):
Expand All @@ -120,12 +121,12 @@ def process_button_presses(self):

def change_tone_if_needed(self):
"""Find the frequency for the current arpeggio and tilt, and restart the tone if changed"""
arpeggio_index = 0 if cpx.switch else 1
arpeggio_index = 0 if cp.switch else 1
freq = self.freq_maker.freq(TiltingArpeggios.tilt(), arpeggio_index)
if freq != self.last_freq:
self.last_freq = freq
cpx.stop_tone()
cpx.start_tone(freq)
cp.stop_tone()
cp.start_tone(freq)


TiltingArpeggios().run()
6 changes: 4 additions & 2 deletions examples/circuitplayground_acceleration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""This example uses the accelerometer on the Circuit Playground. It prints the values. Try moving
the board to see the values change."""
import time
from adafruit_circuitplayground.express import cpx
from adafruit_circuitplayground import cp

while True:
x, y, z = cpx.acceleration
x, y, z = cp.acceleration
print(x, y, z)

time.sleep(0.1)
16 changes: 8 additions & 8 deletions examples/circuitplayground_acceleration_neopixels.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
"""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
left to see the NeoPixels light up in colors related to the accelerometer! The Circuit Playground
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 import cp

# 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 not cp.switch:
# If the switch is to the right, it returns False!
print("Slide switch off!")
cpx.pixels.fill((0, 0, 0))
cp.pixels.fill((0, 0, 0))
continue
else:
R = 0
G = 0
B = 0
x, y, z = cpx.acceleration
x, y, z = cp.acceleration
print((x, y, z))
cpx.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z)))))
cp.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z)))))
6 changes: 3 additions & 3 deletions examples/circuitplayground_button_a.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This example turns on the little red LED when button A is pressed."""
from adafruit_circuitplayground.express import cpx
from adafruit_circuitplayground import cp

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

# 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
if cp.button_b:
cp.red_led = True
else:
cpx.red_led = False
cp.red_led = False

# Can also be written as:
# cpx.red_led = cpx.button_b
# cp.red_led = cp.button_b
20 changes: 10 additions & 10 deletions examples/circuitplayground_buttons_1_neopixel.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""This example lights up the third NeoPixel while button A is being pressed, and lights up the
"""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
from adafruit_circuitplayground import cp

cpx.pixels.brightness = 0.3
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
cp.pixels.brightness = 0.3
cp.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)
if cp.button_a:
cp.pixels[2] = (0, 255, 0)
else:
cpx.pixels[2] = (0, 0, 0)
cp.pixels[2] = (0, 0, 0)

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

cpx.pixels.brightness = 0.3
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
cp.pixels.brightness = 0.3
cp.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
if cp.button_a:
cp.pixels[0:5] = [(255, 0, 0)] * 5
else:
cpx.pixels[0:5] = [(0, 0, 0)] * 5
cp.pixels[0:5] = [(0, 0, 0)] * 5

if cpx.button_b:
cpx.pixels[5:10] = [(0, 255, 0)] * 5
if cp.button_b:
cp.pixels[5:10] = [(0, 255, 0)] * 5
else:
cpx.pixels[5:10] = [(0, 0, 0)] * 5
cp.pixels[5:10] = [(0, 0, 0)] * 5
23 changes: 14 additions & 9 deletions examples/circuitplayground_ir_receive.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
"""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!"""
THIS EXAMPLE WORKS WITH CIRCUIT PLAYGROUND EXPRESS ONLY.

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

# Create a 'pulseio' input, to listen to infrared signals on the IR receiver
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
try:
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
except AttributeError:
raise NotImplementedError("This example does not work with Circuit Playground Bluefruti!")
# Create a decoder that will take pulses and turn them into numbers
decoder = adafruit_irremote.GenericDecode()

while True:
cpx.red_led = True
cp.red_led = True
pulses = decoder.read_pulses(pulsein)
try:
# Attempt to convert received pulses into numbers
received_code = decoder.decode_bits(pulses, debug=False)
received_code = decoder.decode_bits(pulses)
except adafruit_irremote.IRNECRepeatException:
# We got an unusual short code, probably a 'repeat' signal
continue
Expand All @@ -30,7 +35,7 @@
print("Infrared code received: ", received_code)
if received_code == [66, 84, 78, 65]:
print("Button A signal")
cpx.pixels.fill((100, 0, 155))
cp.pixels.fill((100, 0, 155))
if received_code == [66, 84, 78, 64]:
print("Button B Signal")
cpx.pixels.fill((210, 45, 0))
cp.pixels.fill((210, 45, 0))
Loading