]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/mcetest.py
Make compatible with Raspberry Pi Pico (RP2 arch).
[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 pin = Pin(23, Pin.OUT, value = 0) if ESP32 else Pin('X1')
57 irb = MCE(pin) # verbose=True)
58 # Uncomment the following to print transmit timing
59 # irb.timeit = True
60
61 b = [] # Rbutton instances
62 px3 = Pin(18, Pin.IN, Pin.PULL_UP) if ESP32 else Pin('X3', Pin.IN, Pin.PULL_UP)
63 px4 = Pin(19, Pin.IN, Pin.PULL_UP) if ESP32 else Pin('X4', Pin.IN, Pin.PULL_UP)
64 b.append(Rbutton(irb, px3, 0x1, 0x7))
65 b.append(Rbutton(irb, px4, 0xe, 0xb))
66 if ESP32:
67 while True:
68 print('Running')
69 await asyncio.sleep(5)
70 else:
71 led = LED(1)
72 while True:
73 await asyncio.sleep_ms(500) # Obligatory flashing LED.
74 led.toggle()
75
76 # Greeting strings. Common:
77 s = '''Test for IR transmitter. Run:
78 from ir_tx.mcetest import test
79 test()
80 '''
81 # Pyboard:
82 spb = '''
83 IR LED on pin X1
84 Ground pin X3 to send addr 1 data 7
85 Ground pin X4 to send addr 0xe data 0x0b.'''
86 # ESP32
87 sesp = '''
88 IR LED gate on pins 23, 21
89 Ground pin 18 to send addr 1 data 7
90 Ground pin 19 to send addr 0xe data 0x0b.'''
91
92 print(''.join((s, sesp)) if ESP32 else ''.join((s, spb)))
93
94 def test():
95 loop.run_until_complete(main())