Merge pull request #43 from jtanx/master
authorJeremy Tan <[email protected]>
Fri, 27 Sep 2013 11:08:31 +0000 (04:08 -0700)
committerJeremy Tan <[email protected]>
Fri, 27 Sep 2013 11:08:31 +0000 (04:08 -0700)
Fixes and stuff for pin control

22 files changed:
.gitignore
notes/pin maps/Pin correspondence.xls [new file with mode: 0644]
notes/pin maps/Pinout table.doc [new file with mode: 0644]
notes/pin maps/Pinout table.pdf [new file with mode: 0644]
notes/pin maps/gpio/GPIO pin correspondence unrestricted.csv [new file with mode: 0644]
notes/pin maps/gpio/gpioindex_lut.py [new file with mode: 0644]
notes/pin maps/gpio/gpionums.csv [new file with mode: 0644]
notes/pin maps/gpio/parseit.py [new file with mode: 0644]
notes/pin maps/gpio/readme.txt [new file with mode: 0644]
server/Makefile
server/actuator.c
server/bbb_pin.c
server/bbb_pin.h
server/bbb_pin_defines.c [new file with mode: 0644]
server/bbb_pin_defines.h
server/common.h
server/log.h
server/main.c
server/options.h
server/pin_test.c
server/run.sh
server/sensor.c

index 74ede4e..729e81b 100644 (file)
@@ -11,5 +11,7 @@
 ehthumbs.db
 Thumbs.db
 
+__pycache__
+
 server/win32
 **/nbproject/private/
\ No newline at end of file
diff --git a/notes/pin maps/Pin correspondence.xls b/notes/pin maps/Pin correspondence.xls
new file mode 100644 (file)
index 0000000..fca71aa
Binary files /dev/null and b/notes/pin maps/Pin correspondence.xls differ
diff --git a/notes/pin maps/Pinout table.doc b/notes/pin maps/Pinout table.doc
new file mode 100644 (file)
index 0000000..9a5b5f2
Binary files /dev/null and b/notes/pin maps/Pinout table.doc differ
diff --git a/notes/pin maps/Pinout table.pdf b/notes/pin maps/Pinout table.pdf
new file mode 100644 (file)
index 0000000..426d15b
Binary files /dev/null and b/notes/pin maps/Pinout table.pdf differ
diff --git a/notes/pin maps/gpio/GPIO pin correspondence unrestricted.csv b/notes/pin maps/gpio/GPIO pin correspondence unrestricted.csv
new file mode 100644 (file)
index 0000000..e73ff12
--- /dev/null
@@ -0,0 +1,44 @@
+P8_07,66
+P8_08,67
+P8_09,69
+P8_10,68
+P8_11,45
+P8_12,44
+P8_14,26
+P8_15,47
+P8_16,46
+P8_17,27
+P8_18,65
+P8_26,61
+P8_27,86
+P8_28,88
+P8_29,87
+P8_30,89
+P8_31,10
+P8_32,11
+P8_33,9
+P8_34,81
+P8_35,8
+P8_36,80
+P8_37,78
+P8_38,79
+P8_39,76
+P8_40,77
+P8_41,74
+P8_42,75
+P8_43,72
+P8_44,73
+P8_45,70
+P8_46,71
+P9_11,30
+P9_12,60
+P9_13,31
+P9_15,48
+P9_17,5
+P9_18,4
+P9_23,49
+P9_24,15
+P9_25,117
+P9_26,14
+P9_27,115
+P9_30,112
diff --git a/notes/pin maps/gpio/gpioindex_lut.py b/notes/pin maps/gpio/gpioindex_lut.py
new file mode 100644 (file)
index 0000000..a3241e3
--- /dev/null
@@ -0,0 +1,22 @@
+import sys, re, os
+from parseit import printlut
+
+def doit2(x):
+    with open(x) as f:
+        lut = {}
+        rlut = {}
+        i = 0
+        for line in f:
+            gpionum = int(line)
+            lut[gpionum] = i
+            rlut[i] = gpionum
+            i += 1
+
+        lutarr = []
+        reverse = []
+        for i in range(118): #Max safe gpio is 117
+            lutarr.append(lut.get(i, 128))
+        for i in range(len(rlut)):
+            reverse.append(rlut[i])
+        return (lutarr, reverse)
+        
diff --git a/notes/pin maps/gpio/gpionums.csv b/notes/pin maps/gpio/gpionums.csv
new file mode 100644 (file)
index 0000000..a7c061c
--- /dev/null
@@ -0,0 +1,44 @@
+4
+5
+8
+9
+10
+11
+14
+15
+26
+27
+30
+31
+44
+45
+46
+47
+48
+49
+60
+61
+65
+66
+67
+68
+69
+70
+71
+72
+73
+74
+75
+76
+77
+78
+79
+80
+81
+86
+87
+88
+89
+112
+115
+117
diff --git a/notes/pin maps/gpio/parseit.py b/notes/pin maps/gpio/parseit.py
new file mode 100644 (file)
index 0000000..e106f07
--- /dev/null
@@ -0,0 +1,52 @@
+import sys, re, os
+#lut size of 93 (46 pins/header; 2 headers; padding for 1-indexed)
+
+def doit(x):
+    '''generate the lut from the csv'''
+    lut = {}
+    reverselut = {}
+    
+    with open(x) as f:
+        for line in f:
+            m = re.search("P(\d)_(\d+),(\d+)", line)
+            header = int(m.group(1))
+            pin = int(m.group(2))
+            gpionum = int(m.group(3))
+
+            if header==8:
+                header = 0
+            else:
+                header = 1
+
+            lut[header*46+pin] = gpionum
+            reverselut[gpionum] = header*46+pin
+    lutarr = []
+    reverselutarr =[]
+    
+    for i in range(0, 93):
+        lutarr.append(lut.get(i, 0))
+
+    for i in range(0, 118): #Max safe GPIO is 117
+        reverselutarr.append(reverselut.get(i, 0))
+        
+    return (lutarr, reverselutarr)
+
+def printlut(lut, name="g_gpio_lut"):
+    '''print the lut for C'''
+    rowsize = 14
+    print("const unsigned char %s[%d] = {" % (name, len(lut)))
+    low = 0
+    high = rowsize
+    for i in range(0, len(lut), rowsize):
+        print("\t", end="")
+        print(*("%3d" % g for g in lut[low:high]), sep=', ', end="")
+        low = high
+        high += rowsize
+        if low < len(lut):
+            print(",")
+        else:
+            print("")
+    print("}")
+
+
+        
diff --git a/notes/pin maps/gpio/readme.txt b/notes/pin maps/gpio/readme.txt
new file mode 100644 (file)
index 0000000..905aca5
--- /dev/null
@@ -0,0 +1,5 @@
+GPIO Pin correspondence.xls -  all maps with descriptions
+GPIO pin correspondence unrestricted.csv - all unused pins + hdmi pins
+GPIO pin correspondence.csv - all gpio pins
+
+max usable gpio: 117
\ No newline at end of file
index f3ce6b4..929b40d 100644 (file)
@@ -2,7 +2,7 @@
 CXX = gcc
 FLAGS = -std=c99 -Wall -pedantic -g -I/usr/include/opencv -I/usr/include/opencv2/highgui -L/usr/lib
 LIB = -lfcgi -lssl -lcrypto -lpthread -lm -lopencv_highgui -lopencv_core -lopencv_ml -lopencv_imgproc
