]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx_test.py
Initial commit. RC6 rx not working.
[micorpython_ir.git] / ir_tx_test.py
1 # ir_tx_test.py Test for nonblocking NEC/RC-5/RC-6 mode 0 IR blaster.
2
3 # Released under the MIT License (MIT). See LICENSE.
4
5 # Copyright (c) 2020 Peter Hinch
6
7 # Implements a 2-button remote control on a Pyboard
8
9 from pyb import Pin, LED
10 import uasyncio as asyncio
11 from aswitch import Switch, Delay_ms
12 from ir_tx import NEC, RC5, RC6_M0
13
14 loop = asyncio.get_event_loop()
15
16 class Rbutton:
17 toggle = 1 # toggle is ignored in NEC mode
18 def __init__(self, irb, pin, addr, data, rep_code=False):
19 self.irb = irb
20 self.sw = Switch(pin)
21 self.addr = addr
22 self.data = data
23 self.rep_code = rep_code
24 self.sw.close_func(self.cfunc)
25 self.sw.open_func(self.ofunc)
26 self.tim = Delay_ms(self.repeat)
27
28 def cfunc(self): # Button push: send data
29 self.irb.transmit(self.addr, self.data, Rbutton.toggle)
30 # Auto repeat
31 self.tim.trigger(108)
32
33 def ofunc(self): # Button release: cancel repeat timer
34 self.tim.stop()
35 Rbutton.toggle ^= 1 # Toggle control
36
37 async def repeat(self):
38 await asyncio.sleep(0) # Let timer stop before retriggering
39 if not self.sw(): # Button is still pressed: retrigger
40 self.tim.trigger(108)
41 if self.rep_code:
42 self.irb.repeat() # NEC special case: send REPEAT code
43 else:
44 self.irb.transmit(self.addr, self.data, Rbutton.toggle)
45
46 async def main(proto):
47 # Test uses a 38KHz carrier. Some Philips systems use 36KHz.
48 # If button is held down normal behaviour is to retransmit
49 # but most NEC models send a RPEAT code
50 rep_code = False # Don't care for RC-X. NEC protocol only.
51 pin = Pin('X1')
52 if not proto:
53 irb = NEC(pin) # Default NEC freq == 38KHz
54 # Option to send REPEAT code. Most remotes do this.
55 rep_code = True
56 elif proto == 5:
57 irb = RC5(pin, 38000)
58 elif proto == 6:
59 irb = RC6_M0(pin, 38000)
60
61 b = [] # Rbutton instances
62 b.append(Rbutton(irb, Pin('X3', Pin.IN, Pin.PULL_UP), 0x1, 0x7, rep_code))
63 b.append(Rbutton(irb, Pin('X4', Pin.IN, Pin.PULL_UP), 0x10, 0xb, rep_code))
64 led = LED(1)
65 while True:
66 await asyncio.sleep_ms(500) # Obligatory flashing LED.
67 led.toggle()
68
69 s = '''Test for IR transmitter. Run:
70 ir_tx_test.test() for NEC protocol
71 ir_tx_test.test(5) for RC-5 protocol
72 ir_tx_test.test(6) for RC-6 mode 0.
73
74 Ground X3 to send addr 1 data 7
75 Ground X4 to send addr 0x10 data 0x0b.'''
76 print(s)
77
78 def test(proto=0):
79 loop.run_until_complete(main(proto))