Skip to content

Commit 6919bef

Browse files
authored
Merge pull request processing#4931 from gohai/io-changes
Hardware I/O updates
2 parents d560087 + 8b4373b commit 6919bef

8 files changed

Lines changed: 38 additions & 103 deletions

File tree

java/libraries/io/examples/I2CScreen/I2CScreen.pde

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ void setup() {
88

99
// the display can be set to one of these two addresses: 0x3c (default) or 0x3d
1010
// (they might be listed as 0x7a and 0x7b on the circuit board)
11-
oled = new SSD1306(I2C.list()[0], 0x3c);
11+
12+
// you might need to use a different interface on other SBCs
13+
oled = new SSD1306("i2c-1", 0x3c);
1214
}
1315

1416
void draw() {
15-
line(0, 0, 127, 63);
16-
line(0, 63, 127, 0);
17-
oled.sendImage(get());
17+
background(0);
18+
stroke(255);
19+
line(0, 0, 127, 63);
20+
line(0, 63, 127, 0);
21+
oled.sendImage(get());
1822
}

java/libraries/io/examples/I2CScreen/SSD1306.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class SSD1306 extends I2C {
6161
img.loadPixels();
6262
for (int y=startY; y < height && y-startY < 64; y++) {
6363
for (int x=startX; x < width && x-startX < 128; x++) {
64-
if (brightness(img.pixels[y*img.width+x]) < 128) {
64+
if (128 <= brightness(img.pixels[y*img.width+x])) {
6565
// this isn't the normal (scanline) mapping, but 8 pixels below each other at a time
6666
// white pixels have their bit turned on
6767
frame[x + (y/8)*128] |= (1 << (y % 8));

java/libraries/io/examples/SPIAnalogDigitalOOP/MCP3001.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class MCP3001 extends SPI {
77

88
MCP3001(String dev) {
99
super(dev);
10-
super.settings(500000, SPI.MSBFIRST, SPI.MODE0);
10+
settings(500000, SPI.MSBFIRST, SPI.MODE0);
1111
}
1212

1313
float getAnalog() {
1414
// dummy write, actual values don't matter
1515
byte[] out = { 0, 0 };
16-
byte[] in = super.transfer(out);
16+
byte[] in = transfer(out);
1717
// some input bit shifting according to the datasheet p. 16
1818
int val = ((in[0] & 0x1f) << 5) | ((in[1] & 0xf8) >> 3);
1919
// val is between 0 and 1023

java/libraries/io/examples/SPIAnalogDigitalOOP8/MCP3008.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class MCP3008 extends SPI {
88

99
MCP3008(String dev) {
1010
super(dev);
11-
super.settings(500000, SPI.MSBFIRST, SPI.MODE0);
11+
settings(500000, SPI.MSBFIRST, SPI.MODE0);
1212
}
1313

1414
float getAnalog(int channel) {
@@ -19,7 +19,7 @@ class MCP3008 extends SPI {
1919
byte[] out = { 0, 0, 0 };
2020
// encode the channel number in the first byte
2121
out[0] = (byte)(0x18 | channel);
22-
byte[] in = super.transfer(out);
22+
byte[] in = transfer(out);
2323
int val = ((in[1] & 0x03) << 8) | (in[2] & 0xff);
2424
// val is between 0 and 1023
2525
return val/1023.0;

java/libraries/io/src/native/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ OBJS := impl.o
33
CC := gcc
44

55
# prefix with -m32 to compile for linux32
6-
CFLAGS := -std=gnu99 -fPIC -g
6+
CFLAGS := -std=gnu99 -fPIC -g -ffast-math
77
CFLAGS += -I$(shell dirname $(shell realpath $(shell which javac)))/../include
88
CFLAGS += -I$(shell dirname $(shell realpath $(shell which javac)))/../include/linux
99
LDFLAGS := -shared

java/libraries/io/src/processing/io/GPIO.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,32 @@ public static void releasePin(int pin) {
447447
* Waits for the value of an input pin to change
448448
* @param pin GPIO pin
449449
* @param mode what to wait for: GPIO.CHANGE, GPIO.FALLING or GPIO.RISING
450-
* @param timeout don't wait more than timeout milliseconds (-1 waits indefinitely)
451-
* @return true if the interrupt occured, false if the timeout occured
452450
* @webref
453451
*/
454-
public static boolean waitForInterrupt(int pin, int mode, int timeout) {
452+
public static void waitFor(int pin, int mode) {
453+
waitForInterrupt(pin, mode, -1);
454+
}
455+
456+
457+
/**
458+
* Waits for the value of an input pin to change
459+
*
460+
* This function will throw a RuntimeException in case of a timeout.
461+
* @param pin GPIO pin
462+
* @param mode what to wait for: GPIO.CHANGE, GPIO.FALLING or GPIO.RISING
463+
* @param timeout don't wait more than timeout milliseconds
464+
* @webref
465+
*/
466+
public static void waitFor(int pin, int mode, int timeout) {
455467
enableInterrupt(pin, mode);
456-
return waitForInterrupt(pin, timeout);
468+
if (waitForInterrupt(pin, timeout) == false) {
469+
throw new RuntimeException("Timeout occurred");
470+
}
471+
}
472+
473+
474+
public static boolean waitForInterrupt(int pin, int mode, int timeout) {
475+
throw new RuntimeException("The waitForInterrupt function has been renamed to waitFor. Please update your sketch accordingly.");
457476
}
458477

459478

java/libraries/io/src/processing/io/PWM.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void close() {
117117
// XXX: implicit clear()?
118118
// XXX: also check GPIO
119119

120-
String fn = "/sys/class/pwm/" + chip + "/export";
120+
String fn = "/sys/class/pwm/" + chip + "/unexport";
121121
int ret = NativeInterface.writeFile(fn, Integer.toString(channel));
122122
if (ret < 0) {
123123
if (ret == -2) { // ENOENT

java/libraries/io/src/processing/io/RPI.java

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)