(Almost done) pin refactoring
[matches/MCTX3420.git] / server / pin_test.c
index 3ccf458..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();
-*/
-       for (int i = 0; i < PWM_NUM_PINS; ++i)
-               PWM_Export(i);
+               ADC_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]);
 }
 
 /**
@@ -105,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)
@@ -117,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;
@@ -135,14 +149,14 @@ 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);
+                       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);
                }               
        }
@@ -152,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