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