3 * @brief 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)
33 bool Pin_Configure(const char *type, int pin_export, int num)
37 if (strcmp(type, "gpo") == 0 || strcmp(type, "gpi") == 0)
39 //Don't allow unexport of gpio
41 ret = GPIO_Export(num);
43 else if (strcmp(type, "pwm") == 0)
48 ret = PWM_Export(num);
50 else if (strcmp(type, "adc") == 0)
52 //Don't allow unexport of adc
54 ret = ADC_Export(num);
60 * Handle a request to the Pin test module
61 * @param context - The FastCGI context
62 * @param params - key/value pair parameters as a string
64 void Pin_Handler(FCGIContext *context, char * params)
67 const char * type = NULL;
77 FCGIValue values[] = {
78 {"type", &type, FCGI_REQUIRED(FCGI_STRING_T)},
79 {"num", &num, FCGI_REQUIRED(FCGI_INT_T)},
80 {"export", &pin_export, FCGI_INT_T},
81 {"set", &set, FCGI_BOOL_T},
82 {"pol", &pol, FCGI_BOOL_T},
83 {"freq", &freq, FCGI_DOUBLE_T},
84 {"duty", &duty, FCGI_DOUBLE_T}
87 // enum to avoid the use of magic numbers
98 // Fill values appropriately
99 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
101 // Error occured; FCGI_RejectJSON already called
105 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);
108 if (!Pin_Configure(type, pin_export, num))
110 FCGI_RejectJSON(context, "Failed to (un)export the pin. Check that a valid number has been specified.");
113 FCGI_BeginJSON(context, STATUS_OK);
114 FCGI_JSONPair("description", "Pin (un)export OK!");
119 if (strcmp(type, "gpo") == 0)
121 if (num <= 0 || num > GPIO_MAX_NUMBER)
123 FCGI_RejectJSON(context, "Invalid GPIO pin");
127 Log(LOGDEBUG, "Setting GPIO%d to %d", num, set);
128 if (!GPIO_Set(num, set))
130 FCGI_RejectJSON(context, "Failed to set the GPIO pin. Check that it's exported.");
134 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
135 FCGI_PrintRaw("GPIO%d set to %d\n", num, set);
138 else if (strcmp(type, "gpi") == 0)
140 if (num < 0 || num > GPIO_MAX_NUMBER)
142 FCGI_RejectJSON(context, "Invalid GPIO pin");
145 Log(LOGDEBUG, "Reading GPIO%d", num);
147 if (!GPIO_Read(num, &val))
149 FCGI_RejectJSON(context, "Failed to read from the GPIO pin. Check that it's exported.");
153 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
154 FCGI_PrintRaw("GPIO%d reads %d\n", num, val);
157 else if (strcmp(type, "adc") == 0)
159 if (num < 0 || num >= ADC_NUM_PINS)
161 FCGI_RejectJSON(context, "Invalid ADC pin");
164 Log(LOGDEBUG, "Reading ADC%d", num, set);
166 if (!ADC_Read(num, &raw_adc))
168 FCGI_RejectJSON(context, "ADC read failed. Check that it's exported.");
172 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
173 FCGI_PrintRaw("%d\n", raw_adc);
176 else if (strcmp(type, "pwm") == 0)
178 if (num < 0 || num >= PWM_NUM_PINS)
180 FCGI_RejectJSON(context, "Invalid PWM pin");
186 Log(LOGDEBUG, "Setting PWM%d", num);
187 duty = duty < 0 ? 0 : duty > 1 ? 1 : duty;
188 long period_ns = (long)(1e9 / freq);
189 long duty_ns = (long)(duty * period_ns);
190 if (!PWM_Set(num, pol, period_ns, duty_ns))
192 FCGI_RejectJSON(context, "PWM set failed. Check if it's exported, and that there's no channel conflict.");
196 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
197 FCGI_PrintRaw("PWM%d set to period_ns = %lu (%f Hz), duty_ns = %lu (%f), polarity = %d",
198 num, period_ns, freq, duty_ns, duty*100, (int)pol);
203 Log(LOGDEBUG, "Stopping PWM%d",num);
205 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
206 FCGI_PrintRaw("PWM%d stopped",num);
211 Log(LOGDEBUG, "Invalid pin type %s", type);
212 FCGI_RejectJSON(context, "Invalid pin type");