Skip to content

Commit 821c88b

Browse files
committed
2 parents 63421a0 + f0e4fca commit 821c88b

File tree

35 files changed

+1917
-199
lines changed

35 files changed

+1917
-199
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# CircuitPython NeoPixel Color Picker Example
2+
3+
import board
4+
import neopixel
5+
from adafruit_ble import BLERadio
6+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
7+
from adafruit_ble.services.nordic import UARTService
8+
from adafruit_bluefruit_connect.packet import Packet
9+
from adafruit_bluefruit_connect.color_packet import ColorPacket
10+
11+
ble = BLERadio()
12+
uart_service = UARTService()
13+
advertisement = ProvideServicesAdvertisement(uart_service)
14+
15+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1)
16+
17+
while True:
18+
# Advertise when not connected.
19+
ble.start_advertising(advertisement)
20+
while not ble.connected:
21+
pass
22+
23+
while ble.connected:
24+
packet = Packet.from_stream(uart_service)
25+
if isinstance(packet, ColorPacket):
26+
print(packet.color)
27+
pixels.fill(packet.color)

Bluetooth_Luminaries/code.py

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
""" FancyLED Palette and Color Picker Control with BlueFruit App
2+
Code by Phil Burgess, Dan Halbert & Erin St Blaine for Adafruit Industries
3+
"""
4+
import board
5+
import neopixel
6+
import touchio
7+
import adafruit_fancyled.adafruit_fancyled as fancy
8+
# from adafruit_ble.uart import UARTServer
9+
# for >= CPy 5.0.0
10+
from adafruit_ble.uart_server import UARTServer
11+
from adafruit_bluefruit_connect.packet import Packet
12+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
13+
from adafruit_bluefruit_connect.color_packet import ColorPacket
14+
15+
NUM_LEDS = 24 # change to reflect your total number of ring LEDs
16+
RING_PIN = board.A1 # change to reflect your wiring
17+
CPX_PIN = board.D8 # CPX Neopixels live on pin D8
18+
19+
touch_A2 = touchio.TouchIn(board.A2)
20+
touch_A3 = touchio.TouchIn(board.A3)
21+
touch_A4 = touchio.TouchIn(board.A4)
22+
touch_A5 = touchio.TouchIn(board.A5)
23+
touch_A6 = touchio.TouchIn(board.A6)
24+
touch_TX = touchio.TouchIn(board.TX)
25+
26+
# Palettes can have any number of elements in various formats
27+
# check https://learn.adafruit.com/fancyled-library-for-circuitpython/colors
28+
# for more info
29+
# Declare a 6-element RGB rainbow palette
30+
PALETTE_RAINBOW = [fancy.CRGB(1.0, 0.0, 0.0), # Red
31+
fancy.CRGB(0.5, 0.3, 0.0), # Orange
32+
fancy.CRGB(0.5, 0.5, 0.0), # Yellow
33+
fancy.CRGB(0.3, 0.7, 0.0), # Yellow Green
34+
fancy.CRGB(0.0, 1.0, 0.0), # Green
35+
fancy.CRGB(0.0, 0.7, 0.3), # Teal
36+
fancy.CRGB(0.0, 0.5, 0.5), # Cyan
37+
fancy.CRGB(0.0, 0.3, 0.7), # Blue
38+
fancy.CRGB(0.0, 0.0, 1.0), # Blue
39+
fancy.CRGB(0.5, 0.0, 0.5), # Magenta
40+
fancy.CRGB(0.7, 0.0, 0.3)] # Purple
41+
42+
# Reading Lamp mode - Warm Yellow
43+
PALETTE_BRIGHT = [fancy.CRGB(255, 183, 55)]
44+
45+
# Black Only palette for "off" mode
46+
PALETTE_DARK = [fancy.CRGB(0, 0, 0)]
47+
48+
# Declare a FIRE palette
49+
PALETTE_FIRE = [fancy.CRGB(160, 30, 0), # Reds and Yellows
50+
fancy.CRGB(27, 65, 0),
51+
fancy.CRGB(0, 0, 0),
52+
fancy.CRGB(224, 122, 0),
53+
fancy.CRGB(0, 0, 0),
54+
fancy.CRGB(250, 80, 0),
55+
fancy.CRGB(0, 0, 0),
56+
fancy.CRGB(0, 0, 0),
57+
fancy.CRGB(200, 40, 0)]
58+
59+
# Declare a NeoPixel object on NEOPIXEL_PIN with NUM_LEDS pixels,
60+
# no auto-write.
61+
# Set brightness to max because we'll be using FancyLED's brightness control.
62+
ring = neopixel.NeoPixel(RING_PIN, NUM_LEDS, brightness=1.0, auto_write=False)
63+
cpx = neopixel.NeoPixel(CPX_PIN, NUM_LEDS, brightness=1.0, auto_write=False)
64+
65+
offset = 0 # Positional offset into color palette to get it to 'spin'
66+
offset_increment = 6
67+
OFFSET_MAX = 1000000
68+
69+
uart_server = UARTServer()
70+
71+
def set_palette(palette):
72+
for i in range(NUM_LEDS):
73+
# Load each pixel's color from the palette using an offset, run it
74+
# through the gamma function, pack RGB value and assign to pixel.
75+
color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS)
76+
color = fancy.gamma_adjust(color, brightness=1.0)
77+
ring[i] = color.pack()
78+
ring.show()
79+
80+
for i in range(NUM_LEDS):
81+
# Load each pixel's color from the palette using an offset, run it
82+
# through the gamma function, pack RGB value and assign to pixel.
83+
color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS)
84+
color = fancy.gamma_adjust(color, brightness=1.0)
85+
cpx[i] = color.pack()
86+
cpx.show()
87+
88+
# set initial palette to run on startup
89+
palette_choice = PALETTE_FIRE
90+
91+
# True if cycling a palette
92+
cycling = True
93+
94+
# Are we already advertising?
95+
advertising = False
96+
97+
98+
while True:
99+
100+
if cycling:
101+
set_palette(palette_choice)
102+
offset = (offset + offset_increment) % OFFSET_MAX
103+
104+
if not uart_server.connected and not advertising:
105+
uart_server.start_advertising()
106+
advertising = True
107+
108+
# Are we connected via Bluetooth now?
109+
if uart_server.connected:
110+
# Once we're connected, we're not advertising any more.
111+
advertising = False
112+
# Have we started to receive a packet?
113+
if uart_server.in_waiting:
114+
packet = Packet.from_stream(uart_server)
115+
if isinstance(packet, ColorPacket):
116+
cycling = False
117+
# Set all the pixels to one color and stay there.
118+
ring.fill(packet.color)
119+
cpx.fill(packet.color)
120+
ring.show()
121+
cpx.show()
122+
elif isinstance(packet, ButtonPacket):
123+
cycling = True
124+
if packet.pressed:
125+
if packet.button == ButtonPacket.BUTTON_1:
126+
palette_choice = PALETTE_DARK
127+
elif packet.button == ButtonPacket.BUTTON_2:
128+
palette_choice = PALETTE_BRIGHT
129+
elif packet.button == ButtonPacket.BUTTON_3:
130+
palette_choice = PALETTE_FIRE
131+
offset_increment = 6
132+
elif packet.button == ButtonPacket.BUTTON_4:
133+
palette_choice = PALETTE_RAINBOW
134+
offset_increment = 1
135+
136+
# change the speed of the animation by incrementing offset
137+
elif packet.button == ButtonPacket.UP:
138+
offset_increment += 1
139+
elif packet.button == ButtonPacket.DOWN:
140+
offset_increment -= 1
141+
142+
# Whether or not we're connected via Bluetooth,
143+
# we also want to handle touch inputs.
144+
if touch_A2.value:
145+
cycling = True
146+
palette_choice = PALETTE_DARK
147+
elif touch_A3.value:
148+
cycling = True
149+
palette_choice = PALETTE_BRIGHT
150+
elif touch_A4.value:
151+
cycling = True
152+
palette_choice = PALETTE_FIRE
153+
offset_increment = 6
154+
elif touch_A5.value:
155+
cycling = True
156+
palette_choice = PALETTE_RAINBOW
157+
offset_increment = 1
158+
# Also check for touch speed control
159+
# if touch_A6.value:
160+
# offset_increment += 1
161+
# if touch_TX.value:
162+
# offset_increment -= 1

