]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/test.py
c9e3366e35482f57d76e87c6935018770f728fb1
[micorpython_ir.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 # Auto repeat. The Sony protocol specifies 45ms but this is tight.
45 # In 20 bit mode a data burst can be upto 39ms long.
46 self.tim.trigger(108)
47
48 def ofunc(self): # Button release: cancel repeat timer
49 self.tim.stop()
50 Rbutton.toggle ^= 1 # Toggle control
51
52 async def repeat(self):
53 await asyncio.sleep(0) # Let timer stop before retriggering
54 if not self.sw(): # Button is still pressed: retrigger
55 self.tim.trigger(108)
56 if self.proto == 0:
57 self.irb.repeat() # NEC special case: send REPEAT code
58 else:
59 tog = 0 if self.proto < 3 else Rbutton.toggle # NEC, sony 12, 15: toggle==0
60 self.irb.transmit(self.addr, self.data, tog, True) # Test validation
61
62 async def main(proto):
63 # Test uses a 38KHz carrier.
64 if ESP32: # Pins for IR LED gate
65 pin = Pin(23, Pin.OUT, value = 0)
66 elif RP2:
67 pin = Pin(17, Pin.OUT, value = 0)
68 else:
69 pin = Pin('X1')
70 classes = (NEC, SONY_12, SONY_15, SONY_20, RC5, RC6_M0)
71 irb = classes[proto](pin, 38000) # My decoder chip is 38KHz
72 # Uncomment the following to print transmit timing
73 # irb.timeit = True
74
75 b = [] # Rbutton instances
76 px3 = Pin('X3', Pin.IN, Pin.PULL_UP) if PYBOARD else Pin(18, Pin.IN, Pin.PULL_UP)
77 px4 = Pin('X4', Pin.IN, Pin.PULL_UP) if PYBOARD else Pin(19, Pin.IN, Pin.PULL_UP)
78 b.append(Rbutton(irb, px3, 0x1, 0x7, proto))
79 b.append(Rbutton(irb, px4, 0x10, 0xb, proto))
80 if ESP32:
81 while True:
82 print('Running')
83 await asyncio.sleep(5)
84 elif RP2:
85 led = Pin(25, Pin.OUT)
86 while True:
87 await asyncio.sleep_ms(500) # Obligatory flashing LED.
88 led(not led())
89 else:
90 led = LED(1)
91 while True:
92 await asyncio.sleep_ms(500) # Obligatory flashing LED.
93 led.toggle()
94
95 # Greeting strings. Common:
96 s = '''Test for IR transmitter. Run:
97 from ir_tx.test import test
98 test() for NEC protocol
99 test(1) for Sony SIRC 12 bit
100 test(2) for Sony SIRC 15 bit
101 test(3) for Sony SIRC 20 bit
102 test(4) for Philips RC-5 protocol
103 test(5) for Philips RC-6 mode 0.
104 '''
105
106 # Pyboard:
107 spb = '''
108 IR LED on pin X1
109 Ground pin X3 to send addr 1 data 7
110 Ground pin X4 to send addr 0x10 data 0x0b.'''
111
112 # ESP32
113 sesp = '''
114 IR LED gate on pin 23
115 Ground pin 18 to send addr 1 data 7
116 Ground pin 19 to send addr 0x10 data 0x0b.'''
117
118 # RP2
119 srp2 = '''
120 IR LED gate on pin 17
121 Ground pin 18 to send addr 1 data 7
122 Ground pin 19 to send addr 0x10 data 0x0b.'''
123
124 if ESP32:
125 print(''.join((s, sesp)))
126 elif RP2:
127 print(''.join((s, srp2)))
128 else:
129 print(''.join((s, spb)))
130
131 def test(proto=0):
132 loop.run_until_complete(main(proto))