-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
238 lines (189 loc) · 6.27 KB
/
index.js
File metadata and controls
238 lines (189 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* MPR121
*
* Access the MPR121 chipset via node-i2c
*
* --------------------------------------------------------------------------------------------------------------------
*
* @author Amely Kling <mail@dwi.sk>
*
*/
/* Node Inclues
* ==================================================================================================================== */
var i2c = require('i2c-bus');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
/* Constants
* ==================================================================================================================== */
// Register addresses.
var MPR121_I2CADDR_DEFAULT = 0x5A;
var MPR121_TOUCHSTATUS_L = 0x00;
var MPR121_TOUCHSTATUS_H = 0x01;
var MPR121_FILTDATA_0L = 0x04;
var MPR121_FILTDATA_0H = 0x05;
var MPR121_BASELINE_0 = 0x1E;
var MPR121_MHDR = 0x2B;
var MPR121_NHDR = 0x2C;
var MPR121_NCLR = 0x2D;
var MPR121_FDLR = 0x2E;
var MPR121_MHDF = 0x2F;
var MPR121_NHDF = 0x30;
var MPR121_NCLF = 0x31;
var MPR121_FDLF = 0x32;
var MPR121_NHDT = 0x33;
var MPR121_NCLT = 0x34;
var MPR121_FDLT = 0x35;
var MPR121_TOUCHTH_0 = 0x41;
var MPR121_RELEASETH_0 = 0x42;
var MPR121_DEBOUNCE = 0x5B;
var MPR121_CONFIG1 = 0x5C;
var MPR121_CONFIG2 = 0x5D;
var MPR121_CHARGECURR_0 = 0x5F;
var MPR121_CHARGETIME_1 = 0x6C;
var MPR121_ECR = 0x5E;
var MPR121_AUTOCONFIG0 = 0x7B;
var MPR121_AUTOCONFIG1 = 0x7C;
var MPR121_UPLIMIT = 0x7D;
var MPR121_LOWLIMIT = 0x7E;
var MPR121_TARGETLIMIT = 0x7F;
var MPR121_GPIODIR = 0x76;
var MPR121_GPIOEN = 0x77;
var MPR121_GPIOSET = 0x78;
var MPR121_GPIOCLR = 0x79;
var MPR121_GPIOTOGGLE = 0x7A;
var MPR121_SOFTRESET = 0x80;
/* Class Constructor
* ==================================================================================================================== */
function MPR121(address, bus_number) {
EventEmitter.call(this);
this.address = address || MPR121_I2CADDR_DEFAULT;
if (bus_number != undefined) {
this.bus_number = bus_number;
} else {
this.bus_number = 1;
}
}
// MPR121 inherits EventEmitter
util.inherits(MPR121, EventEmitter);
// module export
module.exports = MPR121;
/* Variables
* ==================================================================================================================== */
MPR121.prototype._device;
MPR121.prototype.address;
MPR121.prototype.bus_number = "1";
/* Methods
* ==================================================================================================================== */
var _device;
/*
* Initialize communication with the MPR121.
*
* Returns True if communication with the MPR121 was established, otherwise
* returns False.
*/
MPR121.prototype.begin = function() {
this._device = i2c.openSync(this.bus_number);
return this.reset();
}
/*
* get config value
*/
MPR121.prototype.config = function() {
return this._read8Bits(MPR121_CONFIG2);
}
/*
* reset sensor
*/
MPR121.prototype.reset = function(touch, release){
// Soft reset of device.
this._write8Bits(MPR121_SOFTRESET, 0x63);
// Set electrode configuration to default values.
this._write8Bits(MPR121_ECR, 0x00);
//# Check CDT, SFI, ESI configuration is at default values.
var c = this._read8Bits(MPR121_CONFIG2);
if (c != 0x24) {
console.log("MPR121 Error - device not found. Check address, bus and wiring. ("+c+" != 36)")
return false;
}
// Set threshold for touch and release to default values.
this.set_thresholds(12, 6);
// Configure baseline filtering control registers.
this._write8Bits(MPR121_MHDR, 0x01);
this._write8Bits(MPR121_NHDR, 0x01);
this._write8Bits(MPR121_NCLR, 0x0E);
this._write8Bits(MPR121_FDLR, 0x00);
this._write8Bits(MPR121_MHDF, 0x01);
this._write8Bits(MPR121_NHDF, 0x05);
this._write8Bits(MPR121_NCLF, 0x01);
this._write8Bits(MPR121_FDLF, 0x00);
this._write8Bits(MPR121_NHDT, 0x00);
this._write8Bits(MPR121_NCLT, 0x00);
this._write8Bits(MPR121_FDLT, 0x00);
// Set other configuration registers.
this._write8Bits(MPR121_DEBOUNCE, 0);
this._write8Bits(MPR121_CONFIG1, 0x10); // default, 16uA charge current
this._write8Bits(MPR121_CONFIG2, 0x20); // 0.5uS encoding, 1ms period
// Enable all electrodes.
this._write8Bits(MPR121_ECR, 0x8F); // start with first 5 bits of baseline tracking
// All done, everything succeeded!
return true;
}
/*
* Set the touch and release threshold for all inputs to the provided
* values. Both touch and release should be a value between 0 to 255
* (inclusive).
*/
MPR121.prototype.set_thresholds = function(touch, release) {
if (touch < 0 || touch > 255) return false;
if (release < 0 || release > 255) return false;
for (var i = 0; i <= 12; i++) {
this._write8Bits(MPR121_TOUCHTH_0 + 2 * i, touch);
this._write8Bits(MPR121_RELEASETH_0 + 2 * i, release);
}
}
/*
* Return filtered data register value for the provided pin (0-11).
* Useful for debugging.
*/
MPR121.prototype.filtered_data = function(pin) {
if (pin < 0 || pin >= 12) { return false; }
return this._read16Bits(MPR121_FILTDATA_0L + pin*2);
}
/*
* Return baseline data register value for the provided pin (0-11).
* Useful for debugging.
*/
MPR121.prototype.baseline_data = function(pin) {
if (pin < 0 || pin >= 12) { return false; }
var bl = this._read8Bits(MPR121_BASELINE_0 + pin);
return bl << 2;
}
/*
* Return touch state of all pins as a 12-bit value where each bit
* represents a pin, with a value of 1 being touched and 0 not being touched.
*/
MPR121.prototype.touched = function() {
var t = this._read16Bits(MPR121_TOUCHSTATUS_L);
return t & 0x0FFF;
}
/*
* Return True if the specified pin is being touched, otherwise returns
* False.
*/
MPR121.prototype.is_touched = function(pin) {
if (pin < 0 || pin >= 12) { return false; }
var t = this.touched();
return (t & (1 << pin)) > 0;
}
/*
* Several I2C Helpers
*/
MPR121.prototype._read8Bits = function(reg) {
return this._device.readByteSync(this.address, reg);
};
MPR121.prototype._read16Bits = function(reg) {
return this._device.readWordSync(this.address, reg);
};
MPR121.prototype._write8Bits = function(reg, value) {
this._device.writeByteSync(this.address, reg, value & 0xFF);
};