Add filename and line number to Log/Fatal functions
[matches/MCTX3420.git] / server / gpio.c
1 #include "gpio.h"
2
3 void pinExport(int GPIOPin) {
4         
5         char GPIOString[4];
6         char setValue[4];
7         sprintf(GPIOString, "%d", GPIOPin);
8
9         FILE * myOutputHandle = fopen(exportPath, "ab");
10         if (myOutputHandle == NULL)
11         {
12                 Log(LOGERR, "Unable to export GPIO pin %f\n", GPIOPin);
13         }
14         strcpy(setValue, GPIOString);
15         fwrite(&setValue, sizeof(char), 2, myOutputHandle);
16         fclose(myOutputHandle);
17 }
18
19 void pinDirection(int GPIOPin, int io) {
20         char setValue[4];
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);
26         }
27         if (io == 1) 
28         {
29                 strcpy(setValue,"out");
30                 fwrite(&setValue, sizeof(char), 3, myOutputHandle);
31         }
32         else if (io == 0) 
33         {
34                 strcpy(setValue,"in");
35                 fwrite(&setValue, sizeof(char), 2, myOutputHandle);
36         }
37         else Log(LOGERR, "GPIO direction must be 1 or 0\n");
38         fclose(myOutputHandle);
39 }
40
41 void pinSet(double value, int GPIOPin) {
42         int val = (int)value;
43         char GPIOValue[64];
44         char setValue[4];
45         FILE *myOutputHandle = NULL;
46         snprintf(GPIOValue, sizeof(GPIOValue), "%s%d%s", valuePath, GPIOPin, "/value");
47         if (val == 1) {
48                 if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
49                         Log(LOGERR, "Unable to open value handle for pin %f\n", GPIOPin);
50                 }
51                 strcpy(setValue, "1"); // Set value high
52                 fwrite(&setValue, sizeof(char), 1, myOutputHandle);
53         }
54         else if (val == 0){
55                 if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
56                         Log(LOGERR, "Unable to open value handle for pin %f\n", GPIOPin);
57                 }
58                 strcpy(setValue, "0"); // Set value low
59                 fwrite(&setValue, sizeof(char), 1, myOutputHandle);
60         }
61         else Log(LOGERR, "GPIO value must be 1 or 0\n");
62         fclose(myOutputHandle);
63 }
64
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
68 */
69
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)
72
73 int ADCRead(int adc_num)
74 {
75         char adc_path[64];
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));
80         if (r != -1) {
81                 buffer[r] = '\0';
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
85                 close(sensor);
86                 return convert;
87         }
88         else {
89                 Log(LOGERR, "Failed to get value from ADC %f\n", adc_num);
90                 close(sensor);
91                 return -1;
92         }
93 }
94
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
98 */
99
100 int pinRead(int GPIOPin)
101 {
102         char GPIOValue[64];
103         snprintf(GPIOValue, sizeof(GPIOValue), "%s%d%s", valuePath, GPIOPin, "/value"); //construct pin path
104         int pin = open(GPIOValue, O_RDONLY);
105         char ch;
106         lseek(pin, 0, SEEK_SET);
107         int r = read(pin, &ch, sizeof(ch));
108         if (r != -1) 
109         {
110                 if (ch != '0') {
111                         close(pin);
112                         return 1;
113                 }
114                 else {
115                         close(pin);
116                         return 0;
117                 }
118         }
119         else 
120         {
121                 Log(LOGERR, "Failed to get value from pin %f\n", GPIOPin);
122                 close(pin);
123                 return -1;
124         }
125 }
126
127 void pinUnexport(int GPIOPin) {
128         char setValue[4];
129         char GPIOString[4];
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);
134         }
135         strcpy(setValue, GPIOString);
136         fwrite(&setValue, sizeof(char), 2, myOutputHandle);
137         fclose(myOutputHandle);
138 }

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