Skip to content

Instantly share code, notes, and snippets.

@andrewn
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewn/7800a42917f753b0fcb9 to your computer and use it in GitHub Desktop.
Save andrewn/7800a42917f753b0fcb9 to your computer and use it in GitHub Desktop.
exports = {};
(function () {
/* Copyright (c) 2014 Your Name. See the file LICENSE for copying permission. */
/*
Module for Adafruit CAP1188 8-Key Capacitive Touch Sensor Breakout
Only I2C is supported.
See: https://www.adafruit.com/products/1602
Bit writing code from: http://www.espruino.com/modules/MPU6050.js
*/
var C = {
ADDRESS_DEFAULT : 0x29
};
/* Register addresses*/
var R = {
MAIN_CONTROL : 0x00,
SENSOR_INPUT_STATUS : 0x03,
MULTI_TOUCH_CONFIG : 0x2a,
STANDBY_CONFIG : 0x41,
SENSOR_INPUT_LINKING : 0x72,
LED_OUTPUT_CONTROL : 0x74
};
/* Bits within register for functions */
var B = {
MAIN_CONTROL_INT : 0x0
};
function CAP1188(_i2c, _addr) {
this.i2c = _i2c;
this.addr = (undefined===_addr) ? C.ADDRESS_DEFAULT : _addr;
this.initialize();
}
/* Initialize the chip */
CAP1188.prototype.initialize = function() {
this.linkLedsToSensors();
this.multipleTouches(true);
// "Speed up a bit"
this.i2c.writeTo(this.addr, [R.STANDBY_CONFIG, 0x30]);
};
/* Turn on all LEDs */
// CAP1188.prototype.turnOnLeds = function() {
// for(var i = 0; i < 8; i++) {
// this.led(i, 1);
// }
// };
/* Turn on an LED */
// CAP1188.prototype.led = function(num, enable) {
// this.writeBit(R.LED_OUTPUT_CONTROL, num, enable);
// };
/* How many simultaneous touches to allow */
CAP1188.prototype.multipleTouches = function(enable) {
// 1 will block multiple touches
this.writeBit(R.MULTI_TOUCH_CONFIG, 7, enable ? 0 : 1);
};
/* Link the LED to corresponding sensor */
CAP1188.prototype.linkLedsToSensors = function() {
for(var i = 0; i < 8; i++) {
this.linkLedToSensor(i, 1);
}
};
/* Link LED pin to sensor */
CAP1188.prototype.linkLedToSensor = function(num, enable) {
this.writeBit(R.SENSOR_INPUT_LINKING, num, enable);
};
/* Read state of all sensors */
CAP1188.prototype.readTouches = function() {
var touches = [],
raw;
this.i2c.writeTo(this.addr, R.SENSOR_INPUT_STATUS);
raw = this.i2c.readFrom(this.addr, 1)[0];
if (raw) {
// Clear interrupt to be able to read again
this.writeBit(R.MAIN_CONTROL, B.MAIN_CONTROL_INT, 0);
}
for(var i = 0; i < 8; i++) {
touches[i] = this.getBit(raw, i);
}
return touches;
};
/* */
CAP1188.prototype.getBit = function(byt, position) {
return (1 == ((byt >> position) & 1));
};
/* Set a single bit in a register */
CAP1188.prototype.writeBit = function(reg, bit, val) {
this.i2c.writeTo(this.addr, reg);
var b = this.i2c.readFrom(this.addr, 1)[0];
b = (val !== 0) ? (b | (1 << bit)) : (b & ~(1 << bit));
this.i2c.writeTo(this.addr, [reg, b]);
};
/* Set more bits in a register */
CAP1188.prototype.writeBits = function(reg, shift, val) {
this.i2c.writeTo(this.addr, reg);
var b = this.i2c.readFrom(this.addr, 1)[0];
b = b | (val << shift);
this.i2c.writeTo(this.addr, [reg, b]);
};
exports.connect = function (_i2c,_addr) {
return new CAP1188(_i2c,_addr);
};
})();
// This flag is enabled after a timeout
var shouldEmit = false,
shouldEmitTimeout = 2000;
//
// Generic helper methods
//
function emit(msg) {
if (shouldEmit) {
USB.println( JSON.stringify(msg) );
}
// schedule another timeout?
}
//
// Distance sensor (HC-SR04)
//
// var sensor = require("HC-SR04").connect(/* trig */ A0, /* echo */ A1, function(dist) {
// emit({ type: 'distance', value: dist, unit: 'cm' });
// });
// Setup I2C
I2C1.setup({scl:B6,sda:B7});
//
// Digital accelerometer and gyro (MPU6050)
//
// var mpu = require("MPU6050").connect(I2C1);
//
// Capacitive breakout (CAP1188)
//
var cap = exports.connect(I2C1);
var isOn = false;
setInterval(function () {
// Distance sensor
if (sensor) {
sensor.trigger();
}
// Capacitive
if (cap) {
emit({ type: 'cap', unit: 'touched', pins: cap.readTouches() });
}
// Gyro
if (mpu) {
emit({ type: 'accel', unit: 'raw', xyz : mpu.getAcceleration() });
emit({ type: 'gyro', unit: 'deg', xyz : mpu.getDegreesPerSecond() });
}
// Blink on-board LED
isOn = !isOn; LED2.write(isOn);
}, 500);
setTimeout(function () { shouldEmit = true; }, shouldEmitTimeout);
exports = {};
(function () {
/* Copyright (c) 2014 Your Name. See the file LICENSE for copying permission. */
/*
Module for Adafruit CAP1188 8-Key Capacitive Touch Sensor Breakout
Only I2C is supported.
See: https://www.adafruit.com/products/1602
Bit writing code from: http://www.espruino.com/modules/MPU6050.js
*/
var C = {
ADDRESS_DEFAULT : 0x29
};
/* Register addresses*/
var R = {
MAIN_CONTROL : 0x00,
SENSOR_INPUT_STATUS : 0x03,
MULTI_TOUCH_CONFIG : 0x2a,
STANDBY_CONFIG : 0x41,
SENSOR_INPUT_LINKING : 0x72,
LED_OUTPUT_CONTROL : 0x74
};
/* Bits within register for functions */
var B = {
MAIN_CONTROL_INT : 0x0
};
function CAP1188(_i2c, _addr) {
this.i2c = _i2c;
this.addr = (undefined===_addr) ? C.ADDRESS_DEFAULT : _addr;
this.initialize();
}
/* Initialize the chip */
CAP1188.prototype.initialize = function() {
this.linkLedsToSensors();
this.multipleTouches(true);
// "Speed up a bit"
this.i2c.writeTo(this.addr, [R.STANDBY_CONFIG, 0x30]);
};
/* How many simultaneous touches to allow */
CAP1188.prototype.multipleTouches = function(enable) {
// 1 will block multiple touches
this.writeBit(R.MULTI_TOUCH_CONFIG, 7, enable ? 0 : 1);
};
/* Link the LED to corresponding sensor */
CAP1188.prototype.linkLedsToSensors = function() {
for(var i = 0; i < 8; i++) {
this.linkLedToSensor(i, 1);
}
};
/* Link LED pin to sensor */
CAP1188.prototype.linkLedToSensor = function(num, enable) {
this.writeBit(R.SENSOR_INPUT_LINKING, num, enable);
};
/* Read state of all sensors */
CAP1188.prototype.readTouches = function() {
var touches = [],
raw;
this.i2c.writeTo(this.addr, R.SENSOR_INPUT_STATUS);
raw = this.i2c.readFrom(this.addr, 1)[0];
if (raw) {
// Clear interrupt to be able to read again
this.writeBit(R.MAIN_CONTROL, B.MAIN_CONTROL_INT, 0);
}
for(var i = 0; i < 8; i++) {
touches[i] = this.getBit(raw, i);
}
return touches;
};
/* Get a single bit */
CAP1188.prototype.getBit = function(byt, position) {
return (1 == ((byt >> position) & 1));
};
/* Set a single bit in a register */
CAP1188.prototype.writeBit = function(reg, bit, val) {
this.i2c.writeTo(this.addr, reg);
var b = this.i2c.readFrom(this.addr, 1)[0];
b = (val !== 0) ? (b | (1 << bit)) : (b & ~(1 << bit));
this.i2c.writeTo(this.addr, [reg, b]);
};
/* Set more bits in a register */
CAP1188.prototype.writeBits = function(reg, shift, val) {
this.i2c.writeTo(this.addr, reg);
var b = this.i2c.readFrom(this.addr, 1)[0];
b = b | (val << shift);
this.i2c.writeTo(this.addr, [reg, b]);
};
exports.connect = function (_i2c,_addr) {
return new CAP1188(_i2c,_addr);
};
})();
var isOn = false;
setInterval(function () {
// Blink on-board LED
isOn = !isOn; LED2.write(isOn);
}, 500);
~/Projects/oss/ucl-sensors/sensorama(master) » espruinotool --verbose --port /dev/cu.usbmodem1411 /Users/andrew/Projects/oss/ucl-sensors/sensorama/espruino/source.js
espruino-tools
--------------
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/libs/targz.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/espruino.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/codeWriter.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/config.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/env.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/flasher.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/modules.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/notifications.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/serial_audio.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/serial_chrome.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/serial_chrome_pre_m33.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/serial_nodeserial.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/serial_socket.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/settingsAbout.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/status.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/terminal.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/core/utils.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/_examplePlugin.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/assembler.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/boardJSON.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/compiler.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/getGitHub.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/minify.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/npmModules.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/setTime.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/uiMode.js
Found /Users/andrew/Projects/oss/ucl-sensors/espruino-tools/EspruinoTools/plugins/versionChecker.js
Initialising CodeWriter
Initialising Config
Initialising Env
Initialising Flasher
Initialising Modules
Initialising Notifications
Initialising Serial
Initialising SettingsAbout
Initialising Status
Initialising Terminal
Initialising Utils
Initialising ExamplePlugin
Initialising Assembler
Initialising BoardJSON
Initialising Compiler
Initialising GetGitHub
Initialising Minify
Initialising NPMModules
Initialising UiMode
Initialising VersionChecker
Connecting to '/dev/cu.usbmodem1411'
Connected
Found a prompt... good!
--]_|_|_|___|
--] |_| http://espruino.com
--] 1v78 Copyright 2015 G.Williams
--]
Sending... exports = {};
(function () {
Copyright (c) 2014 Your Name. See the file LICENSE for copying permission. */
Module for Adafruit CAP1188 8-Key Capacitive Touch Sensor Breakout
Only I2C is supported.
See: https://www.adafruit.com/products/1602
Bit writing code from: http://www.espruino.com/modules/MPU6050.js
ar C = {
ADDRESS_DEFAULT : 0x29
;
Register addresses*/
ar R = {
MAIN_CONTROL : 0x00,
SENSOR_INPUT_STATUS : 0x03,
MULTI_TOUCH_CONFIG : 0x2a,
STANDBY_CONFIG : 0x41,
SENSOR_INPUT_LINKING : 0x72,
LED_OUTPUT_CONTROL : 0x74
;
Bits within register for functions */
ar B = {
MAIN_CONTROL_INT : 0x0
;
unction CAP1188(_i2c, _addr) {
this.i2c = _i2c;
this.addr = (undefined===_addr) ? C.ADDRESS_DEFAULT : _addr;
this.initialize();
Initialize the chip */
AP1188.prototype.initialize = function() {
this.linkLedsToSensors();
this.multipleTouches(true);
// "Speed up a bit"
this.i2c.writeTo(this.addr, [R.STANDBY_CONFIG, 0x30]);
;
How many simultaneous touches to allow */
AP1188.prototype.multipleTouches = function(enable) {
// 1 will block multiple touches
this.writeBit(R.MULTI_TOUCH_CONFIG, 7, enable ? 0 : 1);
;
Link the LED to corresponding sensor */
AP1188.prototype.linkLedsToSensors = function() {
for(var i = 0; i < 8; i++) {
this.linkLedToSensor(i, 1);
}
;
Link LED pin to sensor */
AP1188.prototype.linkLedToSensor = function(num, enable) {
this.writeBit(R.SENSOR_INPUT_LINKING, num, enable);
;
Read state of all sensors */
AP1188.prototype.readTouches = function() {
var touches = [],
raw;
this.i2c.writeTo(this.addr, R.SENSOR_INPUT_STATUS);
raw = this.i2c.readFrom(this.addr, 1)[0];
if (raw) {
// Clear interrupt to be able to read again
this.writeBit(R.MAIN_CONTROL, B.MAIN_CONTROL_INT, 0);
}
for(var i = 0; i < 8; i++) {
touches[i] = this.getBit(raw, i);
}
return touches;
;
Get a single bit */
AP1188.prototype.getBit = function(byte, position) {
return (1 == ((byte >> position) & 1));
;
Set a single bit in a register */
AP1188.prototype.writeBit = function(reg, bit, val) {
this.i2c.writeTo(this.addr, reg);
var b = this.i2c.readFrom(this.addr, 1)[0];
b = (val !== 0) ? (b | (1 << bit)) : (b & ~(1 << bit));
this.i2c.writeTo(this.addr, [reg, b]);
;
Set more bits in a register */
AP1188.prototype.writeBits = function(reg, shift, val) {
this.i2c.writeTo(this.addr, reg);
var b = this.i2c.readFrom(this.addr, 1)[0];
b = b | (val << shift);
this.i2c.writeTo(this.addr, [reg, b]);
;
xports.connect = function (_i2c,_addr) {
return new CAP1188(_i2c,_addr);
;
)();
var isOn = false;
setInterval(function () {
// Blink on-board LED
isOn = !isOn; LED2.write(isOn);
, 500);
~/Projects/oss/ucl-sensors/sensorama(master) »
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment