From 41ebf9ac30396b261e7850b211a353e515dc77da Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Sat, 30 Jan 2016 22:24:46 +0100 Subject: [PATCH] Bug fixes for Raspberry sensors Former-commit-id: 59e5c0f293850c14bc500643908262bde02dfa7f --- src/se/hal/plugin/raspberry/RPiController.java | 16 ++++++++-------- .../raspberry/RPiPowerConsumptionSensor.java | 17 +++++++++++------ .../hardware/RPiInteruptPulseFlankCounter.java | 13 ++++++++++--- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/se/hal/plugin/raspberry/RPiController.java b/src/se/hal/plugin/raspberry/RPiController.java index b58f12f0..a11f4fd4 100644 --- a/src/se/hal/plugin/raspberry/RPiController.java +++ b/src/se/hal/plugin/raspberry/RPiController.java @@ -31,20 +31,20 @@ public class RPiController implements HalSensorController { @Override public void register(HalSensorData sensor) { if(sensor instanceof RPiPowerConsumptionSensor){ - RPiPowerConsumptionSensor powerConsumprtionSensor = (RPiPowerConsumptionSensor) sensor; - Pin gpioPin = powerConsumprtionSensor.getGpioPin(); - if(!pinToSensorMap.containsKey(gpioPin.getName())){ + RPiPowerConsumptionSensor powerConsumptionSensor = (RPiPowerConsumptionSensor) sensor; + int gpioPin = powerConsumptionSensor.getGpioPin(); + if(!pinToSensorMap.containsKey("GPIO_"+gpioPin)){ RPiInteruptPulseFlankCounter impulseCounter = new RPiInteruptPulseFlankCounter(gpioPin, this); - pinToSensorMap.put(gpioPin.getName(), impulseCounter); + pinToSensorMap.put("GPIO_"+gpioPin, impulseCounter); }else{ logger.warning("Cannot create a RPiPowerConsumptionSensor on GPIO pin " + gpioPin + " since is already is in use by another sensor."); } } else if(sensor instanceof RPiTemperatureSensor){ RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) sensor; String w1Address = temperatureSensor.get1WAddress(); - if(!pinToSensorMap.containsKey(w1Address)){ + if(!pinToSensorMap.containsKey("W1_"+w1Address)){ RPiDS18B20 ds12b20 = new RPiDS18B20(w1Address, this); - pinToSensorMap.put(w1Address, ds12b20); + pinToSensorMap.put("W1_"+w1Address, ds12b20); }else{ 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) { if(sensor instanceof RPiPowerConsumptionSensor){ RPiPowerConsumptionSensor powerConsumprtionSensor = (RPiPowerConsumptionSensor) sensor; - RPiSensor sensorToDeregister = pinToSensorMap.remove(powerConsumprtionSensor.getGpioPin().getName()); + RPiSensor sensorToDeregister = pinToSensorMap.remove("GPIO_"+powerConsumprtionSensor.getGpioPin()); if(sensorToDeregister != null){ sensorToDeregister.close(); } } else if(sensor instanceof RPiTemperatureSensor){ RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) sensor; - RPiSensor sensorToDeregister = pinToSensorMap.remove(temperatureSensor.get1WAddress()); + RPiSensor sensorToDeregister = pinToSensorMap.remove("W1_"+temperatureSensor.get1WAddress()); if(sensorToDeregister != null){ sensorToDeregister.close(); } diff --git a/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java b/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java index 13e07123..eca9829a 100644 --- a/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java +++ b/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java @@ -18,7 +18,8 @@ public class RPiPowerConsumptionSensor implements PowerConsumptionSensorData { //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.data = data; } @@ -44,12 +45,16 @@ public class RPiPowerConsumptionSensor implements PowerConsumptionSensorData { } public boolean equals(Object obj){ - if(obj instanceof RPiPowerConsumptionSensor) - return obj == this; - return false; + if(!(obj instanceof RPiPowerConsumptionSensor)) + return false; + return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin; } - public Pin getGpioPin() { - return RPiUtility.getPin(gpioPin); + public int getGpioPin() { + return gpioPin; } + + public String toString(){ + return "gpioPin:" + gpioPin +", data:" + data; + } } diff --git a/src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java b/src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java index 92c52bde..cd187133 100644 --- a/src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java +++ b/src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java @@ -8,6 +8,7 @@ import java.util.logging.Logger; import se.hal.plugin.raspberry.RPiController; import se.hal.plugin.raspberry.RPiPowerConsumptionSensor; import se.hal.plugin.raspberry.RPiSensor; +import se.hal.plugin.raspberry.RPiUtility; import zutil.log.LogUtil; 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.event.GpioPinDigitalStateChangeEvent; import com.pi4j.io.gpio.event.GpioPinListenerDigital; +import com.pi4j.wiringpi.GpioUtil; public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDigital, RPiSensor { 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 volatile Integer impulseCount = 0; private GpioPinDigitalInput irLightSensor; + private final int gpioPin; - public RPiInteruptPulseFlankCounter(Pin gpioPin, RPiController controller){ + public RPiInteruptPulseFlankCounter(int gpioPin, RPiController controller){ this.controller = controller; + this.gpioPin = gpioPin; // setup a thread pool for executing jobs this.executorPool = Executors.newCachedThreadPool(); + //Enable non privileged access to the GPIO pins (no sudo required from now) + GpioUtil.enableNonPrivilegedAccess(); + // create gpio controller GpioController gpio = null; 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 - 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 irLightSensor.addListener(this); @@ -131,7 +138,7 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi @Override public void run() { 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)); } }); }