first commit

This commit is contained in:
pandacraft 2025-03-21 16:04:17 +01:00
commit a5a0434432
1126 changed files with 439481 additions and 0 deletions

View file

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>GrovePi-spec</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View file

@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding/<project>=UTF-8

View file

@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8

View file

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.iot.raspberry</groupId>
<artifactId>GrovePi-spec</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

View file

@ -0,0 +1,47 @@
package org.iot.raspberry.grovepi;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.iot.raspberry.grovepi.GrovePiCommands.*;
public class GroveAnalogIn implements Runnable {
private final GrovePi grovePi;
private final int pin;
private GroveAnalogInListener listener;
private final int bufferSize;
public GroveAnalogIn(GrovePi grovePi, int pin, int bufferSize) throws IOException {
this.grovePi = grovePi;
this.pin = pin;
grovePi.execVoid((io) -> io.write(pMode_cmd, pMode_in_arg, pin, unused));
this.bufferSize = bufferSize;
}
@Override
public void run() {
try {
get();
} catch (IOException ex) {
Logger.getLogger("GrovePi").log(Level.SEVERE, null, ex);
}
}
public byte[] get() throws IOException {
byte[] value = grovePi.exec((io) -> {
io.write(aRead_cmd, pin, unused, unused);
io.sleep(100);
return io.read(new byte[bufferSize]);
});
if (listener != null) {
listener.onChange(value);
}
return value;
}
public void setListener(GroveAnalogInListener listener) {
this.listener = listener;
}
}

View file

@ -0,0 +1,6 @@
package org.iot.raspberry.grovepi;
public interface GroveAnalogInListener {
void onChange(byte[] newValue);
}

View file

@ -0,0 +1,27 @@
package org.iot.raspberry.grovepi;
import java.io.IOException;
import static org.iot.raspberry.grovepi.GrovePiCommands.*;
public class GroveAnalogOut {
private final GrovePi grovePi;
private final int pin;
public GroveAnalogOut(GrovePi grovePi, int pin) throws IOException {
this.grovePi = grovePi;
this.pin = pin;
grovePi.execVoid((GroveIO io) -> io.write(pMode_cmd, pin, pMode_out_arg, unused));
}
public void set(double value) throws IOException {
int[] command = new int[8];
command[0] = aWrite_cmd;
command[1] = pin;
command[2] = 1;
command[3] = 3;
command[4] = unused;
grovePi.execVoid((GroveIO io) -> io.write(command));
}
}

View file

@ -0,0 +1,5 @@
package org.iot.raspberry.grovepi;
public @interface GroveAnalogPin {
}

View file

@ -0,0 +1,28 @@
package org.iot.raspberry.grovepi;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GroveDevices implements AutoCloseable {
private final List<AutoCloseable> devices = new ArrayList<>();
public <T extends AutoCloseable> T add(T device) {
devices.add(device);
return device;
}
@Override
public void close() {
devices.stream().forEach((device) -> {
try {
device.close();
} catch (Exception ex) {
Logger.getLogger("RaspberryPi").log(Level.SEVERE, null, ex);
}
});
}
}

View file

@ -0,0 +1,47 @@
package org.iot.raspberry.grovepi;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.iot.raspberry.grovepi.GrovePiCommands.*;
public class GroveDigitalIn implements Runnable {
private final GrovePi grovePi;
private final int pin;
private boolean status = false;
private GroveDigitalInListener listener;
public GroveDigitalIn(GrovePi grovePi, int pin) throws IOException {
this.grovePi = grovePi;
this.pin = pin;
grovePi.execVoid((GroveIO io) -> io.write(pMode_cmd, pin, pMode_in_arg, unused));
}
public boolean get() throws IOException, InterruptedException {
boolean st = grovePi.exec((GroveIO io) -> {
io.write(dRead_cmd, pin, unused, unused);
io.sleep(100);
return io.read() == 1;
});
if (listener != null && status != st) {
listener.onChange(status, st);
}
this.status = st;
return st;
}
public void setListener(GroveDigitalInListener listener) {
this.listener = listener;
}
@Override
public void run() {
try {
get();
} catch (IOException | InterruptedException ex) {
Logger.getLogger("GrovePi").log(Level.SEVERE, null, ex);
}
}
}

View file

@ -0,0 +1,6 @@
package org.iot.raspberry.grovepi;
public interface GroveDigitalInListener {
void onChange(boolean oldValue, boolean newValue);
}

