]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/test.py
c9e3366e35482f57d76e87c6935018770f728fb1
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
44 # Auto repeat. The Sony protocol specifies 45ms but this is tight.
45 # In 20 bit mode a data burst can be upto 39ms long.
48 def ofunc(self
): # Button release: cancel repeat timer
50 Rbutton
.toggle ^
= 1 # Toggle control
52 async def repeat(self
):
53 await asyncio
.sleep(0) # Let timer stop before retriggering
54 if not self
.sw(): # Button is still pressed: retrigger
57 self
.irb
.repeat() # NEC special case: send REPEAT code
59 tog
= 0 if self
.proto
< 3 else Rbutton
.toggle
# NEC, sony 12, 15: toggle==0
60 self
.irb
.transmit(self
.addr
, self
.data
, tog
, True) # Test validation
62 async def main(proto
):
63 # Test uses a 38KHz carrier.
64 if ESP32
: # Pins for IR LED gate
65 pin
= Pin(23, Pin
.OUT
, value
= 0)
67 pin
= Pin(17, Pin
.OUT
, value
= 0)
70 classes
= (NEC
, SONY_12
, SONY_15
, SONY_20
, RC5
, RC6_M0
)
71 irb
= classes
[proto
](pin
, 38000) # My decoder chip is 38KHz
72 # Uncomment the following to print transmit timing
75 b
= [] # Rbutton instances
76 px3
= Pin('X3', Pin
.IN
, Pin
.PULL_UP
) if PYBOARD
else Pin(18, Pin
.IN
, Pin
.PULL_UP
)
77 px4
= Pin('X4', Pin
.IN
, Pin
.PULL_UP
) if PYBOARD
else Pin(19, Pin
.IN
, Pin
.PULL_UP
)
78 b
.append(Rbutton(irb
, px3
, 0x1, 0x7, proto
))
79 b
.append(Rbutton(irb
, px4
, 0x10, 0xb, proto
))
83 await asyncio
.sleep(5)
85 led
= Pin(25, Pin
.OUT
)
87 await asyncio
.sleep_ms(500) # Obligatory flashing LED.
92 await asyncio
.sleep_ms(500) # Obligatory flashing LED.
95 # Greeting strings. Common:
96 s
= '''Test for IR transmitter. Run:
97 from ir_tx.test import test
98 test() for NEC protocol
99 test(1) for Sony SIRC 12 bit
100 test(2) for Sony SIRC 15 bit
101 test(3) for Sony SIRC 20 bit
102 test(4) for Philips RC-5 protocol
103 test(5) for Philips RC-6 mode 0.
109 Ground pin X3 to send addr 1 data 7
110 Ground pin X4 to send addr 0x10 data 0x0b.'''
114 IR LED gate on pin 23
115 Ground pin 18 to send addr 1 data 7
116 Ground pin 19 to send addr 0x10 data 0x0b.'''
120 IR LED gate on pin 17
121 Ground pin 18 to send addr 1 data 7
122 Ground pin 19 to send addr 0x10 data 0x0b.'''
125 print(''.join((s
, sesp
)))
127 print(''.join((s
, srp2
)))
129 print(''.join((s
, spb
)))
132 loop
.run_until_complete(main(proto
))