from machine import Pin
import uasyncio, math
row_pins = [0, 1, 2, 3]
col_pins = [9, 8, 7, 6, 5]
cols, rows, row, col = len(col_pins), len(row_pins), 0, 0
# Dynamically creates a 2D array for the matrix resolution. [row][column]
defaultPatern = [[False for j in range(cols)] for i in range(rows)]
patern = [[False for j in range(cols)] for i in range(rows)]
def ToggleState(lightID):
""" ToggleState
ToggleState Toggles the state of a coordinate in the matrix. It gets the
coordinate from the ID. This has been hard-coded in and can only be changed within this script for now
"""
global patern, cols, rows
r = int(math.floor(lightID/cols))
c = int(math.fmod(lightID, cols))
patern[r][c] = not patern[r][c]
def ChangeState(lightID, state):
""" ChangeState
ChangeState changes the state of a coordinate in the matrix. It gets the
coordinate from the ID. This has been hard-coded in and can only be
changed within this script for now
"""
global patern, cols, rows
r = int(math.floor(lightID/cols))
c = int(math.fmod(lightID, cols))
patern[r][c] = state
def Clear():
global row_pins, col_pins
for r in row_pins:
Pin(r, mode=Pin.OUT, value=0)
for c in col_pins:
Pin(c, mode=Pin.OUT, value=1)
async def UpdateMatrix():
""" UpdateMatrix
This function updates the LED matrix to the latest up-to-date frame
"""
global patern, cols, rows, row, col
print("Draw script running")
while True:
if row < rows:
if col < cols:
Pin(row_pins[row], mode=Pin.OUT, value=patern[row][col])
Pin(col_pins[col], mode=Pin.IN, pull=Pin.PULL_DOWN)
Clear()
col += 1
else:
col = 0
row += 1
else:
row = 0
await uasyncio.sleep_ms(1)
async def ChangeLights():
""" ChangeLights
This function willl cycle the light patern through different uses
"""
global patern
print("ChangeLights script running")
while True:
ToggleState(0)
await uasyncio.sleep_ms(delay_ms)
ToggleState(0)
ToggleState(1)
await uasyncio.sleep_ms(delay_ms)
ToggleState(1)
ToggleState(2)
await uasyncio.sleep_ms(delay_ms)
ToggleState(2)
ToggleState(3)
await uasyncio.sleep_ms(delay_ms)
ToggleState(3)
ToggleState(7)
await uasyncio.sleep_ms(delay_ms)
ToggleState(7)
ToggleState(11)
await uasyncio.sleep_ms(delay_ms)
ToggleState(11)
ToggleState(15)
await uasyncio.sleep_ms(delay_ms)
ToggleState(15)
ToggleState(14)
await uasyncio.sleep_ms(delay_ms)
ToggleState(14)
ToggleState(13)
await uasyncio.sleep_ms(delay_ms)
ToggleState(13)
ToggleState(12)
await uasyncio.sleep_ms(delay_ms)
ToggleState(12)
ToggleState(8)
await uasyncio.sleep_ms(delay_ms)
ToggleState(8)
ToggleState(4)
await uasyncio.sleep_ms(delay_ms)
ToggleState(4)
ToggleState(5)
await uasyncio.sleep_ms(delay_ms)
ToggleState(5)
ToggleState(6)
await uasyncio.sleep_ms(delay_ms)
ToggleState(6)
ToggleState(10)
await uasyncio.sleep_ms(delay_ms)
ToggleState(10)
ToggleState(9)
await uasyncio.sleep_ms(delay_ms)
ToggleState(9)
await uasyncio.sleep_ms(delay_ms)
async def main():
uasyncio.create_task(UpdateMatrix())
uasyncio.create_task(ChangeLights())
while True:
await uasyncio.sleep(1)
uasyncio.run(main())