Automatic commit of irc logs
[matches/MCTX3420.git] / BBB code / gpio.c
1 #include "gpio.h"
2
3 void pinExport(int GPIOPin) {
4         FILE *myOutputHandle = NULL;
5         char GPIOString[4];
6         char setValue[4];
7         sprintf(GPIOString, "%d", GPIOPin);
8         if ((myOutputHandle = fopen(exportPath, "ab")) == NULL){
9                 Log(LOGERR, "Unable to export GPIO pin %f\n", GPIOPin);
10         }
11         strcpy(setValue, GPIOString);
12         fwrite(&setValue, sizeof(char), 2, myOutputHandle);
13         fclose(myOutputHandle);
14 }
15
16 void pinDirection(int GPIOPin, int io) {
17         char setValue[4];
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);
23         }
24         if (io == 1) {
25                 strcpy(setValue,"out");
26                 fwrite(&setValue, sizeof(char), 3, myOutputHandle);
27         else if (io == 0) {
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);
32 }
33
34 void pinSet(double value, int GPIOPin) {
35         int val = (int)value;
36         char GPIOValue[64];
37         char setValue[4];
38         FILE *myOutputHandle = NULL;
39         snprintf(GPIOValue, sizeof(GPIOValue), "%s%d%s", valuePath, GPIOPin, "/value");
40         if (val == 1) {
41                 if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
42                         Log(LOGERR, "Unable to open value handle for pin %f\n", GPIOPin);
43                 }
44                 strcpy(setValue, "1"); // Set value high
45                 fwrite(&setValue, sizeof(char), 1, myOutputHandle);
46         }
47         else if (val == 0){
48                 if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
49                         Log(LOGERR, "Unable to open value handle for pin %f\n", GPIOPin);
50                 }
51                 strcpy(setValue, "0"); // Set value low
52                 fwrite(&setValue, sizeof(char), 1, myOutputHandle);
53         }
54         else Log(LOGERR, "GPIO value must be 1 or 0\n");
55         fclose(myOutputHandle);
56 }
57
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
61 */
62
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)
65
66 int ADCRead(int adc_num)
67 {
68         char adc_path[64];
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);
73         if (read != -1) {
74                 buffer[read] = NULL;
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
78                 close(sensor);
79                 return convert;
80         }
81         else {
82                 Log(LOGERR, "Failed to get value from ADC %f\n", adc_num);
83                 close(sensor);
84                 return -1;
85         }
86 }
87
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
91 */
92
93 int pinRead(int GPIOPin)
94 {
95         char GPIOValue[64];
96         snprintf(GPIOValue, sizeof(GPIOValue), "%s%d%s", valuePath, GPIOPin, "/value"); //construct pin path
97         int pin = open(GPIOValue, O_RDONLY);
98         char ch;
99         lseek(fd, 0, SEEK_SET);
100         int read = read(pin, &ch, sizeof(ch);
101         if (read != -1) {
102                 if (ch != '0') {
103                         close(pin);
104                         return 1;
105                 }
106                 else {
107                         close(pin);
108                         return 0;
109                 }
110         else {
111                 Log(LOGERR, "Failed to get value from pin %f\n", GPIOPin);
112                 close(pin);
113                 return -1;
114         }
115 }
116
117 void pinUnexport(int GPIOPin) {
118         char setValue[4];
119         char GPIOString[4];
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);
124         }
125         strcpy(setValue, GPIOString);
126         fwrite(&setValue, sizeof(char), 2, myOutputHandle);
127         fclose(myOutputHandle);
128 }

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