3 void pinExport(int GPIOPin) {
4 FILE *myOutputHandle = NULL;
7 sprintf(GPIOString, "%d", GPIOPin);
8 if ((myOutputHandle = fopen(exportPath, "ab")) == NULL){
9 Log(LOGERR, "Unable to export GPIO pin %f\n", GPIOPin);
11 strcpy(setValue, GPIOString);
12 fwrite(&setValue, sizeof(char), 2, myOutputHandle);
13 fclose(myOutputHandle);
16 void pinDirection(int GPIOPin, int io) {
18 char GPIODirection[64];
19 FILE *myOutputHandle = NULL;
20 snprintf(GPIODirection, sizeof(GPIODirection), "%s%d%s", directionPath, GPIOPin, "/direction");
21 if ((myOutputHandle = fopen(GPIODirection, "rb+")) == NULL){
22 Log(LOGERR, "Unable to open direction handle for pin %f\n", GPIOPin);
25 strcpy(setValue,"out");
26 fwrite(&setValue, sizeof(char), 3, myOutputHandle);
28 strcpy(setValue,"in");
29 fwrite(&setValue, sizeof(char), 2, myOutputHandle);
30 else Log(LOGERR, "GPIO direction must be 1 or 0\n");
31 fclose(myOutputHandle);
34 void pinSet(double value, int GPIOPin) {
38 FILE *myOutputHandle = NULL;
39 snprintf(GPIOValue, sizeof(GPIOValue), "%s%d%s", valuePath, GPIOPin, "/value");
41 if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
42 Log(LOGERR, "Unable to open value handle for pin %f\n", GPIOPin);
44 strcpy(setValue, "1"); // Set value high
45 fwrite(&setValue, sizeof(char), 1, myOutputHandle);
48 if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
49 Log(LOGERR, "Unable to open value handle for pin %f\n", GPIOPin);
51 strcpy(setValue, "0"); // Set value low
52 fwrite(&setValue, sizeof(char), 1, myOutputHandle);
54 else Log(LOGERR, "GPIO value must be 1 or 0\n");
55 fclose(myOutputHandle);
58 /** Open an ADC and return the voltage value from it
59 * @param adc_num - ADC number, ranges from 0 to 7 on the Beaglebone
60 @return the converted voltage value if successful
63 //TODO: create a function to lookup the ADC or pin number instead of manually
64 // specifying it here (so we can keep all the numbers in one place)
66 int ADCRead(int adc_num)
69 snprintf(adc_path, sizeof(adc_path), "%s%d", ADCPath, adc_num); // Construct ADC path
70 int sensor = open(adc_path, O_RDONLY);
71 char buffer[64]; // I think ADCs are only 12 bits (0-4096), buffer can probably be smaller
72 int read = read(sensor, buffer, sizeof(buffer);
75 int value = atoi(buffer);
76 double convert = (value/4096) * 1000; // Random conversion factor, will be different for each sensor (get from datasheets)
77 // 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
82 Log(LOGERR, "Failed to get value from ADC %f\n", adc_num);
88 /** Open a digital pin and return the value from it
89 * @param pin_num - pin number, specified by electronics team
90 @return 1 or 0 if reading was successful
93 int pinRead(int GPIOPin)
96 snprintf(GPIOValue, sizeof(GPIOValue), "%s%d%s", valuePath, GPIOPin, "/value"); //construct pin path
97 int pin = open(GPIOValue, O_RDONLY);
99 lseek(fd, 0, SEEK_SET);
100 int read = read(pin, &ch, sizeof(ch);
111 Log(LOGERR, "Failed to get value from pin %f\n", GPIOPin);
117 void pinUnexport(int GPIOPin) {
120 sprintf(GPIOString, "%d", GPIOPin);
121 FILE *myOutputHandle = NULL;
122 if ((myOutputHandle = fopen(unexportPath, "ab")) == NULL) {
123 Log(LOGERR, "Couldn't unexport GPIO pin %f\n", GPIOPin);
125 strcpy(setValue, GPIOString);
126 fwrite(&setValue, sizeof(char), 2, myOutputHandle);
127 fclose(myOutputHandle);