]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/mcetest.py
Add MCE. Fix bug with RC5X.
[micorpython_ir.git] / ir_tx / mcetest.py
1 # ir_tx.mcetest Test for nonblocking MCE 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
15 from micropython import const
16 import uasyncio as asyncio
17 from aswitch import Switch, Delay_ms
18 from ir_tx.mce import MCE
19
20 loop = asyncio.get_event_loop()
21 _FIRST = const(0)
22 _REP = const(1)
23 _END = const(2)
24 _REP_DELAY = const(60)
25
26 class Rbutton:
27 def __init__(self, irb, pin, addr, data, rep_code=False):
28 self.irb = irb
29 self.sw = Switch(pin)
30 self.addr = addr
31 self.data = data
32 self.rep_code = rep_code
33 self.sw.close_func(self.cfunc)
34 self.sw.open_func(self.ofunc)
35 self.tim = Delay_ms(self.repeat)
36 self.stop = False
37
38 def cfunc(self): # Button push: send data and set up for repeats
39 print('start')
40 self.irb.transmit(self.addr, self.data, _FIRST)
41 self.tim.trigger(_REP_DELAY)
42
43 def ofunc(self): # Button release: cancel repeat timer
44 self.stop = True
45
46 async def repeat(self):
47 await asyncio.sleep(0) # Let timer stop before retriggering
48 if self.stop: # Button has been released: send last message
49 self.stop = False
50 self.tim.stop() # Not strictly necessary
51 self.irb.transmit(self.addr, self.data, _END)
52 print('stop')
53 else:
54 print('rep')
55 self.tim.trigger(_REP_DELAY)
56 self.irb.transmit(self.addr, self.data, _REP)
57
58 async def main():
59 if ESP32: # Pins for IR LED gate
60 pin = (Pin(23, Pin.OUT, value = 0), Pin(21, Pin.OUT, value = 0))
61 else:
62 pin = Pin('X1')
63 irb = MCE(pin, verbose=True)
64
65 b = [] # Rbutton instances
66 px3 = Pin(18, Pin.IN, Pin.PULL_UP) if ESP32 else Pin('X3', Pin.IN, Pin.PULL_UP)
67 px4 = Pin(19, Pin.IN, Pin.PULL_UP) if ESP32 else Pin('X4', Pin.IN, Pin.PULL_UP)
68 b.append(Rbutton(irb, px3, 0x1, 0x7))
69 b.append(Rbutton(irb, px4, 0xe, 0xb))
70 if ESP32:
71 while True:
72 print('Running')
73 await asyncio.sleep(5)
74 else:
75 led = LED(1)
76 while True:
77 await asyncio.sleep_ms(500) # Obligatory flashing LED.
78 led.toggle()
79
80 # Greeting strings. Common:
81 s = '''Test for IR transmitter. Run:
82 from ir_tx.mcetest import test
83 test()
84 '''
85 # Pyboard:
86 spb = '''
87 IR LED on pin X1
88 Ground pin X3 to send addr 1 data 7
89 Ground pin X4 to send addr 0xe data 0x0b.'''
90 # ESP32
91 sesp = '''
92 IR LED gate on pins 23, 21
93 Ground pin 18 to send addr 1 data 7
94 Ground pin 19 to send addr 0xe data 0x0b.'''
95
96 print(''.join((s, sesp)) if ESP32 else ''.join((s, spb)))
97
98 def test():
99 loop.run_until_complete(main())