add several operating modes (filter / no_filter)
[beacon-rx-433.git] / main.c
diff --git a/main.c b/main.c
index 35a86cf..e176a1f 100644 (file)
--- a/main.c
+++ b/main.c
 #define LED_ON()   PORTB |= _BV(LED_BIT)
 #define LED_OFF()  PORTB &= (~_BV(LED_BIT))
 
+#define MODE_NO_FILTER     0
+#define MODE_FILTER_THRES1 1
+#define MODE_FILTER_THRES2 2
+#define MODE_MAX           3
+
 static uint32_t bitfield;
 
 #ifdef HOST_VERSION
@@ -344,6 +349,20 @@ static uint8_t get_input(int i)
 #endif
 }
 
+#ifndef HOST_VERSION
+static void beep(void)
+{
+       uint16_t i;
+
+       for (i = 0; i < 1000; i++) {
+               if (i < 250 && (i & 1))
+                       BUZZER_ON();
+               else
+                       BUZZER_OFF();
+       }
+}
+#endif
+
 /*
   The CPU runs at 8Mhz.
   Fe = 5Khz
@@ -354,7 +373,11 @@ static uint8_t get_input(int i)
 int main(void)
 {
        int8_t detected = 0;
+       uint8_t mode = MODE_FILTER_THRES2;
        int16_t i;
+#ifndef HOST_VERSION
+       uint8_t button = 0, button_filter = 0;
+#endif
 
        /* led and buzzer are outputs */
 #if defined(__AVR_ATtiny45__)
@@ -366,12 +389,41 @@ int main(void)
 
        while (1) {
 
+               /* wait until 200us is elapsed since previous call (5Khz) */
                wait_period();
 
+#ifndef HOST_VERSION
+               /* filter button */
+               button = 0;
+               if (BUTTON_IS_PRESSED() && button_filter < 10) {
+                       button_filter++;
+                       if (button_filter == 10)
+                               button = 1;
+               }
+               else if (!BUTTON_IS_PRESSED())
+                       button_filter = 0;
+
+               /* change mode if button is pressed */
+               if (button) {
+                       uint8_t j;
+
+                       mode ++;
+                       if (mode >= MODE_MAX)
+                               mode = 0;
+
+                       for (j = 0; j <= mode; j++)
+                               beep();
+
+                       /* reset input history */
+                       bitfield = 0;
+               }
+#endif
+
                /* push one bit in bitfield */
                bitfield <<= 1UL;
                bitfield |= (uint32_t)get_input(i);
 
+               /* filter the input */
                apply_filters();
                mean_pow(&pow_fond, fil_fond);
                mean_pow(&pow_harm1, fil_harm1);
@@ -386,6 +438,22 @@ int main(void)
                else if (cpt_filter > 0)
                        cpt_filter--;
 
+               switch (mode) {
+                       case MODE_NO_FILTER:
+                       case MODE_FILTER_THRES1:
+                               /* low threshold, some risk of false positive */
+                               if (cpt_filter > 100)
+                                       detected = 1;
+                               break;
+                       case MODE_FILTER_THRES2:
+                               /* high threshold */
+                               if (cpt_filter > 250)
+                                       detected = 1;
+                               break;
+                       default:
+                               break;
+               }
+
 #ifdef HOST_VERSION
                /* display values */
                printf("%d fil_fond=%d fil_harm1=%d fil_other=%d\n",
@@ -399,18 +467,28 @@ int main(void)
                else
                        LED_OFF();
 
-               /* output a square signal at 625 hz if beacon is
-                * detected */
-               if ((detected == 1) && (i & 4))
-                       BUZZER_ON();
-               else
-                       BUZZER_OFF();
-
+               /* if filtering is disabled, just copy the radio input
+                * on the buzzer */
+               if (mode == MODE_NO_FILTER) {
+                       if (RADIO_READ())
+                               BUZZER_ON();
+                       else
+                               BUZZER_OFF();
+               }
+               /* with filtering enabled, output a square signal at
+                * 625 hz if beacon is detected */
+               else {
+                       if ((detected == 1) && (i & 4))
+                               BUZZER_ON();
+                       else
+                               BUZZER_OFF();
+               }
 #endif
 
                i ++;
 
 #ifdef HOST_VERSION
+               /* exit when all samples are processed */
                if (i >= sizeof(x))
                        break;
 #endif