Compare commits
7 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d16c916429 | ||
|
|
d0d481ed7e | ||
|
|
5f494e00db | ||
|
|
d675c99134 | ||
|
|
178406039b | ||
|
|
965fdd30c3 | ||
|
|
f8789a496f |
3 changed files with 55 additions and 9 deletions
|
|
@ -51,6 +51,10 @@ import adafruit_lis3dh
|
|||
import adafruit_thermistor
|
||||
import analogio
|
||||
import audioio
|
||||
try:
|
||||
import audiocore
|
||||
except ImportError:
|
||||
audiocore = audioio
|
||||
import board
|
||||
import busio
|
||||
import digitalio
|
||||
|
|
@ -575,7 +579,7 @@ class Express: # pylint: disable=too-many-public-methods
|
|||
self._sine_wave = array.array("H", Express._sine_sample(length))
|
||||
if sys.implementation.version[0] >= 3:
|
||||
self._sample = audioio.AudioOut(board.SPEAKER)
|
||||
self._sine_wave_sample = audioio.RawSample(self._sine_wave)
|
||||
self._sine_wave_sample = audiocore.RawSample(self._sine_wave)
|
||||
else:
|
||||
raise NotImplementedError("Please use CircuitPython 3.0 or higher.")
|
||||
|
||||
|
|
@ -683,7 +687,7 @@ class Express: # pylint: disable=too-many-public-methods
|
|||
self._speaker_enable.value = True
|
||||
if sys.implementation.version[0] >= 3:
|
||||
with audioio.AudioOut(board.SPEAKER) as audio:
|
||||
wavefile = audioio.WaveFile(open(file_name, "rb"))
|
||||
wavefile = audiocore.WaveFile(open(file_name, "rb"))
|
||||
audio.play(wavefile)
|
||||
while audio.playing:
|
||||
pass
|
||||
|
|
|
|||
48
examples/circuitplayground_acceleration_mapping_neopixels.py
Normal file
48
examples/circuitplayground_acceleration_mapping_neopixels.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
"""Maps acceleration (tilt) to Neopixel colors.
|
||||
|
||||
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
|
||||
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
|
||||
along the x axis. Similarly, when changing the pitch from level, we see blue change
|
||||
to green.
|
||||
|
||||
This video walks you through the code: https://youtu.be/eNpPLbYx-iA
|
||||
"""
|
||||
|
||||
import time
|
||||
from adafruit_circuitplayground.express import cpx
|
||||
|
||||
cpx.pixels.brightness = 0.2 # Adjust overall brightness as desired, between 0 and 1
|
||||
|
||||
|
||||
def color_amount(accel_component):
|
||||
"""Convert acceleration component (x, y, or z) to color amount (r, g, or b)"""
|
||||
standard_gravity = 9.81 # Acceleration (m/s²) due to gravity at the earth’s surface
|
||||
accel_magnitude = abs(accel_component) # Ignore the direction
|
||||
constrained_accel = min(accel_magnitude, standard_gravity) # Constrain values
|
||||
normalized_accel = constrained_accel / standard_gravity # Convert to 0–1
|
||||
return round(normalized_accel * 255) # Convert to 0–255
|
||||
|
||||
|
||||
def format_acceleration():
|
||||
return ', '.join(('{:>6.2f}'.format(axis_value) for axis_value in acceleration))
|
||||
|
||||
|
||||
def format_rgb():
|
||||
return ', '.join(('{:>3d}'.format(rgb_amount) for rgb_amount in rgb_amounts))
|
||||
|
||||
|
||||
def log_values():
|
||||
print('({}) ==> ({})'.format(format_acceleration(), format_rgb()))
|
||||
|
||||
|
||||
while True:
|
||||
acceleration = cpx.acceleration
|
||||
rgb_amounts = [color_amount(axis_value) for axis_value in acceleration]
|
||||
cpx.pixels.fill(rgb_amounts)
|
||||
log_values()
|
||||
time.sleep(0.1)
|
||||
|
|
@ -18,10 +18,4 @@ while True:
|
|||
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))
|
||||
cpx.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z)))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue