Commits

Sebastian Bub committed 1c94809

ti.max.counter configurable for loop while reading in readTimeBasedAnalogIn

  • Participants
  • Parent commits d93c884

Comments (0)

Files changed (3)

gpio.conf.MUST_BE_CHANGED

 #       2. It is set to the value of DEFAULT.STATE (required).
 #       3. Wait for the time specified in AUTO.TOGGLE (required).
 #       4. It is reconfigured to be an input port.
-#       5. It is read (in a while loop as fast as possible), until a value
-#          of (required) not-DEFAULT.STATE is returned.
+#       5. It is read (in a while loop as fast as possible (max is ti.max.counter),
+#          until a value of (required) not-DEFAULT.STATE is returned.
 #       6. The counter of the while loop is returned (which can be checked with conditions).
 #       7. It stays an input port until next read.
 # - choose a name which does not get you in trouble addressing it
 # you must set simulate to false for the real world
 simulate.gpios=true
 
+# if direction TI, max looping (it is about 2500ms)
+ti.max.counter=100000
+
 #GPIO.0 = P1-03
 #GPIO.0.DIRECTION=OUT
 #GPIO.0.NAME=g0

src/main/java/de/derbub/rpigpio/SingleContext.java

     private static SingleContext INSTANCE;
     private static Map<String, String> virtualVariables = new HashMap<String, String>();
     public static final String CONFIG_SIMULATE_GPIOS_KEY = "simulate.gpios";
+    public static final String CONFIG_TI_MAX_COUNTER_KEY = "ti.max.counter";
     public static final String VIRTUAL_KEY_PREFIX = "VIRTUAL";
 
 
         }
         // read configuration from configProperties and initialize system
         String simulateGpiosString = configProperties.getProperty(CONFIG_SIMULATE_GPIOS_KEY);
-        Boolean SIMULATE_GPIOS = (null == simulateGpiosString
+        Boolean simulateGpios = (null == simulateGpiosString
             ? Boolean.TRUE
             : Boolean.valueOf(simulateGpiosString));
-        log.warn(CONFIG_SIMULATE_GPIOS_KEY + " is set to " + SIMULATE_GPIOS);
+        log.warn(CONFIG_SIMULATE_GPIOS_KEY + " is set to " + simulateGpios);
+
+        String tiMaxCounterString = configProperties.getProperty(CONFIG_TI_MAX_COUNTER_KEY);
+        int tiMaxCounter = 100000;
+        try{
+            tiMaxCounter = Integer.parseInt(tiMaxCounterString);
+            log.debug(CONFIG_TI_MAX_COUNTER_KEY + " is set to " + tiMaxCounter);
+        } catch(NumberFormatException e){
+            log.error(CONFIG_TI_MAX_COUNTER_KEY + " is not a number. Using default (" + tiMaxCounter + ").");
+        }
 
         Map<String, ConfiguredGpio> gpioMap = new HashMap<String, ConfiguredGpio>();
         for (GpioPin gpioPin : GpioPin.values()) {
             }
         }
         // setup gpio ports
-        gpioInterface = GpioSysFsInterface.getInstance(gpioMap);
-        FileHandlingFactory.setSimulateGpios(SIMULATE_GPIOS);
+        gpioInterface = GpioSysFsInterface.getInstance(gpioMap, tiMaxCounter);
+        FileHandlingFactory.setSimulateGpios(simulateGpios);
         // setup ports and default values
         if (!gpioInterface.setupAllGpioPorts() || !gpioInterface.setAllDefaultState()) {
             log.warn("Setup error, will cleanup immediately");

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

     private static Logger log = Logger.getLogger(GpioSysFsInterface.class);
     private static GpioSysFsInterface INSTANCE;
     private Map<ConfiguredGpio, Long> blockedMap = new HashMap<ConfiguredGpio, Long>();
+    private static int tiMaxCounter = 100000;
 
     protected GpioSysFsInterface() {
         /*
      * methods creates a singleton
      *
      * @param configuredNameGpioMapParam Map with the configured ports
+     * @param tiMaxCounterParam max counter for read loop for direction TI
      * @return a single instance of GpioSysFsInterface or throws a RuntimeException if called twice
      */
-    public static GpioSysFsInterface getInstance(Map<String, ConfiguredGpio> configuredNameGpioMapParam) {
+    public static GpioSysFsInterface getInstance(Map<String, ConfiguredGpio> configuredNameGpioMapParam, int tiMaxCounterParam) {
         if (INSTANCE == null) {
             INSTANCE = new GpioSysFsInterface();
             configuredGpioMap = configuredNameGpioMapParam;
+            tiMaxCounter = tiMaxCounterParam;
             return INSTANCE;
 
         }
             try {
                 aGpioFile = FileHandlingFactory.getFile(SYSFS_GPIO_FILE_PREFIX + gpio.getGpioPin().getNumber() + SYSFS_GPIO_VALUE_FILE_SUFFIX);
                 if (aGpioFile.exists()) {
-                    // TODO use FileHandlingFactory to create RandomAccessFile
+                    // TODO use FileHandlingFactory to create RandomAccessFile for simulation
                     aGpioRandomAccessFile = new RandomAccessFile(aGpioFile, "r");
                     long before = System.currentTimeMillis();
-                    // TODO specify max from property
-                    int maxCounter = 1000000000;
-                    for (int i = 0; i < maxCounter; i++) {
+                    for (int i = 0; i < tiMaxCounter; i++) {
                         aGpioRandomAccessFile.seek(0);
                         currentState = aGpioRandomAccessFile.read();
                         counter++;
-                        log.debug("read " + currentState);
                         if (-1 == currentState) {
                             continue;
                         }
                             break;
                         }
                     }
-                    if (maxCounter == counter) {
+                    if (tiMaxCounter == counter) {
                         counter = 0;
-                        log.warn("Max reached (" + maxCounter + "). Check your configuration or hardware: Setting value to "
+                        log.warn("Max reached (" + tiMaxCounter + "). Check your configuration or hardware: Setting value to "
                             + counter + " (within " + (System.currentTimeMillis() - before) + "ms) for "
                             + gpio + (FileHandlingFactory.isSimulation() ? " (SIMULATION)" : ""));
                     }