I/O: Add examples

This commit is contained in:
gohai
2015-10-12 00:11:55 +02:00
parent 59e05393a5
commit bc02e4b7b6
11 changed files with 275 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
import processing.io.*;
I2C i2c;
// HMC6352 is a digital compass module using I2C
// datasheet: https://www.sparkfun.com/datasheets/Components/HMC6352.pdf
void setup() {
//printArray(I2C.list());
i2c = new I2C(I2C.list()[0]);
setHeadingMode();
}
void draw() {
background(255);
float deg = getHeading();
println(deg + " degrees");
line(width/2, height/2, width/2+sin(radians(deg))*width/2, height/2-cos(radians(deg))*height/2);
}
void setHeadingMode() {
i2c.beginTransmission(0x21);
// command byte for writing to EEPROM
i2c.write((byte) 0x77);
// address of the output data control byte
i2c.write((byte) 0x4e);
// give us the plain heading
i2c.write((byte) 0x00);
i2c.endTransmission();
}
float getHeading() {
i2c.beginTransmission(0x21);
// command byte for reading the data
i2c.write((byte) 0x41);
byte[] in = i2c.read(2);
i2c.endTransmission();
// put bytes together to tenth of degrees
// & 0xff makes sure the byte is not interpreted as a negative value
int deg = (in[0] & 0xff) << 8 | (in[1] & 0xff);
// return degrees
return deg / 10.0;
}

View File

@@ -0,0 +1,27 @@
import processing.io.*;
I2C i2c;
// MCP4725 is a Digital-to-Analog converter using I2C
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/22039d.pdf
void setup() {
//printArray(I2C.list());
i2c = new I2C(I2C.list()[0]);
}
void draw() {
background(map(mouseX, 0, width, 0, 255));
setAnalog(map(mouseX, 0, width, 0.0, 1.0));
}
// outputs voltages from 0V to the supply voltage
// (works with 3.3V and 5V)
void setAnalog(float fac) {
fac = constrain(fac, 0.0, 1.0);
// convert to 12 bit value
int val = int(4095 * fac);
i2c.beginTransmission(0x60);
i2c.write((byte) (val >> 8));
i2c.write((byte) (val & 255));
i2c.endTransmission();
}

View File

@@ -0,0 +1,12 @@
import processing.io.*;
MCP4725 dac;
void setup() {
//printArray(I2C.list());
dac = new MCP4725(I2C.list()[0], 0x60);
}
void draw() {
background(map(mouseX, 0, width, 0, 255));
dac.setAnalog(map(mouseX, 0, width, 0.0, 1.0));
}

View File

@@ -0,0 +1,27 @@
import processing.io.I2C;
// MCP4725 is a Digital-to-Analog converter using I2C
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/22039d.pdf
class MCP4725 extends I2C {
int address;
// there can be more than one device connected to the bus
// as long as they have different addresses
MCP4725(String dev, int address) {
super(dev);
this.address = address;
}
// outputs voltages from 0V to the supply voltage
// (works with 3.3V and 5V)
void setAnalog(float fac) {
fac = constrain(fac, 0.0, 1.0);
// convert to 12 bit value
int val = int(4095 * fac);
beginTransmission(address);
write((byte) (val >> 8));
write((byte) (val & 255));
endTransmission();
}
}

View File

@@ -0,0 +1,25 @@
import processing.io.*;
color bgcolor = 0;
// RPI.PIN3 refers to the physical pin 3 on the Raspberry Pi's
// pin header, which is located on the third row, next to a
// Ground pin
void setup() {
GPIO.pinMode(RPI.PIN3, GPIO.INPUT);
GPIO.attachInterrupt(RPI.PIN3, this, "pinEvent", GPIO.RISING);
}
void draw() {
background(bgcolor);
}
// this function will be called whenever pin 3 is brought from LOW to HIGH
void pinEvent(int pin) {
println("Received interrupt");
if (bgcolor == 0) {
bgcolor = color(255);
} else {
bgcolor = color(0);
}
}

View File

@@ -0,0 +1,39 @@
import processing.io.*;
LED leds[];
// the Raspberry Pi has two build-in LEDs we can control
// led0 (green) and led1 (red)
void setup() {
String available[] = LED.list();
print("Available: ");
println(available);
// create an object for each LED and store it in an array
leds = new LED[available.length];
for (int i=0; i < available.length; i++) {
leds[i] = new LED(available[i]);
}
frameRate(1);
}
void draw() {
// make the leds count in binary
for (int i=0; i < leds.length; i++) {
if ((frameCount & (1 << i)) != 0) {
leds[i].set(1.0);
} else {
leds[i].set(0.0);
}
}
println(frameCount);
}
void keyPressed() {
// cleanup
for (int i=0; i < leds.length; i++) {
leds[i].close();
}
exit();
}

View File

@@ -0,0 +1,21 @@
import processing.io.*;
SPI spi;
// MCP3001 is a Analog-to-Digital converter using SPI
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf
void setup() {
//printArray(SPI.list());
spi = new SPI(SPI.list()[0]);
spi.settings(500000, SPI.MSBFIRST, SPI.MODE0);
}
void draw() {
// dummy write, actual values don't matter
byte[] out = { 0, 0 };
byte[] in = spi.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
background(map(val, 0, 1023, 0, 255));
}

View File

@@ -0,0 +1,22 @@
import processing.io.SPI;
// MCP3001 is a Analog-to-Digital converter using SPI
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf
class MCP3001 extends SPI {
MCP3001(String dev) {
super(dev);
super.settings(500000, SPI.MSBFIRST, SPI.MODE0);
}
float getAnalog() {
// dummy write, actual values don't matter
byte[] out = { 0, 0 };
byte[] in = super.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
return val/1023.0;
}
}

View File

@@ -0,0 +1,11 @@
import processing.io.*;
MCP3001 adc;
void setup() {
//printArray(SPI.list());
adc = new MCP3001(SPI.list()[0]);
}
void draw() {
background(adc.getAnalog() * 255);
}

View File

@@ -0,0 +1,22 @@
import processing.io.*;
// RPI.PIN3 refers to the physical pin 3 on the Raspberry Pi's
// pin header, which is located on the second row, next to the
// 5v power pin
void setup() {
GPIO.pinMode(RPI.PIN3, GPIO.INPUT);
// this is equivalent to addressing the pin with its GPIO number:
// GPIO.pinMode(2, GPIO.INPUT);
}
void draw() {
// sense the input pin
if (GPIO.digitalRead(RPI.PIN3) == GPIO.HIGH) {
fill(255);
} else {
fill(204);
}
stroke(255);
ellipse(width/2, height/2, width*0.75, height*0.75);
}

View File

@@ -0,0 +1,27 @@
import processing.io.*;
boolean ledOn = false;
// RPI.PIN5 refers to the physical pin 5 on the Raspberry Pi's
// pin header, which is located on the third row, next to a
// Ground pin
void setup() {
GPIO.pinMode(RPI.PIN5, GPIO.OUTPUT);
// this is equivalent to addressing the pin with its GPIO number:
// GPIO.pinMode(3, GPIO.OUTPUT);
frameRate(0.5);
}
void draw() {
// make the LED blink
ledOn = !ledOn;
if (ledOn) {
GPIO.digitalWrite(RPI.PIN5, GPIO.LOW);
fill(204);
} else {
GPIO.digitalWrite(RPI.PIN5, GPIO.HIGH);
fill(255);
}
stroke(255);
ellipse(width/2, height/2, width*0.75, height*0.75);
}