Bug fixes for Raspberry sensors

Former-commit-id: 59e5c0f293850c14bc500643908262bde02dfa7f
This commit is contained in:
Daniel Collin 2016-01-30 22:24:46 +01:00
parent 279c5deeb0
commit 41ebf9ac30
3 changed files with 29 additions and 17 deletions

View file

@ -31,20 +31,20 @@ public class RPiController implements HalSensorController {
@Override @Override
public void register(HalSensorData sensor) { public void register(HalSensorData sensor) {
if(sensor instanceof RPiPowerConsumptionSensor){ if(sensor instanceof RPiPowerConsumptionSensor){
RPiPowerConsumptionSensor powerConsumprtionSensor = (RPiPowerConsumptionSensor) sensor; RPiPowerConsumptionSensor powerConsumptionSensor = (RPiPowerConsumptionSensor) sensor;
Pin gpioPin = powerConsumprtionSensor.getGpioPin(); int gpioPin = powerConsumptionSensor.getGpioPin();
if(!pinToSensorMap.containsKey(gpioPin.getName())){ if(!pinToSensorMap.containsKey("GPIO_"+gpioPin)){
RPiInteruptPulseFlankCounter impulseCounter = new RPiInteruptPulseFlankCounter(gpioPin, this); RPiInteruptPulseFlankCounter impulseCounter = new RPiInteruptPulseFlankCounter(gpioPin, this);
pinToSensorMap.put(gpioPin.getName(), impulseCounter); pinToSensorMap.put("GPIO_"+gpioPin, impulseCounter);
}else{ }else{
logger.warning("Cannot create a RPiPowerConsumptionSensor on GPIO pin " + gpioPin + " since is already is in use by another sensor."); logger.warning("Cannot create a RPiPowerConsumptionSensor on GPIO pin " + gpioPin + " since is already is in use by another sensor.");
} }
} else if(sensor instanceof RPiTemperatureSensor){ } else if(sensor instanceof RPiTemperatureSensor){
RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) sensor; RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) sensor;
String w1Address = temperatureSensor.get1WAddress(); String w1Address = temperatureSensor.get1WAddress();
if(!pinToSensorMap.containsKey(w1Address)){ if(!pinToSensorMap.containsKey("W1_"+w1Address)){
RPiDS18B20 ds12b20 = new RPiDS18B20(w1Address, this); RPiDS18B20 ds12b20 = new RPiDS18B20(w1Address, this);
pinToSensorMap.put(w1Address, ds12b20); pinToSensorMap.put("W1_"+w1Address, ds12b20);
}else{ }else{
logger.warning("Cannot create a RPi1WireTemperatureSensor on 1-Wire address " + w1Address + " since is already is in use by another sensor."); logger.warning("Cannot create a RPi1WireTemperatureSensor on 1-Wire address " + w1Address + " since is already is in use by another sensor.");
} }
@ -57,13 +57,13 @@ public class RPiController implements HalSensorController {
public void deregister(HalSensorData sensor) { public void deregister(HalSensorData sensor) {
if(sensor instanceof RPiPowerConsumptionSensor){ if(sensor instanceof RPiPowerConsumptionSensor){
RPiPowerConsumptionSensor powerConsumprtionSensor = (RPiPowerConsumptionSensor) sensor; RPiPowerConsumptionSensor powerConsumprtionSensor = (RPiPowerConsumptionSensor) sensor;
RPiSensor sensorToDeregister = pinToSensorMap.remove(powerConsumprtionSensor.getGpioPin().getName()); RPiSensor sensorToDeregister = pinToSensorMap.remove("GPIO_"+powerConsumprtionSensor.getGpioPin());
if(sensorToDeregister != null){ if(sensorToDeregister != null){
sensorToDeregister.close(); sensorToDeregister.close();
} }
} else if(sensor instanceof RPiTemperatureSensor){ } else if(sensor instanceof RPiTemperatureSensor){
RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) sensor; RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) sensor;
RPiSensor sensorToDeregister = pinToSensorMap.remove(temperatureSensor.get1WAddress()); RPiSensor sensorToDeregister = pinToSensorMap.remove("W1_"+temperatureSensor.get1WAddress());
if(sensorToDeregister != null){ if(sensorToDeregister != null){
sensorToDeregister.close(); sensorToDeregister.close();
} }

View file

@ -18,7 +18,8 @@ public class RPiPowerConsumptionSensor implements PowerConsumptionSensorData {
//need to be empty for the framework to create an instance //need to be empty for the framework to create an instance
} }
public RPiPowerConsumptionSensor(long timestamp, double data) { public RPiPowerConsumptionSensor(int gpioPin, long timestamp, double data) {
this.gpioPin = gpioPin;
this.timestamp = timestamp; this.timestamp = timestamp;
this.data = data; this.data = data;
} }
@ -44,12 +45,16 @@ public class RPiPowerConsumptionSensor implements PowerConsumptionSensorData {
} }
public boolean equals(Object obj){ public boolean equals(Object obj){
if(obj instanceof RPiPowerConsumptionSensor) if(!(obj instanceof RPiPowerConsumptionSensor))
return obj == this; return false;
return false; return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin;
} }
public Pin getGpioPin() { public int getGpioPin() {
return RPiUtility.getPin(gpioPin); return gpioPin;
} }
public String toString(){
return "gpioPin:" + gpioPin +", data:" + data;
}
} }

View file

@ -8,6 +8,7 @@ import java.util.logging.Logger;
import se.hal.plugin.raspberry.RPiController; import se.hal.plugin.raspberry.RPiController;
import se.hal.plugin.raspberry.RPiPowerConsumptionSensor; import se.hal.plugin.raspberry.RPiPowerConsumptionSensor;
import se.hal.plugin.raspberry.RPiSensor; import se.hal.plugin.raspberry.RPiSensor;
import se.hal.plugin.raspberry.RPiUtility;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioController;
@ -18,6 +19,7 @@ import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState; import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent; import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital; import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import com.pi4j.wiringpi.GpioUtil;
public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDigital, RPiSensor { public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDigital, RPiSensor {
private static final int REPORT_TIMEOUT = 60_000; //one minute private static final int REPORT_TIMEOUT = 60_000; //one minute
@ -29,13 +31,18 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
private long nanoSecondsSleep = REPORT_TIMEOUT * 1_000_000L; private long nanoSecondsSleep = REPORT_TIMEOUT * 1_000_000L;
private volatile Integer impulseCount = 0; private volatile Integer impulseCount = 0;
private GpioPinDigitalInput irLightSensor; private GpioPinDigitalInput irLightSensor;
private final int gpioPin;
public RPiInteruptPulseFlankCounter(Pin gpioPin, RPiController controller){ public RPiInteruptPulseFlankCounter(int gpioPin, RPiController controller){
this.controller = controller; this.controller = controller;
this.gpioPin = gpioPin;
// setup a thread pool for executing jobs // setup a thread pool for executing jobs
this.executorPool = Executors.newCachedThreadPool(); this.executorPool = Executors.newCachedThreadPool();
//Enable non privileged access to the GPIO pins (no sudo required from now)
GpioUtil.enableNonPrivilegedAccess();
// create gpio controller // create gpio controller
GpioController gpio = null; GpioController gpio = null;
try{ try{
@ -49,7 +56,7 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
} }
// provision gpio pin as an input pin with its internal pull up resistor enabled // provision gpio pin as an input pin with its internal pull up resistor enabled
irLightSensor = gpio.provisionDigitalInputPin(gpioPin, PinPullResistance.PULL_UP); irLightSensor = gpio.provisionDigitalInputPin(RPiUtility.getPin(gpioPin), PinPullResistance.PULL_UP);
// create and register gpio pin listener. May require the program to be run as sudo if the GPIO pin has not been exported // create and register gpio pin listener. May require the program to be run as sudo if the GPIO pin has not been exported
irLightSensor.addListener(this); irLightSensor.addListener(this);
@ -131,7 +138,7 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
@Override @Override
public void run() { public void run() {
logger.log(Level.INFO, "Reporting data. timestamp_end="+timestamp_end+", data="+data); logger.log(Level.INFO, "Reporting data. timestamp_end="+timestamp_end+", data="+data);
controller.sendDataReport(new RPiPowerConsumptionSensor(timestamp_end, data)); controller.sendDataReport(new RPiPowerConsumptionSensor(gpioPin, timestamp_end, data));
} }
}); });
} }