From bc02e4b7b6ff5d7fe5422abe6944d053bd9b7ac0 Mon Sep 17 00:00:00 2001 From: gohai Date: Mon, 12 Oct 2015 00:11:55 +0200 Subject: [PATCH] I/O: Add examples --- .../io/examples/I2CCompass/I2CCompass.pde | 42 +++++++++++++++++++ .../I2CDigitalAnalog/I2CDigitalAnalog.pde | 27 ++++++++++++ .../I2CDigitalAnalogOOP.pde | 12 ++++++ .../examples/I2CDigitalAnalogOOP/MCP4725.pde | 27 ++++++++++++ .../io/examples/Interrupt/Interrupt.pde | 25 +++++++++++ .../io/examples/LedCounter/LedCounter.pde | 39 +++++++++++++++++ .../SPIAnalogDigital/SPIAnalogDigital.pde | 21 ++++++++++ .../examples/SPIAnalogDigitalOOP/MCP3001.pde | 22 ++++++++++ .../SPIAnalogDigitalOOP.pde | 11 +++++ .../io/examples/SimpleInput/SimpleInput.pde | 22 ++++++++++ .../io/examples/SimpleOutput/SimpleOutput.pde | 27 ++++++++++++ 11 files changed, 275 insertions(+) create mode 100644 java/libraries/io/examples/I2CCompass/I2CCompass.pde create mode 100644 java/libraries/io/examples/I2CDigitalAnalog/I2CDigitalAnalog.pde create mode 100644 java/libraries/io/examples/I2CDigitalAnalogOOP/I2CDigitalAnalogOOP.pde create mode 100644 java/libraries/io/examples/I2CDigitalAnalogOOP/MCP4725.pde create mode 100644 java/libraries/io/examples/Interrupt/Interrupt.pde create mode 100644 java/libraries/io/examples/LedCounter/LedCounter.pde create mode 100644 java/libraries/io/examples/SPIAnalogDigital/SPIAnalogDigital.pde create mode 100644 java/libraries/io/examples/SPIAnalogDigitalOOP/MCP3001.pde create mode 100644 java/libraries/io/examples/SPIAnalogDigitalOOP/SPIAnalogDigitalOOP.pde create mode 100644 java/libraries/io/examples/SimpleInput/SimpleInput.pde create mode 100644 java/libraries/io/examples/SimpleOutput/SimpleOutput.pde diff --git a/java/libraries/io/examples/I2CCompass/I2CCompass.pde b/java/libraries/io/examples/I2CCompass/I2CCompass.pde new file mode 100644 index 000000000..af518e9ae --- /dev/null +++ b/java/libraries/io/examples/I2CCompass/I2CCompass.pde @@ -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; +} diff --git a/java/libraries/io/examples/I2CDigitalAnalog/I2CDigitalAnalog.pde b/java/libraries/io/examples/I2CDigitalAnalog/I2CDigitalAnalog.pde new file mode 100644 index 000000000..880fc2e95 --- /dev/null +++ b/java/libraries/io/examples/I2CDigitalAnalog/I2CDigitalAnalog.pde @@ -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(); +} diff --git a/java/libraries/io/examples/I2CDigitalAnalogOOP/I2CDigitalAnalogOOP.pde b/java/libraries/io/examples/I2CDigitalAnalogOOP/I2CDigitalAnalogOOP.pde new file mode 100644 index 000000000..2c69daec4 --- /dev/null +++ b/java/libraries/io/examples/I2CDigitalAnalogOOP/I2CDigitalAnalogOOP.pde @@ -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)); +} diff --git a/java/libraries/io/examples/I2CDigitalAnalogOOP/MCP4725.pde b/java/libraries/io/examples/I2CDigitalAnalogOOP/MCP4725.pde new file mode 100644 index 000000000..55d34a213 --- /dev/null +++ b/java/libraries/io/examples/I2CDigitalAnalogOOP/MCP4725.pde @@ -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(); + } +} diff --git a/java/libraries/io/examples/Interrupt/Interrupt.pde b/java/libraries/io/examples/Interrupt/Interrupt.pde new file mode 100644 index 000000000..ea3433297 --- /dev/null +++ b/java/libraries/io/examples/Interrupt/Interrupt.pde @@ -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); + } +} diff --git a/java/libraries/io/examples/LedCounter/LedCounter.pde b/java/libraries/io/examples/LedCounter/LedCounter.pde new file mode 100644 index 000000000..cc57abd33 --- /dev/null +++ b/java/libraries/io/examples/LedCounter/LedCounter.pde @@ -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(); +} diff --git a/java/libraries/io/examples/SPIAnalogDigital/SPIAnalogDigital.pde b/java/libraries/io/examples/SPIAnalogDigital/SPIAnalogDigital.pde new file mode 100644 index 000000000..4e8ee3b4a --- /dev/null +++ b/java/libraries/io/examples/SPIAnalogDigital/SPIAnalogDigital.pde @@ -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)); +} diff --git a/java/libraries/io/examples/SPIAnalogDigitalOOP/MCP3001.pde b/java/libraries/io/examples/SPIAnalogDigitalOOP/MCP3001.pde new file mode 100644 index 000000000..e25869633 --- /dev/null +++ b/java/libraries/io/examples/SPIAnalogDigitalOOP/MCP3001.pde @@ -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; + } +} diff --git a/java/libraries/io/examples/SPIAnalogDigitalOOP/SPIAnalogDigitalOOP.pde b/java/libraries/io/examples/SPIAnalogDigitalOOP/SPIAnalogDigitalOOP.pde new file mode 100644 index 000000000..85be661a6 --- /dev/null +++ b/java/libraries/io/examples/SPIAnalogDigitalOOP/SPIAnalogDigitalOOP.pde @@ -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); +} diff --git a/java/libraries/io/examples/SimpleInput/SimpleInput.pde b/java/libraries/io/examples/SimpleInput/SimpleInput.pde new file mode 100644 index 000000000..3935f9bf6 --- /dev/null +++ b/java/libraries/io/examples/SimpleInput/SimpleInput.pde @@ -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); +} diff --git a/java/libraries/io/examples/SimpleOutput/SimpleOutput.pde b/java/libraries/io/examples/SimpleOutput/SimpleOutput.pde new file mode 100644 index 000000000..dfde0ca39 --- /dev/null +++ b/java/libraries/io/examples/SimpleOutput/SimpleOutput.pde @@ -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); +}