CPX_Walking_Stick/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## The Musical Walking Stick tutorial on the Adafruit Learning System
2+
3+
The code in this repo accompanies the Adafruit tutorial
4+
Musical Walking Stick
5+
https://learn.adafruit.com/musical-cane/
6+
7+
The *appropriate code file file should be copied onto the **CIRCUITPY** flash drive as **code.py** that appears
8+
when you plug the CircuitPlayground Express into your computer via a known good USB cable.
9+
10+
If the only drive name you get is named **CPLAYBOOT**, CircuitPython may not be loaded
11+
on the board. You can load CircuitPython [per this guide](https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart). Then you should be able to see the **CIRCUITPY** drive when connected via USB.
12+
13+
CircuitPython resources may be found at https://CircuitPython.Org/
14+
15+
Learning Guide by Dano Wall
16+
Code written by Dano Wall and Anne Barela for Adafruit Industries, November 2019
17+
18+
MIT License, include all this text in any redistribution
19+
20+
Support Open Source development by buying your materials at [Adafruit.com](https://www.adafruit.com/).

CPX_Walking_Stick/code.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Talking Cane
2+
# for Adafruit Circuit Playground Express with CircuitPython
3+
from adafruit_circuitplayground.express import cpx
4+
5+
# Change this number to adjust touch sensitivity threshold
6+
cpx.adjust_touch_threshold(600)
7+
# Set the tap type: 1=single, 2=double
8+
cpx.detect_taps = 1
9+
10+
# NeoPixel colors used
11+
RED = (90, 0, 0)
12+
BLACK = (0, 0, 0)
13+
14+
cpx.pixels.brightness = 0.1 # set brightness value
15+
16+
# The audio file assigned to the touchpad
17+
audio_file = "imperial_march.wav"
18+
19+
def play_it():
20+
cpx.pixels.fill(RED) # Light neopixels
21+
cpx.play_file(audio_file) # play audio clip
22+
print("playing file ", audio_file)
23+
cpx.pixels.fill(BLACK) # unlight lights
24+
25+
while True:
26+
# playback mode. Use the slide switch to change between
27+
# trigger via touch or via single tap
28+
if cpx.switch:
29+
if cpx.touch_A1:
30+
play_it()
31+
else:
32+
if cpx.tapped:
33+
play_it()

CircuitPython_Flying_Toasters/code.py

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@
4444
ANIMATED = [_sprite >= FIRST_CELL and _sprite <= LAST_CELL for _sprite in range(NUMBER_OF_SPRITES)]
4545

4646

47-
# The chance (out of 10) that a new toaster, or toast will enter
48-
CHANCE_OF_NEW_TOASTER = 5
47+
# The chance (out of 10) that toast will enter
4948
CHANCE_OF_NEW_TOAST = 2
5049

5150
# How many sprites to styart with
52-
INITIAL_NUMBER_OF_SPRITES= 5
51+
INITIAL_NUMBER_OF_SPRITES = 4
5352

5453
# Global variables
5554
display = None
@@ -66,25 +65,22 @@ def make_display():
6665
pass
6766
spi.configure(baudrate=24000000) # Configure SPI for 24MHz
6867
spi.unlock()
69-
tft_cs = board.D10
70-
tft_dc = board.D7
71-
7268
displayio.release_displays()
73-
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D9)
69+
display_bus = displayio.FourWire(spi, command=board.D7, chip_select=board.D10, reset=board.D9)
7470

