]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx_test.py
ad55a22acb181d912218968f23acc9b8d4bed069
1 # ir_tx_test.py Test for nonblocking NEC/SONY/RC-5/RC-6 mode 0 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.
9 from pyb
import Pin
, LED
10 import uasyncio
as asyncio
11 from aswitch
import Switch
, Delay_ms
12 from ir_tx
import NEC
, SONY
, RC5
, RC6_M0
14 loop
= asyncio
.get_event_loop()
17 toggle
= 1 # toggle is ignored in NEC mode
18 def __init__(self
, irb
, pin
, addr
, data
, rep_code
=False):
23 self
.rep_code
= rep_code
24 self
.sw
.close_func(self
.cfunc
)
25 self
.sw
.open_func(self
.ofunc
)
26 self
.tim
= Delay_ms(self
.repeat
)
28 def cfunc(self
): # Button push: send data
29 self
.irb
.transmit(self
.addr
, self
.data
, Rbutton
.toggle
)
30 # Auto repeat. The Sony protocol specifies 45ms but this is tight.
31 # In 20 bit mode a data burst can be upto 39ms long.
34 def ofunc(self
): # Button release: cancel repeat timer
36 Rbutton
.toggle ^
= 1 # Toggle control
38 async def repeat(self
):
39 await asyncio
.sleep(0) # Let timer stop before retriggering
40 if not self
.sw(): # Button is still pressed: retrigger
43 self
.irb
.repeat() # NEC special case: send REPEAT code
45 self
.irb
.transmit(self
.addr
, self
.data
, Rbutton
.toggle
)
47 async def main(proto
):
48 # Test uses a 38KHz carrier. Some Philips systems use 36KHz.
49 # If button is held down normal behaviour is to retransmit
50 # but most NEC models send a REPEAT code
51 rep_code
= False # Rbutton constructor requires False for RC-X. NEC protocol only.
54 irb
= NEC(pin
) # Default NEC freq == 38KHz
55 # Option to send REPEAT code. Most remotes do this.
58 bits
= (12, 15, 20)[proto
- 1]
59 irb
= SONY(pin
, bits
, 38000) # My decoder chip is 38KHz
61 irb
= RC5(pin
, 38000) # My decoder chip is 38KHz
63 irb
= RC6_M0(pin
, 38000)
65 b
= [] # Rbutton instances
66 b
.append(Rbutton(irb
, Pin('X3', Pin
.IN
, Pin
.PULL_UP
), 0x1, 0x7, rep_code
))
67 b
.append(Rbutton(irb
, Pin('X4', Pin
.IN
, Pin
.PULL_UP
), 0x10, 0xb, rep_code
))
70 await asyncio
.sleep_ms(500) # Obligatory flashing LED.
73 s
= '''Test for IR transmitter. Run:
74 from ir_tx_test import test
75 test() for NEC protocol
76 test(1) for Sony SIRC 12 bit
77 test(2) for Sony SIRC 15 bit
78 test(3) for Sony SIRC 20 bit
79 test(5) for Philips RC-5 protocol
80 test(6) for Philips RC-6 mode 0.
82 Ground X3 to send addr 1 data 7
83 Ground X4 to send addr 0x10 data 0x0b.'''
87 loop
.run_until_complete(main(proto
))