first commit
This commit is contained in:
commit
a5a0434432
1126 changed files with 439481 additions and 0 deletions
7
Software/Java/.classpath
Normal file
7
Software/Java/.classpath
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry excluding="com/dexterind/grovepi/sensors/base/" kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="src/com/dexterind/grovepi/sensors/base"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
Software/Java/.project
Normal file
17
Software/Java/.project
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>GrovePi</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
50
Software/Java/README.md
Normal file
50
Software/Java/README.md
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
## Java Library
|
||||
### This repository contains the Java library for the GrovePi
|
||||
#### Installation ####
|
||||
Before to proceed you should install the Pi4J libraries. The easiest way to install it is using the following command:
|
||||
|
||||
```curl -s get.pi4j.com | sudo bash```
|
||||
|
||||
If you need more details you can visit the official website: http://pi4j.com/install.html
|
||||
|
||||
#### Compile and Execute the test program ####
|
||||
Once your installation is complete and your local repository is ready then you can compile the GrovePi libraries and run the test program.
|
||||
First, enter the Java directory:
|
||||
|
||||
```$ cd ./Java/```
|
||||
|
||||
The folder structure will be:
|
||||
|
||||
```$ config doc scripts src test```
|
||||
|
||||
* **bin** - it's the destination folder for the compiler and it will be created automatically, if not present, by the Bash script.
|
||||
* **config** - contains the default configuration.
|
||||
* **doc** - contains the GrovePi library documentation.
|
||||
* **scripts** - contains some Bash scripts to compile the library and execute the basic test program.
|
||||
* **src** - contains the sources of the GrovePi library.
|
||||
* **test** - contains some example code/programs.
|
||||
|
||||
To compile the library and run the test program you can use the following command:
|
||||
|
||||
```$ ./scripts/compile.sh && ./scripts/Test.sh```
|
||||
|
||||
By default you will find some logs inside the /var/log/GrovePi folder.
|
||||
|
||||
In case of any trouble or if you need further information don't hesitate to leave a comment on the official forum: http://forum.dexterindustries.com/c/grovepi
|
||||
|
||||
## License
|
||||
GrovePi for the Raspberry Pi: an open source robotics platform for the Raspberry Pi.
|
||||
Copyright (C) 2017 Dexter Industries
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
3
Software/Java/config/default.properties
Normal file
3
Software/Java/config/default.properties
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
debug=true
|
||||
logDir=../log
|
||||
logFile=all.log
|
||||
3
Software/Java/scripts/Test.sh
Normal file
3
Software/Java/scripts/Test.sh
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#/usr/bin/env bash
|
||||
# $ JAVA_OPTS='-Dconfig=default' Java/bin/runTest.sh
|
||||
cd ./bin; sudo java $JAVA_OPTS -classpath .:classes:/opt/pi4j/lib/'*' Test
|
||||
3
Software/Java/scripts/compile.sh
Normal file
3
Software/Java/scripts/compile.sh
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#/usr/bin/env bash
|
||||
mkdir -p ./bin
|
||||
javac -d ./bin -classpath .:classes:/opt/pi4j/lib/'*' ./src/com/dexterind/grovepi/*.java ./src/com/dexterind/grovepi/sensors/*.java ./src/com/dexterind/grovepi/sensors/base/*.java ./src/com/dexterind/grovepi/events/*.java ./src/com/dexterind/grovepi/utils/*.java ./test/tests/*.java ./test/*.java;
|
||||
102
Software/Java/src/com/dexterind/grovepi/Board.java
Normal file
102
Software/Java/src/com/dexterind/grovepi/Board.java
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
package com.dexterind.grovepi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
|
||||
import com.dexterind.grovepi.*;
|
||||
import com.dexterind.grovepi.events.*;
|
||||
import com.dexterind.grovepi.utils.*;
|
||||
|
||||
import com.pi4j.io.i2c.I2CBus;
|
||||
import com.pi4j.io.i2c.I2CDevice;
|
||||
import com.pi4j.io.i2c.I2CFactory;
|
||||
import com.pi4j.system.NetworkInfo;
|
||||
import com.pi4j.system.SystemInfo;
|
||||
|
||||
public class Board {
|
||||
|
||||
private static Board instance = null;
|
||||
private final I2CDevice device;
|
||||
|
||||
public static final byte PIN_MODE_OUTPUT = 1;
|
||||
public static final byte PIN_MODE_INPUT = 0;
|
||||
private static final byte ADDRESS = 0x04;
|
||||
|
||||
private Debug debug;
|
||||
|
||||
public Board() throws IOException, InterruptedException, Exception {
|
||||
int busId;
|
||||
|
||||
String type = SystemInfo.getBoardType().name();
|
||||
|
||||
if (type.indexOf("ModelA") > 0) {
|
||||
busId = I2CBus.BUS_0;
|
||||
} else {
|
||||
busId = I2CBus.BUS_1;
|
||||
}
|
||||
|
||||
final I2CBus bus = I2CFactory.getInstance(busId);
|
||||
device = bus.getDevice(ADDRESS);
|
||||
}
|
||||
|
||||
public static Board getInstance() throws IOException, InterruptedException, Exception {
|
||||
if(instance == null) {
|
||||
instance = new Board();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public int writeI2c(int... bytes) throws IOException {
|
||||
// Convert array: int[] to byte[]
|
||||
final ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
|
||||
for (int i = 0, len = bytes.length; i < len; i++) {
|
||||
byteBuffer.put((byte) bytes[i]);
|
||||
}
|
||||
sleep(100);
|
||||
device.write(0xfe, byteBuffer.array(), 0, byteBuffer.limit());
|
||||
return Statuses.OK;
|
||||
}
|
||||
|
||||
public byte[] readI2c(int numberOfBytes) throws IOException {
|
||||
byte[] buffer = new byte[numberOfBytes];
|
||||
device.read(1, buffer, 0, buffer.length);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public int setPinMode(int pin, int pinMode) throws IOException {
|
||||
return writeI2c(Commands.PMODE, pin, pinMode, Commands.UNUSED);
|
||||
}
|
||||
|
||||
public void sleep(int msec) {
|
||||
try {
|
||||
Thread.sleep(msec);
|
||||
} catch (InterruptedException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
try {
|
||||
device.write(0xfe, (byte)0x04);
|
||||
} catch (IOException e) {
|
||||
StringWriter sw = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(sw));
|
||||
String exceptionDetails = sw.toString();
|
||||
debug.log(Debug.SEVERE, exceptionDetails);
|
||||
}
|
||||
}
|
||||
|
||||
public String version() throws IOException {
|
||||
writeI2c(Commands.VERSION, Commands.UNUSED, Commands.UNUSED, Commands.UNUSED);
|
||||
sleep(100);
|
||||
|
||||
byte[] b = readI2c(4);
|
||||
readI2c(1);
|
||||
|
||||
return String.format("%s.%s.%s", (int)b[1], (int)b[2], (int)b[3]);
|
||||
}
|
||||
|
||||
}
|
||||
86
Software/Java/src/com/dexterind/grovepi/Grovepi.java
Normal file
86
Software/Java/src/com/dexterind/grovepi/Grovepi.java
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
package com.dexterind.grovepi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import com.dexterind.grovepi.*;
|
||||
import com.dexterind.grovepi.sensors.*;
|
||||
import com.dexterind.grovepi.events.*;
|
||||
import com.dexterind.grovepi.utils.*;
|
||||
|
||||
public final class Grovepi {
|
||||
private static Grovepi instance;
|
||||
|
||||
private boolean isInit = false;
|
||||
private boolean isHalt = false;
|
||||
|
||||
public Board board;
|
||||
private final CopyOnWriteArrayList<GrovepiListener> listeners;
|
||||
|
||||
private Debug debug;
|
||||
|
||||
public Grovepi() throws Exception {
|
||||
debug = new Debug("com.dexterind.gopigo.Grovepi");
|
||||
debug.log(Debug.FINEST, "Instancing a new GrovePi");
|
||||
|
||||
try {
|
||||
board = Board.getInstance();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
listeners = new CopyOnWriteArrayList<GrovepiListener>();
|
||||
}
|
||||
|
||||
public static Grovepi getInstance() {
|
||||
if(instance == null) {
|
||||
try {
|
||||
instance = new Grovepi();
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("There was an error");
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
debug.log(Debug.FINE, "Init " + isInit);
|
||||
board.init();
|
||||
isInit = true;
|
||||
StatusEvent statusEvent = new StatusEvent(this, Statuses.INIT);
|
||||
fireEvent(statusEvent);
|
||||
}
|
||||
|
||||
public void addListener(GrovepiListener listener) {
|
||||
debug.log(Debug.INFO, "Adding listener");
|
||||
listeners.addIfAbsent(listener);
|
||||
}
|
||||
|
||||
public void removeListener(GrovepiListener listener) {
|
||||
if (listeners != null) {
|
||||
debug.log(Debug.INFO, "Removing listener");
|
||||
listeners.remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
protected void fireEvent(EventObject event) {
|
||||
int i = 0;
|
||||
debug.log(Debug.INFO, "Firing event [" + listeners.toArray().length + " listeners]");
|
||||
|
||||
for (GrovepiListener listener : listeners) {
|
||||
debug.log(Debug.INFO, "listener[" + i + "]");
|
||||
debug.log(Debug.INFO, event.getClass().toString());
|
||||
|
||||
if (event instanceof StatusEvent) {
|
||||
listener.onStatusEvent((StatusEvent) event);
|
||||
} else if (event instanceof SensorEvent) {
|
||||
listener.onSensorEvent((SensorEvent) event);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.dexterind.grovepi;
|
||||
|
||||
import com.dexterind.grovepi.events.*;
|
||||
import java.util.EventListener;
|
||||
|
||||
public interface GrovepiListener extends EventListener {
|
||||
public void onStatusEvent(StatusEvent event);
|
||||
public void onSensorEvent(SensorEvent event);
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package com.dexterind.grovepi.events;
|
||||
|
||||
import java.util.EventObject;
|
||||
|
||||
public class SensorEvent extends EventObject {
|
||||
private static final long serialVersionUID = 5992043874332938157L;
|
||||
public String value;
|
||||
|
||||
public SensorEvent(Object source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
public SensorEvent(Object source, String value) {
|
||||
this(source);
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.dexterind.grovepi.events;
|
||||
|
||||
import java.util.EventObject;
|
||||
|
||||
public class StatusEvent extends EventObject {
|
||||
|
||||
private static final long serialVersionUID = -2236533038040111378L;
|
||||
public int status;
|
||||
|
||||
public StatusEvent(Object source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
public StatusEvent(Object source, int status) {
|
||||
this(source);
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
package com.dexterind.grovepi.sensors;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.dexterind.grovepi.*;
|
||||
import com.dexterind.grovepi.sensors.base.*;
|
||||
import com.dexterind.grovepi.utils.*;
|
||||
|
||||
public class DHTDigitalSensor extends DigitalSensor {
|
||||
private int moduleType = 0;
|
||||
private int scale = 0;
|
||||
|
||||
private Debug debug;
|
||||
|
||||
public static final int MODULE_DHT11 = 0;
|
||||
public static final int MODULE_DHT22 = 1;
|
||||
public static final int MODULE_DHT21 = 2;
|
||||
public static final int MODULE_AM2301 = 3;
|
||||
|
||||
public static final int SCALE_C = 0;
|
||||
public static final int SCALE_F = 1;
|
||||
|
||||
private byte[] bytes;
|
||||
|
||||
private float convertCtoF(double temp) {
|
||||
return (float) temp * 9 / 5 + 32;
|
||||
}
|
||||
|
||||
private float convertFtoC(double temp) {
|
||||
return (float) ((temp - 32) * 5 / 9);
|
||||
}
|
||||
|
||||
private float getHeatIndex(float temp, float hum, int scale) {
|
||||
boolean needsConversion = scale == DHTDigitalSensor.SCALE_C;
|
||||
temp = needsConversion ? this.convertCtoF(temp) : temp;
|
||||
|
||||
double hi = -42.379 +
|
||||
2.04901523 * temp +
|
||||
10.14333127 * hum +
|
||||
-0.22475541 * temp * hum +
|
||||
-0.00683783 * Math.pow(temp, 2) +
|
||||
-0.05481717 * Math.pow(hum, 2) +
|
||||
0.00122874 * Math.pow(temp, 2) * hum +
|
||||
0.00085282 * temp * Math.pow(hum, 2) +
|
||||
-0.00000199 * Math.pow(temp, 2) * Math.pow(hum, 2);
|
||||
|
||||
return (float) (needsConversion ? this.convertFtoC(hi) : hi);
|
||||
}
|
||||
|
||||
public DHTDigitalSensor(int pin, int moduleType, int scale) throws IOException, InterruptedException, Exception {
|
||||
super(pin);
|
||||
this.moduleType = moduleType;
|
||||
this.scale = scale;
|
||||
}
|
||||
|
||||
private void storeBytes() throws IOException {
|
||||
this.board.writeI2c(Commands.DHT_TEMP, this.pin, this.moduleType, Commands.UNUSED);
|
||||
this.board.sleep(500);
|
||||
|
||||
this.board.readI2c(1);
|
||||
this.board.sleep(200);
|
||||
|
||||
this.bytes = this.board.readI2c(9);
|
||||
}
|
||||
|
||||
public float[] read() {
|
||||
try {
|
||||
this.storeBytes();
|
||||
} catch(IOException e) {
|
||||
System.err.println("IOException: " + e.getMessage());
|
||||
}
|
||||
|
||||
float temp = ByteBuffer.wrap(this.bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat(1);
|
||||
float hum = ByteBuffer.wrap(this.bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat(5);
|
||||
float heatIndex = this.getHeatIndex(temp, hum, this.scale);
|
||||
|
||||
return new float[]{temp, hum , heatIndex};
|
||||
}
|
||||
}
|
||||
150
Software/Java/src/com/dexterind/grovepi/sensors/Led.java
Normal file
150
Software/Java/src/com/dexterind/grovepi/sensors/Led.java
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
package com.dexterind.grovepi.sensors;
|
||||
|
||||
/*
|
||||
* **********************************************************************
|
||||
* PROJECT : GrovePi Java Library
|
||||
*
|
||||
* This file is part of the GrovePi Java Library project. More information about
|
||||
* this project can be found here: https://github.com/DexterInd/GrovePi
|
||||
* **********************************************************************
|
||||
*
|
||||
* ## License
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.dexterind.grovepi.sensors.base.*;
|
||||
|
||||
/**
|
||||
* Handles control of LED sensor for GrovePi.
|
||||
*
|
||||
* Note the LED will only be dimmable if the sensor is connected
|
||||
* to a port that supports Pulse Width Modulation
|
||||
*
|
||||
* Digital ports that support Pulse Width Modulation (PWM)
|
||||
* D3, D5, D6
|
||||
*
|
||||
* Digital ports that do not support PWM
|
||||
* D2, D4, D7, D8
|
||||
*
|
||||
* @author Chad Williams
|
||||
*
|
||||
*/
|
||||
public class Led extends AnalogSensor{
|
||||
private SensorStatus status = SensorStatus.OFF;
|
||||
public final static int MAX_BRIGHTNESS = 255;
|
||||
|
||||
/**
|
||||
* Create a new LED connected to the specified pin.
|
||||
*
|
||||
* @param pin GrovePi pin number
|
||||
* @throws IOException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public Led(int pin) throws IOException, InterruptedException, Exception {
|
||||
super(pin, MAX_BRIGHTNESS + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the LED on to the maximum brightness.
|
||||
* @throws IOException
|
||||
*/
|
||||
public void turnOn() throws IOException{
|
||||
this.write(MAX_BRIGHTNESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the LED off.
|
||||
* @throws IOException
|
||||
*/
|
||||
public void turnOff() throws IOException{
|
||||
this.write(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the LED brightness to the specified percentage of the maximum brightness,
|
||||
* note dimming only works if connected to a pin that supports PWM otherwise this
|
||||
* will just appear to turn the LED on/off.
|
||||
* @param percent Percentage of maximum brightness, expects a number from 0 to 100
|
||||
* @throws IOException
|
||||
*/
|
||||
public void setBrightness(float percent) throws IOException{
|
||||
if (percent <= 0){
|
||||
turnOff();
|
||||
}else if(percent >= 100){
|
||||
turnOn();
|
||||
}else{
|
||||
this.write((int)(MAX_BRIGHTNESS * percent/100));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the analog value of the LED and sensor status.
|
||||
* @param value Expects a value from 0 to MAX_BRIGHTNESS
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public boolean write(int value) throws IOException {
|
||||
if (value <= 0){
|
||||
status = SensorStatus.OFF;
|
||||
super.write(0);
|
||||
}else{
|
||||
status = SensorStatus.ON;
|
||||
super.write(Math.min(value, MAX_BRIGHTNESS));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles the LED on/off.
|
||||
* @return Returns the new status of the LED
|
||||
* @throws IOException
|
||||
*/
|
||||
public SensorStatus toggle() throws IOException{
|
||||
setStatus(SensorStatus.toggle(status));
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current status of the LED.
|
||||
* @return Returns the status of the LED
|
||||
*/
|
||||
public SensorStatus getStatus(){
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the status of the LED to the passed status.
|
||||
* @param status
|
||||
* @throws IOException
|
||||
*/
|
||||
public void setStatus(SensorStatus status) throws IOException{
|
||||
if (status == SensorStatus.OFF){
|
||||
turnOff();
|
||||
}else{
|
||||
turnOn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.dexterind.grovepi.sensors.base;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.dexterind.grovepi.*;
|
||||
import com.dexterind.grovepi.utils.*;
|
||||
|
||||
public class AnalogSensor extends Sensor {
|
||||
protected int pin = 0;
|
||||
private int length = 4;
|
||||
|
||||
protected Debug debug;
|
||||
|
||||
public AnalogSensor(int pin, int length) throws IOException, InterruptedException, Exception {
|
||||
super();
|
||||
this.pin = pin;
|
||||
this.length = length == -1 ? this.length : length;
|
||||
}
|
||||
|
||||
public byte[] readBytes() throws IOException {
|
||||
this.board.writeI2c(Commands.AREAD, this.pin, Commands.UNUSED, Commands.UNUSED);
|
||||
this.board.readI2c(1);
|
||||
return this.board.readI2c(this.length);
|
||||
}
|
||||
|
||||
public boolean write(int value) throws IOException {
|
||||
this.board.writeI2c(Commands.AWRITE, this.pin, value, Commands.UNUSED);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package com.dexterind.grovepi.sensors.base;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.dexterind.grovepi.*;
|
||||
import com.dexterind.grovepi.utils.*;
|
||||
|
||||
public class DigitalSensor extends Sensor {
|
||||
protected int pin = 0;
|
||||
protected Debug debug;
|
||||
|
||||
public DigitalSensor(int pin) throws IOException, InterruptedException, Exception {
|
||||
super();
|
||||
this.pin = pin;
|
||||
}
|
||||
|
||||
public byte[] readBytes() throws IOException {
|
||||
this.board.writeI2c(Commands.DREAD, this.pin, Commands.UNUSED, Commands.UNUSED);
|
||||
return this.board.readI2c(1);
|
||||
}
|
||||
|
||||
public boolean write(int value) throws IOException {
|
||||
this.board.writeI2c(Commands.DWRITE, this.pin, value, Commands.UNUSED);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package com.dexterind.grovepi.sensors.base;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.dexterind.grovepi.*;
|
||||
import com.dexterind.grovepi.utils.*;
|
||||
|
||||
public class I2cSensor extends Sensor {
|
||||
protected int pin = 0;
|
||||
protected Debug debug;
|
||||
|
||||
public I2cSensor(int pin) throws IOException, InterruptedException, Exception {
|
||||
super();
|
||||
this.pin = pin;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/**
|
||||
* TODO: Implement "watch" and "stream" methods, such as the Node.js library
|
||||
*/
|
||||
package com.dexterind.grovepi.sensors.base;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.dexterind.grovepi.*;
|
||||
import com.dexterind.grovepi.utils.*;
|
||||
|
||||
public class Sensor {
|
||||
protected Board board;
|
||||
|
||||
public Sensor() throws IOException, InterruptedException, Exception {
|
||||
board = Board.getInstance();
|
||||
}
|
||||
|
||||
public byte[] readBytes() throws IOException { byte[] ret = {}; return ret; }
|
||||
public boolean write() throws IOException { return true; }
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
package com.dexterind.grovepi.sensors.base;
|
||||
|
||||
/*
|
||||
* **********************************************************************
|
||||
* PROJECT : GrovePi Java Library
|
||||
*
|
||||
* This file is part of the GrovePi Java Library project. More information about
|
||||
* this project can be found here: https://github.com/DexterInd/GrovePi
|
||||
* **********************************************************************
|
||||
*
|
||||
* ## License
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Enumeration for Sensor state.
|
||||
*
|
||||
* @author Chad Williams
|
||||
*/
|
||||
public enum SensorStatus {
|
||||
ON("On"),
|
||||
OFF("Off");
|
||||
|
||||
private final String status;
|
||||
|
||||
private SensorStatus(String status){
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public static SensorStatus toggle(SensorStatus status){
|
||||
if (status == ON){
|
||||
return OFF;
|
||||
}else{
|
||||
return ON;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return status;
|
||||
}
|
||||
}
|
||||
52
Software/Java/src/com/dexterind/grovepi/utils/Commands.java
Normal file
52
Software/Java/src/com/dexterind/grovepi/utils/Commands.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package com.dexterind.grovepi.utils;
|
||||
|
||||
public class Commands {
|
||||
public static final int DREAD = 1;
|
||||
public static final int DWRITE = 2;
|
||||
public static final int AREAD = 3;
|
||||
public static final int AWRITE = 4;
|
||||
public static final int PMODE = 5;
|
||||
public static final int UREAD = 7;
|
||||
public static final int VERSION = 8;
|
||||
public static final int ACC_XYZ = 20;
|
||||
public static final int RTC_GET_TIME = 30;
|
||||
public static final int DHT_TEMP = 40;
|
||||
public static final int LEDBAR_INIT = 50;
|
||||
public static final int LEDBAR_ORIENT = 51;
|
||||
public static final int LEDBAR_LEVEL = 52;
|
||||
public static final int LEDBAR_SET_ONE = 53;
|
||||
public static final int LEDBAR_TOGGLE_ONE = 54;
|
||||
public static final int LEDBAR_SET = 55;
|
||||
public static final int LEDBAR_GET = 56;
|
||||
public static final int FOUR_DIGIT_INIT = 70;
|
||||
public static final int FOUR_DIGIT_BRIGHTNESS = 71;
|
||||
public static final int FOUR_DIGIT_VALUE = 72;
|
||||
public static final int FOUR_DIGIT_VALUE_ZEROS = 73;
|
||||
public static final int FOUR_DIGIT_INDIVIDUAL_DIGIT = 74;
|
||||
public static final int FOUR_DIGIT_INDIVIDUAL_LEDS = 75;
|
||||
public static final int FOUR_DIGIT_SCORE = 76;
|
||||
public static final int FOUR_DIGIT_AREAD = 77;
|
||||
public static final int FOUR_DIGIT_ALL_ON = 78;
|
||||
public static final int FOUR_DIGIT_ALL_OFF = 79;
|
||||
public static final int STORE_COLOR = 90;
|
||||
public static final int CHAINABLE_RGB_LED_INIT = 91;
|
||||
public static final int CHAINABLE_RGB_LED_TEST = 92;
|
||||
public static final int CHAINABLE_RGB_LED_SET_PATTERN = 93;
|
||||
public static final int CHAINABLE_RGB_LED_SET_MODULO = 94;
|
||||
public static final int CHAINABLE_RGB_LED_SET_LEVEL = 95;
|
||||
public static final int IR_READ = 21;
|
||||
public static final int IR_RECV_PIN = 22;
|
||||
public static final int DUST_SENSOR_READ = 10;
|
||||
public static final int DUST_SENSOR_EN = 14;
|
||||
public static final int DUST_SENSOR_DIS = 15;
|
||||
public static final int ENCODER_READ = 11;
|
||||
public static final int ENCODER_EN = 16;
|
||||
public static final int ENCODER_DIS = 17;
|
||||
public static final int FLOW_READ = 12;
|
||||
public static final int FLOW_EN = 18;
|
||||
public static final int FLOW_DIS = 13;
|
||||
|
||||
public static final int UNUSED = 0;
|
||||
|
||||
public Commands(){}
|
||||
}
|
||||
105
Software/Java/src/com/dexterind/grovepi/utils/Debug.java
Normal file
105
Software/Java/src/com/dexterind/grovepi/utils/Debug.java
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
package com.dexterind.grovepi.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.FileHandler;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.SimpleFormatter;
|
||||
|
||||
public class Debug {
|
||||
private Logger logger;
|
||||
private FileHandler fh;
|
||||
|
||||
public static final int FINEST = 1;
|
||||
public static final int FINER = 2;
|
||||
public static final int FINE = 3;
|
||||
public static final int CONFIG = 4;
|
||||
public static final int INFO = 5;
|
||||
public static final int WARNING = 6;
|
||||
public static final int SEVERE = 7;
|
||||
|
||||
private Boolean debug = false;
|
||||
private String logDir = "/var/log/grovepi";
|
||||
private String logFile = "all.log";
|
||||
|
||||
public Debug(String target) {
|
||||
Properties prop = new Properties();
|
||||
InputStream input = null;
|
||||
|
||||
try {
|
||||
String configProp = System.getProperty("config") == null ? "default" : System.getProperty("config");
|
||||
input = new FileInputStream(System.getProperty("user.dir") + "/../config/" + configProp + ".properties");
|
||||
prop.load(input);
|
||||
|
||||
debug = Boolean.valueOf(prop.getProperty("debug"));
|
||||
logDir = prop.getProperty("logDir");
|
||||
logFile = prop.getProperty("logFile");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (input != null) {
|
||||
try {
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger = Logger.getLogger(target);
|
||||
try {
|
||||
File file = new File(logDir);
|
||||
if (!file.exists()) {
|
||||
if (file.mkdir()) {
|
||||
System.out.println("Log Directory is created!");
|
||||
} else {
|
||||
System.out.println("Failed to create log directory!");
|
||||
}
|
||||
}
|
||||
fh = new FileHandler(logDir + "/" + logFile, true);
|
||||
logger.addHandler(fh);
|
||||
logger.setUseParentHandlers(false);
|
||||
// this one disables the console log
|
||||
SimpleFormatter formatter = new SimpleFormatter();
|
||||
fh.setFormatter(formatter);
|
||||
} catch (SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void log(int level, String message) {
|
||||
if (!debug) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (level) {
|
||||
default:
|
||||
case Debug.FINEST:
|
||||
logger.finest(message);
|
||||
break;
|
||||
case Debug.FINER:
|
||||
logger.finer(message);
|
||||
break;
|
||||
case Debug.FINE:
|
||||
logger.fine(message);
|
||||
break;
|
||||
case Debug.CONFIG:
|
||||
logger.config(message);
|
||||
break;
|
||||
case Debug.INFO:
|
||||
logger.info(message);
|
||||
break;
|
||||
case Debug.WARNING:
|
||||
logger.warning(message);
|
||||
break;
|
||||
case Debug.SEVERE:
|
||||
logger.severe(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.dexterind.grovepi.utils;
|
||||
|
||||
public class Statuses {
|
||||
public static final int OK = 1;
|
||||
public static final int ERROR = -1;
|
||||
public static final int INIT = 2;
|
||||
|
||||
public Statuses(){}
|
||||
}
|
||||
9
Software/Java/test/Test.java
Normal file
9
Software/Java/test/Test.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/* Run: Java$ ./bin/compile.sh && ./bin/Test.sh */
|
||||
import java.io.IOException;
|
||||
import tests.*;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) throws IOException, InterruptedException, Exception {
|
||||
BaseTest grovepiTest = new BaseTest();
|
||||
}
|
||||
}
|
||||
60
Software/Java/test/tests/BaseTest.java
Normal file
60
Software/Java/test/tests/BaseTest.java
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package tests;
|
||||
|
||||
import com.dexterind.grovepi.*;
|
||||
import com.dexterind.grovepi.sensors.base.*;
|
||||
import com.dexterind.grovepi.sensors.*;
|
||||
import com.dexterind.grovepi.events.*;
|
||||
import com.dexterind.grovepi.utils.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BaseTest implements GrovepiListener {
|
||||
private static Grovepi grovepi = null;
|
||||
|
||||
public BaseTest() throws IOException, InterruptedException, Exception {
|
||||
System.out.println("-----------------------------------------------------");
|
||||
System.out.println(" GrovePi test application");
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
grovepi = Grovepi.getInstance();
|
||||
grovepi.addListener(this);
|
||||
grovepi.init();
|
||||
}
|
||||
|
||||
public void onStatusEvent(StatusEvent event) {
|
||||
if (event.status == 2) {
|
||||
|
||||
try {
|
||||
System.out.print( grovepi.board.version() );
|
||||
} catch( IOException e) {
|
||||
System.out.print( e.getMessage() );
|
||||
}
|
||||
|
||||
try {
|
||||
System.out.print( "\n" );
|
||||
this.getDHTValue();
|
||||
System.out.print( "\n" );
|
||||
} catch ( Exception e) {
|
||||
System.out.print( "Err:" + e.getMessage() );
|
||||
}
|
||||
}
|
||||
};
|
||||
public void onSensorEvent(SensorEvent event) {
|
||||
System.out.print(event.value);
|
||||
};
|
||||
|
||||
public void getDHTValue() throws IOException, InterruptedException, Exception {
|
||||
int pin = 2; // D2
|
||||
DHTDigitalSensor sensor = new DHTDigitalSensor(
|
||||
pin,
|
||||
DHTDigitalSensor.MODULE_DHT22,
|
||||
DHTDigitalSensor.SCALE_C
|
||||
);
|
||||
float[] output = sensor.read();
|
||||
System.out.print( Arrays.toString(output) );
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue