3 * @purpose Implementations to allow direct control over pins through FastCGI
11 * Export *ALL* pins for control
15 for (int i = 0; i < GPIO_NUM_PINS; ++i)
18 for (int i = 0; i < ADC_NUM_PINS; ++i)
21 for (int i = 0; i < PWM_NUM_PINS; ++i)
30 for (int i = 0; i < GPIO_NUM_PINS; ++i)
33 for (int i = 0; i < ADC_NUM_PINS; ++i)
36 for (int i = 0; i < PWM_NUM_PINS; ++i)
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
45 void Pin_Handler(FCGIContext *context, char * params)
57 FCGIValue values[] = {
58 {"type", &type, FCGI_REQUIRED(FCGI_STRING_T)},
59 {"num", &num, FCGI_REQUIRED(FCGI_INT_T)},
60 {"set", &set, FCGI_INT_T},
61 {"pol", &pol, FCGI_INT_T},
62 {"freq", &freq, FCGI_DOUBLE_T},
63 {"duty", &duty, FCGI_DOUBLE_T}
66 // enum to avoid the use of magic numbers
75 // Fill values appropriately
76 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
78 // Error occured; FCGI_RejectJSON already called
82 Log(LOGDEBUG, "Params: type = %s, num = %d, set = %d, pol = %d, freq = %f, duty = %f", type, num, set, pol, freq, duty);
84 if (strcmp(type, "gpo") == 0)
86 if (num <= 0 || num > GPIO_NUM_PINS)
88 FCGI_RejectJSON(context, "Invalid GPIO pin");
92 Log(LOGDEBUG, "Setting GPIO%d to %d", num, set);
95 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
96 FCGI_PrintRaw("GPIO%d set to %d\n", num, set);
98 else if (strcmp(type, "gpi") == 0)
100 if (num < 0 || num >= GPIO_NUM_PINS)
102 FCGI_RejectJSON(context, "Invalid GPIO pin");
105 Log(LOGDEBUG, "Reading GPIO%d", num);
106 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
107 FCGI_PrintRaw("GPIO%d reads %d\n", num, GPIO_Read(num));
110 else if (strcmp(type, "adc") == 0)
112 if (num < 0 || num >= ADC_NUM_PINS)
114 FCGI_RejectJSON(context, "Invalid ADC pin");
117 Log(LOGDEBUG, "Reading ADC%d", num, set);
118 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
119 FCGI_PrintRaw("ADC%d reads %d\n", num, ADC_Read(num));
121 else if (strcmp(type, "pwm") == 0)
123 if (num < 0 || num >= PWM_NUM_PINS)
125 FCGI_RejectJSON(context, "Invalid PWM pin");
129 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
133 Log(LOGDEBUG, "Setting PWM%d", num);
134 long period_ns = (long)(1e9 / freq);
135 long duty_ns = (long)(duty * period_ns);
136 PWM_Set(num, pol, period_ns, duty_ns);
137 FCGI_PrintRaw("PWM%d set to period_ns = %lu (%f Hz), duty_ns = %lu (%d), polarity = %d", num, period_ns, freq, duty_ns, duty*100, pol);
141 Log(LOGDEBUG, "Stopping PWM%d",num);
143 FCGI_PrintRaw("PWM%d stopped",num);
148 Log(LOGDEBUG, "Invalid pin type %s", type);
149 FCGI_RejectJSON(context, "Invalid pin type");