]>
vault307.fbx.one Git - micorpython_ir.git/blob - 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
7 loop
= asyncio
.get_event_loop()
9 # If button is held down normal behaviour is to retransmit
10 # but most NEC models send a REPEAT code
12 toggle
= 1 # toggle is ignored in NEC mode
13 def __init__(self
, irb
, pin
, addr
, data
, proto
):
20 self
.sw
.close_func(self
.cfunc
)
21 self
.sw
.open_func(self
.ofunc
)
22 self
.tim
= Delay_ms(self
.repeat
)
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
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.
32 def ofunc(self
): # Button release: cancel repeat timer
34 Rbutton
.toggle ^
= 1 # Toggle control
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
41 self
.irb
.repeat() # NEC special case: send REPEAT code
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
48 async def main(proto
):
49 pin
= Pin(16, Pin
.OUT
, value
= 0) #IRled=irb
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
64 irb
= classes
[proto
](pin
, 38000) # My decoder chip is 38KHz
65 # Uncomment the following to print transmit timing
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
)
80 await asyncio
.sleep_ms(500)
83 def test(proto
=0): #(runs script allowing to try different classes)
84 loop
.run_until_complete(main(proto
))