]> vault307.fbx.one Git - micorpython_ir.git/blob - RECEIVER.md
RMT tested.
[micorpython_ir.git] / RECEIVER.md
1 # IR Receiver
2
3 ##### [Main README](./README.md#1-ir-communication)
4
5 # 1. Hardware Requirements
6
7 The receiver is cross-platform. It requires an IR receiver chip to demodulate
8 the carrier. The chip must be selected for the frequency in use by the remote.
9 For 38KHz devices a receiver chip such as the Vishay TSOP4838 or the
10 [adafruit one](https://www.adafruit.com/products/157) is required. This
11 demodulates the 38KHz IR pulses and passes the demodulated pulse train to the
12 microcontroller. The tested chip returns a 0 level on carrier detect, but the
13 driver design ensures operation regardless of sense.
14
15 In my testing a 38KHz demodulator worked with 36KHz and 40KHz remotes, but this
16 is obviously neither guaranteed nor optimal.
17
18 The pin used to connect the decoder chip to the target is arbitrary. The test
19 program assumes pin X3 on the Pyboard, pin 23 on ESP32 and pin 13 on ESP8266.
20 On the WeMos D1 Mini the equivalent pin is D7.
21
22 A remote using the NEC protocol is [this one](https://www.adafruit.com/products/389).
23
24 # 2. Installation and demo script
25
26 The receiver is a Python package. This minimises RAM usage: applications only
27 import the device driver for the protocol in use.
28
29
30 Copy the following to the target filesystem:
31 1. `ir_rx` Directory and contents.
32
33 There are no dependencies.
34
35 The demo can be used to characterise IR remotes. It displays the codes returned
36 by each button. This can aid in the design of receiver applications. The demo
37 prints "running" every 5 seconds and reports any data received from the remote.
38
39 ```python
40 from ir_rx.test import test
41 ```
42 Instructions will be displayed at the REPL.
43
44 # 3. The driver
45
46 This implements a class for each supported protocol. Each class is subclassed
47 from a common abstract base class in `__init__.py`.
48
49 Applications should instantiate the appropriate class with a callback. The
50 callback will run whenever an IR pulse train is received. Example running on a
51 Pyboard:
52 ```python
53 import time
54 from machine import Pin
55 from pyb import LED
56 from ir_rx.nec import NEC_8 # NEC remote, 8 bit addresses
57
58 red = LED(1)
59
60 def callback(data, addr, ctrl):
61 if data < 0: # NEC protocol sends repeat codes.
62 print('Repeat code.')
63 else:
64 print('Data {:02x} Addr {:04x}'.format(data, addr))
65
66 ir = NEC_8(Pin('X3', Pin.IN), callback)
67 while True:
68 time.sleep_ms(500)
69 red.toggle()
70 ```
71
72 #### Common to all classes
73
74 Constructor args:
75 1. `pin` is a `machine.Pin` instance configured as an input, connected to the
76 IR decoder chip.
77 2. `callback` is the user supplied callback.
78 3. `*args` Any further args will be passed to the callback.
79
80 The user callback takes the following args:
81 1. `data` (`int`) Value from the remote. Normally in range 0-255. A value < 0
82 signifies an NEC repeat code.
83 2. `addr` (`int`) Address from the remote.
84 3. `ctrl` (`int`) The meaning of this is protocol dependent:
85 NEC: 0
86 Philips: this is toggled 1/0 on repeat button presses. If the button is held
87 down it is not toggled. The transmitter demo implements this behaviour.
88 Sony: 0 unless receiving a 20-bit stream, in which case it holds the extended
89 value.
90 4. Any args passed to the constructor.
91
92 Bound variable:
93 1. `verbose=False` If `True` emits debug output.
94
95 Method:
96 1. `error_function` Arg: a function taking a single `int` arg. If this is
97 specified it will be called if an error occurs. The value corresponds to the
98 error code (see below). Typical usage might be to provide some user feedback
99 of incorrect reception although beware of occasional triggers by external
100 events. In my testing the TSOP4838 produces 200µs pulses on occasion for no
101 obvious reason. See [section 4](./RECEIVER.md#4-errors).
102
103 A function is provided to print errors in human readable form. This may be
104 invoked as follows:
105
106 ```python
107 from ir_rx.print_error import print_error # Optional print of error codes
108 # Assume ir is an instance of an IR receiver class
109 ir.error_function(print_error)
110 ```
111 Class variables:
112 1. These are constants defining the NEC repeat code and the error codes sent
113 to the error function. They are discussed in [section 4](./RECEIVER.md#4-errors).
114
115 #### NEC classes
116
117 `NEC_8`, `NEC_16`
118
119 Typical invocation:
120 ```python
121 from ir_rx.nec import NEC_8
122 ```
123
124 Remotes using the NEC protocol can send 8 or 16 bit addresses. If the `NEC_16`
125 class receives an 8 bit address it will get a 16 bit value comprising the
126 address in bits 0-7 and its one's complement in bits 8-15.
127 The `NEC_8` class enables error checking for remotes that return an 8 bit
128 address: the complement is checked and the address returned as an 8-bit value.
129 A 16-bit address will result in an error.
130
131 #### Sony classes
132
133 `SONY_12`, `SONY_15`, `SONY_20`
134
135 Typical invocation:
136 ```python
137 from ir_rx.sony import SONY_15
138 ```
139
140 The SIRC protocol comes in 3 variants: 12, 15 and 20 bits. `SONY_20` handles
141 bitstreams from all three types of remote. Choosing a class matching the remote
142 improves the timing reducing the likelihood of errors when handling repeats: in
143 20-bit mode SIRC timing when a button is held down is tight. A worst-case 20
144 bit block takes 39ms nominal, yet the repeat time is 45ms nominal.
145 A single physical remote can issue more than one type of bitstream. The Sony
146 remote tested issued both 12 bit and 15 bit streams.
147
148 #### Philips classes
149
150 `RC5_IR`, `RC6_M0`
151
152 Typical invocation:
153 ```python
154 from ir_rx.philips import RC5_IR
155 ```
156
157 These support the RC-5 and RC-6 mode 0 protocols respectively.
158
159 # 4. Errors
160
161 IR reception is inevitably subject to errors, notably if the remote is operated
162 near the limit of its range, if it is not pointed at the receiver or if its
163 batteries are low. The user callback is not called when an error occurs.
164
165 On ESP8266 and ESP32 there is a further source of errors. This results from the
166 large and variable interrupt latency of the device which can exceed the pulse
167 duration. This causes pulses to be missed or their timing measured incorrectly.
168 On ESP8266 some improvment may be achieved by running the chip at 160MHz.
169
170 In general applications should provide user feedback of correct reception.
171 Users tend to press the key again if the expected action is absent.
172
173 In debugging a callback can be specified for reporting errors. The value passed
174 to the error function are represented by constants indicating the cause of the
175 error. These are driver ABC class variables and are as follows:
176
177 `BADSTART` A short (<= 4ms) start pulse was received. May occur due to IR
178 interference, e.g. from fluorescent lights. The TSOP4838 is prone to producing
179 200µs pulses on occasion, especially when using the ESP8266.
180 `BADBLOCK` A normal data block: too few edges received. Occurs on the ESP8266
181 owing to high interrupt latency.
182 `BADREP` A repeat block: an incorrect number of edges were received.
183 `OVERRUN` A normal data block: too many edges received.
184 `BADDATA` Data did not match check byte.
185 `BADADDR` (`NEC_IR`) If `extended` is `False` the 8-bit address is checked
186 against the check byte. This code is returned on failure.
187
188 # 5. Receiver platforms
189
190 Currently the ESP8266 suffers from [this issue](https://github.com/micropython/micropython/issues/5714).
191 Testing was therefore done without WiFi connectivity. If the application uses
192 the WiFi regularly, or if an external process pings the board repeatedly, the
193 crash does not occur.
194
195 Philips protocols (especially RC-6) have tight timing constraints with short
196 pulses whose length must be determined with reasonable accuracy. The Sony 20
197 bit protocol also has a timing issue in that the worst case bit pattern takes
198 39ms nominal, yet the repeat time is 45ms nominal. These issues can lead to
199 errors particularly on slower targets. As discussed above, errors are to be
200 expected. It is up to the user to decide if the error rate is acceptable.
201
202 Reception was tested using Pyboard D SF2W, ESP8266 and ESP32 with signals from
203 remote controls (where available) and from the tranmitter in this repo. Issues
204 are listed below.
205
206 NEC: No issues.
207 Sony 12 and 15 bit: No issues.
208 Sony 20 bit: On ESP32 some errors occurred when repeats occurred.
209 Philips RC-5: On ESP32 with one remote control many errors occurred, but paired
210 with the transmitter in this repo it worked.
211 Philips RC-6: No issues. Only tested against the transmitter in this repo.
212
213 # 6. Principle of operation
214
215 Protocol classes inherit from the abstract base class `IR_RX`. This uses a pin
216 interrupt to store in an array the time (in μs) of each transition of the pulse
217 train from the receiver chip. Arrival of the first edge starts a software timer
218 which runs for the expected duration of an IR block (`tblock`). The use of a
219 software timer ensures that `.decode` and the user callback can allocate.
220
221 When the timer times out its callback (`.decode`) decodes the data. `.decode`
222 is a method of the protocol specific subclass; on completion it calls the
223 `do_callback` method of the ABC. This resets the edge reception and calls
224 either the user callback or the error function (if provided).
225
226 The size of the array and the duration of the timer are protocol dependent and
227 are set by the subclasses. The `.decode` method is provided in the subclass.
228
229 CPU times used by `.decode` (not including the user callback) were measured on
230 a Pyboard D SF2W at stock frequency. They were: NEC 1ms for normal data, 100μs
231 for a repeat code. Philips codes: RC-5 900μs, RC-6 mode 0 5.5ms.
232
233 # 7. References
234
235 [General information about IR](https://www.sbprojects.net/knowledge/ir/)
236
237 The NEC protocol:
238 [altium](http://techdocs.altium.com/display/FPGA/NEC+Infrared+Transmission+Protocol)
239 [circuitvalley](http://www.circuitvalley.com/2013/09/nec-protocol-ir-infrared-remote-control.html)
240
241 Philips protocols:
242 [RC5](https://en.wikipedia.org/wiki/RC-5)
243 [RC5](https://www.sbprojects.net/knowledge/ir/rc5.php)
244 [RC6](https://www.sbprojects.net/knowledge/ir/rc6.php)
245
246 Sony protocol:
247 [SIRC](https://www.sbprojects.net/knowledge/ir/sirc.php)
248
249 # Appendix 1 NEC Protocol description
250
251 A normal burst comprises exactly 68 edges, the exception being a repeat code
252 which has 4. An incorrect number of edges is treated as an error. All bursts
253 begin with a 9ms pulse. In a normal code this is followed by a 4.5ms space; a
254 repeat code is identified by a 2.25ms space. A data burst lasts for 67.5ms.
255
256 Data bits comprise a 562.5µs mark followed by a space whose length determines
257 the bit value. 562.5µs denotes 0 and 1.6875ms denotes 1.
258
259 In 8 bit address mode the complement of the address and data values is sent to
260 provide error checking. This also ensures that the number of 1's and 0's in a
261 burst is constant, giving a constant burst length of 67.5ms. In extended
262 address mode this constancy is lost. The burst length can (by my calculations)
263 run to 76.5ms.