]>
vault307.fbx.one Git - ir_remote.git/blob - ir_tx/test.py
1 # ir_tx.test 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.
8 from sys
import platform
9 ESP32
= platform
== 'esp32'
10 RP2
= platform
== 'rp2'
11 PYBOARD
= platform
== 'pyboard'
13 from machine
import Pin
15 from pyb
import Pin
, LED
16 import uasyncio
as asyncio
17 from primitives
.switch
import Switch
18 from primitives
.delay_ms
import Delay_ms
19 # Import all implemented classes
20 from ir_tx
.nec
import NEC
21 from ir_tx
.sony
import SONY_12
, SONY_15
, SONY_20
22 from ir_tx
.philips
import RC5
, RC6_M0
24 loop
= asyncio
.get_event_loop()
26 # If button is held down normal behaviour is to retransmit
27 # but most NEC models send a REPEAT code
29 toggle
= 1 # toggle is ignored in NEC mode
30 def __init__(self
, irb
, pin
, addr
, data
, proto
):
37 self
.sw
.close_func(self
.cfunc
)
38 self
.sw
.open_func(self
.ofunc
)
39 self
.tim
= Delay_ms(self
.repeat
)
41 def cfunc(self
): # Button push: send data
42 tog
= 0 if self
.proto
< 3 else Rbutton
.toggle
# NEC, sony 12, 15: toggle==0
43 self
.irb
.transmit(self
.addr
, self
.data
, tog
, True) # Test validation
45 # Auto repeat. The Sony protocol specifies 45ms but this is tight.
46 # In 20 bit mode a data burst can be upto 39ms long.
49 def ofunc(self
): # Button release: cancel repeat timer
51 Rbutton
.toggle ^
= 1 # Toggle control
53 async def repeat(self
):
54 await asyncio
.sleep(0) # Let timer stop before retriggering
55 if not self
.sw(): # Button is still pressed: retrigger
58 self
.irb
.repeat() # NEC special case: send REPEAT code
61 tog
= 0 if self
.proto
< 3 else Rbutton
.toggle
# NEC, sony 12, 15: toggle==0
62 self
.irb
.transmit(self
.addr
, self
.data
, tog
, True) # Test validation
65 async def main(proto
):
66 # Test uses a 38KHz carrier.
67 if ESP32
: # Pins for IR LED gate
68 pin
= Pin(23, Pin
.OUT
, value
= 0)
70 pin
= Pin(17, Pin
.OUT
, value
= 0)
73 classes
= (NEC
, SONY_12
, SONY_15
, SONY_20
, RC5
, RC6_M0
)
74 irb
= classes
[proto
](pin
, 38000) # My decoder chip is 38KHz
75 # Uncomment the following to print transmit timing
78 b
= [] # Rbutton instances
79 px3
= Pin('X3', Pin
.IN
, Pin
.PULL_UP
) if PYBOARD
else Pin(18, Pin
.IN
, Pin
.PULL_UP
)
80 px4
= Pin('X4', Pin
.IN
, Pin
.PULL_UP
) if PYBOARD
else Pin(19, Pin
.IN
, Pin
.PULL_UP
)
81 b
.append(Rbutton(irb
, px3
, 0xef00, 0x03, proto
))
82 b
.append(Rbutton(irb
, px4
, 0xef00, 0x02, proto
))
86 await asyncio
.sleep(5)
88 led
= Pin(25, Pin
.OUT
)
90 await asyncio
.sleep_ms(500) # Obligatory flashing LED.
95 await asyncio
.sleep_ms(500) # Obligatory flashing LED.
98 # Greeting strings. Common:
99 s
= '''Test for IR transmitter. Run:
100 from ir_tx.test import test
101 test() for NEC protocol
102 test(1) for Sony SIRC 12 bit
103 test(2) for Sony SIRC 15 bit
104 test(3) for Sony SIRC 20 bit
105 test(4) for Philips RC-5 protocol
106 test(5) for Philips RC-6 mode 0.
112 Ground pin X3 to send addr 1 data 7
113 Ground pin X4 to send addr 0x10 data 0x0b.'''
117 IR LED gate on pin 23
118 Ground pin 18 to send addr 1 data 7
119 Ground pin 19 to send addr 0x10 data 0x0b.'''
123 IR LED gate on pin 17
124 Ground pin 18 to send addr 1 data 7
125 Ground pin 19 to send addr 0x10 data 0x0b.'''
128 print(''.join((s
, sesp
)))
130 print(''.join((s
, srp2
)))
132 print(''.join((s
, spb
)))
135 loop
.run_until_complete(main(proto
))