(Almost done) pin refactoring
[matches/MCTX3420.git] / server / pin_test.c
index cd8c307..2db079d 100644 (file)
  */
 void Pin_Init()
 {
-       for (int i = 0; i < GPIO_NUM_PINS; ++i)
-               GPIO_Export(i);
+       //Don't export anything; make the user do it.
+       /*for (int i = 0; i < GPIO_NUM_PINS; ++i)
+               GPIO_Export(g_index_to_gpio[i]);
 
        for (int i = 0; i < ADC_NUM_PINS; ++i)
-               ADC_Export();
+               ADC_Export(i);
 
-       for (int i = 0; i < PWM_NUM_PINS; ++i)
-               PWM_Export(i);
+       //Only export 'safe' PWM pins that don't interfere with one another
+       for (int i = 0; i < PWM_NUM_SAFE_PINS; ++i)
+               PWM_Export(g_pin_safe_pwm[i]);*/
 }
 
 /**
@@ -28,13 +30,13 @@ 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);
 
        for (int i = 0; i < PWM_NUM_PINS; ++i)
-               PWM_Unexport(i);
+               PWM_Unexport(g_pin_safe_pwm[i]);
 }
 
 /**
@@ -57,8 +59,8 @@ void Pin_Handler(FCGIContext *context, char * params)
        FCGIValue values[] = {
                {"type", &type, FCGI_REQUIRED(FCGI_STRING_T)},
                {"num", &num, FCGI_REQUIRED(FCGI_INT_T)}, 
-               {"set", &set, FCGI_INT_T},
-               {"pol", &pol, FCGI_INT_T},
+               {"set", &set, FCGI_BOOL_T},
+               {"pol", &pol, FCGI_BOOL_T},
                {"freq", &freq, FCGI_DOUBLE_T},
                {"duty", &duty, FCGI_DOUBLE_T}
        };
@@ -68,6 +70,7 @@ void Pin_Handler(FCGIContext *context, char * params)
                TYPE,
                NUM,
                SET,
+               POL,
                FREQ,
                DUTY
        } SensorParams;
@@ -104,7 +107,11 @@ void Pin_Handler(FCGIContext *context, char * params)
                }
                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 ret;
+               if (!GPIO_Read(num, &ret))
+                       FCGI_PrintRaw("GPIO%d read failed. Is it exported?", num);
+               else
+                       FCGI_PrintRaw("GPIO%d reads %d\n", num, ret);
 
        }
        else if (strcmp(type, "adc") == 0)
@@ -116,11 +123,19 @@ void Pin_Handler(FCGIContext *context, char * params)
                }
                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_PrintRaw("ADC%d read failed. Is it initialised?", num);
+               }
+               else
+               {
+                       FCGI_PrintRaw("ADC%d reads %d\n", num, raw_adc);
+               }
        }
        else if (strcmp(type, "pwm") == 0)
        {
-               if (num < 0 || num >= PWM_NUM_PINS)
+               if (num < 0 || num >= PWM_NUM_SAFE_PINS)
                {
                        FCGI_RejectJSON(context, "Invalid PWM pin");
                        return;
@@ -131,15 +146,17 @@ void Pin_Handler(FCGIContext *context, char * params)
                if (set)
                {
                        Log(LOGDEBUG, "Setting PWM%d", num);
+                       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 (%d), polarity = %d", num, period_ns, freq, duty_ns, duty*100, pol);
+                       PWM_Set(g_pin_safe_pwm[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);
                }
                else
                {
                        Log(LOGDEBUG, "Stopping PWM%d",num);
-                       PWM_Stop(num);
+                       PWM_Stop(g_pin_safe_pwm[num]);
                        FCGI_PrintRaw("PWM%d stopped",num);
                }               
        }
@@ -149,8 +166,4 @@ void Pin_Handler(FCGIContext *context, char * params)
                FCGI_RejectJSON(context, "Invalid pin type");
        }
 
-       
-
-}
-
-//EOF
+}
\ No newline at end of file

UCC git Repository :: git.ucc.asn.au