Merge branch 'main' into pyportal
|
|
@ -2,34 +2,49 @@
|
|||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
"""CircuitPython I2C Device Address Scan"""
|
||||
# If you run this and it seems to hang, try manually unlocking
|
||||
# your I2C bus from the REPL with
|
||||
# >>> import board
|
||||
# >>> board.I2C().unlock()
|
||||
# pylint: disable=bare-except, eval-used, unused-import
|
||||
|
||||
"""CircuitPython I2C Device Address Scan"""
|
||||
import time
|
||||
import board
|
||||
import busio
|
||||
|
||||
# To use default I2C bus (most boards)
|
||||
i2c = board.I2C() # uses board.SCL and board.SDA
|
||||
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
|
||||
# List of potential I2C busses
|
||||
ALL_I2C = ("board.I2C()", "board.STEMMA_I2C()", "busio.I2C(board.GP1, board.GP0)")
|
||||
|
||||
# To create I2C bus on specific pins
|
||||
# import busio
|
||||
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
|
||||
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040
|
||||
# Determine which busses are valid
|
||||
found_i2c = []
|
||||
for name in ALL_I2C:
|
||||
try:
|
||||
print("Checking {}...".format(name), end="")
|
||||
bus = eval(name)
|
||||
bus.unlock()
|
||||
found_i2c.append((name, bus))
|
||||
print("ADDED.")
|
||||
except:
|
||||
print("SKIPPED.")
|
||||
|
||||
while not i2c.try_lock():
|
||||
pass
|
||||
|
||||
try:
|
||||
# Scan valid busses
|
||||
if len(found_i2c):
|
||||
print("-" * 40)
|
||||
print("I2C SCAN")
|
||||
print("-" * 40)
|
||||
while True:
|
||||
print(
|
||||
"I2C addresses found:",
|
||||
[hex(device_address) for device_address in i2c.scan()],
|
||||
)
|
||||
time.sleep(2)
|
||||
for bus_info in found_i2c:
|
||||
name = bus_info[0]
|
||||
bus = bus_info[1]
|
||||
|
||||
finally: # unlock the i2c bus when ctrl-c'ing out of the loop
|
||||
i2c.unlock()
|
||||
while not bus.try_lock():
|
||||
pass
|
||||
|
||||
print(
|
||||
name,
|
||||
"addresses found:",
|
||||
[hex(device_address) for device_address in bus.scan()],
|
||||
)
|
||||
|
||||
bus.unlock()
|
||||
|
||||
time.sleep(2)
|
||||
else:
|
||||
print("No valid I2C bus found.")
|
||||
|
|
|
|||
62
LEGO_Lighting/code.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# SPDX-FileCopyrightText: Copyright (c) 2023 john park for Adafruit Industries
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
import asyncio
|
||||
from random import randint, uniform
|
||||
import busio
|
||||
import board
|
||||
import adafruit_aw9523
|
||||
|
||||
# pin descriptions are based on physical LED placement
|
||||
# 0 bakery window, 1 lamp1 A, 2 lamp1 B, 3 bakery sconce, 4 lamp 2, 5 music sconce a,
|
||||
# 6 music sconce b, 7 music candle, 8 bakery red a, 9 bakery red b, 10 bakery green a,
|
||||
# 11 bakery green b
|
||||
|
||||
i2c = busio.I2C(board.SCL1, board.SDA1)
|
||||
leddriver = adafruit_aw9523.AW9523(i2c)
|
||||
|
||||
# Set all pins to outputs and LED (const current) mode
|
||||
leddriver.LED_modes = 0xFFFF
|
||||
leddriver.directions = 0xFFFF
|
||||
|
||||
window_set = [8, 10, 9, 11] # red/green string
|
||||
always_on_set = [0, 1, 2, 4] # window and lanterns
|
||||
always_on_set_maxes = [100, 30, 30, 40] # maximum brightness per light
|
||||
|
||||
# lights that are always on
|
||||
for n in range(len(always_on_set)):
|
||||
leddriver.set_constant_current(always_on_set[n], always_on_set_maxes[n])
|
||||
|
||||
async def flicker(pin, min_curr, max_curr, interval):
|
||||
while True:
|
||||
rand_max_curr = randint(min_curr, max_curr)
|
||||
for i in range(min_curr, rand_max_curr):
|
||||
leddriver.set_constant_current(pin, i) # aw9523 pin, current out of 255
|
||||
await asyncio.sleep(0.07)
|
||||
await asyncio.sleep(uniform(0.0, interval))
|
||||
|
||||
async def string_lights(interval, max_curr):
|
||||
while True:
|
||||
for i in range(len(window_set)):
|
||||
# fade up
|
||||
for j in range(max_curr):
|
||||
leddriver.set_constant_current(window_set[i], j)
|
||||
print(j)
|
||||
await asyncio.sleep(interval)
|
||||
for i in range(len(window_set)):
|
||||
# fade down
|
||||
for j in range(max_curr):
|
||||
leddriver.set_constant_current(window_set[i], max_curr-j)
|
||||
print(j)
|
||||
await asyncio.sleep(interval)
|
||||
|
||||
|
||||
async def main():
|
||||
led0_task = asyncio.create_task(flicker(3, 3, 10, 0.7)) # music candle
|
||||
led1_task = asyncio.create_task(flicker(5, 6, 12, 0.7)) # music sconce a
|
||||
led2_task = asyncio.create_task(flicker(6, 6, 12, 0.7)) # music sconce b
|
||||
led3_task = asyncio.create_task(flicker(7, 3, 10, 0.7)) # music candle
|
||||
led4_task = asyncio.create_task(string_lights(0.03, 30))
|
||||
await asyncio.gather(led0_task, led1_task, led2_task, led3_task, led4_task)
|
||||
|
||||
asyncio.run(main())
|
||||
BIN
LEGO_Lighting/lego_lighting_01.uf2
Normal file
|
Before Width: | Height: | Size: 111 KiB After Width: | Height: | Size: 111 KiB |
|
Before Width: | Height: | Size: 37 KiB After Width: | Height: | Size: 37 KiB |
|
Before Width: | Height: | Size: 74 KiB After Width: | Height: | Size: 74 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |