Commits

Sebastian Bub committed fe73370

added some more comments and refactored GpioSysFsInterface

  • Participants
  • Parent commits 8181429

Comments (0)

Files changed (8)

 
 
 ### Planned Features
+
 * cronjob configuration for output pings
 * sunset and sunrise aware cronjobs (maybe weather aware), e.g. for lights
 * more status information via web
+
+
+### Possible Unplaned Features
+
+* bit sequences (especially with AUTO.TOGGLE.TIME) for serial output or to control a servo
 * some kind of PLC (Programmable Logic Controller)
 
 
 
 
 ## Configuration and Logging and Debugging
+
 * To enable logging, check log4j.properties and start.sh.
 * For configuration, check gpio.conf and start.sh
 
 
 ## Going Productive
+
 * Make sure you disable simulate mode
 * turn down logging
 
 
 
 ## License
+
 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

gpio.conf.MUST_BE_CHANGED

 # limitations under the License.
 #
 ##############################################################################
+#
 # simple configuration file to define handling of GPIO ports
 #
 # use GPIO names here (not RPi connections)
 # - valid values for DIRECTION are IN or OUT
 # - choose a name which does not get you in trouble addressing it
 #   as a http parameter (umlaut, spaces), e.g. sensor1
-# - if you do not set the default state, it is 0
+# - if you do not set the default state, it is defined by the OS
 # - auto toggle interval is specified in ms (requires  default state),
 #   e.g. if you set this gpio, it will automatically flip to default state
 #   after specified time. auto toggle must be smaller than block time
 # - block time is specified in ms, e.g. you can not set the gpio again during
 #   this interval (ensure that user does not "click multiple times"
 # - only comment out those ports that you need (as they are set up)
-#   on startup
+#   on startup (they are exported in sysfs)
 
 # you must set simulate to false for the real world
 simulate.gpios=true

src/main/java/de/derbub/rpigpio/gpio/ConfiguredGpio.java

  * limitations under the License.
  *
  */
-
 package de.derbub.rpigpio.gpio;
 
 import org.apache.log4j.Logger;
 
-/** Class represents a configuration for a gpio pin. Therefore it has a reference
- *  to GpioPin and a Direction
+/**
+ * Class represents a configuration for a gpio pin. Therefore it has a reference
+ * to GpioPin and a Direction and for each possible configuration property a
+ * variable
  *
  * @author sb
  */
         return blockTime;
     }
 
