]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx_test.py
Prior to portable tx branch
[micorpython_ir.git] / ir_tx_test.py
1 # ir_tx_test.py Test for nonblocking NEC/SONY/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 with auto repeat.
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, SONY, 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. The Sony protocol specifies 45ms but this is tight.
31 # In 20 bit mode a data burst can be upto 39ms long.
32 self.tim.trigger(108)
33
34 def ofunc(self): # Button release: cancel repeat timer
35 self.tim.stop()
36 Rbutton.toggle ^= 1 # Toggle control
37
38 async def repeat(self):
39 await asyncio.sleep(0) # Let timer stop before retriggering
40 if not self.sw(): # Button is still pressed: retrigger
41 self.tim.trigger(108)
42 if self.rep_code:
43 self.irb.repeat() # NEC special case: send REPEAT code
44 else:
45 self.irb.transmit(self.addr, self.data, Rbutton.toggle)
46
47 async def main(proto):
48 # Test uses a 38KHz carrier. Some Philips systems use 36KHz.
49 # If button is held down normal behaviour is to retransmit
50 # but most NEC models send a REPEAT code
51 rep_code = False # Rbutton constructor requires False for RC-X. NEC protocol only.
52 pin = Pin('X1')
53 if not proto:
54 irb = NEC(pin) # Default NEC freq == 38KHz
55 # Option to send REPEAT code. Most remotes do this.
56 rep_code = True
57 elif proto < 4:
58 bits = (12, 15, 20)[proto - 1]
59 irb = SONY(pin, bits, 38000) # My decoder chip is 38KHz
60 elif proto == 5:
61 irb = RC5(pin, 38000) # My decoder chip is 38KHz
62 elif proto == 6:
63 irb = RC6_M0(pin, 38000)
64
65 b = [] # Rbutton instances
66 b.append(Rbutton(irb, Pin('X3', Pin.IN, Pin.PULL_UP), 0x1, 0x7, rep_code))
67 b.append(Rbutton(irb, Pin('X4', Pin.IN, Pin.PULL_UP), 0x10, 0xb, rep_code))
68 led = LED(1)
69 while True:
70 await asyncio.sleep_ms(500) # Obligatory flashing LED.
71 led.toggle()
72
73 s = '''Test for IR transmitter. Run:
74 from ir_tx_test import test
75 test() for NEC protocol
76 test(1) for Sony SIRC 12 bit
77 test(2) for Sony SIRC 15 bit
78 test(3) for Sony SIRC 20 bit
79 test(5) for Philips RC-5 protocol
80 test(6) for Philips RC-6 mode 0.
81
82 Ground X3 to send addr 1 data 7
83 Ground X4 to send addr 0x10 data 0x0b.'''
84 print(s)
85
86 def test(proto=0):
87 loop.run_until_complete(main(proto))