7571
return ST7789(display_bus, width=240, height=240, rowstart=80, auto_refresh=True)
7672

7773
def make_tilegrid():
7874
"""Construct and return the tilegrid."""
7975
group = displayio.Group(max_size=10)
8076

81-
sprite_sheet, palette = adafruit_imageload.load("/spritesheet.bmp",
77+
sprite_sheet, palette = adafruit_imageload.load("/spritesheet-2x.bmp",
8278
bitmap=displayio.Bitmap,
8379
palette=displayio.Palette)
8480
grid = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
85-
width=9, height=9,
86-
tile_height=32, tile_width=32,
87-
x=0, y=-32,
81+
width=5, height=5,
82+
tile_height=64, tile_width=64,
83+
x=0, y=-64,
8884
default_tile=EMPTY)
8985
group.append(grid)
9086
display.show(group)
@@ -104,8 +100,8 @@ def seed_toasters(number_of_toasters):
104100
"""Create the initial toasters so it doesn't start empty"""
105101
for _ in range(number_of_toasters):
106102
while True:
107-
row = randint(0, 8)
108-
col = randint(0, 8)
103+
row = randint(0, 4)
104+
col = randint(0, 4)
109105
if evaluate_position(row, col):
110106
break
111107
tilegrid[col, row] = random_cell()
@@ -117,7 +113,7 @@ def next_sprite(sprite):
117113

