X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fpin_test.c;h=8640a60df6c69999cbc49ea9c4ecf09649568872;hb=27ff700c938e48bd88ca63575d65575150d9e842;hp=ca857ecd4452af72a3b1ec42fb1d4e4c8b55a8b0;hpb=1faf2ef76c719ed79e13b359591082854e739fa3;p=matches%2FMCTX3420.git diff --git a/server/pin_test.c b/server/pin_test.c index ca857ec..8640a60 100644 --- a/server/pin_test.c +++ b/server/pin_test.c @@ -1,6 +1,6 @@ /** * @file pin_test.c - * @purpose Implementations to allow direct control over pins through FastCGI + * @brief Implementations to allow direct control over pins through FastCGI */ #include "pin_test.h" @@ -12,14 +12,7 @@ */ void Pin_Init() { - for (int i = 0; i < 128; ++i) - GPIO_Export(i); - - for (int i = 0; i < ADC_NUM_PINS; ++i) - ADC_Export(); - - for (int i = 0; i < PWM_NUM_PINS; ++i) - PWM_Export(i); + } /** @@ -28,7 +21,7 @@ void Pin_Init() void Pin_Close() { for (int i = 0; i < GPIO_NUM_PINS; ++i) - GPIO_Unexport(i); + GPIO_Unexport(g_pin_index_to_gpio[i]); for (int i = 0; i < ADC_NUM_PINS; ++i) ADC_Unexport(i); @@ -37,6 +30,38 @@ void Pin_Close() PWM_Unexport(i); } +/** + * Configures a pin (Export/Unexport business) + * @param type The pin type (GPIO/PWM/ADC) + * @param pin_export Whether to export/unexport/leave-as-is the pin + * @param num The pin number + */ +bool Pin_Configure(const char *type, int pin_export, int num) +{ + bool ret = true; + + if (strcmp(type, "gpo") == 0 || strcmp(type, "gpi") == 0) + { + //Don't allow unexport of gpio + if (pin_export > 0) + ret = GPIO_Export(num); + } + else if (strcmp(type, "pwm") == 0) + { + if (pin_export < 0) + PWM_Unexport(num); + else + ret = PWM_Export(num); + } + else if (strcmp(type, "adc") == 0) + { + //Don't allow unexport of adc + if (pin_export > 0) + ret = ADC_Export(num); + } + return ret; +} + /** * Handle a request to the Pin test module * @param context - The FastCGI context @@ -45,9 +70,10 @@ void Pin_Close() void Pin_Handler(FCGIContext *context, char * params) { - char * type = NULL; + const char * type = NULL; int num = 0; - bool set = true; + int pin_export = 0; + bool set = false; bool pol = false; double freq = 50; double duty = 0.5; @@ -57,6 +83,7 @@ void Pin_Handler(FCGIContext *context, char * params) FCGIValue values[] = { {"type", &type, FCGI_REQUIRED(FCGI_STRING_T)}, {"num", &num, FCGI_REQUIRED(FCGI_INT_T)}, + {"export", &pin_export, FCGI_INT_T}, {"set", &set, FCGI_BOOL_T}, {"pol", &pol, FCGI_BOOL_T}, {"freq", &freq, FCGI_DOUBLE_T}, @@ -67,6 +94,7 @@ void Pin_Handler(FCGIContext *context, char * params) typedef enum { TYPE, NUM, + EXPORT, SET, POL, FREQ, @@ -80,33 +108,57 @@ void Pin_Handler(FCGIContext *context, char * params) return; } - Log(LOGDEBUG, "Params: type = %s, num = %d, set = %d, pol = %d, freq = %f, duty = %f", type, num, set, pol, freq, duty); + 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); + if (pin_export != 0) + { + if (!Pin_Configure(type, pin_export, num)) + { + FCGI_RejectJSON(context, "Failed to (un)export the pin. Check that a valid number has been specified."); + return; + } + FCGI_BeginJSON(context, STATUS_OK); + FCGI_JSONPair("description", "Pin (un)export OK!"); + FCGI_EndJSON(); + return; + } if (strcmp(type, "gpo") == 0) { - if (num <= 0 || num > GPIO_NUM_PINS) + if (num <= 0 || num > GPIO_MAX_NUMBER) { FCGI_RejectJSON(context, "Invalid GPIO pin"); return; } Log(LOGDEBUG, "Setting GPIO%d to %d", num, set); - GPIO_Set(num, set); - - FCGI_PrintRaw("Content-type: text/plain\r\n\r\n"); - FCGI_PrintRaw("GPIO%d set to %d\n", num, set); + if (!GPIO_Set(num, set)) + { + FCGI_RejectJSON(context, "Failed to set the GPIO pin. Check that it's exported."); + } + else + { + FCGI_PrintRaw("Content-type: text/plain\r\n\r\n"); + FCGI_PrintRaw("GPIO%d set to %d\n", num, set); + } } else if (strcmp(type, "gpi") == 0) { - if (num < 0 || num >= GPIO_NUM_PINS) + if (num < 0 || num > GPIO_MAX_NUMBER) { FCGI_RejectJSON(context, "Invalid GPIO pin"); return; } Log(LOGDEBUG, "Reading GPIO%d", num); - FCGI_PrintRaw("Content-type: text/plain\r\n\r\n"); - FCGI_PrintRaw("GPIO%d reads %d\n", num, GPIO_Read(num)); - + bool val; + if (!GPIO_Read(num, &val)) + { + FCGI_RejectJSON(context, "Failed to read from the GPIO pin. Check that it's exported."); + } + else + { + FCGI_PrintRaw("Content-type: text/plain\r\n\r\n"); + FCGI_PrintRaw("GPIO%d reads %d\n", num, val); + } } else if (strcmp(type, "adc") == 0) { @@ -116,8 +168,16 @@ void Pin_Handler(FCGIContext *context, char * params) return; } Log(LOGDEBUG, "Reading ADC%d", num, set); - FCGI_PrintRaw("Content-type: text/plain\r\n\r\n"); - FCGI_PrintRaw("ADC%d reads %d\n", num, ADC_Read(num)); + int raw_adc; + if (!ADC_Read(num, &raw_adc)) + { + FCGI_RejectJSON(context, "ADC read failed. Check that it's exported."); + } + else + { + FCGI_PrintRaw("Content-type: text/plain\r\n\r\n"); + FCGI_PrintRaw("%d\n", raw_adc); + } } else if (strcmp(type, "pwm") == 0) { @@ -126,8 +186,6 @@ void Pin_Handler(FCGIContext *context, char * params) FCGI_RejectJSON(context, "Invalid PWM pin"); return; } - - FCGI_PrintRaw("Content-type: text/plain\r\n\r\n"); if (set) { @@ -135,14 +193,22 @@ void Pin_Handler(FCGIContext *context, char * params) duty = duty < 0 ? 0 : duty > 1 ? 1 : duty; long period_ns = (long)(1e9 / freq); long duty_ns = (long)(duty * period_ns); - PWM_Set(num, pol, period_ns, duty_ns); - FCGI_PrintRaw("PWM%d set to period_ns = %lu (%f Hz), duty_ns = %lu (%f), polarity = %d", - num, period_ns, freq, duty_ns, duty*100, (int)pol); + if (!PWM_Set(num, pol, period_ns, duty_ns)) + { + FCGI_RejectJSON(context, "PWM set failed. Check if it's exported, and that there's no channel conflict."); + } + else + { + FCGI_PrintRaw("Content-type: text/plain\r\n\r\n"); + FCGI_PrintRaw("PWM%d set to period_ns = %lu (%f Hz), duty_ns = %lu (%f), polarity = %d", + num, period_ns, freq, duty_ns, duty*100, (int)pol); + } } else { Log(LOGDEBUG, "Stopping PWM%d",num); PWM_Stop(num); + FCGI_PrintRaw("Content-type: text/plain\r\n\r\n"); FCGI_PrintRaw("PWM%d stopped",num); } } @@ -152,8 +218,4 @@ void Pin_Handler(FCGIContext *context, char * params) FCGI_RejectJSON(context, "Invalid pin type"); } - - } - -//EOF