from machine import Pin
import uasyncio as asyncio
from primitives.switch import Switch
from primitives.delay_ms import Delay_ms
from ir_tx.nec import NEC

loop = asyncio.get_event_loop()

# If button is held down normal behaviour is to retransmit
# but most NEC models send a REPEAT code
class Rbutton:
    toggle = 1  # toggle is ignored in NEC mode
    def __init__(self, irb, pin, addr, data, proto):
        self.irb = irb
        self.sw = Switch(pin)
        self.addr = addr
        self.data = data
        self.proto = proto

        self.sw.close_func(self.cfunc)
        self.sw.open_func(self.ofunc)
        self.tim = Delay_ms(self.repeat)

    def cfunc(self):  # Button push: send data
        tog = 0 if self.proto < 3 else Rbutton.toggle  # NEC, sony 12, 15: toggle==0
        self.irb.transmit(self.addr, self.data, tog, True)  # Test validation
        print(self.data)
        # Auto repeat. The Sony protocol specifies 45ms but this is tight.
        # In 20 bit mode a data burst can be upto 39ms long.
        self.tim.trigger(108)

    def ofunc(self):  # Button release: cancel repeat timer
        self.tim.stop()
        Rbutton.toggle ^= 1  # Toggle control

    async def repeat(self):
        await asyncio.sleep(0)  # Let timer stop before retriggering
        if not self.sw():  # Button is still pressed: retrigger
            self.tim.trigger(108)
            if self.proto == 0:
                self.irb.repeat()  # NEC special case: send REPEAT code
                print(self.data)
            else:
                tog = 0 if self.proto < 3 else Rbutton.toggle  # NEC, sony 12, 15: toggle==0
                self.irb.transmit(self.addr, self.data, tog, True)  # Test validation
                print(self.data)
                
async def main(proto):
    pin = Pin(16, Pin.OUT, value = 0) #IRled=irb
    addr=0xc7ea
    #TODO: Set up buttons (see ir_tx.test)
    #Power  0x17  \button=Pin(pin, Pin.IN, Pin.PULL_UP)\ (yellow)5
    #Home   0x03  \Pin(pin, Pin.IN, Pin.PULL_UP)\
    #Back   0x66  \Pin(pin, Pin.IN, Pin.PULL_UP)\
    #VolU   0x0f  \Pin(pin, Pin.IN, Pin.PULL_UP)\
    #VolD   0x10  \Pin(pin, Pin.IN, Pin.PULL_UP)\
    #Up     0x19  \Pin(pin, Pin.IN, Pin.PULL_UP)\  (green)20
    #Down   0x33  \Pin(pin, Pin.IN, Pin.PULL_UP)\
    #Left   0x1e  \Pin(pin, Pin.IN, Pin.PULL_UP)\  (brown)26
    #Right  0x2d  \Pin(pin, Pin.IN, Pin.PULL_UP)\
    #OK     0x2a  \Pin(pin, Pin.IN, Pin.PULL_UP)\  (blue)27
    
    classes = (NEC,0)
    irb = classes[proto](pin, 38000)  # My decoder chip is 38KHz
    # Uncomment the following to print transmit timing
    # irb.timeit = True

    b = []  # Rbutton instances
    pbutton=Pin(5,Pin.IN,Pin.PULL_UP)
    ubutton=Pin(20,Pin.IN,Pin.PULL_UP)
    lbutton=Pin(26,Pin.IN,Pin.PULL_UP)
    ebutton=Pin(27,Pin.IN,Pin.PULL_UP)
    b.append(Rbutton(irb, pbutton, addr, 0x17, proto))
    b.append(Rbutton(irb, ubutton,addr,0x19,proto))
    b.append(Rbutton(irb, lbutton,addr,0x1e,proto))
    b.append(Rbutton(irb, ebutton,addr,0x2a,proto))
    #b.append(Rbutton(irb, buttonName,addr,DATA,proto))\replace buttonName, DATA
    led = Pin(25, Pin.OUT)
    while True:
            await asyncio.sleep_ms(500)
            led(not led())
    
def test(proto=0):  #(runs script allowing to try different classes)
    loop.run_until_complete(main(proto))

test()