# mPy - shuttle 2 x 3 one wire 2-aspect signals
#    simple linear programming with background task
# by Anthony Vogelaar, Perth Scotland
# rev 0.10  2024-10-30  initial release

from	machine	import	Pin
import	time

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

RED   = 0
GREEN = 1

pin_pa0  = 16									# used pin numbers on Esp32s2
pin_pa1  = 18
pin_pa2  = 33
pin_pb0  = 35
pin_pb1  = 37
pin_pb2  = 39

signal_a0 = Pin(pin_pa0, Pin.OUT, value=RED)	# instantiate signal objects in RED
signal_a1 = Pin(pin_pa1, Pin.OUT, value=RED)
signal_a2 = Pin(pin_pa2, Pin.OUT, value=RED)
signal_b0 = Pin(pin_pb0, Pin.OUT, value=RED)
signal_b1 = Pin(pin_pb1, Pin.OUT, value=RED)
signal_b2 = Pin(pin_pb2, Pin.OUT, value=RED)

# ============================= 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):
            pass						# nothing to do for background task
            time.sleep(0.1)
        print('*', end='')
    
# ============================= main ============================

while True:
    signal_a0.value(GREEN)				# train departs A
    signal_a1.value(GREEN)
    signal_a2.value(GREEN)
    delay(2)
    signal_a0.value(RED)				# train passes A0
    delay(2)
    signal_a1.value(RED)				# train passes A1
    delay(2)
    signal_a2.value(RED)				# train passes A2, arrives in B
    delay(5)							# train waits in B

    signal_b0.value(GREEN)				# train departs B
    signal_b1.value(GREEN)
    signal_b2.value(GREEN)
    delay(2)
    signal_b0.value(RED)				# train passes B0
    delay(2)
    signal_b1.value(RED)				# train passes B1
    delay(2)
    signal_b2.value(RED)				# train passes B2, arrives in A
    delay(5)							# train waits in A

# ============================== end ===============================
