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