View file

@ -0,0 +1,22 @@
package org.iot.raspberry.grovepi;
import java.io.IOException;
import static org.iot.raspberry.grovepi.GrovePiCommands.*;
public class GroveDigitalOut {
private final GrovePi grovePi;
private final int pin;
public GroveDigitalOut(GrovePi grovePi, int pin) throws IOException {
this.grovePi = grovePi;
this.pin = pin;
grovePi.execVoid((GroveIO io) -> io.write(pMode_cmd, pin, pMode_out_arg, unused));
set(false);
}
public final void set(boolean value) throws IOException {
int val = value ? 1 : 0;
grovePi.execVoid((GroveIO io) -> io.write(dWrite_cmd, pin, val, unused));
}
}

View file

@ -0,0 +1,5 @@
package org.iot.raspberry.grovepi;
public @interface GroveDigitalPin {
}

View file

@ -0,0 +1,5 @@
package org.iot.raspberry.grovepi;
public @interface GroveI2CPin {
}

View file

@ -0,0 +1,19 @@
package org.iot.raspberry.grovepi;
import java.io.IOException;
public interface GroveIO {
public void write(int... command) throws IOException;
public int read() throws IOException;
public byte[] read(byte[] buffer) throws IOException;
default public void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ex) {
}
}
}

View file

@ -0,0 +1,33 @@
package org.iot.raspberry.grovepi;
import org.iot.raspberry.grovepi.devices.GroveRgbLcd;
import java.io.IOException;
public interface GrovePi extends AutoCloseable {
default public GroveDigitalOut getDigitalOut(int digitalPort) throws IOException {
return new GroveDigitalOut(this, digitalPort);
}
default public GroveDigitalIn getDigitalIn(int digitalPort) throws IOException {
return new GroveDigitalIn(this, digitalPort);
}
default public GroveAnalogOut getAnalogOut(int digitalPort) throws IOException {
return new GroveAnalogOut(this, digitalPort);
}
default public GroveAnalogIn getAnalogIn(int digitalPort, int bufferSize) throws IOException {
return new GroveAnalogIn(this, digitalPort, bufferSize);
}
public GroveRgbLcd getLCD() throws IOException;
public <T> T exec(GrovePiSequence<T> sequence) throws IOException;
public void execVoid(GrovePiSequenceVoid sequence) throws IOException;
@Override
public void close();
}

View file

@ -0,0 +1,82 @@
package org.iot.raspberry.grovepi;
public class GrovePiCommands {
public static final int unused = 0;
// Command Format
// digitalRead()command format header
public static final int dRead_cmd = 1;
// digitalWrite() command format header
public static final int dWrite_cmd = 2;
// analogRead() command format header
public static final int aRead_cmd = 3;
// analogWrite() command format header
public static final int aWrite_cmd = 4;
// pinMode() command format header
public static final int pMode_cmd = 5;
// Ultrasonic read
public static final int uRead_cmd = 7;
// Get firmware version
public static final int version_cmd = 8;
// Accelerometer (+/- 1.5g) read
public static final int acc_xyz_cmd = 20;
// RTC get time
public static final int rtc_getTime_cmd = 30;
// DHT Pro sensor temperature
public static final int dht_temp_cmd = 40;
// Grove LED Bar commands
// Initialise
public static final int ledBarInit_cmd = 50;
// Set orientation
public static final int ledBarOrient_cmd = 51;
// Set level
public static final int ledBarLevel_cmd = 52;
// Set single LED
public static final int ledBarSetOne_cmd = 53;
// Toggle single LED
public static final int ledBarToggleOne_cmd = 54;
// Set all LEDs
public static final int ledBarSet_cmd = 55;
// Get current state
public static final int ledBarGet_cmd = 56;
// Grove 4 Digit Display commands
// Initialise
public static final int fourDigitInit_cmd = 70;
// Set brightness, not visible until next cmd
public static final int fourDigitBrightness_cmd = 71;
// Set numeric value without leading zeros
public static final int fourDigitValue_cmd = 72;
// Set numeric value with leading zeros
public static final int fourDigitValueZeros_cmd = 73;
// Set individual digit
public static final int fourDigitIndividualDigit_cmd = 74;
// Set individual leds of a segment
public static final int fourDigitIndividualLeds_cmd = 75;
// Set left and right values with colon
public static final int fourDigitScore_cmd = 76;
// Analog read for n seconds
public static final int fourDigitAnalogRead_cmd = 77;
// Entire display on
public static final int fourDigitAllOn_cmd = 78;
// Entire display off
public static final int fourDigitAllOff_cmd = 79;
// Grove Chainable RGB LED commands
// Store color for later use
public static final int storeColor_cmd = 90;
// Initialise
public static final int chainableRgbLedInit_cmd = 91;
// Initialise and test with a simple color
public static final int chainableRgbLedTest_cmd = 92;
// Set one or more leds to the stored color by pattern
public static final int chainableRgbLedSetPattern_cmd = 93;
// set one or more leds to the stored color by modulo
public static final int chainableRgbLedSetModulo_cmd = 94;
// sets leds similar to a bar graph, reversible
public static final int chainableRgbLedSetLevel_cmd = 95;
public static final int pMode_out_arg = 1;
public static final int pMode_in_arg = 0;
}

