debugging lightswitch
authorArjen Baart <arjen@andromeda.nl>
Thu, 1 Apr 2021 05:36:40 +0000 (07:36 +0200)
committerArjen Baart <arjen@andromeda.nl>
Thu, 1 Apr 2021 05:36:40 +0000 (07:36 +0200)
src/lightswitch.c

index d23997f..6701960 100644 (file)
@@ -47,9 +47,16 @@ void setup_io();
 
 #define SWITCH_IN    2
 
+#define FIRMASK 0x03
+
+#define TEST_CYCLES  (60 * 5)  //  1 minute
+
+unsigned fir_record[TEST_CYCLES];
 
 int main(int argc, char *argv[])
 {
+   unsigned int fir;  // A simple FIR filter
+
    int    old_state, new_state;
    struct timespec interval;
 
@@ -60,16 +67,35 @@ int main(int argc, char *argv[])
    setup_io();
  
    // Initialize the IO pins.
+   //GPIO_PULL = 0;
    INP_GPIO(SWITCH_IN);
 
-   old_state = GET_GPIO(SWITCH_IN);
+   //   Initialize to switched off state
+   fir = 0xffff;
+   old_state = 1;  // Off
 
-   while (1)
+   int cycle = 0;
+   //while (1)
+   while (cycle < TEST_CYCLES)
    {
-      new_state = GET_GPIO(SWITCH_IN);
+      unsigned input = GET_GPIO(SWITCH_IN) == 0 ? 0 : 1;
+      fir <<= 1;
+      fir |= input;
+
+      fprintf(stderr, "FIR =  0x%x\r", fir);
+      if ( (fir & FIRMASK) == 0)
+      {
+         new_state = 0;   // On
+      }
+      if ( (fir & FIRMASK) == FIRMASK)
+      {
+         new_state = 1;   // Off
+      }
+
       if (new_state != old_state)
       {
-         //fprintf(stderr, "Changed state to %d\n", new_state);
+         //fprintf(stderr, "Changed state to %d\r", new_state);
          if (new_state == 0)
          {
             system("lightcontrol -r 100 -g 100 -b 100 -w 100");
@@ -81,9 +107,16 @@ int main(int argc, char *argv[])
 
          old_state = new_state;
       }
+      fir_record[cycle++] = fir;
+
       nanosleep(&interval, NULL);
    }
 
+   for (int i = 0; i < TEST_CYCLES; i++)
+   {
+      //printf("FIR = 0x%08x\n", fir_record[i]);
+   }
+
    return 0;
 }