3 * @purpose Implementations to allow direct control over pins through FastCGI
11 * Export *ALL* pins for control
23 for (int i = 0; i < GPIO_NUM_PINS; ++i)
24 GPIO_Unexport(g_pin_index_to_gpio[i]);
26 for (int i = 0; i < ADC_NUM_PINS; ++i)
29 for (int i = 0; i < PWM_NUM_PINS; ++i)
30 PWM_Unexport(g_pin_safe_pwm[i]);
33 bool Pin_Configure(const char *type, int pin_export, int num)
37 if (strcmp(type, "gpo") == 0 || strcmp(type, "gpi") == 0)
42 ret = GPIO_Export(num);
44 else if (strcmp(type, "pwm") == 0)
49 ret = PWM_Export(num);
51 else if (strcmp(type, "adc") == 0)
56 ret = ADC_Export(num);
62 * Handle a request to the Pin test module
63 * @param context - The FastCGI context
64 * @param params - key/value pair parameters as a string
66 void Pin_Handler(FCGIContext *context, char * params)
69 const char * type = NULL;
79 FCGIValue values[] = {
80 {"type", &type, FCGI_REQUIRED(FCGI_STRING_T)},
81 {"num", &num, FCGI_REQUIRED(FCGI_INT_T)},
82 {"export", &pin_export, FCGI_INT_T},
83 {"set", &set, FCGI_BOOL_T},
84 {"pol", &pol, FCGI_BOOL_T},
85 {"freq", &freq, FCGI_DOUBLE_T},
86 {"duty", &duty, FCGI_DOUBLE_T}
89 // enum to avoid the use of magic numbers
100 // Fill values appropriately
101 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
103 // Error occured; FCGI_RejectJSON already called
107 Log(LOGDEBUG, "Params: type = %s, num = %d, export = %d, set = %d, pol = %d, freq = %f, duty = %f", type, num, pin_export, set, pol, freq, duty);
110 if (!Pin_Configure(type, pin_export, num))
112 FCGI_RejectJSON(context, "Failed to (un)export the pin. Check that a valid number has been specified.");
115 FCGI_BeginJSON(context, STATUS_OK);
116 FCGI_JSONPair("description", "Pin (un)export OK!");
121 if (strcmp(type, "gpo") == 0)
123 if (num <= 0 || num > GPIO_NUM_PINS)
125 FCGI_RejectJSON(context, "Invalid GPIO pin");
129 Log(LOGDEBUG, "Setting GPIO%d to %d", num, set);
130 if (!GPIO_Set(num, set))
132 FCGI_RejectJSON(context, "Failed to set the GPIO pin. Check that it's exported.");
136 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
137 FCGI_PrintRaw("GPIO%d set to %d\n", num, set);
140 else if (strcmp(type, "gpi") == 0)
142 if (num < 0 || num >= GPIO_NUM_PINS)
144 FCGI_RejectJSON(context, "Invalid GPIO pin");
147 Log(LOGDEBUG, "Reading GPIO%d", num);
149 if (!GPIO_Read(num, &val))
151 FCGI_RejectJSON(context, "Failed to read from the GPIO pin. Check that it's exported.");
155 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
156 FCGI_PrintRaw("GPIO%d reads %d\n", num, val);
159 else if (strcmp(type, "adc") == 0)
161 if (num < 0 || num >= ADC_NUM_PINS)
163 FCGI_RejectJSON(context, "Invalid ADC pin");
166 Log(LOGDEBUG, "Reading ADC%d", num, set);
168 if (!ADC_Read(num, &raw_adc))
170 FCGI_RejectJSON(context, "ADC read failed. Check that it's exported.");
174 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
175 FCGI_PrintRaw("ADC%d reads %d\n", num, raw_adc);
178 else if (strcmp(type, "pwm") == 0)
180 if (num < 0 || num >= PWM_NUM_SAFE_PINS)
182 FCGI_RejectJSON(context, "Invalid PWM pin");
188 Log(LOGDEBUG, "Setting PWM%d", num);
189 duty = duty < 0 ? 0 : duty > 1 ? 1 : duty;
190 long period_ns = (long)(1e9 / freq);
191 long duty_ns = (long)(duty * period_ns);
192 if (!PWM_Set(num, pol, period_ns, duty_ns))
194 FCGI_RejectJSON(context, "PWM set failed. Check if it's exported, and that there's no channel conflict.");
198 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
199 FCGI_PrintRaw("PWM%d set to period_ns = %lu (%f Hz), duty_ns = %lu (%f), polarity = %d",
200 num, period_ns, freq, duty_ns, duty*100, (int)pol);
205 Log(LOGDEBUG, "Stopping PWM%d",num);
206 PWM_Stop(g_pin_safe_pwm[num]);
207 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
208 FCGI_PrintRaw("PWM%d stopped",num);
213 Log(LOGDEBUG, "Invalid pin type %s", type);
214 FCGI_RejectJSON(context, "Invalid pin type");