3 void pinExport(int GPIOPin) {
7 sprintf(GPIOString, "%d", GPIOPin);
9 FILE * myOutputHandle = fopen(exportPath, "ab");
10 if (myOutputHandle == NULL)
12 Log(LOGERR, "Unable to export GPIO pin %f\n", GPIOPin);
14 strcpy(setValue, GPIOString);
15 fwrite(&setValue, sizeof(char), 2, myOutputHandle);
16 fclose(myOutputHandle);
19 void pinDirection(int GPIOPin, int io) {
21 char GPIODirection[64];
22 FILE *myOutputHandle = NULL;
23 snprintf(GPIODirection, sizeof(GPIODirection), "%s%d%s", directionPath, GPIOPin, "/direction");
24 if ((myOutputHandle = fopen(GPIODirection, "rb+")) == NULL){
25 Log(LOGERR, "Unable to open direction handle for pin %f\n", GPIOPin);
29 strcpy(setValue,"out");
30 fwrite(&setValue, sizeof(char), 3, myOutputHandle);
34 strcpy(setValue,"in");
35 fwrite(&setValue, sizeof(char), 2, myOutputHandle);
37 else Log(LOGERR, "GPIO direction must be 1 or 0\n");
38 fclose(myOutputHandle);
41 void pinSet(double value, int GPIOPin) {
45 FILE *myOutputHandle = NULL;
46 snprintf(GPIOValue, sizeof(GPIOValue), "%s%d%s", valuePath, GPIOPin, "/value");
48 if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
49 Log(LOGERR, "Unable to open value handle for pin %f\n", GPIOPin);
51 strcpy(setValue, "1"); // Set value high
52 fwrite(&setValue, sizeof(char), 1, myOutputHandle);
55 if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
56 Log(LOGERR, "Unable to open value handle for pin %f\n", GPIOPin);
58 strcpy(setValue, "0"); // Set value low
59 fwrite(&setValue, sizeof(char), 1, myOutputHandle);
61 else Log(LOGERR, "GPIO value must be 1 or 0\n");
62 fclose(myOutputHandle);
65 /** Open an ADC and return the voltage value from it
66 * @param adc_num - ADC number, ranges from 0 to 7 on the Beaglebone
67 @return the converted voltage value if successful
70 //TODO: create a function to lookup the ADC or pin number instead of manually
71 // specifying it here (so we can keep all the numbers in one place)
73 int ADCRead(int adc_num)
76 snprintf(adc_path, sizeof(adc_path), "%s%d", ADCPath, adc_num); // Construct ADC path
77 int sensor = open(adc_path, O_RDONLY);
78 char buffer[64]; // I think ADCs are only 12 bits (0-4096), buffer can probably be smaller
79 int r = read(sensor, buffer, sizeof(buffer));
82 int value = atoi(buffer);
83 double convert = (value/4096) * 1000; // Random conversion factor, will be different for each sensor (get from datasheets)
84 // lseek(sensor, 0, 0); (I think this is uneeded as we are reopening the file on each sensor read; if sensor is read continously we'll need this
89 Log(LOGERR, "Failed to get value from ADC %f\n", adc_num);
95 /** Open a digital pin and return the value from it
96 * @param pin_num - pin number, specified by electronics team
97 @return 1 or 0 if reading was successful
100 int pinRead(int GPIOPin)
103 snprintf(GPIOValue, sizeof(GPIOValue), "%s%d%s", valuePath, GPIOPin, "/value"); //construct pin path
104 int pin = open(GPIOValue, O_RDONLY);
106 lseek(pin, 0, SEEK_SET);
107 int r = read(pin, &ch, sizeof(ch));
121 Log(LOGERR, "Failed to get value from pin %f\n", GPIOPin);
127 void pinUnexport(int GPIOPin) {
130 sprintf(GPIOString, "%d", GPIOPin);
131 FILE *myOutputHandle = NULL;
132 if ((myOutputHandle = fopen(unexportPath, "ab")) == NULL) {
133 Log(LOGERR, "Couldn't unexport GPIO pin %f\n", GPIOPin);
135 strcpy(setValue, GPIOString);
136 fwrite(&setValue, sizeof(char), 2, myOutputHandle);
137 fclose(myOutputHandle);