Skip to content

Commit 570d441

Browse files
committed
Merge pull request processing#4384 from gohai/arm-next
Add a few IO library examples
2 parents fbcc85e + c2cd9df commit 570d441

16 files changed

Lines changed: 435 additions & 12 deletions

File tree

java/libraries/io/build.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,46 @@
2929
<target name="build" depends="compile" description="Build I/O library">
3030
<jar basedir="bin" destfile="library/io.jar" />
3131
</target>
32+
33+
<target name="dist" depends="build" description="Package standalone library">
34+
<!-- set revision number as library version -->
35+
<loadfile srcfile="../../../todo.txt" property="revision">
36+
<filterchain>
37+
<headfilter lines="1"/>
38+
<tokenfilter>
39+
<stringtokenizer suppressdelims="true"/>
40+
<!-- grab the thing from the first line that's 4 digits -->
41+
<containsregex pattern="(\d\d\d\d)" />
42+
</tokenfilter>
43+
</filterchain>
44+
</loadfile>
45+
<replaceregexp file="library.properties" match="version = .*" replace="version = ${revision}" flags="g" />
46+
<replaceregexp file="library.properties" match="prettyVersion = .*" replace="prettyVersion = ${revision}" flags="g" />
47+
48+
<get src="http://download.processing.org/reference.zip"
49+
dest="reference.zip"
50+
usetimestamp="true" />
51+
<mkdir dir="reference" />
52+
<unzip dest="."
53+
src="reference.zip"
54+
overwrite="true">
55+
<patternset>
56+
<include name="reference/css/**" />
57+
<include name="reference/img/**" />
58+
<include name="reference/javascript/**" />
59+
<include name="reference/libraries/io/**" />
60+
</patternset>
61+
</unzip>
62+
<delete file="reference.zip" />
63+
<echo file="reference/index.html" message="&lt;html&gt;&lt;head&gt;&lt;meta http-equiv='refresh' content='0; url=libraries/io/index.html'&gt;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;" />
64+
65+
<zip destfile="../io.zip">
66+
<zipfileset dir="." prefix="io">
67+
<exclude name="bin/**"/>
68+
</zipfileset>
69+
</zip>
70+
71+
<copy file="library.properties"
72+
toFile="../io.txt"/>
73+
</target>
3274
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import processing.io.*;
2+
3+
// 0.96" 128x64 OLED display ("SKU 346540")
4+
SSD1306 oled;
5+
6+
void setup() {
7+
size(128, 64);
8+
9+
// the display can be set to one of these two addresses: 0x3c (default) or 0x3d
10+
// (they might be listed as 0x7a and 0x7b on the circuit board)
11+
oled = new SSD1306(I2C.list()[0], 0x3c);
12+
}
13+
14+
void draw() {
15+
line(0, 0, 127, 63);
16+
line(0, 63, 127, 0);
17+
oled.sendImage(get());
18+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import processing.io.I2C;
2+
3+
// SSD1306 is a small, inexpensive 128x64 pixels monochrome OLED display
4+
// available online as "0.96" 128x64 OLED display", SKU 346540
5+
// or from Adafruit
6+
// datasheet: https://www.adafruit.com/datasheets/SSD1306.pdf
7+
8+
class SSD1306 extends I2C {
9+
int address;
10+
11+
// there can be more than one device connected to the bus
12+
// as long as they have different addresses
13+
SSD1306(String dev, int address) {
14+
super(dev);
15+
this.address = address;
16+
init();
17+
}
18+
19+
protected void init() {
20+
writeCommand(0xae); // turn display off
21+
writeCommand(0xa8, 0x3f); // set multiplex ratio to the highest setting
22+
writeCommand(0x8d, 0x14); // enable charge pump
23+
writeCommand(0x20, 0x00); // set memory addressing mode to horizontal
24+
writeCommand(0xd5, 0x80); // set display clock divide ratio & oscillator frequency to default
25+
writeCommand(0xd3, 0x00); // no display offset
26+
writeCommand(0x40 | 0x00); // set default display start line
27+
28+
// use the following two lines to flip the display
29+
writeCommand(0xa0 | 0x01); // set segment re-map
30+
writeCommand(0xc8); // set COM output scan direction
31+
32+
writeCommand(0xda, 0x12); // set COM pins hardware configuration
33+
writeCommand(0xd9, 0xf1); // set pre-charge period to 241x DCLK
34+
writeCommand(0xdB, 0x40); // set VCOMH deselect level
35+
writeCommand(0xa4); // display RAM content (not all-on)
36+
writeCommand(0xa6); // set normal (not-inverted) display
37+
38+
// set this since we don't have access to the OLED's reset pins (?)
39+
writeCommand(0x21, 0, 127); // set column address
40+
writeCommand(0x22, 0, 7); // set page address
41+
42+
writeCommand(0x81, 0xcf); // set contrast
43+
writeCommand(0x2e); // deactivate scroll
44+
writeCommand(0xaf); // turn display on
45+
}
46+
47+
void invert(boolean inverted) {
48+
if (inverted) {
49+
writeCommand(0xa7);
50+
} else {
51+
writeCommand(0xa6);
52+
}
53+
}
54+
55+
void sendImage(PImage img) {
56+
sendImage(img, 0, 0);
57+
}
58+
59+
void sendImage(PImage img, int startX, int startY) {
60+
byte[] frame = new byte[1024];
61+
img.loadPixels();
62+
for (int y=startY; y < height && y-startY < 64; y++) {
63+
for (int x=startX; x < width && x-startX < 128; x++) {
64+
if (brightness(img.pixels[y*img.width+x]) < 128) {
65+
// this isn't the normal (scanline) mapping, but 8 pixels below each other at a time
66+
// white pixels have their bit turned on
67+
frame[x + (y/8)*128] |= (1 << (y % 8));
68+
}
69+
}
70+
}
71+
sendFramebuffer(frame);
72+
}
73+
74+
void sendFramebuffer(byte[] buf) {
75+
if (buf.length != 1024) {
76+
System.err.println("The framebuffer should be 1024 bytes long, with one bit per pixel");
77+
throw new IllegalArgumentException("Unexpected buffer size");
78+
}
79+
80+
writeCommand(0x00 | 0x0); // set start address
81+
writeCommand(0x10 | 0x0); // set higher column start address
82+
writeCommand(0x40 | 0x0); // set start line
83+
84+
// send the frame buffer as 16 byte long packets
85+
for (int i=0; i < buf.length/16; i++) {
86+
super.beginTransmission(address);
87+
super.write(0x40); // indicates data write
88+
for (int j=0; j < 16; j++) {
89+
super.write(buf[i*16+j]);
90+
}
91+
super.endTransmission();
92+
}
93+
}
94+
95+
protected void writeCommand(int arg1) {
96+
super.beginTransmission(address);
97+
super.write(0x00); // indicates command write
98+
super.write(arg1);
99+
super.endTransmission();
100+
}
101+
102+
protected void writeCommand(int arg1, int arg2) {
103+
super.beginTransmission(address);
104+
super.write(0x00);
105+
super.write(arg1);
106+
super.write(arg2);
107+
super.endTransmission();
108+
}
109+
110+
protected void writeCommand(int arg1, int arg2, int arg3) {
111+
super.beginTransmission(address);
112+
super.write(0x00);
113+
super.write(arg1);
114+
super.write(arg2);
115+
super.write(arg3);
116+
super.endTransmission();
117+
}
118+
}

java/libraries/io/examples/LedCounter/LedCounter.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ void draw() {
2222
// make the LEDs count in binary
2323
for (int i=0; i < leds.length; i++) {
2424
if ((frameCount & (1 << i)) != 0) {
25-
leds[i].set(1.0);
25+
leds[i].brightness(1.0);
2626
} else {
27-
leds[i].set(0.0);
27+
leds[i].brightness(0.0);
2828
}
2929
}
3030
println(frameCount);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import processing.io.SPI;
2+
3+
// MCP3008 is a Analog-to-Digital converter using SPI
4+
// other than the MCP3001, this has 8 input channels
5+
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21295d.pdf
6+
7+
class MCP3008 extends SPI {
8+
9+
MCP3008(String dev) {
10+
super(dev);
11+
super.settings(500000, SPI.MSBFIRST, SPI.MODE0);
12+
}
13+
14+
float getAnalog(int channel) {
15+
if (channel < 0 || 7 < channel) {
16+
System.err.println("The channel needs to be from 0 to 7");
17+
throw new IllegalArgumentException("Unexpected channel");
18+
}
19+
byte[] out = { 0, 0, 0 };
20+
// encode the channel number in the first byte
21+
out[0] = (byte)(0x18 | channel);
22+
byte[] in = super.transfer(out);
23+
int val = ((in[1] & 0x03) << 8) | (in[2] & 0xff);
24+
// val is between 0 and 1023
25+
return val/1023.0;
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import processing.io.*;
2+
MCP3008 adc;
3+
4+
// see setup.png in the sketch folder for wiring details
5+
6+
void setup() {
7+
//printArray(SPI.list());
8+
adc = new MCP3008(SPI.list()[0]);
9+
}
10+
11+
void draw() {
12+
background(adc.getAnalog(0) * 255);
13+
fill(adc.getAnalog(1) * 255);
14+
ellipse(width/2, height/2, width * 0.75, width * 0.75);
15+
}
68.4 KB
Loading
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import processing.io.*;
2+
3+
// using a capacitor that gets charged and discharged, while
4+
// measuring the time it takes, is an inexpensive way to
5+
// read the value of an (analog) resistive sensor, such as
6+
// a photocell
7+
// kudos to ladyada for the original tutorial
8+
9+
// see setup.png in the sketch folder for wiring details
10+
11+
int max = 0;
12+
int min = 9999;
13+
14+
void setup() {
15+
}
16+
17+
void draw() {
18+
int val = sensorRead(4);
19+
println(val);
20+
21+
// track largest and smallest reading, to get a sense
22+
// how we compare
23+
if (max < val) {
24+
max = val;
25+
}
26+
if (val < min) {
27+
min = val;
28+
}
29+
30+
// convert current reading into a number between 0.0 and 1.0
31+
float frac = map(val, min, max, 0.0, 1.0);
32+
33+
background(255 * frac);
34+
}
35+
36+
int sensorRead(int pin) {
37+
// discharge the capacitor
38+
GPIO.pinMode(pin, GPIO.OUTPUT);
39+
GPIO.digitalWrite(pin, GPIO.LOW);
40+
delay(100);
41+
// now the capacitor should be empty
42+
43+
// measure the time takes to fill it
44+
// up to ~ 1.4V again
45+
GPIO.pinMode(pin, GPIO.INPUT);
46+
int start = millis();
47+
while (GPIO.digitalRead(pin) == GPIO.LOW) {
48+
// wait
49+
}
50+
51+
// return the time elapsed
52+
// this will vary based on the value of the
53+
// resistive sensor (lower resistance will
54+
// make the capacitor charge faster)
55+
return millis() - start;
56+
}
64.4 KB
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
name = Hardware I/O
2+
authors = The Processing Foundation
3+
url = http://processing.org/reference/libraries/io/index.html
4+
categories = Hardware
5+
sentence = Access peripherals on the Raspberry Pi and other Linux-based computers.
6+
paragraph = For other platforms, this is solely provided in order to build and export sketches that require processing.io.
27
version = 1
8+
prettyVersion = 1
9+
minRevision = 247

0 commit comments

Comments
 (0)