UNPKG

5.62 kBMarkdownView Raw
1#Infrared
2Driver for the ir-attx4 Tessel infrared module. The hardware documentation for this module can be found [here](https://github.com/tessel/hardware/blob/master/modules-overview.md#infrared).
3
4If you run into any issues you can ask for support on the [IR Module Forums](http://forums.tessel.io/category/ir).
5
6Note: This library is responsible for sending and receiving IR data but we've just started another library, [ir-codes](github.com/technicalmachine/ir-codes), to generate and parse IR signals from different manufacturers. It's currently missing most manufacturers so we encourage developers to help us build out that library.
7
8###Installation
9```sh
10npm install ir-attx4
11```
12
13###Example
14```js
15/*********************************************
16This infrared module example transmits the
17power signal sequence of an Insignia brand
18television every three seconds, while also
19listening for (and logging) any incoming
20infrared data.
21*********************************************/
22
23var tessel = require('tessel');
24var infraredlib = require('ir-attx4');
25var infrared = infraredlib.use(tessel.port['A']);
26
27// When we're connected
28infrared.on('ready', function() {
29 if (!err) {
30 console.log("Connected to IR!");
31 // Start sending a signal every three seconds
32 setInterval(function() {
33 // Make a buffer of on/off durations (each duration is 16 bits, off durations are negative)
34 var powerBuffer = new Buffer([0x22,0xc4,0xee,0xd0,0x2,0x58,0xfe,0xc,0x2,0x8a,0xf9,0xf2,0x2,0x8a,0xf9,0xc0,0x2,0x58,0xfe,0xc,0x2,0x58,0xfe,0xc,0x2,0x8a,0xfe,0x3e,0x2,0x8a,0xfe,0x3e,0x2,0x8a,0xf9,0xc0,0x2,0x58,0xf9,0xc0,0x2,0x8a,0xfe,0x3e,0x2,0x8a,0xf9,0xc0,0x2,0x58,0xfe,0xc,0x2,0x58,0xfe,0xc,0x2,0x58,0xfe,0xc,0x2,0x58,0xfe,0xc,0x2,0x8a,0xfe,0x3e,0x2,0x8a,0xf9,0xc0,0x2,0x58,0xf9,0xc0,0x2,0x8a,0xf9,0xf2,0x2,0x8a,0xf9,0xc0,0x2,0x58,0xfe,0xc,0x2,0x58,0xfe,0xc,0x2,0x8a,0xfe,0x3e,0x2,0x8a,0xfe,0x3e,0x2,0x8a,0xfe,0xc,0x2,0x58,0xfe,0xc,0x2,0x58,0xfe,0xc,0x2,0x58,0xfe,0xc,0x2,0x58,0xf9,0xc0,0x2,0x8a,0xf9,0xc0,0x2,0x58,0xf9,0xc0,0x2,0x58,0xf9,0xc0,0x2,0x58]);
35 // Send the signal at 38 kHz
36 infrared.sendRawSignal(38, powerBuffer, function(err) {
37 if (err) {
38 console.log("Unable to send signal: ", err);
39 } else {
40 console.log("Signal sent!");
41 }
42 });
43 }, 3000); // Every 3 seconds
44 } else {
45 console.log(err);
46 }
47});
48
49// If we get data, print it out
50infrared.on('data', function(data) {
51 console.log("Received RX Data: ", data);
52});
53```
54
55###Methods
56&#x20;<a href="#api-infrared-sendRawSignal-frequency-signalDurations-callback-The-primary-method-for-sending-data-The-first-argument-is-a-frequency-of-signal-in-Hz-typically-38-but-can-range-from-36-to-40-The-second-argument-is-a-buffer-of-unsigned-16-bit-integers-representing-the-number-of-microseconds-the-transmission-should-be-on-The-max-length-of-the-signal-durations-is-100-durations" name="api-infrared-sendRawSignal-frequency-signalDurations-callback-The-primary-method-for-sending-data-The-first-argument-is-a-frequency-of-signal-in-Hz-typically-38-but-can-range-from-36-to-40-The-second-argument-is-a-buffer-of-unsigned-16-bit-integers-representing-the-number-of-microseconds-the-transmission-should-be-on-The-max-length-of-the-signal-durations-is-100-durations">#</a> infrared<b>.sendRawSignal</b>( frequency, signalDurations, callback )
57The primary method for sending data. The first argument is a frequency of signal in Hz, typically 38 but can range from 36 to 40. The second argument is a buffer of unsigned 16 bit integers representing the number of microseconds the transmission should be on or off. On durations are positive numbers and off durations are negative numbers. The max length of the signal durations is 100 durations (200 bytes).
58
59###Events
60&#x20;<a href="#api-infrared-on-data-callback-data-Emitted-when-an-infrared-signal-is-detected" name="api-infrared-on-data-callback-data-Emitted-when-an-infrared-signal-is-detected">#</a> infrared<b>.on</b>( 'data', callback(data) )
61
62Emitted when an infrared signal is detected. The returned data is an a buffer containing 16-bit words representing the number of microseconds a signal was on and off. For example, a buffer of <0x22,0xc4,0xee,0xd0,0x2,0x58,0xfe,0xc> represents four durations: on for 8900 uS (0x22c4), off for 4400 microseconds (0xeed0 as 2's compliment), on for 600 uS, then off for 320 microseconds.
63
64You may notice that some durations you received are occasionally off by 50 uS. That's because of the way the receiver logic is implemented. The state of a pin is checked on a 50uS timer and all of those timer ticks are multiplied by 50. If the timer is just a little bit off (or the light is reflected/transformed), it will miss the very start or end of a signal. IR detectors have a high margin of error so this usually has little effect.
65
66
67&#x20;<a href="#api-infrared-on-error-callback-err-Emitted-when-there-is-an-error-communicating-with-the-module" name="api-infrared-on-error-callback-err-Emitted-when-there-is-an-error-communicating-with-the-module">#</a> infrared<b>.on</b>( 'error', callback(err) )
68Emitted when there is an error communicating with the module.
69
70&#x20;<a href="#api-infrared-on-ready-callback-Emitted-upon-first-successful-communication-between-the-Tessel-and-the-module" name="api-infrared-on-ready-callback-Emitted-upon-first-successful-communication-between-the-Tessel-and-the-module">#</a> infrared<b>.on</b>( 'ready', callback() )
71Emitted upon first successful communication between the Tessel and the module.
72
73###License
74Released under the MIT and Apache 2.0 licenses.