Skip to content

Commit 13dd48b

Browse files
authored
Merge branch 'main' into pyportal
2 parents 4ff18d1 + 9de211f commit 13dd48b

177 files changed

Lines changed: 219 additions & 25 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,49 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
"""CircuitPython I2C Device Address Scan"""
6-
# If you run this and it seems to hang, try manually unlocking
7-
# your I2C bus from the REPL with
8-
# >>> import board
9-
# >>> board.I2C().unlock()
5+
# pylint: disable=bare-except, eval-used, unused-import
106

7+
"""CircuitPython I2C Device Address Scan"""
118
import time
129
import board
10+
import busio
1311

14-
# To use default I2C bus (most boards)
15-
i2c = board.I2C() # uses board.SCL and board.SDA
16-
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12+
# List of potential I2C busses
13+
ALL_I2C = ("board.I2C()", "board.STEMMA_I2C()", "busio.I2C(board.GP1, board.GP0)")
1714

18-
# To create I2C bus on specific pins
19-
# import busio
20-
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
21-
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040
15+
# Determine which busses are valid
16+
found_i2c = []
17+
for name in ALL_I2C:
18+
try:
19+
print("Checking {}...".format(name), end="")
20+
bus = eval(name)
21+
bus.unlock()
22+
found_i2c.append((name, bus))
23+
print("ADDED.")
24+
except:
25+
print("SKIPPED.")
2226

23-
while not i2c.try_lock():
24-
pass
25-
26-
try:
27+
# Scan valid busses
28+
if len(found_i2c):
29+
print("-" * 40)
30+
print("I2C SCAN")
31+
print("-" * 40)
2732
while True:
28-
print(
29-
"I2C addresses found:",
30-
[hex(device_address) for device_address in i2c.scan()],
31-
)
32-
time.sleep(2)
33+
for bus_info in found_i2c:
34+
name = bus_info[0]
35+
bus = bus_info[1]
3336

34-
finally: # unlock the i2c bus when ctrl-c'ing out of the loop
35-
i2c.unlock()
37+
while not bus.try_lock():
38+
pass
39+
40+
print(
41+
name,
42+
"addresses found:",
43+
[hex(device_address) for device_address in bus.scan()],
44+
)
45+
46+
bus.unlock()
47+
48+
time.sleep(2)
49+
else:
50+
print("No valid I2C bus found.")

LEGO_Lighting/code.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 john park for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
import asyncio
5+
from random import randint, uniform
6+
import busio
7+
import board
8+
import adafruit_aw9523
9+
10+
# pin descriptions are based on physical LED placement
11+
# 0 bakery window, 1 lamp1 A, 2 lamp1 B, 3 bakery sconce, 4 lamp 2, 5 music sconce a,
12+
# 6 music sconce b, 7 music candle, 8 bakery red a, 9 bakery red b, 10 bakery green a,
13+
# 11 bakery green b
14+
15+
i2c = busio.I2C(board.SCL1, board.SDA1)
16+
leddriver = adafruit_aw9523.AW9523(i2c)
17+
18+
# Set all pins to outputs and LED (const current) mode
19+
leddriver.LED_modes = 0xFFFF
20+
leddriver.directions = 0xFFFF
21+
22+
window_set = [8, 10, 9, 11] # red/green string
23+
always_on_set = [0, 1, 2, 4] # window and lanterns
24+
always_on_set_maxes = [100, 30, 30, 40] # maximum brightness per light
25+
26+
# lights that are always on
27+
for n in range(len(always_on_set)):
28+
leddriver.set_constant_current(always_on_set[n], always_on_set_maxes[n])
29+
30+
async def flicker(pin, min_curr, max_curr, interval):
31+
while True:
32+
rand_max_curr = randint(min_curr, max_curr)
33+
for i in range(min_curr, rand_max_curr):
34+
leddriver.set_constant_current(pin, i) # aw9523 pin, current out of 255
35+
await asyncio.sleep(0.07)
36+
await asyncio.sleep(uniform(0.0, interval))
37+
38+
async def string_lights(interval, max_curr):
39+
while True:
40+
for i in range(len(window_set)):
41+
# fade up
42+
for j in range(max_curr):
43+
leddriver.set_constant_current(window_set[i], j)
44+
print(j)
45+
await asyncio.sleep(interval)
46+
for i in range(len(window_set)):
47+
# fade down
48+
for j in range(max_curr):
49+
leddriver.set_constant_current(window_set[i], max_curr-j)
50+
print(j)
51+
await asyncio.sleep(interval)
52+
53+
54+
async def main():
55+
led0_task = asyncio.create_task(flicker(3, 3, 10, 0.7)) # music candle
56+
led1_task = asyncio.create_task(flicker(5, 6, 12, 0.7)) # music sconce a
57+
led2_task = asyncio.create_task(flicker(6, 6, 12, 0.7)) # music sconce b
58+
led3_task = asyncio.create_task(flicker(7, 3, 10, 0.7)) # music candle
59+
led4_task = asyncio.create_task(string_lights(0.03, 30))
60+
await asyncio.gather(led0_task, led1_task, led2_task, led3_task, led4_task)
61+
62+
asyncio.run(main())

LEGO_Lighting/lego_lighting_01.uf2

16 MB
Binary file not shown.
File renamed without changes.
File renamed without changes.

MagTag_Arduino_Demos/button_demo/.magtag.test.only renamed to MagTag/MagTag_Arduino_Demos/button_demo/.magtag.test.only

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)