+    /**
+     * methods sets default state
+     *
+     * @param s
+     */
     private void setDefaultState(String s) {
         if (direction.isInput()) {
             log.debug("Default state not set for " + gpioPin);

src/main/java/de/derbub/rpigpio/gpio/Direction.java

  * limitations under the License.
  *
  */
-
 package de.derbub.rpigpio.gpio;
 
-/** Class represents the direction of a gpio pin
+/**
+ * Class represents the direction of a gpio pin
  *
  * @author sb
  */
 public enum Direction {
 
-    IN("in"),
-    OUT("out");
+    IN("in"), OUT("out");
     private String directionString;
 
     private Direction(String directionString) {

src/main/java/de/derbub/rpigpio/gpio/GpioOutAutoToggleThread.java

  * limitations under the License.
  *
  */
-
 package de.derbub.rpigpio.gpio;
 
 import java.util.HashMap;
 import org.apache.log4j.Logger;
 
 /**
+ * Class is a thread which will set given gpios to default state after auto
+ * toogle time
  *
  * @author sb
  */
             }
             // remove notable from list and handle them
             gpioList.removeAll(outGpioValueMap.keySet());
-            GpioSysFsInterface.getInstance().writeGpios(outGpioValueMap, true);
+            GpioSysFsInterface.getInitializedInstance().writeGpios(outGpioValueMap, true);
         }
         log.debug("run() finished");
     }

src/main/java/de/derbub/rpigpio/gpio/GpioPin.java

  */
 public enum GpioPin {
 
-    GPIO0(0, "GPIO.0"),
-    GPIO1(1, "GPIO.1"),
-    GPIO4(4, "GPIO.4"),
-    GPIO7(7, "GPIO.7"),
-    GPIO8(8, "GPIO.8"),
-    GPIO9(9, "GPIO.9"),
-    GPIO10(10, "GPIO.10"),
-    GPIO11(11, "GPIO.11"),
-    GPIO14(14, "GPIO.14"),
-    GPIO15(15, "GPIO.15"),
-    GPIO17(17, "GPIO.17"),
-    GPIO18(18, "GPIO.18"),
-    GPIO21(21, "GPIO.21"),
-    GPIO22(22, "GPIO.22"),
-    GPIO23(23, "GPIO.23"),
-    GPIO24(24, "GPIO.24"),
-    GPIO25(25, "GPIO.25");
+    GPIO0(0, "GPIO.0"), GPIO1(1, "GPIO.1"), GPIO4(4, "GPIO.4"),
+    GPIO7(7, "GPIO.7"), GPIO8(8, "GPIO.8"), GPIO9(9, "GPIO.9"),
+    GPIO10(10, "GPIO.10"), GPIO11(11, "GPIO.11"), GPIO14(14, "GPIO.14"),
+    GPIO15(15, "GPIO.15"), GPIO17(17, "GPIO.17"), GPIO18(18, "GPIO.18"),
+    GPIO21(21, "GPIO.21"), GPIO22(22, "GPIO.22"), GPIO23(23, "GPIO.23"),
+    GPIO24(24, "GPIO.24"), GPIO25(25, "GPIO.25");
     private int number;
     private String configPrefix;
 

src/main/java/de/derbub/rpigpio/gpio/GpioSysFsInterface.java

  * limitations under the License.
  *
  */
-
 package de.derbub.rpigpio.gpio;
 
 import java.io.*;
 import org.apache.log4j.Logger;
 
 /**
+ * this class writes and reads to/from the gpio pins using Sysfs
  *
  * @author sb
  */
          */
     }
 
-    public static void init(Map<String, ConfiguredGpio> configuredNameGpioMap) {
-        if (INSTANCE != null) {
-            throw new RuntimeException("GpioSysFsInterface already initialized");
+    public static GpioSysFsInterface getInstance(Map<String, ConfiguredGpio> configuredNameGpioMap) {
+        if (INSTANCE == null) {
+            INSTANCE = new GpioSysFsInterface();
+
+            INSTANCE.configuredGpioMap = configuredNameGpioMap;
+            return INSTANCE;
+
         }
-        INSTANCE = new GpioSysFsInterface();
-
-        INSTANCE.configuredGpioMap = configuredNameGpioMap;
+        throw new RuntimeException("GpioSysFsInterface already initialized");
     }
 
-    public static GpioSysFsInterface getInstance() {
+    public static GpioSysFsInterface getInitializedInstance() {
         if (INSTANCE == null) {
-            throw new RuntimeException("GpioSysFsInterface must be initialized");
+            throw new RuntimeException("GpioSysFsInterface has not been initialized");
         }
         return INSTANCE;
     }
     /**
      * return the internal map which must not be changed!
      *
-     * @return
+     * @return Map
      */
     public Map<String, ConfiguredGpio> getConfiguredGpioMap() {
         return configuredGpioMap;
     }
 
     /**
+     * methods exports gpio ports and sets its direction
      *
      * @return boolean success
      */
     }
 
     /**
+     * methods sets the default state of each configured gpio port
      *
      * @return boolean success
      */
     }
 
     /**
+     * method will set or read gpioports
+     *
+     * @param nameCheckedValuesMap must contain only valid configured names and
+     * valid values
+     * @return Map consists of names as keys and for value, either set value to
+     * set or -1 if it failed or the value of IN
+     */
+    public synchronized Map<String, String> handleGpioPorts(Map<String, String> nameCheckedValuesMap) {
+        log.debug("handleGpioPorts()");
+        Map<String, String> returnMap = new HashMap();
+
+        Map<ConfiguredGpio, String> batchedGpioOutMap = new HashMap();
+        for (String name : nameCheckedValuesMap.keySet()) {
+            ConfiguredGpio gpio = configuredGpioMap.get(name);
+            if (gpio.getDirection().isInput()) {
+                String readValue = readGpio(gpio);
+                returnMap.put(name, readValue);
+            } else {
+                batchedGpioOutMap.put(gpio, nameCheckedValuesMap.get(name));
+            }
+        }
+        if (!batchedGpioOutMap.isEmpty()) {
+            returnMap.putAll(writeGpios(batchedGpioOutMap, false));
+        }
+        return returnMap;
+    }
+
+    /**
+     * methods resets all gpio ports to 0, afterwards it unexports the gpios and
+     * removes the singleton instance.
      *
      * @return boolean success
      */
-    public boolean resetAllState() {
+    public boolean closeAllGpioPorts() {
+        boolean isReset = resetAllState();
+        boolean isUnexport = unExportAllGpioPorts();
+        INSTANCE = null;
+        return isReset && isUnexport;
+    }
+
+    /**
+     * methods sets all output pins to 0
+     *
+     * @return boolean success
+     */
+    private boolean resetAllState() {
         // set state 0 for all pins
         FileWriter aGpioFileWriter = null;
         File aGpioFile = null;
         boolean isSuccessful = true;
         for (ConfiguredGpio gpio : configuredGpioMap.values()) {
-            if(gpio.getDirection().isInput()){
+            if (gpio.getDirection().isInput()) {
                 continue;
             }
             try {
     }
 
     /**
+     * method unexports all configured gpio pins
      *
      * @return boolean success
      */
-    public boolean cleanupAllGpioPorts() {
+    private boolean unExportAllGpioPorts() {
         FileWriter exportFileWriter = null;
         boolean isSuccessful = true;
         // unexport all gpio pins
         return isSuccessful;
     }
 
-    /**
-     * method will set or read
-     *
-     * @param Map nameCheckedValuesMap must consist of correct names and valid
-     * values as 0,1 or IN
-     * @return Map consists of names as keys and for value, either set value to
-     * set or -1 if it failed or the value of IN
-     */
-    public synchronized Map<String, String> handleGpioPorts(Map<String, String> nameCheckedValuesMap) {
-        log.debug("handleGpioPorts()");
-        Map<String, String> returnMap = new HashMap();
-
-        Map<ConfiguredGpio, String> batchedGpioOutMap = new HashMap();
-        for (String name : nameCheckedValuesMap.keySet()) {
-            ConfiguredGpio gpio = configuredGpioMap.get(name);
-            if (gpio.getDirection().isInput()) {
-                String readValue = readGpio(gpio);
-                returnMap.put(name, readValue);
-            } else {
-                batchedGpioOutMap.put(gpio, nameCheckedValuesMap.get(name));
-            }
-        }
-        if (!batchedGpioOutMap.isEmpty()) {
-            returnMap.putAll(writeGpios(batchedGpioOutMap, false));
-        }
-        return returnMap;
-    }
-
     /*
-     * @return read value or -1 in case of error
+     * @return String read value or -1 in case of error
      */
     private String readGpio(ConfiguredGpio gpio) {
         String returnValue = Integer.toString(ConfiguredGpio.NO_STATE);
     }
 
     /**
+     *
+     * @param outGpioValueMap Map must contain gpios (configured as output) and
+     * value to set
+     * @param isAutoTogglingThread must be true if called from
+     * GpioOutAutoToggleThread
      * @return Map name as key and written value or -1 in case of error
      */
     synchronized Map<String, String> writeGpios(Map<ConfiguredGpio, String> outGpioValueMap, boolean isAutoTogglingThread) {
                         aGpioFileWriter.flush();
                         log.info("Set state " + value + (isAutoTogglingThread ? " (autoToggle)" : "") + " for " + gpio);
                         returnMap.put(gpio.getUserdefinedName(), value);
+                        // if not running in autoToggling, check if configured and keep a list
                         if (!isAutoTogglingThread && gpio.getAutoToogleTime() != ConfiguredGpio.NO_DEFAULT_TIME) {
                             autoToggleList.add(gpio);
                         }
                 }
             }
         }
+        // if we have something to toggle back to default state later, do it
         if (!isAutoTogglingThread && !autoToggleList.isEmpty()) {
             GpioOutAutoToggleThread atThread = new GpioOutAutoToggleThread(autoToggleList);
             atThread.start();

src/main/java/de/derbub/rpigpio/web/GpioServlet.java

             }
         }
         // setup gpio ports
-        GpioSysFsInterface.init(gpioMap);
-        gpioInterface = GpioSysFsInterface.getInstance();
+        gpioInterface = GpioSysFsInterface.getInstance(gpioMap);
         gpioInterface.setSimulateGpios(SIMULATE_GPIOS);
         // setup ports and default values
         if (!gpioInterface.setupAllGpioPorts() || !gpioInterface.setAllDefaultState()) {
             log.warn("Setup error, will cleanup immediately");
-            gpioInterface.resetAllState();
-            gpioInterface.cleanupAllGpioPorts();
+            gpioInterface.closeAllGpioPorts();
         }
     }
 
     @Override
     public void destroy() {
         log.debug("destroy()");
-        gpioInterface.resetAllState();
-        gpioInterface.cleanupAllGpioPorts();
+        gpioInterface.closeAllGpioPorts();
     }
 
     /**