IO: Reorganize OOP examples

This commit is contained in:
gohai
2018-06-29 08:23:30 -07:00
parent e19fab1b2b
commit def4a9b4db
13 changed files with 63 additions and 43 deletions

View File

@@ -0,0 +1,19 @@
import processing.io.*;
HMC6352 compass;
// see setup.png in the sketch folder for wiring details
void setup() {
// the module's I2C address can be changed by modifying values in its EEPROM
// 0x21 is however the default address
//printArray(I2C.list());
compass = new HMC6352("i2c-1", 0x21);
}
void draw() {
background(255);
float deg = compass.heading();
println(deg + " degrees");
line(width/2, height/2, width/2+sin(radians(deg))*width/2, height/2-cos(radians(deg))*height/2);
}

View File

@@ -0,0 +1,38 @@
import processing.io.I2C;
// HMC6352 is a digital compass using I2C
// datasheet: https://www.sparkfun.com/datasheets/Components/HMC6352.pdf
class HMC6352 extends I2C {
int address;
HMC6352(String dev, int address) {
super(dev);
this.address = address;
setHeadingMode();
}
void setHeadingMode() {
beginTransmission(address);
// command byte for writing to EEPROM
write(0x77);
// address of the output data control byte
write(0x4e);
// give us the plain heading
write(0x00);
endTransmission();
}
float heading() {
beginTransmission(address);
// command byte for reading the data
write(0x41);
byte[] in = read(2);
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

@@ -1,43 +0,0 @@
import processing.io.*;
I2C i2c;
// HMC6352 is a digital compass module using I2C
// datasheet: https://www.sparkfun.com/datasheets/Components/HMC6352.pdf
// see setup.png in the sketch folder for wiring details
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(0x77);
// address of the output data control byte
i2c.write(0x4e);
// give us the plain heading
i2c.write(0x00);
i2c.endTransmission();
}
float getHeading() {
i2c.beginTransmission(0x21);
// command byte for reading the data
i2c.write(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

@@ -4,6 +4,9 @@ I2C i2c;
// MCP4725 is a Digital-to-Analog converter using I2C
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/22039d.pdf
// also see DigitalAnalog_I2C_MCP4725 for how to write the
// same sketch in an object-oriented way
void setup() {
//printArray(I2C.list());
i2c = new I2C(I2C.list()[0]);

View File

@@ -5,6 +5,9 @@ SPI spi;
// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf
// see setup.png in the sketch folder for wiring details
// also see AnalogDigital_SPI_MCP3001 for how to write the
// same sketch in an object-oriented way
void setup() {
//printArray(SPI.list());
spi = new SPI(SPI.list()[0]);