from time import sleep_ms
from machine import Pin
pins: list[int] = [19, 18, 5, 17]
LEDS = [Pin(p, Pin.OUT, value=0) for p in pins]
reg: list[int] = [1, 0, 0, 0]
direction: int = 1 # 1 = right; -1 = left
# extras
extra = False # off by-default. set to True to see the extra functionality
first_run = True
pattern: list[int] = [1, 0, 0, 1, 1, 0, 1, 1, 1]
pos: int = 0 # current bit position in pattern
def shift_right(reg: list[int], val: int):
"""shift all bits to the right and insert new value in the start"""
for i in range(len(reg) - 1, 0, -1):
reg[i] = reg[i - 1]
reg[0] = val
def shift_left(reg: list[int], val: int):
"""shift all bits to the left and insert new value to the end"""
for i in range(len(reg) - 1):
reg[i] = reg[i + 1]
reg[len(reg) - 1] = val
def write_leds():
"""maps the items of register to leds"""
for i, led in enumerate(LEDS):
led.value(reg[i])
def main_loop():
"""the main activity loop"""
global direction
write_leds()
sleep_ms(200)
if reg == [0, 0, 0, 1]:
direction = -1 # go left
elif reg == [1, 0, 0, 0]:
direction = 1 # go right
if direction == 1:
shift_right(reg, 0)
else:
shift_left(reg, 0)
def extra_loop():
"""extra functionality"""
global reg
global first_run
global pos
# extra requires the register to be empty
if first_run:
reg = [0, 0, 0, 0] # reset reg
first_run = False
if pos == len(pattern) - 1:
pos = 0
print(pos, reg)
shift_right(reg, pattern[pos])
pos += 1
write_leds()
sleep_ms(750) # longer period to notice the pattern
while True:
if extra:
extra_loop()
else:
main_loop()