Skip to content

Commit 7a274b0

Browse files
committed
Adds support for optional RESET pin
1 parent e5ac9fc commit 7a274b0

File tree

7 files changed

+135
-46
lines changed

7 files changed

+135
-46
lines changed

README.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ The CAP1188 is an 8-channel capacitive touch sensor and is available in a handy
66

77
You can wire this up as follows:
88

9-
| Device Pin | Pi |
10-
| ---------- | ------------------------------------------------- |
11-
| 1 (GND) | [Ground](https://pinout.xyz/pinout/ground) |
12-
| 2 (VIN) | [3.3v](https://pinout.xyz/pinout/pin1_3v3_power) |
13-
| 3 (SDA) | [BCM 2/SDA](https://pinout.xyz/pinout/pin3_gpio2) |
14-
| 4 (SCK) | [BCM 3/SCL](https://pinout.xyz/pinout/pin5_gpio3) |
9+
| Device Pin | Pi |
10+
| ---------- | --------------------------------------------------------------- |
11+
| 1 (GND) | [Ground](https://pinout.xyz/pinout/ground) |
12+
| 2 (VIN) | [3.3v](https://pinout.xyz/pinout/pin1_3v3_power) |
13+
| 3 (SDA) | [BCM 2/SDA](https://pinout.xyz/pinout/pin3_gpio2) |
14+
| 4 (SCK) | [BCM 3/SCL](https://pinout.xyz/pinout/pin5_gpio3) |
15+
| RST | [BCM 17 (0)](https://pinout.xyz/pinout/pin11_gpio17) Optional\* |
16+
17+
* Any GPIO pin can be used, see [Reset]() below.
1518

1619
## Install
1720

@@ -50,6 +53,21 @@ connect().then(cap1188 => {
5053

5154
See the [`examples`](./examples) directory for more examples.
5255

56+
### Reset
57+
58+
Optionally, you can connect the reset pin on the board (marked RST), to a pin on the Pi. Call the `reset()` method to reinitialize the sensor which can recalibrate it instead of having to cycle the power. A Promise is returned that resolves when the board has been reset.
59+
60+
See [this page about how to specify a pin](https://github.com/nebrius/raspi-gpio#pin-naming).
61+
62+
In addition, the `reset` event is also emitted.
63+
64+
```
65+
var cap = require("CAP1188").connect(I2C1, { resetPin: 0 }); // using WiringPi number for BCM17
66+
cap.reset(function () {
67+
// the board has been reset
68+
});
69+
```
70+
5371
## GUI
5472

5573
There's a tiny GUI you can run:

examples/cap1188-events.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ const connect = require("../").connect;
33
// Connect to the CAP1188 breakout.
44
// Returns a promise that resolves to the
55
// CAP1188 instance when it's ready.
6-
const connectionPromise = connect();
6+
const connectionPromise = connect({ resetPin: 0 });
77

88
// When it's ready, add a listener that emits
99
// when a touch is registered
1010
connectionPromise.then(cap1188 => {
1111
cap1188.on("change", function(evt) {
1212
console.log(evt);
1313
});
14-
});
14+
15+
cap1188.on("reset", function(evt) {
16+
console.log("reset");
17+
});
18+
19+
cap1188.reset();
20+
}, console.error);

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const init = require('raspi').init;
2-
const I2C = require('raspi-i2c').I2C;
1+
const init = require("raspi").init;
2+
const I2C = require("raspi-i2c").I2C;
33

4-
const connect = require('./lib/CAP1188').connect;
4+
const connect = require("./lib/CAP1188").connect;
55

6-
module.exports.connect = ({ i2c = new I2C() } = {}) => {
6+
module.exports.connect = ({ i2c = new I2C(), ...opts } = {}) => {
77
return new Promise((resolve, reject) => {
88
init(() => {
9-
const cap1188 = connect(i2c);
9+
const cap1188 = connect(i2c, opts);
1010
resolve(cap1188);
1111
});
1212
});

lib/CAP1188.js

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
http://www.espruino.com/CAP1188
99
*/
1010
const EventEmitter = require("events").EventEmitter;
11+
const gpio = require("raspi-gpio");
12+
const createPromiseTimeout = require("./createPromiseTimeout");
1113

1214
var C = {
1315
ADDRESS_DEFAULT: 0x29
@@ -40,13 +42,24 @@ class CAP1188 extends EventEmitter {
4042
this.addr = _opts;
4143
} else {
4244
this.addr = _opts.address || C.ADDRESS_DEFAULT;
43-
this.resetPin = _opts.resetPin || null;
45+
this.resetPin =
46+
_opts.resetPin != null ? new gpio.DigitalOutput(_opts.resetPin) : null;
4447
}
4548

4649
this.pollingIntervalMs = _opts.pollingIntervalMs || 500;
4750
this.pollingId = null;
4851
this.lastTouches = [false, false, false, false, false, false, false, false];
4952

53+
this.scheduleTouchPoll = this.scheduleTouchPoll.bind(this);
54+
55+
// Begin polling touches when a "change" listener is
56+
// registered
57+
this.on("newListener", function(eventName) {
58+
if (eventName === "change" && this.pollingId == null) {
59+
this.scheduleTouchPoll();
60+
}
61+
});
62+
5063
this.initialize();
5164
}
5265

@@ -56,19 +69,13 @@ class CAP1188 extends EventEmitter {
5669
this.multipleTouches(true);
5770
// "Speed up a bit"
5871
this.i2c.writeSync(this.addr, R.STANDBY_CONFIG, Buffer.from([0x30]));
59-
60-
this.scheduleTouchPoll = this.scheduleTouchPoll.bind(this);
61-
62-
// Begin polling touches when a listener is
63-
// registered
64-
this.once("newListener", this.scheduleTouchPoll);
6572
}
6673

6774
/* Begins polling for touches and
6875
emits events when they change */
6976
scheduleTouchPoll() {
7077
this.readTouchesAndEmit();
71-
setTimeout(this.scheduleTouchPoll, this.pollingIntervalMs);
78+
this.pollingId = setTimeout(this.scheduleTouchPoll, this.pollingIntervalMs);
7279
}
7380

7481
readTouchesAndEmit() {
@@ -129,6 +136,42 @@ class CAP1188 extends EventEmitter {
129136
return touches;
130137
}
131138

139+
/* Reset the chip if a reset pin has been specified */
140+
reset() {
141+
const delay = 100;
142+
const pin = this.resetPin;
143+
const self = this;
144+
145+
if (pin == null) {
146+
throw new Error("CAP1188 reset called but no resetPin given");
147+
}
148+
149+
const timeout = createPromiseTimeout(delay);
150+
151+
const writeHigh = function() {
152+
pin.write(gpio.HIGH);
153+
};
154+
155+
const writeLow = function() {
156+
pin.write(gpio.LOW);
157+
};
158+
159+
return new Promise(function(resolve, reject) {
160+
writeLow();
161+
timeout()
162+
.then(writeHigh)
163+
.then(timeout)
164+
.then(writeLow)
165+
.then(timeout)
166+
.then(function() {
167+
self.initialize();
168+
self.emit("reset");
169+
resolve();
170+
})
171+
.catch(reject);
172+
});
173+
}
174+
132175
/* */
133176
getBit(byt, position) {
134177
return 1 == ((byt >> position) & 1);

lib/createPromiseTimeout.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function(delay) {
2+
return function() {
3+
return new Promise(function(resolve) {
4+
setTimeout(resolve, delay);
5+
});
6+
};
7+
};

package-lock.json

Lines changed: 30 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"name": "raspi-cap",
33
"version": "0.0.2",
4-
"description":
5-
"Raspberry Pi / Node.js module for the CAP1188 capacitive touch breakout ",
4+
"description": "Raspberry Pi / Node.js module for the CAP1188 capacitive touch breakout ",
65
"main": "index.js",
76
"bin": {
87
"raspi-cap-debug": "./bin/cap1188"
@@ -14,12 +13,19 @@
1413
"bugs": {
1514
"url": "https://github.com/andrewn/raspi-cap/issues"
1615
},
17-
"os": ["linux"],
18-
"keywords": ["raspberry pi", "cap1188", "touch"],
16+
"os": [
17+
"linux"
18+
],
19+
"keywords": [
20+
"raspberry pi",
21+
"cap1188",
22+
"touch"
23+
],
1924
"author": "Andrew Nicolaou <[email protected]>",
2025
"license": "Apache-2.0",
2126
"dependencies": {
2227
"raspi": "^5.0.2",
28+
"raspi-gpio": "^5.0.0",
2329
"raspi-i2c": "^6.1.0"
2430
}
2531
}

0 commit comments

Comments
 (0)