# cPy - simulated shuttle 2 x 3 one wire 2-aspect signals
#    sequencing through a pattern
# by Anthony Vogelaar, Perth Scotland
# rev 0.10  2024-10-30  initial release

from	machine_sim import	Pin
import	time

# =========================== Hardware set-up =============================

RED   = 0
GREEN = 1

portA    = (16, 18, 33, 35, 37, 39)
signals  = [Pin(pin_nr, Pin.OUT, value=RED) for pin_nr in portA]
patterns = ((1, 1, 1, 0, 0, 0), (0, 1, 1, 0, 0, 0), (0, 0, 1, 0, 0, 0), (0, 0, 0, 0, 0, 0),
            (0, 0, 0, 1, 1, 1), (0, 0, 0, 0, 1, 1), (0, 0, 0, 0, 0, 1), (0, 0, 0, 0, 0, 0))

# ============================= local ===========================

# delay for dly seconds, print '*' @ 1 Hz and background task @ 10 Hz
def delay(dly):
    for tSec in range(dly):
        for t100mS in range(10):
            Pin.qtExe()						# background task - GPIO simulator
            time.sleep(0.1)
        print('*', end='', flush=True)


# set 6 signals according pattern
def setPattern(pattern):
    for signal, aspect in zip(signals, pattern):
        signal.value(aspect)			# set signal as in pattern
    
# ============================== main ===========================

# cycle through shuttle patterns
def main():
    while True:
        for pattern in patterns:
            print(pattern)
            setPattern(pattern)
            delay(2)
            if pattern == (0, 0, 0, 0, 0, 0):	# if train in A or B
                print('reverse')
                delay(3)
        

main()

# ================================= End =====================================
