Commits

Sebastian Bub  committed d93c884

RandomAccessFile for reading in readTimeBasedAnalogIn

  • Participants
  • Parent commits 504bc07

Comments (0)

Files changed (1)

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

 import de.derbub.rpigpio.gpio.files.FileHandlingFactory;
 import org.apache.log4j.Logger;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.*;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
             return returnValue;
         }
         // 5. It is read (in a while loop as fast as possible), until a value of (required) !DEFAULT.STATE is returned.
-        FileReader aGpioFileReader = null;
+        RandomAccessFile aGpioRandomAccessFile = null;
         File aGpioFile = null;
         int defaultState = Integer.toString(gpio.getDefaultState()).charAt(0);
         int currentState;
             try {
                 aGpioFile = FileHandlingFactory.getFile(SYSFS_GPIO_FILE_PREFIX + gpio.getGpioPin().getNumber() + SYSFS_GPIO_VALUE_FILE_SUFFIX);
                 if (aGpioFile.exists()) {
-                    aGpioFileReader = FileHandlingFactory.getFileReader(aGpioFile);
+                    // TODO use FileHandlingFactory to create RandomAccessFile
+                    aGpioRandomAccessFile = new RandomAccessFile(aGpioFile, "r");
                     long before = System.currentTimeMillis();
-                    do {
-                        currentState = aGpioFileReader.read();
+                    // TODO specify max from property
+                    int maxCounter = 1000000000;
+                    for (int i = 0; i < maxCounter; i++) {
+                        aGpioRandomAccessFile.seek(0);
+                        currentState = aGpioRandomAccessFile.read();
                         counter++;
-                    } while (defaultState == currentState);
-                    log.info("Setting value to " + counter + " (within " + (System.currentTimeMillis() - before) + "ms) for "
-                        + gpio + (FileHandlingFactory.isSimulation() ? " (SIMULATION)" : ""));
+                        log.debug("read " + currentState);
+                        if (-1 == currentState) {
+                            continue;
+                        }
+                        if (defaultState != currentState) {
+                            log.info("Setting value to " + counter + " (within " + (System.currentTimeMillis() - before) + "ms) for "
+                                + gpio + (FileHandlingFactory.isSimulation() ? " (SIMULATION)" : ""));
+                            break;
+                        }
+                    }
+                    if (maxCounter == counter) {
+                        counter = 0;
+                        log.warn("Max reached (" + maxCounter + "). Check your configuration or hardware: Setting value to "
+                            + counter + " (within " + (System.currentTimeMillis() - before) + "ms) for "
+                            + gpio + (FileHandlingFactory.isSimulation() ? " (SIMULATION)" : ""));
+                    }
                 } else {
                     log.error("File " + aGpioFile + " does not exist. Check previous errors.");
                 }
                 log.error("Got " + e.getClass().getName() + " with message: " + e.getMessage() + " with file " + aGpioFile);
             } finally {
                 try {
-                    if (null != aGpioFileReader) {
-                        aGpioFileReader.close();
+                    if (null != aGpioRandomAccessFile) {
+                        aGpioRandomAccessFile.close();
                     }
                 } catch (Exception e) {
                     // catch away
      * @param gpio which will be read
      * @return String read value or -1 in case of error
      */
+
     private String readGpio(ConfiguredGpio gpio) {
         String returnValue = Integer.toString(ConfiguredGpio.NO_STATE);
         FileReader aGpioFileReader = null;