mirror of
https://github.com/processing/processing4.git
synced 2026-02-05 06:39:20 +01:00
IO: Reorganize OOP examples
This commit is contained in:
@@ -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);
|
||||
}
|
||||
38
java/libraries/io/examples/Compass_I2C_HMC6352/HMC6352.pde
Normal file
38
java/libraries/io/examples/Compass_I2C_HMC6352/HMC6352.pde
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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]);
|
||||
@@ -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]);
|
||||
Reference in New Issue
Block a user