-OBJ = log.o control.o data.o fastcgi.o main.o sensor.o actuator.o image.o bbb_pin.o pin_test.o
+OBJ = log.o control.o data.o fastcgi.o main.o sensor.o actuator.o image.o bbb_pin.o bbb_pin_defines.o pin_test.o
 RM = rm -f
 
 BIN = server
index 40f9b46..f5d7cd6 100644 (file)
@@ -32,7 +32,6 @@ void Actuator_Init()
        // Initialise pins used
        GPIO_Export(GPIO1_16);
        PWM_Export(EHRPWM0A);
-       PWM_Export(EHRPWM0B);
        
 }
 
index 4569578..a652b97 100644 (file)
@@ -4,12 +4,12 @@
  * THIS CODE IS NOT THREADSAFE
  */
 
+#define _BBB_PIN_SRC
 #include "bbb_pin.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <ctype.h>
 #include "options.h"
 
 /**
@@ -18,6 +18,7 @@
  */
 typedef struct
 {
+       bool initialised;
        int fd_value;
        int fd_direction;
 } GPIO_Pin;
@@ -28,6 +29,7 @@ typedef struct
  */
 typedef struct
 {
+       bool initialised;
        int fd_value;
 } ADC_Pin;
 
@@ -37,6 +39,7 @@ typedef struct
  */
 typedef struct
 {
+       bool initialised;
        int fd_run;
        FILE * file_duty;
        FILE * file_period;
@@ -50,319 +53,430 @@ static ADC_Pin g_adc[ADC_NUM_PINS] = {{0}};
 /** Array of PWM pins **/
 static PWM_Pin g_pwm[PWM_NUM_PINS] = {{0}};
 
-static char g_buffer[BUFSIZ] = "";
-
-
-
+static char g_buffer[BUFSIZ] = {0};
 
 /**
  * Export a GPIO pin and open the file descriptors
+ * @param pin The GPIO number to be exported
+ * @return true on success, false otherwise
  */
-void GPIO_Export(int pin)
+bool GPIO_Export(int pin)
 {
-       if (pin < 0 || pin > GPIO_NUM_PINS)
+       if (pin < 0 || pin > GPIO_MAX_NUMBER || g_pin_gpio_to_index[pin] == 128)
        {
-               Abort("Invalid pin number %d", pin);
+               AbortBool("Not a useable pin number: %d", pin);
        }
 
-       
+       GPIO_Pin *gpio = &g_gpio[g_pin_gpio_to_index[pin]];
+       if (gpio->initialised)
+       {
+               Log(LOGNOTE, "GPIO %d already initialised.", pin);
+               return true;
+       }
 
        // Export the pin
        sprintf(g_buffer, "%s/export", GPIO_DEVICE_PATH);
-       FILE * export = fopen(g_buffer, "w");
-       if (export == NULL)
+       FILE * file_export = fopen(g_buffer, "w");
+       if (file_export == NULL)
        {
-               Abort("Couldn't open %s to export GPIO pin %d - %s", g_buffer, pin, strerror(errno));
+               AbortBool("Couldn't open %s to export GPIO pin %d - %s", g_buffer, pin, strerror(errno));
        }
-
-       fprintf(export, "%d", pin);     
-       fclose(export);
+       fprintf(file_export, "%d", pin);        
+       fclose(file_export);
        
        // Setup direction file descriptor
        sprintf(g_buffer, "%s/gpio%d/direction", GPIO_DEVICE_PATH, pin);
-       g_gpio[pin].fd_direction = open(g_buffer, O_RDWR);
-       if (g_gpio[pin].fd_direction < 0)
+       gpio->fd_direction = open(g_buffer, O_RDWR);
+       if (gpio->fd_direction < 0)
        {
-               Abort("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
+               AbortBool("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
        }
 
-
        // Setup value file descriptor
        sprintf(g_buffer, "%s/gpio%d/value", GPIO_DEVICE_PATH, pin);
-       g_gpio[pin].fd_value = open(g_buffer, O_RDWR);
-       if (g_gpio[pin].fd_value < 0)
+       gpio->fd_value = open(g_buffer, O_RDWR);
+       if (gpio->fd_value < 0)
        {
-               Abort("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
+               close(gpio->fd_direction);
+               AbortBool("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
        }
 
+       gpio->initialised = true;
        Log(LOGDEBUG, "Exported GPIO%d", pin);
-       //sleep(1);
+       return true;
 }
 
 /**
  * Unexport a GPIO pin and close its' file descriptors
+ * @param pin The GPIO number to be unexported
  */
 void GPIO_Unexport(int pin)
 {
+       if (pin < 0 || pin > GPIO_MAX_NUMBER || g_pin_gpio_to_index[pin] == 128)
+       {
+               Abort("Not a useable pin number: %d", pin);
+       }
 
-       if (pin < 0 || pin > GPIO_NUM_PINS)
+       GPIO_Pin *gpio = &g_gpio[g_pin_gpio_to_index[pin]];
+       if (!gpio->initialised)
        {
-               Abort("Invalid pin number %d", pin);
+               Abort("GPIO %d is already uninitialised", pin);
        }
 
        // Close file descriptors
-       close(g_gpio[pin].fd_value);
-       close(g_gpio[pin].fd_direction);
+       close(gpio->fd_value);
+       close(gpio->fd_direction);
+       // Uninitialise this one
+       gpio->initialised = false;
 
        // Unexport the pin
-
        if (g_buffer[0] == '\0')
                sprintf(g_buffer, "%s/unexport", GPIO_DEVICE_PATH);     
-       FILE * export = fopen(g_buffer, "w");
-       if (export == NULL)
+       FILE * file_export = fopen(g_buffer, "w");
+       if (file_export == NULL)
        {
                Abort("Couldn't open %s to export GPIO pin %d - %s", g_buffer, pin, strerror(errno));
        }
 
-       fprintf(export, "%d", pin);     
-       fclose(export);
+       fprintf(file_export, "%d", pin);        
+       fclose(file_export);
 }
 
-
-
-
 /**
- * Export all PWM pins and open file descriptors
- * @param pin - The pin number
+ * Initialise all PWM pins and open file descriptors
+ * @param pin - The sysfs pin number
+ * @return true if exported, false otherwise
  */
-void PWM_Export(int pin)
+bool PWM_Export(int pin)
 {
-       if (pin < 0 || pin > PWM_NUM_PINS)
+       //goto would make this easier...
+       if (pin < 0 || pin >= PWM_NUM_PINS)
        {
-               Abort("Invalid pin number %d", pin);
+               AbortBool("Invalid PWM pin %d specified.", pin);
        }
-       
-       // Export the pin
+
+       PWM_Pin *pwm = &g_pwm[pin];
+       if (pwm->initialised)
+       {
+               Log(LOGNOTE, "PWM %d already exported.", pin);
+               return true;
+       }
+
+       // Try export the pin, doesn't matter if it's already exported.
        sprintf(g_buffer, "%s/export", PWM_DEVICE_PATH);
-       FILE * export = fopen(g_buffer, "w");
-       if (export == NULL)
+       FILE * file_export = fopen(g_buffer, "w");
+       if (file_export == NULL)
        {
-               Abort("Couldn't open %s to export PWM pin %d - %s", g_buffer, pin, strerror(errno));
+               AbortBool("Couldn't open %s to export PWM pin %d - %s", 
+                               g_buffer, pin, strerror(errno));
        }
-       
-       fprintf(export, "%d\n", pin);
-       fclose(export);
+       fprintf(file_export, "%d\n", pin);
+       fclose(file_export);
 
        // Open file descriptors
        sprintf(g_buffer, "%s/pwm%d/run", PWM_DEVICE_PATH, pin);
-       g_pwm[pin].fd_run = open(g_buffer, O_WRONLY);
-       if (g_pwm[pin].fd_run < 0)
+       pwm->fd_run = open(g_buffer, O_WRONLY);
+       if (pwm->fd_run < 0)
        {
-               Abort("Couldn't open %s for PWM pin %d - %s", g_buffer, pin, strerror(errno));
+               AbortBool("Couldn't open %s for PWM%d - %s", g_buffer, pin, strerror(errno));
        }
 
-       sprintf(g_buffer, "%s/pwm%d/polarity",PWM_DEVICE_PATH, pin);
-       g_pwm[pin].fd_polarity = open(g_buffer, O_WRONLY);
-       if (g_pwm[pin].fd_polarity < 0)
+       sprintf(g_buffer, "%s/pwm%d/polarity", PWM_DEVICE_PATH, pin);
+       pwm->fd_polarity = open(g_buffer, O_WRONLY);
+       if (pwm->fd_polarity < 0)
        {
-               Abort("Couldn't open %s for PWM pin %d - %s", g_buffer, pin, strerror(errno));
+               close(pwm->fd_run);
+               AbortBool("Couldn't open %s for PWM%d - %s", g_buffer, pin, strerror(errno));
        }
 
-       sprintf(g_buffer, "%s/pwm%d/period_ns",PWM_DEVICE_PATH, pin);
-       g_pwm[pin].file_period = fopen(g_buffer, "w");
-       if (g_pwm[pin].file_period == NULL)
+       sprintf(g_buffer, "%s/pwm%d/period_ns", PWM_DEVICE_PATH, pin);
+       pwm->file_period = fopen(g_buffer, "w");
+       if (pwm->file_period == NULL)
        {
-               Abort("Couldn't open %s for PWM pin %d - %s", g_buffer, pin, strerror(errno));
+               close(pwm->fd_run);
+               close(pwm->fd_polarity);
+               AbortBool("Couldn't open %s for PWM%d - %s", g_buffer, pin, strerror(errno));
        }
 
-       sprintf(g_buffer, "%s/pwm%d/duty_ns",PWM_DEVICE_PATH, pin);
-       g_pwm[pin].file_duty = fopen(g_buffer, "w");
-       if (g_pwm[pin].file_duty == NULL)
+       sprintf(g_buffer, "%s/pwm%d/duty_ns", PWM_DEVICE_PATH, pin);
+       pwm->file_duty = fopen(g_buffer, "w");
+       if (pwm->file_duty == NULL)
        {
-               Abort("Couldn't open %s for PWM pin %d - %s", g_buffer, pin, strerror(errno));
+               close(pwm->fd_run);
+               close(pwm->fd_polarity);
+               fclose(pwm->file_period);
+               AbortBool("Couldn't open %s for PWM%d - %s", g_buffer, pin, strerror(errno));
        }
 
        // Don't buffer the streams
-       setbuf(g_pwm[pin].file_period, NULL);
-       setbuf(g_pwm[pin].file_duty, NULL);
+       setbuf(pwm->file_period, NULL);
+       setbuf(pwm->file_duty, NULL);   
 
-       
+       pwm->initialised = true;
+       Log(LOGDEBUG, "Exported PWM%d", pin);
+       return true;
 }
 
+
 /**
  * Unexport a PWM pin and close its file descriptors
- * @param pin - The pin number
+ * @param pin - The sysfs pin number
  */
 void PWM_Unexport(int pin)
 {
-       if (pin < 0 || pin > PWM_NUM_PINS)
+       if (pin < 0 || pin >= PWM_NUM_PINS)
+       {
+               Abort("Invalid PWM pin number %d specified.", pin);
+       }
+
+       PWM_Pin *pwm = &g_pwm[pin];
+       if (!pwm->initialised)
        {
-               Abort("Invalid pin number %d", pin);
+               Abort("PWM %d not initialised", pin);
        }
 
        // Close the file descriptors
-       close(g_pwm[pin].fd_polarity);
-       close(g_pwm[pin].fd_run);
-       fclose(g_pwm[pin].file_period);
-       fclose(g_pwm[pin].file_duty);
+       close(pwm->fd_polarity);
+       //Stop it, if it's still running
+       pwrite(pwm->fd_run, "0", 1, 0);
+       close(pwm->fd_run);
+       fclose(pwm->file_period);
+       fclose(pwm->file_duty);
+
+       pwm->initialised = false;
 
-       //Unexport the pin
+       // Try unexport the pin, doesn't matter if it's already unexported.
        sprintf(g_buffer, "%s/unexport", PWM_DEVICE_PATH);
-       FILE * export = fopen(g_buffer, "w");
-       if (export == NULL)
+       FILE * file_unexport = fopen(g_buffer, "w");
+       if (file_unexport == NULL)
        {
-               Abort("Couldn't open %s to unexport PWM pin %d - %s", g_buffer, pin, strerror(errno));  
+               Abort("Couldn't open %s to unexport PWM pin %d - %s", g_buffer, pin, strerror(errno));
        }
-       
-       fprintf(export, "%d", pin);
-       fclose(export);
-
-
+       fprintf(file_unexport, "%d\n", pin);
+       fclose(file_unexport);
 }
 
 /**
- * Export ADC pins; http://beaglebone.cameon.net/home/reading-the-analog-inputs-adc
- * Can't use sysfs like GPIO or PWM pins
- * Bloody annoying how inconsistent stuff is on the Beaglebone
+ * Initialise ADC structures
+ * @param pin The ADC pin number
  */
-void ADC_Export()
+bool ADC_Export(int pin)
 {
-       for (int i = 0; i < ADC_NUM_PINS; ++i)
+       if (pin < 0 || pin >= ADC_NUM_PINS)
        {
-               sprintf(g_buffer, "%s/AIN%d", g_options.adc_device_path, i);
-               g_adc[i].fd_value = open(g_buffer, O_RDONLY);
-               if (g_adc[i].fd_value < 0)
-               {
-                       Abort("Couldn't open ADC %d device file %s - %s", i, g_buffer, strerror(errno));
-               }
-
-               //setbuf(g_adc[i].file_value, NULL);
+               AbortBool("Invalid ADC pin %d specified.", pin);
+       }
+       else if (g_adc[pin].initialised)
+       {
+               Log(LOGNOTE, "ADC %d already initialised", pin);
+               return true;
+       }
 
+       sprintf(g_buffer, "%s/in_voltage%d_raw", g_options.adc_device_path, pin);
+       g_adc[pin].fd_value = open(g_buffer, O_RDONLY);
+       if (g_adc[pin].fd_value <0)
+       {
+               AbortBool("Couldn't open ADC %d device file %s - %s", pin, g_buffer, strerror(errno));
        }
+
+       g_adc[pin].initialised = true;
+       Log(LOGDEBUG, "Opened ADC %d", pin);
+       return true;
 }
 
 /**
  * Unexport ADC pins
+ * @param pin The ADC pin number
  */
-void ADC_Unexport()
+void ADC_Unexport(int pin)
 {
-       for (int i = 0; i < ADC_NUM_PINS; ++i)
-               close(g_adc[i].fd_value);
+       if (pin < 0 || pin >= ADC_NUM_PINS)
+       {
+               Abort("Invalid ADC pin %d specified.", pin);
+       }
+       else if (!g_adc[pin].initialised)
+       {
+               Abort("ADC %d already uninitialised", pin);
+       }
+
+       close(g_adc[pin].fd_value);     
+       g_adc[pin].fd_value = -1;
+       g_adc[pin].initialised = false;
 }
 
 /**
  * Set a GPIO pin
  * @param pin - The pin to set. MUST have been exported before calling this function.
  */
-void GPIO_Set(int pin, bool value)
+bool GPIO_Set(int pin, bool value)
 {
-       if (pwrite(g_gpio[pin].fd_direction, "out", 3, 0) != 3)
+       if (pin < 0 || pin > GPIO_MAX_NUMBER || g_pin_gpio_to_index[pin] == 128)
+       {
+               AbortBool("Not a useable pin number: %d", pin);
+       }
+
+       GPIO_Pin *gpio = &g_gpio[g_pin_gpio_to_index[pin]];
+       if (!gpio->initialised)
+       {
+               AbortBool("GPIO %d is not initialised.", pin);
+       }
+       //Set the pin direction
+       if (pwrite(gpio->fd_direction, "out", 3, 0) != 3)
        {
-               Abort("Couldn't set GPIO %d direction - %s", pin, strerror(errno));
+               AbortBool("Couldn't set GPIO %d direction - %s", pin, strerror(errno));
        }
 
-       char c = '0' + (value);
-       if (pwrite(g_gpio[pin].fd_value, &c, 1, 0) != 1)
+       char c = value ? '1' : '0';
+       if (pwrite(gpio->fd_value, &c, 1, 0) != 1)
        {
-               Abort("Couldn't read GPIO %d value - %s", pin, strerror(errno));
+               AbortBool("Couldn't read GPIO %d value - %s", pin, strerror(errno));
        }
 
+       return true;
 }
 
 /** 
  * Read from a GPIO Pin
  * @param pin - The pin to read
+ * @param result A pointer to store the result
+ * @return true on success, false otherwise
  */
-bool GPIO_Read(int pin)
+bool GPIO_Read(int pin, bool *result)
 {
-       if (pwrite(g_gpio[pin].fd_direction, "in", 2, 0) != 2)
-               Log(LOGERR,"Couldn't set GPIO %d direction - %s", pin, strerror(errno)); 
-       char c = '0';
-       if (pread(g_gpio[pin].fd_value, &c, 1, 0) != 1)
-               Log(LOGERR,"Couldn't read GPIO %d value - %s", pin, strerror(errno));
+       if (pin < 0 || pin > GPIO_MAX_NUMBER || g_pin_gpio_to_index[pin] == 128)
+       {
+               AbortBool("Not a useable pin number: %d", pin);
+       }
 
-       return (c == '1');
+       GPIO_Pin *gpio = &g_gpio[g_pin_gpio_to_index[pin]];
+       if (!gpio->initialised)
+       {
+               AbortBool("GPIO %d is not initialised.", pin);
+       }
+
+       if (pwrite(gpio->fd_direction, "in", 2, 0) != 2)
+       {
+               AbortBool("Couldn't set GPIO %d direction - %s", pin, strerror(errno));
+       }
+       
+       char c = '0';
+       if (pread(gpio->fd_value, &c, 1, 0) != 1)
+       {
+               AbortBool("Couldn't read GPIO %d value - %s", pin, strerror(errno));
+       }
 
+       *result = (c == '1');
+       return true;
 }
 
 /**
  * Activate a PWM pin
- * @param pin - The pin to activate
+ * @param pin - The sysfs pin number
  * @param polarity - if true, pin is active high, else active low
  * @param period - The period in ns
  * @param duty - The time the pin is active in ns
  */
-void PWM_Set(int pin, bool polarity, long period, long duty)
+bool PWM_Set(int pin, bool polarity, long period, long duty)
 {
+       Log(LOGDEBUG, "Pin %d, pol %d, period: %lu, duty: %lu", pin, polarity, period, duty);
+       
+       if (pin < 0 || pin >= PWM_NUM_PINS)
+       {
+               AbortBool("Invalid PWM pin number %d specified.", pin);
+       }
+
+       PWM_Pin *pwm = &g_pwm[pin];
+       if (!pwm->initialised)
+       {
+               AbortBool("PWM %d is not initialised.", pin);
+       }
+
        // Have to stop PWM before changing it
-       if (pwrite(g_pwm[pin].fd_run, "0", 1, 0) != 1)
+       if (pwrite(pwm->fd_run, "0", 1, 0) != 1)
        {
-               Abort("Couldn't stop PWM %d - %s", pin, strerror(errno));
+               AbortBool("Couldn't stop PWM %d - %s", pin, strerror(errno));
        }
 
-       char c = '0' + polarity;
-       if (pwrite(g_pwm[pin].fd_polarity, &c, 1, 0) != 1)
+       char c = polarity ? '1' : '0';
+       if (pwrite(pwm->fd_polarity, &c, 1, 0) != 1)
        {
-               Abort("Couldn't set PWM %d polarity - %s", pin, strerror(errno));
+               AbortBool("Couldn't set PWM %d polarity - %s", pin, strerror(errno));
        }
-       
-       rewind(g_pwm[pin].file_period); 
-       rewind(g_pwm[pin].file_duty);
 
-       if (fprintf(g_pwm[pin].file_duty, "%lu", duty) == 0)
+       //This must be done first, otherwise period/duty settings can conflict
+       if (fwrite("0", 1, 1, pwm->file_duty) < 1)
        {
-               Abort("Couldn't set duty cycle for PWM %d - %s", pin, strerror(errno));
+               AbortBool("Couldn't zero the duty for PWM %d - %s", pin, strerror(errno));
        }
-       if (fprintf(g_pwm[pin].file_period, "%lu", period) == 0)
+
+       if (fprintf(pwm->file_period, "%lu", period) < 0)
        {
-               Abort("Couldn't set period for PWM %d - %s", pin, strerror(errno));
+               AbortBool("Couldn't set period for PWM %d - %s", pin, strerror(errno));
        }
-       if (pwrite(g_pwm[pin].fd_run, "1", 1, 0) != 1)
+
+
+       if (fprintf(pwm->file_duty, "%lu", duty) < 0)
        {
-               Abort("Couldn't start PWM %d - %s", pin, strerror(errno));
+               AbortBool("Couldn't set duty cycle for PWM %d - %s", pin, strerror(errno));
        }
 
+
+       if (pwrite(pwm->fd_run, "1", 1, 0) != 1)
+       {
+               AbortBool("Couldn't start PWM %d - %s", pin, strerror(errno));
+       }
+
+       return true;
 }
 
 /**
  * Deactivate a PWM pin
- * @param pin - The pin to turn off
+ * @param pin - The syfs pin number
+ * @return true on success, false otherwise
  */
-void PWM_Stop(int pin)
+bool PWM_Stop(int pin)
 {
+       if (pin < 0 || pin >= PWM_NUM_PINS)
+       {
+               AbortBool("Invalid PWM pin number %d specified.", pin);
+       }
+       else if (!g_pwm[pin].initialised)
+       {
+               AbortBool("PWM %d is not initialised.", pin);
+       }
+
        if (pwrite(g_pwm[pin].fd_run, "0", 1, 0) != 1)
        {
-               Abort("Couldn't stop PWM %d - %s", pin, strerror(errno));
+               AbortBool("Couldn't stop PWM %d - %s", pin, strerror(errno));
        }
+
+       return true;
 }
 
 /**
  * Read an ADC value
  * @param id - The ID of the ADC pin to read
- * @returns - The reading of the ADC channel
+ * @param value - A pointer to store the value read from the ADC
+ * @returns - The true if succeeded, false otherwise.
  */
-int ADC_Read(int id)
+bool ADC_Read(int id, int *value)
 {
-       char adc_str[ADC_DIGITS] = "";
-       lseek(g_adc[id].fd_value, 0, SEEK_SET);
-       
-       int i = 0;
-       for (i = 0; i < ADC_DIGITS-1; ++i)
+       char adc_str[ADC_DIGITS] = {0};
+
+       if (id < 0 || id >= ADC_NUM_PINS)
        {
-               if (read(g_adc[id].fd_value, adc_str+i, 1) != 1)
-                       break;
-               if (adc_str[i] == '\n')
-               {
-                       adc_str[i] = '\0';
-                       break;
-               }
+               AbortBool("Invalid ADC pin %d specified.", id);
+       }
+       else if (!g_adc[id].initialised)
+       {
+               AbortBool("ADC %d is not initialised.", id);
        }
 
-       char * end;
-       int val = strtol(adc_str, &end, 10);
-       if (*end != '\0')
+       if (pread(g_adc[id].fd_value, adc_str, ADC_DIGITS-1, 0) == -1)
        {
-               Log(LOGERR, "Read non integer from ADC %d - %s", id, adc_str);
-       }       
-       return val;     
-}
+               AbortBool("ADC %d read failed: %s", id, strerror(errno));
+       }
+
+       *value = strtol(adc_str, NULL, 10);
+       return true;
+}
\ No newline at end of file
index 04c02b6..9eb011e 100644 (file)
 
 #include "bbb_pin_defines.h"
 
+#if defined(_BBB) || defined(_BBB_PIN_SRC)
 // Initialise / Deinitialise functions
-extern void GPIO_Export(int pin);
+extern bool GPIO_Export(int pin);
 extern void GPIO_Unexport(int pin);
 
-extern void PWM_Export(int pin);
+extern bool PWM_Export(int pin);
 extern void PWM_Unexport(int pin);
 
-extern void ADC_Export();
-extern void ADC_Unexport();
+extern bool ADC_Export(int pin);
+extern void ADC_Unexport(int pin);
 
 // Pin reading/setting functions
-extern bool GPIO_Read(int pin);
-extern void GPIO_Set(int pin, bool value);
+extern bool GPIO_Read(int pin, bool *result);
+extern bool GPIO_Set(int pin, bool value);
 
-extern int ADC_Read(int pin);
+extern bool ADC_Read(int id, int *value);
 
-extern void PWM_Set(int pin, bool polarity, long period, long duty); // period and duty are in ns
-extern void PWM_Stop(int pin);
+extern bool PWM_Set(int pin, bool polarity, long period, long duty); // period and duty are in ns
+extern bool PWM_Stop(int pin);
 
+#else
+//'Empty' defines so it compiles on any platform that's not the BBB
 
+extern bool GPIO_Export(int pin);
+extern void GPIO_Unexport(int pin);
+
+#define GPIO_Export(pin) true
+#define GPIO_Unexport(pin)
+
+#define PWM_Export(pin) true
+#define PWM_Unexport(pin)
+
+#define ADC_Export(pin) true
+#define ADC_Unexport(pin)
+
+//Hack to both zero the result field (so it's never uninitialised) and return true
+#define GPIO_Read(pin, result) ((*(result) = 0) == 0)
+#define GPIO_Set(pin, value) true
+
+#define ADC_Read(id, value) ((*(value) = 0) == 0)
+
+#define PWM_Set(pin, polarity, period, duty) true
+#define PWM_Stop(pin) true
+
+#endif //_BBB
 
 #endif //_BBB_PIN_H
 
diff --git a/server/bbb_pin_defines.c b/server/bbb_pin_defines.c
new file mode 100644 (file)
index 0000000..23e03c5
--- /dev/null
@@ -0,0 +1,55 @@
+#include "bbb_pin_defines.h"
+
+/* Luts and stuff. Yay magic numbers **/
+
+/** 
+ * A lookup table from the actual pin number to GPIO number.
+ * e.g P8_13 is g_pin_real_to_gpio[0*46+13] = g_pin_real_to_gpio[13]
+ * e.g P9_13 is g_pin_real_to_gpio[1*46+13] = g_pin_real_to_gpio[59]
+ *
+ * Where the returned value is 0, there is no GPIO pin
+ * at that location.
+ */
+const unsigned char g_pin_real_to_gpio[BBB_PIN_COUNT+1] = {
+         0,   0,   0,   0,   0,   0,   0,  66,  67,  69,  68,  45,  44,   0,
+        26,  47,  46,  27,  65,   0,   0,   0,   0,   0,   0,   0,  61,  86,
+        88,  87,  89,  10,  11,   9,  81,   8,  80,  78,  79,  76,  77,  74,
+        75,  72,  73,  70,  71,   0,   0,   0,   0,   0,   0,   0,   0,   0,
+         0,  30,  60,  31,   0,  48,   0,   5,   4,   0,   0,   0,   0,  49,
+        15, 117,  14, 115,   0,   0, 112,   0,   0,   0,   0,   0,   0,   0,
+         0,   0,   0,   0,   0,   0,   0,   0,   0
+};
+
+/**
+ * Maps a GPIO number to an index into g_gpio (only for use in bbb_pin.c)
+ * If there is no index for that GPIO number, 128 is returned.
+ */
+const unsigned char g_pin_gpio_to_index[GPIO_MAX_NUMBER+1] = {
+       128, 128, 128, 128,   0,   1, 128, 128,   2,   3,   4,   5, 128, 128,
+         6,   7, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,   8,   9,
+       128, 128,  10,  11, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128,  12,  13,  14,  15,  16,  17, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128,  18,  19, 128, 128, 128,  20,  21,  22,  23,  24,
+        25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36, 128, 128,
+       128, 128,  37,  38,  39,  40, 128, 128, 128, 128, 128, 128, 128, 128,
+       128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+        41, 128, 128,  42, 128,  43
+};
+
+/**
+ * Maps an index in g_gpio to the corresponding GPIO number.
+ */
+const unsigned char g_pin_index_to_gpio[GPIO_NUM_PINS] = {
+         4,   5,   8,   9,  10,  11,  14,  15,  26,  27,  30,  31,  44,  45,
+        46,  47,  48,  49,  60,  61,  65,  66,  67,  68,  69,  70,  71,  72,
+        73,  74,  75,  76,  77,  78,  79,  80,  81,  86,  87,  88,  89, 112,
+       115, 117
+};
+
+/**
+ * Converts PWM index to PWM number
+ * e.g index 3 becomes 6 for /sys/class/pwm/pwm6
+ */
+const unsigned char g_pin_safe_pwm[PWM_NUM_SAFE_PINS] = {
+       0, 2, 4, 6, 7
+}; //blergh
\ No newline at end of file
index dba2d5c..ea7de7e 100644 (file)
@@ -6,12 +6,15 @@
 #ifndef _BBB_PIN_DEFINES_H
 #define _BBB_PIN_DEFINES_H
 
+/** The number of expansion pins on the BBB **/
+#define BBB_PIN_COUNT 92
+
 /** GPIO0 defines **/
 
 #define GPIO0_1 1
-#define GPIO0_2 2
-//#define GPIO0_3 3 // Used for PWM
-//#define GPIO0_4 4 // Used for PWM
+#define GPIO0_2 2 // Used for PWM
+#define GPIO0_3 3 // Used for PWM
+#define GPIO0_4 4 
 #define GPIO0_5 5
 #define GPIO0_6 6
 #define GPIO0_7 7
 #define GPIO2_31 95
 #define GPIO2_32 96
 
-/** Number of GPIO pins **/
-#define GPIO_NUM_PINS 97
-
 /** Export path **/
 #define GPIO_DEVICE_PATH "/sys/class/gpio"
 
+/** Number of useable GPIO pins **/
+#define GPIO_NUM_PINS 44
+/** The max usable GPIO number **/
+#define GPIO_MAX_NUMBER 117
+
+/* Luts */
+extern const unsigned char g_pin_real_to_gpio[BBB_PIN_COUNT+1];
+extern const unsigned char g_pin_gpio_to_index[GPIO_MAX_NUMBER+1];
+extern const unsigned char g_pin_index_to_gpio[GPIO_NUM_PINS];
+
 #define ADC_BITS 12
 #define ADC_DIGITS 5
 #define ADC0 0
 /** Number of ADC pins **/
 #define ADC_NUM_PINS 8
 
-/** Path to export ADCs with**/
-#define ADC_EXPORT_PATH "/sys/devices/bone_capemgr.9/slots"
-/** Path at which ADCs appear **/
-#define ADC_DEVICE_PATH "/sys/devices/ocp.3/helper.16"
+#define ADC_DEVICE_PATH "/sys/bus/iio/devices/iio:device0/"
 
-/** PWM defines **/
-#define EHRPWM0A 0
-#define EHRPWM0B 1
-// No other PWM pins work!
+/** PWM names to sysfs numbers **/
+#define EHRPWM0A 0 //P9_22
+#define EHRPWM0B 1 //P9_21 - period paired with EHRPWM0A
+#define EHRPWM1A 3 //P9_14
+#define EHRPWM1B 4 //P9_16 - period paired with EHRPWM1A
+#define ECAP0    2 //P9_42
+#define ECAP2   7 //P9_28
+#define EHRPWM2A 5 //P8_19
+#define EHRPWM2B 6 //P8_13 - period paired with EHRPWM2A
 
 /** Number of PWM pins **/
-#define PWM_NUM_PINS 2
+#define PWM_NUM_PINS 8
+
+/** Number of PWM pins which are guaranteed not to interfere with one another **/
+#define PWM_NUM_SAFE_PINS 5
 
 /** Path to PWM sysfs **/
 #define PWM_DEVICE_PATH "/sys/class/pwm"
 
-
+/** Maps internal pin number to safe 'pwmX' number **/
+extern const unsigned char g_pin_safe_pwm[PWM_NUM_SAFE_PINS];
 
 #endif //_BBB_PIN_DEFINES_H
 
index 43bb8bd..0092598 100644 (file)
 #define _BSD_SOURCE
 #define _XOPEN_SOURCE 600
 
+/** Determine if we're running on the BBB **/
+#ifdef __arm__
+#define _BBB
+#endif
+
 /** The current API version **/
 #define API_VERSION 0
 
index cc6a7e5..cc038a0 100644 (file)
@@ -11,7 +11,8 @@
 #define Fatal(...) FatalEx(__func__, __FILE__, __LINE__, __VA_ARGS__)
 
 /*** Macro to abort function ***/
-#define Abort(...) LogEx(LOGERR, __func__, __FILE__, __LINE__, __VA_ARGS__); return
+#define Abort(...) { LogEx(LOGERR, __func__, __FILE__, __LINE__, __VA_ARGS__); return; }
+#define AbortBool(...) { LogEx(LOGERR, __func__, __FILE__, __LINE__, __VA_ARGS__); return false; }
 
 // An enum to make the severity of log messages human readable in code
 enum {LOGERR=0, LOGWARN=1, LOGNOTE=2, LOGINFO=3,LOGDEBUG=4};
index aff5b83..2ad9dcc 100644 (file)
@@ -9,6 +9,7 @@
 #include "sensor.h"
 #include "actuator.h"
 #include "control.h"
+#include "pin_test.h"
 #include "bbb_pin_defines.h"
 
 // --- Standard headers --- //
index f702e01..05f7ddf 100644 (file)
@@ -20,7 +20,7 @@ typedef struct
        struct timeval end_time;
 
        /** Path to ADC files **/
-       char * adc_device_path;
+       const char * adc_device_path;
 
        /*** Horrible horrible hack ***/
        int argc;
index cd8c307..90ae626 100644 (file)
  */
 void Pin_Init()
 {
-       for (int i = 0; i < GPIO_NUM_PINS; ++i)
-               GPIO_Export(i);
-
-       for (int i = 0; i < ADC_NUM_PINS; ++i)
-               ADC_Export();
-
-       for (int i = 0; i < PWM_NUM_PINS; ++i)
-               PWM_Export(i);
+       
 }
 
 /**
@@ -28,13 +21,41 @@ void Pin_Init()
 void Pin_Close()
 {
        for (int i = 0; i < GPIO_NUM_PINS; ++i)
-               GPIO_Unexport(i);
+               GPIO_Unexport(g_pin_index_to_gpio[i]);
 
        for (int i = 0; i < ADC_NUM_PINS; ++i)
                ADC_Unexport(i);
 
        for (int i = 0; i < PWM_NUM_PINS; ++i)
-               PWM_Unexport(i);
+               PWM_Unexport(g_pin_safe_pwm[i]);
+}
+
+bool Pin_Configure(const char *type, int pin_export, int num)
+{
+       bool ret = true;
+
+       if (strcmp(type, "gpo") == 0 || strcmp(type, "gpi") == 0)
+       {
+               if (pin_export < 0)
+                       GPIO_Unexport(num);
+               else
+                       ret = GPIO_Export(num);
+       }
+       else if (strcmp(type, "pwm") == 0)
+       {
+               if (pin_export < 0)
+                       PWM_Unexport(num);
+               else
+                       ret = PWM_Export(num);          
+       }
+       else if (strcmp(type, "adc") == 0)
+       {
+               if (pin_export < 0)
+                       ADC_Unexport(num);
+               else
+                       ret = ADC_Export(num);
+       }
+       return ret;
 }
 
 /**
@@ -45,9 +66,10 @@ void Pin_Close()
 void Pin_Handler(FCGIContext *context, char * params)
 {
        
-       char * type = NULL;
+       const char * type = NULL;
        int num = 0;
-       bool set = true;
+       int pin_export = 0;
+       bool set = false;
        bool pol = false;
        double freq = 50;
        double duty = 0.5;
@@ -57,8 +79,9 @@ void Pin_Handler(FCGIContext *context, char * params)
        FCGIValue values[] = {
                {"type", &type, FCGI_REQUIRED(FCGI_STRING_T)},
                {"num", &num, FCGI_REQUIRED(FCGI_INT_T)}, 
-               {"set", &set, FCGI_INT_T},
-               {"pol", &pol, FCGI_INT_T},
+               {"export", &pin_export, FCGI_INT_T},
+               {"set", &set, FCGI_BOOL_T},
+               {"pol", &pol, FCGI_BOOL_T},
                {"freq", &freq, FCGI_DOUBLE_T},
                {"duty", &duty, FCGI_DOUBLE_T}
        };
@@ -67,7 +90,9 @@ void Pin_Handler(FCGIContext *context, char * params)
        typedef enum {
                TYPE,
                NUM,
+               EXPORT,
                SET,
+               POL,
                FREQ,
                DUTY
        } SensorParams;
@@ -79,7 +104,19 @@ void Pin_Handler(FCGIContext *context, char * params)
                return;
        }
 
-       Log(LOGDEBUG, "Params: type = %s, num = %d, set = %d, pol = %d, freq = %f, duty = %f", type, num, set, pol, freq, duty);
+       Log(LOGDEBUG, "Params: type = %s, num = %d, export = %d, set = %d, pol = %d, freq = %f, duty = %f", type, num, pin_export, set, pol, freq, duty);
+       if (pin_export != 0)
+       {
+               if (!Pin_Configure(type, pin_export, num))
+               {
+                       FCGI_RejectJSON(context, "Failed to (un)export the pin. Check that a valid number has been specified.");
+                       return;
+               }
+               FCGI_BeginJSON(context, STATUS_OK);
+               FCGI_JSONPair("description", "Pin (un)export OK!");
+               FCGI_EndJSON();
+               return;
+       }
 
        if (strcmp(type, "gpo") == 0)
        {
@@ -90,10 +127,15 @@ void Pin_Handler(FCGIContext *context, char * params)
                }
 
                Log(LOGDEBUG, "Setting GPIO%d to %d", num, set);
-               GPIO_Set(num, set);
-
-               FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
-               FCGI_PrintRaw("GPIO%d set to %d\n", num, set);
+               if (!GPIO_Set(num, set))
+               {
+                       FCGI_RejectJSON(context, "Failed to set the GPIO pin. Check that it's exported.");
+               }
+               else
+               {
+                       FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
+                       FCGI_PrintRaw("GPIO%d set to %d\n", num, set);
+               }
        }
        else if (strcmp(type, "gpi") == 0)
        {
@@ -103,9 +145,16 @@ void Pin_Handler(FCGIContext *context, char * params)
                        return;
                }
                Log(LOGDEBUG, "Reading GPIO%d", num);
-               FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
-               FCGI_PrintRaw("GPIO%d reads %d\n", num, GPIO_Read(num));
-
+               bool val;
+               if (!GPIO_Read(num, &val))
+               {
+                       FCGI_RejectJSON(context, "Failed to read from the GPIO pin. Check that it's exported.");
+               }
+               else
+               {
+                       FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
+                       FCGI_PrintRaw("GPIO%d reads %d\n", num, val);
+               }
        }
        else if (strcmp(type, "adc") == 0)
        {
@@ -115,31 +164,47 @@ void Pin_Handler(FCGIContext *context, char * params)
                        return;
                }
                Log(LOGDEBUG, "Reading ADC%d", num, set);
-               FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
-               FCGI_PrintRaw("ADC%d reads %d\n", num, ADC_Read(num));
+               int raw_adc;
+               if (!ADC_Read(num, &raw_adc))
+               {
+                       FCGI_RejectJSON(context, "ADC read failed. Check that it's exported.");
+               }
+               else
+               {
+                       FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
+                       FCGI_PrintRaw("ADC%d reads %d\n", num, raw_adc);
+               }
        }
        else if (strcmp(type, "pwm") == 0)
        {
-               if (num < 0 || num >= PWM_NUM_PINS)
+               if (num < 0 || num >= PWM_NUM_SAFE_PINS)
                {
                        FCGI_RejectJSON(context, "Invalid PWM pin");
                        return;
                }
-
-               FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
                
                if (set)
                {
                        Log(LOGDEBUG, "Setting PWM%d", num);
+                       duty = duty < 0 ? 0 : duty > 1 ? 1 : duty;
                        long period_ns = (long)(1e9 / freq);
                        long duty_ns = (long)(duty * period_ns);
-                       PWM_Set(num, pol, period_ns, duty_ns);
-                       FCGI_PrintRaw("PWM%d set to period_ns = %lu (%f Hz), duty_ns = %lu (%d), polarity = %d", num, period_ns, freq, duty_ns, duty*100, pol);
+                       if (!PWM_Set(num, pol, period_ns, duty_ns))
+                       {
+                               FCGI_RejectJSON(context, "PWM set failed. Check if it's exported, and that there's no channel conflict.");
+                       }
+                       else
+                       {
+                               FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
+                               FCGI_PrintRaw("PWM%d set to period_ns = %lu (%f Hz), duty_ns = %lu (%f), polarity = %d", 
+                                       num, period_ns, freq, duty_ns, duty*100, (int)pol);
+                       }
                }
                else
                {
                        Log(LOGDEBUG, "Stopping PWM%d",num);
-                       PWM_Stop(num);
+                       PWM_Stop(g_pin_safe_pwm[num]);
+                       FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
                        FCGI_PrintRaw("PWM%d stopped",num);
                }               
        }
@@ -149,8 +214,4 @@ void Pin_Handler(FCGIContext *context, char * params)
                FCGI_RejectJSON(context, "Invalid pin type");
        }
 
-       
-
-}
-
-//EOF
+}
\ No newline at end of file
index d9cb343..5c0c61f 100755 (executable)
Binary files a/server/run.sh and b/server/run.sh differ
index 63e69d8..8e4773f 100644 (file)
@@ -45,8 +45,8 @@ void Sensor_Init()
                Data_Init(&(g_sensors[i].data_file));
        }
 
-       // Get the ADCs
-       //ADC_Export();
+       // Get the required ADCs
+       ADC_Export(0);
 
        // GPIO1_28 used as a pulse for sampling
        //GPIO_Export(GPIO1_28);
@@ -178,9 +178,10 @@ bool Sensor_Read(Sensor * s, DataPoint * d)
                case 2:
                {
                        static bool set = false;
-                       
+                       int raw_adc = 0;
                        //GPIO_Set(GPIO0_30, true);
-                       d->value = (double)ADC_Read(ADC0);      //ADC #0 on the Beaglebone
+                       ADC_Read(ADC0, &raw_adc);
+                       d->value = (double)raw_adc;     //ADC #0 on the Beaglebone
                        //Log(LOGDEBUG, "Got value %f from ADC0", d->value);
                        //GPIO_Set(GPIO0_30, false);
                        set = !set;
@@ -251,6 +252,11 @@ bool Sensor_Read(Sensor * s, DataPoint * d)
                s->newest_data.time_stamp = d->time_stamp;
                s->newest_data.value = d->value;
        }
+
+#ifdef _BBB
+       //Not all cases have usleep, easiest here.
+       usleep(1000000);
+#endif
        return result;
 }
 

UCC git Repository :: git.ucc.asn.au