]> vault307.fbx.one Git - micorpython_ir.git/blob - irRemote/rokuRemote.py
micropython_ir projects
[micorpython_ir.git] / irRemote / rokuRemote.py
1 from machine import Pin
2 import uasyncio as asyncio
3 from primitives.switch import Switch
4 from primitives.delay_ms import Delay_ms
5 from ir_tx.nec import NEC
6
7 loop = asyncio.get_event_loop()
8
9 # If button is held down normal behaviour is to retransmit
10 # but most NEC models send a REPEAT code
11 class Rbutton:
12 toggle = 1 # toggle is ignored in NEC mode
13 def __init__(self, irb, pin, addr, data, proto):
14 self.irb = irb
15 self.sw = Switch(pin)
16 self.addr = addr
17 self.data = data
18 self.proto = proto
19
20 self.sw.close_func(self.cfunc)
21 self.sw.open_func(self.ofunc)
22 self.tim = Delay_ms(self.repeat)
23
24 def cfunc(self): # Button push: send data
25 tog = 0 if self.proto < 3 else Rbutton.toggle # NEC, sony 12, 15: toggle==0
26 self.irb.transmit(self.addr, self.data, tog, True) # Test validation
27 print(self.data)
28 # Auto repeat. The Sony protocol specifies 45ms but this is tight.
29 # In 20 bit mode a data burst can be upto 39ms long.
30 self.tim.trigger(108)
31
32 def ofunc(self): # Button release: cancel repeat timer
33 self.tim.stop()
34 Rbutton.toggle ^= 1 # Toggle control
35
36 async def repeat(self):
37 await asyncio.sleep(0) # Let timer stop before retriggering
38 if not self.sw(): # Button is still pressed: retrigger
39 self.tim.trigger(108)
40 if self.proto == 0:
41 self.irb.repeat() # NEC special case: send REPEAT code
42 print(self.data)
43 else:
44 tog = 0 if self.proto < 3 else Rbutton.toggle # NEC, sony 12, 15: toggle==0
45 self.irb.transmit(self.addr, self.data, tog, True) # Test validation
46 print(self.data)
47
48 async def main(proto):
49 pin = Pin(16, Pin.OUT, value = 0) #IRled=irb
50 addr=0xc7ea
51 #TODO: Set up buttons (see ir_tx.test)
52 #Power 0x17 \button=Pin(pin, Pin.IN, Pin.PULL_UP)\ (yellow)5
53 #Home 0x03 \Pin(pin, Pin.IN, Pin.PULL_UP)\
54 #Back 0x66 \Pin(pin, Pin.IN, Pin.PULL_UP)\
55 #VolU 0x0f \Pin(pin, Pin.IN, Pin.PULL_UP)\
56 #VolD 0x10 \Pin(pin, Pin.IN, Pin.PULL_UP)\
57 #Up 0x19 \Pin(pin, Pin.IN, Pin.PULL_UP)\ (green)20
58 #Down 0x33 \Pin(pin, Pin.IN, Pin.PULL_UP)\
59 #Left 0x1e \Pin(pin, Pin.IN, Pin.PULL_UP)\ (brown)26
60 #Right 0x2d \Pin(pin, Pin.IN, Pin.PULL_UP)\
61 #OK 0x2a \Pin(pin, Pin.IN, Pin.PULL_UP)\ (blue)27
62
63 classes = (NEC,0)
64 irb = classes[proto](pin, 38000) # My decoder chip is 38KHz
65 # Uncomment the following to print transmit timing
66 # irb.timeit = True
67
68 b = [] # Rbutton instances
69 pbutton=Pin(5,Pin.IN,Pin.PULL_UP)
70 ubutton=Pin(20,Pin.IN,Pin.PULL_UP)
71 lbutton=Pin(26,Pin.IN,Pin.PULL_UP)
72 ebutton=Pin(27,Pin.IN,Pin.PULL_UP)
73 b.append(Rbutton(irb, pbutton, addr, 0x17, proto))
74 b.append(Rbutton(irb, ubutton,addr,0x19,proto))
75 b.append(Rbutton(irb, lbutton,addr,0x1e,proto))
76 b.append(Rbutton(irb, ebutton,addr,0x2a,proto))
77 #b.append(Rbutton(irb, buttonName,addr,DATA,proto))\replace buttonName, DATA
78 led = Pin(25, Pin.OUT)
79 while True:
80 await asyncio.sleep_ms(500)
81 led(not led())
82
83 def test(proto=0): #(runs script allowing to try different classes)
84 loop.run_until_complete(main(proto))
85
86 test()