Skip to content

Commit bc02e4b

Browse files
committed
I/O: Add examples
1 parent 59e0539 commit bc02e4b

File tree

11 files changed

+275
-0
lines changed

11 files changed

+275
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import processing.io.*;
2+
I2C i2c;
3+
4+
// HMC6352 is a digital compass module using I2C
5+
// datasheet: https://www.sparkfun.com/datasheets/Components/HMC6352.pdf
6+
7+
void setup() {
8+
//printArray(I2C.list());
9+
i2c = new I2C(I2C.list()[0]);
10+
setHeadingMode();
11+
}
12+
13+
void draw() {
14+
background(255);
15+
float deg = getHeading();
16+
println(deg + " degrees");
17+
line(width/2, height/2, width/2+sin(radians(deg))*width/2, height/2-cos(radians(deg))*height/2);
18+
}
19+
20+
void setHeadingMode() {
21+
i2c.beginTransmission(0x21);
22+
// command byte for writing to EEPROM
23+
i2c.write((byte) 0x77);
24+
// address of the output data control byte
25+
i2c.write((byte) 0x4e);
26+
// give us the plain heading
27+
i2c.write((byte) 0x00);
28+
i2c.endTransmission();
29+
}
30+
31+
float getHeading() {
32+
i2c.beginTransmission(0x21);
33+
// command byte for reading the data
34+
i2c.write((byte) 0x41);
35+
byte[] in = i2c.read(2);
36+
i2c.endTransmission();
37+
// put bytes together to tenth of degrees
38+
// & 0xff makes sure the byte is not interpreted as a negative value
39+
int deg = (in[0] & 0xff) << 8 | (in[1] & 0xff);
40+
// return degrees
41+
return deg / 10.0;
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import processing.io.*;
2+
I2C i2c;
3+
4+
// MCP4725 is a Digital-to-Analog converter using I2C
5+
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/22039d.pdf
6+
7+
void setup() {
8+
//printArray(I2C.list());
9+
i2c = new I2C(I2C.list()[0]);
10+
}
11+
12+
void draw() {
13+
background(map(mouseX, 0, width, 0, 255));
14+
setAnalog(map(mouseX, 0, width, 0.0, 1.0));
15+
}
16+
17+
// outputs voltages from 0V to the supply voltage
18+
// (works with 3.3V and 5V)
19+
void setAnalog(float fac) {
20+
fac = constrain(fac, 0.0, 1.0);
21+
// convert to 12 bit value
22+
int val = int(4095 * fac);
23+
i2c.beginTransmission(0x60);
24+
i2c.write((byte) (val >> 8));
25+
i2c.write((byte) (val & 255));
26+
i2c.endTransmission();
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import processing.io.*;
2+
MCP4725 dac;
3+
4+
void setup() {
5+
//printArray(I2C.list());
6+
dac = new MCP4725(I2C.list()[0], 0x60);
7+
}
8+
9+
void draw() {
10+
background(map(mouseX, 0, width, 0, 255));
11+
dac.setAnalog(map(mouseX, 0, width, 0.0, 1.0));
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import processing.io.I2C;
2+
3+
// MCP4725 is a Digital-to-Analog converter using I2C
4+
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/22039d.pdf
5+
6+
class MCP4725 extends I2C {
7+
int address;
8+
9+
// there can be more than one device connected to the bus
10+
// as long as they have different addresses
11+
MCP4725(String dev, int address) {
12+
super(dev);
13+
this.address = address;
14+
}
15+
16+
// outputs voltages from 0V to the supply voltage
17+
// (works with 3.3V and 5V)
18+
void setAnalog(float fac) {
19+
fac = constrain(fac, 0.0, 1.0);
20+
// convert to 12 bit value
21+
int val = int(4095 * fac);
22+
beginTransmission(address);
23+
write((byte) (val >> 8));
24+
write((byte) (val & 255));
25+
endTransmission();
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import processing.io.*;
2+
color bgcolor = 0;
3+
4+
// RPI.PIN3 refers to the physical pin 3 on the Raspberry Pi's
5+
// pin header, which is located on the third row, next to a
6+
// Ground pin
7+
8+
void setup() {
9+
GPIO.pinMode(RPI.PIN3, GPIO.INPUT);
10+
GPIO.attachInterrupt(RPI.PIN3, this, "pinEvent", GPIO.RISING);
11+
}
12+
13+
void draw() {
14+
background(bgcolor);
15+
}
16+
17+
// this function will be called whenever pin 3 is brought from LOW to HIGH
18+
void pinEvent(int pin) {
19+
println("Received interrupt");
20+
if (bgcolor == 0) {
21+
bgcolor = color(255);
22+
} else {
23+
bgcolor = color(0);
24+
}
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import processing.io.*;
2+
LED leds[];
3+
4+
// the Raspberry Pi has two build-in LEDs we can control
5+
// led0 (green) and led1 (red)
6+
7+
void setup() {
8+
String available[] = LED.list();
9+
print("Available: ");
10+
println(available);
11+
12+
// create an object for each LED and store it in an array
13+
leds = new LED[available.length];
14+
for (int i=0; i < available.length; i++) {
15+
leds[i] = new LED(available[i]);
16+
}
17+
18+
frameRate(1);
19+
}
20+
21+
void draw() {
22+
// make the leds count in binary
23+
for (int i=0; i < leds.length; i++) {
24+
if ((frameCount & (1 << i)) != 0) {
25+
leds[i].set(1.0);
26+
} else {
27+
leds[i].set(0.0);
28+
}
29+
}
30+
println(frameCount);
31+
}
32+
33+
void keyPressed() {
34+
// cleanup
35+
for (int i=0; i < leds.length; i++) {
36+
leds[i].close();
37+
}
38+
exit();
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import processing.io.*;
2+
SPI spi;
3+
4+
// MCP3001 is a Analog-to-Digital converter using SPI
5+
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf
6+
7+
void setup() {
8+
//printArray(SPI.list());
9+
spi = new SPI(SPI.list()[0]);
10+
spi.settings(500000, SPI.MSBFIRST, SPI.MODE0);
11+
}
12+
13+
void draw() {
14+
// dummy write, actual values don't matter
15+
byte[] out = { 0, 0 };
16+
byte[] in = spi.transfer(out);
17+
// some input bit shifting according to the datasheet p. 16
18+
int val = ((in[0] & 0x1f) << 5) | ((in[1] & 0xf8) >> 3);
19+
// val is between 0 and 1023
20+
background(map(val, 0, 1023, 0, 255));
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import processing.io.SPI;
2+
3+
// MCP3001 is a Analog-to-Digital converter using SPI
4+
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf
5+
6+
class MCP3001 extends SPI {
7+
8+
MCP3001(String dev) {
9+
super(dev);
10+
super.settings(500000, SPI.MSBFIRST, SPI.MODE0);
11+
}
12+
13+
float getAnalog() {
14+
// dummy write, actual values don't matter
15+
byte[] out = { 0, 0 };
16+
byte[] in = super.transfer(out);
17+
// some input bit shifting according to the datasheet p. 16
18+
int val = ((in[0] & 0x1f) << 5) | ((in[1] & 0xf8) >> 3);
19+
// val is between 0 and 1023
20+
return val/1023.0;
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import processing.io.*;
2+
MCP3001 adc;
3+
4+
void setup() {
5+
//printArray(SPI.list());
6+
adc = new MCP3001(SPI.list()[0]);
7+
}
8+
9+
void draw() {
10+
background(adc.getAnalog() * 255);
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import processing.io.*;
2+
3+
// RPI.PIN3 refers to the physical pin 3 on the Raspberry Pi's
4+
// pin header, which is located on the second row, next to the
5+
// 5v power pin
6+
7+
void setup() {
8+
GPIO.pinMode(RPI.PIN3, GPIO.INPUT);
9+
// this is equivalent to addressing the pin with its GPIO number:
10+
// GPIO.pinMode(2, GPIO.INPUT);
11+
}
12+
13+
void draw() {
14+
// sense the input pin
15+
if (GPIO.digitalRead(RPI.PIN3) == GPIO.HIGH) {
16+
fill(255);
17+
} else {
18+
fill(204);
19+
}
20+
stroke(255);
21+
ellipse(width/2, height/2, width*0.75, height*0.75);
22+
}

0 commit comments

Comments
 (0)