Merge pull request #4931 from gohai/io-changes

Hardware I/O updates
This commit is contained in:
Ben Fry
2017-04-20 14:38:51 -04:00
committed by GitHub
8 changed files with 38 additions and 103 deletions
@@ -8,11 +8,15 @@ void setup() {
// the display can be set to one of these two addresses: 0x3c (default) or 0x3d
// (they might be listed as 0x7a and 0x7b on the circuit board)
oled = new SSD1306(I2C.list()[0], 0x3c);
// you might need to use a different interface on other SBCs
oled = new SSD1306("i2c-1", 0x3c);
}
void draw() {
line(0, 0, 127, 63);
line(0, 63, 127, 0);
oled.sendImage(get());
background(0);
stroke(255);
line(0, 0, 127, 63);
line(0, 63, 127, 0);
oled.sendImage(get());
}
@@ -61,7 +61,7 @@ class SSD1306 extends I2C {
img.loadPixels();
for (int y=startY; y < height && y-startY < 64; y++) {
for (int x=startX; x < width && x-startX < 128; x++) {
if (brightness(img.pixels[y*img.width+x]) < 128) {
if (128 <= brightness(img.pixels[y*img.width+x])) {
// this isn't the normal (scanline) mapping, but 8 pixels below each other at a time
// white pixels have their bit turned on
frame[x + (y/8)*128] |= (1 << (y % 8));
@@ -7,13 +7,13 @@ class MCP3001 extends SPI {
MCP3001(String dev) {
super(dev);
super.settings(500000, SPI.MSBFIRST, SPI.MODE0);
settings(500000, SPI.MSBFIRST, SPI.MODE0);
}
float getAnalog() {
// dummy write, actual values don't matter
byte[] out = { 0, 0 };
byte[] in = super.transfer(out);
byte[] in = transfer(out);
// some input bit shifting according to the datasheet p. 16
int val = ((in[0] & 0x1f) << 5) | ((in[1] & 0xf8) >> 3);
// val is between 0 and 1023
@@ -8,7 +8,7 @@ class MCP3008 extends SPI {
MCP3008(String dev) {
super(dev);
super.settings(500000, SPI.MSBFIRST, SPI.MODE0);
settings(500000, SPI.MSBFIRST, SPI.MODE0);
}
float getAnalog(int channel) {
@@ -19,7 +19,7 @@ class MCP3008 extends SPI {
byte[] out = { 0, 0, 0 };
// encode the channel number in the first byte
out[0] = (byte)(0x18 | channel);
byte[] in = super.transfer(out);
byte[] in = transfer(out);
int val = ((in[1] & 0x03) << 8) | (in[2] & 0xff);
// val is between 0 and 1023
return val/1023.0;
+1 -1
View File
@@ -3,7 +3,7 @@ OBJS := impl.o
CC := gcc
# prefix with -m32 to compile for linux32
CFLAGS := -std=gnu99 -fPIC -g
CFLAGS := -std=gnu99 -fPIC -g -ffast-math
CFLAGS += -I$(shell dirname $(shell realpath $(shell which javac)))/../include
CFLAGS += -I$(shell dirname $(shell realpath $(shell which javac)))/../include/linux
LDFLAGS := -shared
+23 -4
View File
@@ -447,13 +447,32 @@ public class GPIO {
* Waits for the value of an input pin to change
* @param pin GPIO pin
* @param mode what to wait for: GPIO.CHANGE, GPIO.FALLING or GPIO.RISING
* @param timeout don't wait more than timeout milliseconds (-1 waits indefinitely)
* @return true if the interrupt occured, false if the timeout occured
* @webref
*/
public static boolean waitForInterrupt(int pin, int mode, int timeout) {
public static void waitFor(int pin, int mode) {
waitForInterrupt(pin, mode, -1);
}
/**
* Waits for the value of an input pin to change
*
* This function will throw a RuntimeException in case of a timeout.
* @param pin GPIO pin
* @param mode what to wait for: GPIO.CHANGE, GPIO.FALLING or GPIO.RISING
* @param timeout don't wait more than timeout milliseconds
* @webref
*/
public static void waitFor(int pin, int mode, int timeout) {
enableInterrupt(pin, mode);
return waitForInterrupt(pin, timeout);
if (waitForInterrupt(pin, timeout) == false) {
throw new RuntimeException("Timeout occurred");
}
}
public static boolean waitForInterrupt(int pin, int mode, int timeout) {
throw new RuntimeException("The waitForInterrupt function has been renamed to waitFor. Please update your sketch accordingly.");
}
+1 -1
View File
@@ -117,7 +117,7 @@ public class PWM {
// XXX: implicit clear()?
// XXX: also check GPIO
String fn = "/sys/class/pwm/" + chip + "/export";
String fn = "/sys/class/pwm/" + chip + "/unexport";
int ret = NativeInterface.writeFile(fn, Integer.toString(channel));
if (ret < 0) {
if (ret == -2) { // ENOENT
@@ -1,88 +0,0 @@
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Copyright (c) The Processing Foundation 2015
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.io;
/**
* @webref
*/
public class RPI {
/*
* The Raspberry Pi has a 2x20 pin header for connecting various peripherals.
* The following constants describe how the pins of this header correspond
* to the pin numbering used by the CPU and by software ("GPIO pin number").
*
* You can use this class to refer to a pin by its location on the header:
* e.g. GPIO.digitalWrite(RPI.PIN7, GPIO.HIGH)
*
* Alternatively, if you know a pins "true" pin number (GPIO number), you
* can use it directly. The following is equivalent to the example above:
* GPIO.digitalWrite(4, GPIO.HIGH)
*
* PIN1 is located on the "top left" of the column of pins, close to the two
* LEDs. PIN2 is next to it. PIN3 is below PIN1, and it goes on like this.
* See also: http://pi.gadgetoid.com/pinout
*/
public static final int PIN1 = -1; /* 3v3 Power, can source up to 50 mA */
public static final int PIN2 = -1; /* 5v Power, connected to input power */
public static final int PIN3 = 2; /* GPIO 2, also: I2C data */
public static final int PIN4 = -1; /* 5v Power, connected to input power */
public static final int PIN5 = 3; /* GPIO 3, also: I2C clock */
public static final int PIN6 = -1; /* Ground */
public static final int PIN7 = 4; /* GPIO 4 */
public static final int PIN8 = 14; /* GPIO 14, also: Serial TX */
public static final int PIN9 = -1; /* Ground */
public static final int PIN10 = 15; /* GPIO 15, also: Serial RX */
public static final int PIN11 = 17; /* GPIO 17 */
public static final int PIN12 = 18; /* GPIO 18 */
public static final int PIN13 = 27; /* GPIO 27 */
public static final int PIN14 = -1; /* Ground */
public static final int PIN15 = 22; /* GPIO 22 */
public static final int PIN16 = 23; /* GPIO 23 */
public static final int PIN17 = -1; /* 3v3 Power, can source up to 50 mA */
public static final int PIN18 = 24; /* GPIO 24 */
public static final int PIN19 = 10; /* GPIO 10, also: SPI MOSI */
public static final int PIN20 = -1; /* Ground */
public static final int PIN21 = 9; /* GPIO 9, also: SPI MISO */
public static final int PIN22 = 25; /* GPIO 25 */
public static final int PIN23 = 11; /* GPIO 11, also: SPI SCLK */
public static final int PIN24 = 8; /* GPIO 8, also: SPI Chip Select 0 */
public static final int PIN25 = -1; /* Ground */
public static final int PIN26 = 7; /* GPIO 7, also: SPI Chip Select 1 */
public static final int PIN27 = 0; /* GPIO 0, also: HAT I2C data, reserved [currenly not accessible] */
public static final int PIN28 = 1; /* GPIO 1, also HAT I2C data, reserved [currenly not accessible] */
public static final int PIN29 = 5; /* GPIO 5 */
public static final int PIN30 = -1; /* Ground */
public static final int PIN31 = 6; /* GPIO 6 */
public static final int PIN32 = 12; /* GPIO 12 */
public static final int PIN33 = 13; /* GPIO 13 */
public static final int PIN34 = -1; /* Ground */
public static final int PIN35 = 19; /* GPIO 19, also: SPI MISO [currenly not accessible] */
public static final int PIN36 = 16; /* GPIO 16 */
public static final int PIN37 = 26; /* GPIO 26 */
public static final int PIN38 = 20; /* GPIO 20, also: SPI MISO [currenly not accessible] */
public static final int PIN39 = -1; /* Ground */
public static final int PIN40 = 21; /* GPIO 21, also: SPI CLK [currenly not accessible] */
}