semi update lut stuff
[matches/MCTX3420.git] / server / pin_test.c
1 /**
2  * @file pin_test.c
3  * @purpose Implementations to allow direct control over pins through FastCGI
4  */
5
6 #include "pin_test.h"
7
8 #include "bbb_pin.h"
9
10 /**
11  * Export *ALL* pins for control
12  */
13 void Pin_Init()
14 {
15         for (int i = 0; i < GPIO_NUM_PINS; ++i)
16                 GPIO_Export(g_index_to_gpio[i]);
17
18         for (int i = 0; i < ADC_NUM_PINS; ++i)
19                 ADC_Export();
20
21         for (int i = 0; i < PWM_NUM_PINS; ++i)
22                 PWM_Export(i);
23 }
24
25 /**
26  * Unexport all pins
27  */
28 void Pin_Close()
29 {
30         for (int i = 0; i < GPIO_NUM_PINS; ++i)
31                 GPIO_Unexport(g_index_to_gpio[i]);
32
33         for (int i = 0; i < ADC_NUM_PINS; ++i)
34                 ADC_Unexport(i);
35
36         for (int i = 0; i < PWM_NUM_PINS; ++i)
37                 PWM_Unexport(i);
38 }
39
40 /**
41  * Handle a request to the Pin test module
42  * @param context - The FastCGI context
43  * @param params - key/value pair parameters as a string
44  */
45 void Pin_Handler(FCGIContext *context, char * params)
46 {
47         
48         char * type = NULL;
49         int num = 0;
50         bool set = true;
51         bool pol = false;
52         double freq = 50;
53         double duty = 0.5;
54         
55
56         // key/value pairs
57         FCGIValue values[] = {
58                 {"type", &type, FCGI_REQUIRED(FCGI_STRING_T)},
59                 {"num", &num, FCGI_REQUIRED(FCGI_INT_T)}, 
60                 {"set", &set, FCGI_BOOL_T},
61                 {"pol", &pol, FCGI_BOOL_T},
62                 {"freq", &freq, FCGI_DOUBLE_T},
63                 {"duty", &duty, FCGI_DOUBLE_T}
64         };
65
66         // enum to avoid the use of magic numbers
67         typedef enum {
68                 TYPE,
69                 NUM,
70                 SET,
71                 POL,
72                 FREQ,
73                 DUTY
74         } SensorParams;
75         
76         // Fill values appropriately
77         if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
78         {
79                 // Error occured; FCGI_RejectJSON already called
80                 return;
81         }
82
83         Log(LOGDEBUG, "Params: type = %s, num = %d, set = %d, pol = %d, freq = %f, duty = %f", type, num, set, pol, freq, duty);
84
85         if (strcmp(type, "gpo") == 0)
86         {
87                 if (num <= 0 || num > GPIO_NUM_PINS)
88                 {
89                         FCGI_RejectJSON(context, "Invalid GPIO pin");
90                         return;
91                 }
92
93                 Log(LOGDEBUG, "Setting GPIO%d to %d", num, set);
94                 GPIO_Set(num, set);
95
96                 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
97                 FCGI_PrintRaw("GPIO%d set to %d\n", num, set);
98         }
99         else if (strcmp(type, "gpi") == 0)
100         {
101                 if (num < 0 || num >= GPIO_NUM_PINS)
102                 {
103                         FCGI_RejectJSON(context, "Invalid GPIO pin");
104                         return;
105                 }
106                 Log(LOGDEBUG, "Reading GPIO%d", num);
107                 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
108                 FCGI_PrintRaw("GPIO%d reads %d\n", num, GPIO_Read(num));
109
110         }
111         else if (strcmp(type, "adc") == 0)
112         {
113                 if (num < 0 || num >= ADC_NUM_PINS)
114                 {
115                         FCGI_RejectJSON(context, "Invalid ADC pin");
116                         return;
117                 }
118                 Log(LOGDEBUG, "Reading ADC%d", num, set);
119                 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
120                 FCGI_PrintRaw("ADC%d reads %d\n", num, ADC_Read(num));
121         }
122         else if (strcmp(type, "pwm") == 0)
123         {
124                 if (num < 0 || num >= PWM_NUM_PINS)
125                 {
126                         FCGI_RejectJSON(context, "Invalid PWM pin");
127                         return;
128                 }
129
130                 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
131                 
132                 if (set)
133                 {
134                         Log(LOGDEBUG, "Setting PWM%d", num);
135                         duty = duty < 0 ? 0 : duty > 1 ? 1 : duty;
136                         long period_ns = (long)(1e9 / freq);
137                         long duty_ns = (long)(duty * period_ns);
138                         PWM_Set(num, pol, period_ns, duty_ns);
139                         FCGI_PrintRaw("PWM%d set to period_ns = %lu (%f Hz), duty_ns = %lu (%f), polarity = %d", 
140                                 num, period_ns, freq, duty_ns, duty*100, (int)pol);
141                 }
142                 else
143                 {
144                         Log(LOGDEBUG, "Stopping PWM%d",num);
145                         PWM_Stop(num);
146                         FCGI_PrintRaw("PWM%d stopped",num);
147                 }               
148         }
149         else
150         {
151                 Log(LOGDEBUG, "Invalid pin type %s", type);
152                 FCGI_RejectJSON(context, "Invalid pin type");
153         }
154
155         
156
157 }
158
159 //EOF

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