118114
def advance_animation():
119115
"""Cycle through animation cells each time."""
120-
for tile_number in range(81):
116+
for tile_number in range(25):
121117
tilegrid[tile_number] = next_sprite(tilegrid[tile_number])
122118

123119
def slide_tiles():
@@ -127,43 +123,39 @@ def slide_tiles():
127123

128124
def shift_tiles():
129125
"""Move tiles one spot to the left, and reset the tilegrid's position"""
130-
for row in range(8, 0, -1):
131-
for col in range(8):
126+
for row in range(4, 0, -1):
127+
for col in range(4):
132128
tilegrid[col, row] = tilegrid[col + 1, row - 1]
133-
tilegrid[8, row] = EMPTY
134-
for col in range(9):
129+
tilegrid[4, row] = EMPTY
130+
for col in range(5):
135131
tilegrid[col, 0] = EMPTY
136132
tilegrid.x = 0
137-
tilegrid.y = -32
133+
tilegrid.y = -64
138134

139135
def get_entry_row():
140136
while True:
141-
row = randint(0, 8)
142-
if tilegrid[8, row] == EMPTY and tilegrid[7, row] == EMPTY:
137+
row = randint(0, 4)
138+
if tilegrid[4, row] == EMPTY and tilegrid[3, row] == EMPTY:
143139
return row
144140

145141
def get_entry_column():
146142
while True:
147-
col = randint(0, 8)
143+
col = randint(0, 3)
148144
if tilegrid[col, 0] == EMPTY and tilegrid[col, 1] == EMPTY:
149145
return col
150146

151147
def add_toaster_or_toast():
152148
"""Maybe add a new toaster or toast on the right and/or top at a randon open location"""
153149
if randint(1, 10) <= CHANCE_OF_NEW_TOAST:
154150
tile = TOAST
155-
elif randint(1, 10) <= CHANCE_OF_NEW_TOASTER:
156-
tile = random_cell()
157151
else:
158-
tile = EMPTY
159-
tilegrid[8, get_entry_row()] = tile
152+
tile = random_cell()
153+
tilegrid[4, get_entry_row()] = tile
160154

161155
if randint(1, 10) <= CHANCE_OF_NEW_TOAST:
162156
tile = TOAST
163-
elif randint(1, 8) <= CHANCE_OF_NEW_TOASTER:
164-
tile = random_cell()
165157
else:
166-
tile = EMPTY
158+
tile = random_cell()
167159
tilegrid[get_entry_column(), 0] = tile
168160

169161
display = make_display()
@@ -172,7 +164,7 @@ def add_toaster_or_toast():
172164
display.refresh()
173165

174166
while True:
175-
for _ in range(32):
167+
for _ in range(64):
176168
display.refresh(target_frames_per_second=80)
177169
advance_animation()
178170
slide_tiles()
36.1 KB
Binary file not shown.
-9.12 KB
Binary file not shown.

0 commit comments

Comments
 (0)