]>
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
40 self
.irb
.transmit(self
.addr
, self
.data
, _FIRST
)
41 self
.tim
.trigger(_REP_DELAY
)
43 def ofunc(self
): # Button release: cancel repeat timer
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
50 self
.tim
.stop() # Not strictly necessary
51 self
.irb
.transmit(self
.addr
, self
.data
, _END
)
55 self
.tim
.trigger(_REP_DELAY
)
56 self
.irb
.transmit(self
.addr
, self
.data
, _REP
)
59 if ESP32
: # Pins for IR LED gate
60 pin
= (Pin(23, Pin
.OUT
, value
= 0), Pin(21, Pin
.OUT
, value
= 0))
63 irb
= MCE(pin
, verbose
=True)
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))
73 await asyncio
.sleep(5)
77 await asyncio
.sleep_ms(500) # Obligatory flashing LED.
80 # Greeting strings. Common:
81 s
= '''Test for IR transmitter. Run:
82 from ir_tx.mcetest import test
88 Ground pin X3 to send addr 1 data 7
89 Ground pin X4 to send addr 0xe data 0x0b.'''
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.'''
96 print(''.join((s
, sesp
)) if ESP32
else ''.join((s
, spb
)))
99 loop
.run_until_complete(main())