Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
32a8fbf
Serial: Add support for opening Raspbian-style named ports
gohai Mar 16, 2018
01ccaf1
Increase the maximum number of lights to 4 for Mesa vc4
gohai Mar 16, 2018
bf4e7a3
ARM Mali: Implement glRenderbufferStorageMultisample for gl3es3
gohai Apr 18, 2018
fada294
ARM Mali: Implement glReadBuffer for gl3es3
gohai Apr 18, 2018
e151628
ARM Mali: Report "es" as part of GLSL versions
gohai Apr 18, 2018
0965370
ARM Mali: Specify precision for _fragColor
gohai Apr 18, 2018
e5ba6a9
ARM Mali: Implement glBlitFramebuffer for gl3es3
gohai Apr 18, 2018
d96aa3a
ARM Mali: Implement gDrawBuffer for gl3es3
gohai Apr 18, 2018
f73f460
ARM Mali: Don't assume anisotropic filtering for gl3es3
gohai Apr 18, 2018
a476177
update to Java 8u172
benfry Apr 23, 2018
ec47beb
IO: Implement Raspbian-only method for setting pull{up,down} resistors
gohai Jun 16, 2018
7cde730
IO: Clarify SimpleInput example
gohai Jun 27, 2018
f7e530a
IO: Speed up GPIO.pinMode()
gohai Jun 25, 2018
dc11272
ARM Mali: Don't use GL_MULTISAMPLE or GL_POLYGON_SMOOTH on ES
gohai Apr 23, 2018
5518417
Unconditionally disable POLYGON_SMOOTH in restoreGL()
gohai Apr 24, 2018
89c3ffa
Enable exporting of Windows applications on ARM
gohai Apr 24, 2018
3b88eeb
IO: Fix crash when I2C.read() was called without prior write()
gohai Jun 28, 2018
657ce85
IO: Add a 100ms timeout to I2C to allow communication with Arduino
gohai Jun 28, 2018
ed6d36d
IO: Reorganize OOP examples
gohai Jun 29, 2018
4e30acd
IO: Add MPR121 capacitive touch example
gohai Jun 29, 2018
afe8609
IO: Add BME280 environmental sensor example
Jun 30, 2018
aea3f99
IO: Add TSL2561 environmental sensor example
Jun 30, 2018
961505d
IO: Add PCA9685 servo/PWM controller example
Jun 30, 2018
841f942
IO: Remove the duplicate "duty" argument from the reference for PWM.s…
gohai Jul 1, 2018
ab1b958
IO: Rename ServoSweep example
gohai Jul 1, 2018
2e1eccd
IO: Make I2C errors more verbose
gohai Jul 10, 2018
c1d9528
IO: Fix pinMode() retry logic
gohai Jul 11, 2018
05d67ba
IO: Add a dispose method to PCA9685 example, document pulse widths
gohai Jul 12, 2018
780cc16
OpenGL ES: Fix GLSL version number for 1.00 (#5582)
gohai Jul 20, 2018
8efa774
Update Java to 8u181
gohai Jul 22, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
IO: Add MPR121 capacitive touch example
  • Loading branch information
gohai committed Jul 1, 2018
commit 4e30acd17642d5633f27b8b54d5e488934e745e0
112 changes: 112 additions & 0 deletions java/libraries/io/examples/Touch_I2C_MPR121/MPR121.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import processing.io.I2C;

// MPR121 is a capacitive-touch sensor controller with 12 channels
// datasheet: https://www.nxp.com/docs/en/data-sheet/MPR121.pdf

class MPR121 extends I2C {
int address;
int touched;

// registers used (there are more)
static final int EFD0LB = 0x04; // ELE0 Electrode Filtered Data LSB
static final int E0TTH = 0x41; // ELE0 Touch Threshold
static final int E0RTH = 0x42; // ELE0 Release Threshold
static final int E0BV = 0x1e; // ELE0 Baseline Value
static final int MHDR = 0x2b; // MHD Rising
static final int NHDR = 0x2c; // NHD Amount Rising
static final int NCLR = 0x2d; // NCL Rising
static final int MHDF = 0x2f; // MHD Falling
static final int NHDF = 0x30; // NHD Amount Falling
static final int NCLF = 0x31; // NCL Falling
static final int CDT = 0x5d; // Filter/Global CDT Configuration
static final int ECR = 0x5e; // Electrode Configuration
static final int SRST = 0x80; // Soft Reset

// there can be more than one device connected to the bus
// as long as they have different addresses
// possible addresses: 0x5a (default) - 0x5d
MPR121(String dev, int address) {
super(dev);
this.address = address;
reset();
}

void update() {
beginTransmission(address);
write(0x00);
byte[] in = read(2);
// & 0xff makes sure the byte is not interpreted as a negative value
touched = (in[1] & 0xff) << 8 | (in[0] & 0xff);
}

boolean touched(int channel) {
if (channel < 0 || 11 < channel) {
return false;
}
if ((touched & (1 << channel)) != 0) {
return true;
} else {
return false;
}
}

void threshold(int touch, int release) {
for (int i=0; i < 12; i++) {
threshold(touch, release, i);
}
}

void threshold(int touch, int release, int channel) {
if (channel < 0 || 11 < channel) {
return;
}
touch = constrain(touch, 0, 255);
release = constrain(release, 0, 255);
writeRegister(E0TTH + 2*channel, touch);
writeRegister(E0RTH + 2*channel, release);
}

int analogRead(int channel) {
if (channel < 0 || 11 < channel) {
return 0;
}
beginTransmission(address);
write(EFD0LB + 2*channel);
byte[] in = read(2);
return (in[1] & 0xff) << 8 | (in[0] & 0xff);
}

int analogReadBaseline(int channel) {
if (channel < 0 || 11 < channel) {
return 0;
}
beginTransmission(address);
write(E0BV + channel);
byte[] in = read(1);
return (in[0] & 0xff) << 2;
}

void reset() {
writeRegister(SRST, 0x63);
delay(1);
threshold(12, 6);
// set baseline filtering control registers (see p. 12)
writeRegister(MHDR, 0x01);
writeRegister(NHDR, 0x01);
writeRegister(NCLR, 0x0e);
writeRegister(MHDF, 0x01);
writeRegister(NHDF, 0x05);
writeRegister(NCLF, 0x01);
// change sample interval to 1ms period from default 16ms
writeRegister(CDT, 0x20);
// start sampling
writeRegister(ECR, 0x8f);
}

void writeRegister(int register, int value) {
beginTransmission(address);
write(register);
write(value);
endTransmission();
}
}
26 changes: 26 additions & 0 deletions java/libraries/io/examples/Touch_I2C_MPR121/Touch_I2C_MPR121.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import processing.io.*;
MPR121 touch;

// see setup.png in the sketch folder for wiring details

void setup() {
size(600, 200);
//printArray(I2C.list());
touch = new MPR121("i2c-1", 0x5a);
}

void draw() {
background(204);
noStroke();

touch.update();

for (int i=0; i < 12; i++) {
if (touch.touched(i)) {
fill(255, 0, 0);
} else {
fill(255, 255, 255);
}
ellipse((width/12) * (i+0.5), height/2, 20, 20);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.