View file

@ -0,0 +1,9 @@
package org.iot.raspberry.grovepi;
import java.io.IOException;
public interface GrovePiSequence<T> {
T execute(GroveIO io) throws IOException;
}

View file

@ -0,0 +1,9 @@
package org.iot.raspberry.grovepi;
import java.io.IOException;
public interface GrovePiSequenceVoid<T> {
void execute(GroveIO io) throws IOException;
}

View file

@ -0,0 +1,16 @@
package org.iot.raspberry.grovepi;
public class GroveUtil {
public static int[] unsign(byte[] b) {
int[] v = new int[b.length];
for (int i = 0; i < b.length; i++) {
v[i] = unsign(b[i]);
}
return v;
}
public static int unsign(byte b) {
return b & 0xFF;
}
}

View file

@ -0,0 +1,38 @@
package org.iot.raspberry.grovepi.devices;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveAnalogIn;
import org.iot.raspberry.grovepi.GroveAnalogInListener;
public abstract class GroveAnalogInputDevice<T> implements Runnable, GroveAnalogInListener {
private final GroveAnalogIn in;
private GroveInputDeviceListener<T> listener;
public GroveAnalogInputDevice(GroveAnalogIn in) {
this.in = in;
in.setListener(this);
}
@Override
public void run() {
in.run();
}
@Override
public void onChange(byte[] newValue) {
if (listener != null) {
listener.onChange(get(newValue));
}
}
public void setListener(GroveInputDeviceListener listener) {
this.listener = listener;
}
public T get() throws IOException {
return get(in.get());
}
public abstract T get(byte[] data);
}

View file

@ -0,0 +1,6 @@
package org.iot.raspberry.grovepi.devices;
public interface GroveInputDeviceListener<T> {
void onChange(T t);
}

View file

@ -0,0 +1,34 @@
package org.iot.raspberry.grovepi.devices;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveDigitalPin;
import org.iot.raspberry.grovepi.GroveIO;
import org.iot.raspberry.grovepi.GrovePi;
import static org.iot.raspberry.grovepi.GrovePiCommands.*;
@GroveDigitalPin
public class GroveLed {
public static int MAX_BRIGTHNESS = 255;
private final GrovePi grovePi;
private final int pin;
public GroveLed(GrovePi grovePi, int pin) throws IOException {
this.grovePi = grovePi;
this.pin = pin;
grovePi.execVoid((GroveIO io) -> io.write(pMode_cmd, pin, pMode_out_arg, unused));
set(false);
}
public final void set(boolean value) throws IOException {
int val = value ? 1 : 0;
grovePi.execVoid((GroveIO io) -> io.write(dWrite_cmd, pin, val, unused));
}
public final void set(int value) throws IOException {
final int val = ((value > 255) ? 255 : (value < 0 ? 0 : value));
grovePi.execVoid((GroveIO io) -> io.write(aWrite_cmd, pin, val, unused));
}
}

View file

@ -0,0 +1,20 @@
package org.iot.raspberry.grovepi.devices;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveAnalogPin;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.GroveUtil;
@GroveAnalogPin
public class GroveLightSensor extends GroveAnalogInputDevice<Double> {
public GroveLightSensor(GrovePi grovePi, int pin) throws IOException {
super(grovePi.getAnalogIn(pin, 4));
}
@Override
public Double get(byte[] data) {
int[] v = GroveUtil.unsign(data);
return (double) (v[1] * 256) + v[2];
}
}

