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