]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/mcetest.py
Release 0.1 Various bugfixes and improvements.
[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 self.irb.transmit(self.addr, self.data, _FIRST, True)
40 self.tim.trigger(_REP_DELAY)
41
42 def ofunc(self): # Button release: cancel repeat timer
43 self.stop = True
44
45 async def repeat(self):
46 await asyncio.sleep(0) # Let timer stop before retriggering
47 if self.stop: # Button has been released: send last message
48 self.stop = False
49 self.tim.stop() # Not strictly necessary
50 self.irb.transmit(self.addr, self.data, _END, True)
51 else:
52 self.tim.trigger(_REP_DELAY)
53 self.irb.transmit(self.addr, self.data, _REP, True)
54
55 async def main():
56 if ESP32: # Pins for IR LED gate
57 pin = (Pin(23, Pin.OUT, value = 0), Pin(21, Pin.OUT, value = 0))
58 else:
59 pin = Pin('X1')
60 irb = MCE(pin) # verbose=True)
61 # Uncomment the following to print transmit timing
62 # irb.timeit = True
63
64 b = [] # Rbutton instances
65 px3 = Pin(18, Pin.IN, Pin.PULL_UP) if ESP32 else Pin('X3', Pin.IN, Pin.PULL_UP)
66 px4 = Pin(19, Pin.IN, Pin.PULL_UP) if ESP32 else Pin('X4', Pin.IN, Pin.PULL_UP)
67 b.append(Rbutton(irb, px3, 0x1, 0x7))
68 b.append(Rbutton(irb, px4, 0xe, 0xb))
69 if ESP32:
70 while True:
71 print('Running')
72 await asyncio.sleep(5)
73 else:
74 led = LED(1)
75 while True:
76 await asyncio.sleep_ms(500) # Obligatory flashing LED.
77 led.toggle()
78
79 # Greeting strings. Common:
80 s = '''Test for IR transmitter. Run:
81 from ir_tx.mcetest import test
82 test()
83 '''
84 # Pyboard:
85 spb = '''
86 IR LED on pin X1
87 Ground pin X3 to send addr 1 data 7
88 Ground pin X4 to send addr 0xe data 0x0b.'''
89 # ESP32
90 sesp = '''
91 IR LED gate on pins 23, 21
92 Ground pin 18 to send addr 1 data 7
93 Ground pin 19 to send addr 0xe data 0x0b.'''
94
95 print(''.join((s, sesp)) if ESP32 else ''.join((s, spb)))
96
97 def test():
98 loop.run_until_complete(main())