View file

@ -0,0 +1,30 @@
package org.iot.raspberry.grovepi.devices;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveDigitalPin;
import org.iot.raspberry.grovepi.GroveIO;
import org.iot.raspberry.grovepi.GrovePi;
import static org.iot.raspberry.grovepi.GrovePiCommands.dWrite_cmd;
import static org.iot.raspberry.grovepi.GrovePiCommands.pMode_cmd;
import static org.iot.raspberry.grovepi.GrovePiCommands.pMode_out_arg;
import static org.iot.raspberry.grovepi.GrovePiCommands.unused;
@GroveDigitalPin
public class GroveRelay {
private final GrovePi grovePi;
private final int pin;
public GroveRelay(GrovePi grovePi, int pin) throws IOException {
this.grovePi = grovePi;
this.pin = pin;
grovePi.execVoid((GroveIO io) -> io.write(pMode_cmd, pin, pMode_out_arg, unused));
set(false);
}
public final void set(boolean value) throws IOException {
int val = value ? 1 : 0;
grovePi.execVoid((GroveIO io) -> io.write(dWrite_cmd, pin, val, unused));
}
}

View file

@ -0,0 +1,78 @@
package org.iot.raspberry.grovepi.devices;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveI2CPin;
import org.iot.raspberry.grovepi.GrovePiSequenceVoid;
@GroveI2CPin
public abstract class GroveRgbLcd implements AutoCloseable {
public static final int DISPLAY_RGB_ADDR = 0x62;
public static final int DISPLAY_TEXT_ADDR = 0x3e;
private static final int LCD_COMMAND = 0x80;
private static final int LCD_WRITECHAR = 0x40;
private static final int LCD_CMD_CLEARDISPLAY = 0x01;
private static final int LCD_CMD_NEWLINE = 0xc0;
private static final int REG_RED = 0x04;
private static final int REG_GREEN = 0x03;
private static final int REG_BLUE = 0x02;
protected void init() throws IOException {
execRGB((io) -> {
io.write(0, 0);
io.write(1, 0);
io.write(0x08, 0xaa);
});
execTEXT((io) -> {
io.write(LCD_COMMAND, 0x08 | 0x04); // display on, no cursor
io.write(LCD_COMMAND, 0x28); // 2 Lines
io.write(LCD_COMMAND, LCD_CMD_CLEARDISPLAY); // clear Display
io.sleep(50);
});
}
public void setRGB(int r, int g, int b) throws IOException {
execRGB((io) -> {
io.write(REG_RED, r);
io.write(REG_GREEN, g);
io.write(REG_BLUE, b);
io.sleep(50);
});
}
public void setText(String text) throws IOException {
execTEXT((io) -> {
io.write(LCD_COMMAND, LCD_CMD_CLEARDISPLAY); // clear Display
io.sleep(50);
int count = 0;
int row = 0;
for (char c : text.toCharArray()) {
if (c == '\n' || count == 16) {
count = 0;
row += 1;
if (row == 2) {
break;
}
io.write(LCD_COMMAND, LCD_CMD_NEWLINE); // new line
if (c == '\n') {
continue;
}
}
count++;
io.write(LCD_WRITECHAR, c); // Write character
}
io.sleep(100);
});
}
public abstract void execRGB(GrovePiSequenceVoid sequence) throws IOException;
public abstract void execTEXT(GrovePiSequenceVoid sequence) throws IOException;
@Override
public abstract void close();
}

View file

@ -0,0 +1,31 @@
package org.iot.raspberry.grovepi.devices;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveAnalogPin;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.GroveUtil;
@GroveAnalogPin
public class GroveRotarySensor extends GroveAnalogInputDevice<GroveRotaryValue> {
//Reference voltage of ADC is 5v
public static final double ADC_REF = 5;
//Vcc of the grove interface is normally 5v
public static final double GROVE_VCC = 5;
//Full value of the rotary angle is 300 degrees, as per it's specs (0 to 300)
public static final double FULL_ANGLE = 300;
public GroveRotarySensor(GrovePi grovePi, int pin) throws IOException {
super(grovePi.getAnalogIn(pin, 4));
}
@Override
public GroveRotaryValue get(byte[] b) {
int[] v = GroveUtil.unsign(b);
double sensor_value = (v[1] * 256) + v[2];
double voltage = (sensor_value * ADC_REF / 1023);
double degrees = voltage * FULL_ANGLE / GROVE_VCC;
return new GroveRotaryValue(sensor_value, voltage, degrees);
}
}

