]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/mcetest.py
1 # ir_tx.mcetest Test for nonblocking MCE IR blaster.
3 # Released under the MIT License (MIT). See LICENSE.
5 # Copyright (c) 2020 Peter Hinch
7 # Implements a 2-button remote control on a Pyboard with auto repeat.
8 from sys
import platform
9 ESP32
= platform
== 'esp32'
11 from machine
import Pin
13 from pyb
import Pin
, LED
15 from micropython
import const
16 import uasyncio
as asyncio
17 from aswitch
import Switch
, Delay_ms
18 from ir_tx
.mce
import MCE
20 loop
= asyncio
.get_event_loop()
24 _REP_DELAY
= const(60)
27 def __init__(self
, irb
, pin
, addr
, data
, rep_code
=False):
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
)
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
)
42 def ofunc(self
): # Button release: cancel repeat timer
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
49 self
.tim
.stop() # Not strictly necessary
50 self
.irb
.transmit(self
.addr
, self
.data
, _END
, True)
52 self
.tim
.trigger(_REP_DELAY
)
53 self
.irb
.transmit(self
.addr
, self
.data
, _REP
, True)
56 if ESP32
: # Pins for IR LED gate
57 pin
= (Pin(23, Pin
.OUT
, value
= 0), Pin(21, Pin
.OUT
, value
= 0))
60 irb
= MCE(pin
) # verbose=True)
61 # Uncomment the following to print transmit timing
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))
72 await asyncio
.sleep(5)
76 await asyncio
.sleep_ms(500) # Obligatory flashing LED.
79 # Greeting strings. Common:
80 s
= '''Test for IR transmitter. Run:
81 from ir_tx.mcetest import test
87 Ground pin X3 to send addr 1 data 7
88 Ground pin X4 to send addr 0xe data 0x0b.'''
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.'''
95 print(''.join((s
, sesp
)) if ESP32
else ''.join((s
, spb
)))
98 loop
.run_until_complete(main())