# mPy - 8 inputs to pulse 8 outputs using IRQ
# by Anthony Vogelaar, Perth Scotland
# rev 0.10  2024-10-31  initial release

from	machine_sim	import	Pin
import	time
from	ve_rw2050	import	portA, portB

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

RED   = 0
GREEN = 1

pins_out = portA							# PA0..PA7
pins_inp = portB							# PB0..PB7
outs	 = [Pin(p, Pin.OUT, value=RED)  for p in pins_out]
inps     = [Pin(p, Pin.IN, Pin.PULL_UP) for p in pins_inp]

# ============================== main ====================================

class Main:

    # instantiate required objects an start main loop
    def __init__(self):
        self.pulses = [0]*len(pins_out)		# pulse length to go on output
        for inp in inps:
            inp.irq(self.onInp, Pin.IRQ_FALLING)
        self.task()
        

    # inp IRQ handler
    def onInp(self, pin):
        idx = inps.index(pin)
        outs[idx].value(1)			# set corresponding output
        self.pulses[idx] = 20		# set pulse length to 2 sec
   
        
    # main loop
    def task(self):
        while True:
            for _ in range(10):				# do @ 100 mSec
                for idx, out in enumerate(outs):
                    if self.pulses[idx]:	# no zip because of write
                        self.pulses[idx] -= 1
                        if self.pulses[idx] == 0:
                            out.value(0)
                Pin.qtExe()				# background task - GPIO simulator
                time.sleep(0.1)
            print('*', end='')				# do @ 1 sec
 
 
Main()

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