]> vault307.fbx.one Git - ir_remote.git/blob - ir_tx/test.py
infrared remote
[ir_remote.git] / ir_tx / test.py
1 # ir_tx.test 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 from sys import platform
9 ESP32 = platform == 'esp32'
10 RP2 = platform == 'rp2'
11 PYBOARD = platform == 'pyboard'
12 if ESP32 or RP2:
13 from machine import Pin
14 else:
15 from pyb import Pin, LED
16 import uasyncio as asyncio
17 from primitives.switch import Switch
18 from primitives.delay_ms import Delay_ms
19 # Import all implemented classes
20 from ir_tx.nec import NEC
21 from ir_tx.sony import SONY_12, SONY_15, SONY_20
22 from ir_tx.philips import RC5, RC6_M0
23
24 loop = asyncio.get_event_loop()
25
26 # If button is held down normal behaviour is to retransmit
27 # but most NEC models send a REPEAT code
28 class Rbutton:
29 toggle = 1 # toggle is ignored in NEC mode
30 def __init__(self, irb, pin, addr, data, proto):
31 self.irb = irb
32 self.sw = Switch(pin)
33 self.addr = addr
34 self.data = data
35 self.proto = proto
36
37 self.sw.close_func(self.cfunc)
38 self.sw.open_func(self.ofunc)
39 self.tim = Delay_ms(self.repeat)
40
41 def cfunc(self): # Button push: send data
42 tog = 0 if self.proto < 3 else Rbutton.toggle # NEC, sony 12, 15: toggle==0
43 self.irb.transmit(self.addr, self.data, tog, True) # Test validation
44 #print(self.data)
45 # Auto repeat. The Sony protocol specifies 45ms but this is tight.
46 # In 20 bit mode a data burst can be upto 39ms long.
47 self.tim.trigger(108)
48
49 def ofunc(self): # Button release: cancel repeat timer
50 self.tim.stop()
51 Rbutton.toggle ^= 1 # Toggle control
52
53 async def repeat(self):
54 await asyncio.sleep(0) # Let timer stop before retriggering
55 if not self.sw(): # Button is still pressed: retrigger
56 self.tim.trigger(108)
57 if self.proto == 0:
58 self.irb.repeat() # NEC special case: send REPEAT code
59 #print(self.data)
60 else:
61 tog = 0 if self.proto < 3 else Rbutton.toggle # NEC, sony 12, 15: toggle==0
62 self.irb.transmit(self.addr, self.data, tog, True) # Test validation
63 #print(self.data)
64
65 async def main(proto):
66 # Test uses a 38KHz carrier.
67 if ESP32: # Pins for IR LED gate
68 pin = Pin(23, Pin.OUT, value = 0)
69 elif RP2:
70 pin = Pin(17, Pin.OUT, value = 0)
71 else:
72 pin = Pin('X1')
73 classes = (NEC, SONY_12, SONY_15, SONY_20, RC5, RC6_M0)
74 irb = classes[proto](pin, 38000) # My decoder chip is 38KHz
75 # Uncomment the following to print transmit timing
76 # irb.timeit = True
77
78 b = [] # Rbutton instances
79 px3 = Pin('X3', Pin.IN, Pin.PULL_UP) if PYBOARD else Pin(18, Pin.IN, Pin.PULL_UP)
80 px4 = Pin('X4', Pin.IN, Pin.PULL_UP) if PYBOARD else Pin(19, Pin.IN, Pin.PULL_UP)
81 b.append(Rbutton(irb, px3, 0xef00, 0x03, proto))
82 b.append(Rbutton(irb, px4, 0xef00, 0x02, proto))
83 if ESP32:
84 while True:
85 print('Running')
86 await asyncio.sleep(5)
87 elif RP2:
88 led = Pin(25, Pin.OUT)
89 while True:
90 await asyncio.sleep_ms(500) # Obligatory flashing LED.
91 led(not led())
92 else:
93 led = LED(1)
94 while True:
95 await asyncio.sleep_ms(500) # Obligatory flashing LED.
96 led.toggle()
97
98 # Greeting strings. Common:
99 s = '''Test for IR transmitter. Run:
100 from ir_tx.test import test
101 test() for NEC protocol
102 test(1) for Sony SIRC 12 bit
103 test(2) for Sony SIRC 15 bit
104 test(3) for Sony SIRC 20 bit
105 test(4) for Philips RC-5 protocol
106 test(5) for Philips RC-6 mode 0.
107 '''
108
109 # Pyboard:
110 spb = '''
111 IR LED on pin X1
112 Ground pin X3 to send addr 1 data 7
113 Ground pin X4 to send addr 0x10 data 0x0b.'''
114
115 # ESP32
116 sesp = '''
117 IR LED gate on pin 23
118 Ground pin 18 to send addr 1 data 7
119 Ground pin 19 to send addr 0x10 data 0x0b.'''
120
121 # RP2
122 srp2 = '''
123 IR LED gate on pin 17
124 Ground pin 18 to send addr 1 data 7
125 Ground pin 19 to send addr 0x10 data 0x0b.'''
126
127 if ESP32:
128 print(''.join((s, sesp)))
129 elif RP2:
130 print(''.join((s, srp2)))
131 else:
132 print(''.join((s, spb)))
133
134 def test(proto=0):
135 loop.run_until_complete(main(proto))