UNPKG

3.04 kBMarkdownView Raw
1#Infrared
2Driver for the ir-attx4 Tessel infrared module ([Attinyx4](http://www.atmel.com/Images/doc8006.pdf)).
3
4##Installation
5```sh
6npm install ir-attx4
7```
8##Example
9```js
10/*********************************************
11This infrared module example transmits the
12power signal sequence of an Insignia brand
13television every three seconds, while also
14listening for (and logging) any incoming
15infrared data.
16*********************************************/
17
18// Any copyright is dedicated to the Public Domain.
19// http://creativecommons.org/publicdomain/zero/1.0/
20
21var tessel = require('tessel');
22
23// Connect the IR module to port a
24var hardware = tessel.port['A'];
25
26// Import library and connect to module
27var infrared = require('../').use(hardware);
28
29var counter = 0;
30
31// When we're connected
32infrared.on('ready', function() {
33 if (!err) {
34 console.log("Connected to IR!");
35
36 // Start turning tv on and off every 3 seconds
37 setInterval(powerTV, 3000);
38 }
39 else {
40 console.log("Err initializing: ", err.message );
41 }
42});
43
44// If we get data, print it out
45infrared.on('data', function(data) {
46 console.log("Received RX Data: ", data);
47});
48
49var powerTV = function() {
50
51 // Make a buffer off on/off durations (each duration is 16 bits)
52 var powerBuffer = new Buffer([0, 178, 255, 168, 0, 12, 255, 246, 0, 13, 255, 225, 0, 13, 255, 224, 0, 12, 255, 246, 0, 12, 255, 246, 0, 13, 255, 247, 0, 13, 255, 247, 0, 13, 255, 224, 0, 12, 255, 224, 0, 13, 255, 247, 0, 13, 255, 224, 0, 12, 255, 246, 0, 12, 255, 246, 0, 12, 255, 246, 0, 12, 255, 246, 0, 13, 255, 247, 0, 13, 255, 224, 0, 12, 255, 224, 0, 13, 255, 225, 0, 13, 255, 224, 0, 12, 255, 246, 0, 12, 255, 246, 0, 13, 255, 247, 0, 13, 255, 247, 0, 13, 255, 246, 0, 12, 255, 246, 0, 12, 255, 246, 0, 12, 255, 246, 0, 12, 255, 224, 0, 13, 255, 224, 0, 12, 255, 224, 0, 12, 255, 224, 0, 12]);
53
54 // Send the signal at 38 kHz
55 infrared.sendRawSignal(38, powerBuffer, function(err) {
56 if (err) console.log("Unable to send signal: ", err);
57 else {
58 console.log("TV Should be powered...");
59 }
60 });
61};
62```
63
64##Methods
65
66##### * `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.
67
68##### * `infrared.setListening(set, callback)` Determines whether the module is listening for incoming signals. Will automatically be set and unset depending on listeners for the `data` event.
69
70
71## Events
72
73##### * `infrared.on('data', callback(data))` Emitted when an infrared signal is detected.
74
75##### * `infrared.on('error', callback(err))` Emitted when there is an error communicating with the module.
76
77##### * `infrared.on('ready', callback())` Emitted upon first successful communication between the Tessel and the module.
78
79
80## License
81
82Released under the MIT and Apache 2.0 licenses.
83