3 * @purpose Implementations to allow direct control over pins through FastCGI
11 * Export *ALL* pins for control
15 for (int i = 0; i < 128; ++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_BOOL_T},
61 {"pol", &pol, FCGI_BOOL_T},
62 {"freq", &freq, FCGI_DOUBLE_T},
63 {"duty", &duty, FCGI_DOUBLE_T}
66 // enum to avoid the use of magic numbers
76 // Fill values appropriately
77 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
79 // Error occured; FCGI_RejectJSON already called
83 Log(LOGDEBUG, "Params: type = %s, num = %d, set = %d, pol = %d, freq = %f, duty = %f", type, num, set, pol, freq, duty);
85 if (strcmp(type, "gpo") == 0)
87 if (num <= 0 || num > GPIO_NUM_PINS)
89 FCGI_RejectJSON(context, "Invalid GPIO pin");
93 Log(LOGDEBUG, "Setting GPIO%d to %d", num, set);
96 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
97 FCGI_PrintRaw("GPIO%d set to %d\n", num, set);
99 else if (strcmp(type, "gpi") == 0)
101 if (num < 0 || num >= GPIO_NUM_PINS)
103 FCGI_RejectJSON(context, "Invalid GPIO pin");
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));
111 else if (strcmp(type, "adc") == 0)
113 if (num < 0 || num >= ADC_NUM_PINS)
115 FCGI_RejectJSON(context, "Invalid ADC pin");
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));
122 else if (strcmp(type, "pwm") == 0)
124 if (num < 0 || num >= PWM_NUM_PINS)
126 FCGI_RejectJSON(context, "Invalid PWM pin");
130 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
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);
144 Log(LOGDEBUG, "Stopping PWM%d",num);
146 FCGI_PrintRaw("PWM%d stopped",num);
151 Log(LOGDEBUG, "Invalid pin type %s", type);
152 FCGI_RejectJSON(context, "Invalid pin type");