View file

@ -0,0 +1,40 @@
package org.iot.raspberry.grovepi.devices;
public class GroveRotaryValue {
private final double sensorValue;
private final double voltage;
private final double degrees;
public GroveRotaryValue(double sensorValue, double voltage, double degrees) {
this.sensorValue = sensorValue;
this.voltage = voltage;
this.degrees = degrees;
}
public double getSensorValue() {
return sensorValue;
}
public double getVoltage() {
return voltage;
}
public double getDegrees() {
return degrees;
}
@Override
public String toString() {
return "S:" + sensorValue + ",V:" + voltage + ",D:" + getDegrees();
}
/**
* 0 to 1 factor
*
* @return a number between 0 and 1
*/
public double getFactor() {
return (getDegrees() / GroveRotarySensor.FULL_ANGLE);
}
}

View file

@ -0,0 +1,20 @@
package org.iot.raspberry.grovepi.devices;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveAnalogPin;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.GroveUtil;
@GroveAnalogPin
public class GroveSoundSensor extends GroveAnalogInputDevice<Double> {
public GroveSoundSensor(GrovePi grovePi, int pin) throws IOException {
super(grovePi.getAnalogIn(pin, 4));
}
@Override
public Double get(byte[] b) {
int[] v = GroveUtil.unsign(b);
return (double) (v[1] * 256) + v[2];
}
}

View file

@ -0,0 +1,50 @@
package org.iot.raspberry.grovepi.devices;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.iot.raspberry.grovepi.GroveDigitalPin;
import org.iot.raspberry.grovepi.GrovePi;
import static org.iot.raspberry.grovepi.GrovePiCommands.*;
@GroveDigitalPin
public class GroveTemperatureAndHumiditySensor {
public enum Type {
DHT11(0), //blue one
DHT22(1), //White one (PRO) AKA (AM2302)
DHT21(2), //Black One AKA (AM2301)
AM2301(3); //White one (PRO)
private final int moduleType;
private Type(int moduleType) {
this.moduleType = moduleType;
}
public int getModuleType() {
return moduleType;
}
}
private final GrovePi grovePi;
private final int pin;
private final Type dhtType;
public GroveTemperatureAndHumiditySensor(GrovePi grovePi, int pin, Type dhtType) {
this.grovePi = grovePi;
this.pin = pin;
this.dhtType = dhtType;
}
public GroveTemperatureAndHumidityValue get() throws IOException {
byte[] data = grovePi.exec((io) -> {
io.write(dht_temp_cmd, pin, dhtType.moduleType, unused);
io.sleep(600);
return io.read(new byte[9]);
});
double temp = ByteBuffer.wrap(new byte[]{data[4], data[3], data[2], data[1]}).getFloat();
double humid = ByteBuffer.wrap(new byte[]{data[8], data[7], data[6], data[5]}).getFloat();
return new GroveTemperatureAndHumidityValue(temp, humid);
}
}

View file

@ -0,0 +1,26 @@
package org.iot.raspberry.grovepi.devices;
public class GroveTemperatureAndHumidityValue {
private final double temperature;
private final double humidity;
public GroveTemperatureAndHumidityValue(double temperature, double humidity) {
this.temperature = temperature;
this.humidity = humidity;
}
public double getTemperature() {
return temperature;
}
public double getHumidity() {
return humidity;
}
@Override
public String toString() {
return "temperature=" + temperature + ", humidity=" + humidity;
}
}

View file

@ -0,0 +1,29 @@
package org.iot.raspberry.grovepi.devices;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveDigitalPin;
import org.iot.raspberry.grovepi.GrovePi;
import static org.iot.raspberry.grovepi.GrovePiCommands.*;
import org.iot.raspberry.grovepi.GroveUtil;
@GroveDigitalPin
public class GroveUltrasonicRanger {
private final GrovePi grovePi;
private final int pin;
public GroveUltrasonicRanger(GrovePi grovePi, int pin) {
this.grovePi = grovePi;
this.pin = pin;
}
public double get() throws IOException {
return grovePi.exec((io) -> {
io.write(uRead_cmd, pin, unused, unused);
io.sleep(200);
int[] v = GroveUtil.unsign(io.read(new byte[4]));
return (v[1] * 256) + v[2];
});
}
}