fix the button action
[beacon-rx-433.git] / main.c
diff --git a/main.c b/main.c
index 1a9b01e..82f12d7 100644 (file)
--- a/main.c
+++ b/main.c
 #endif
 
 /* port B */
-#define BUZ_BIT   0
-#define RADIO_BIT 2
-#define LED_BIT   4
+#define BUZZER_BIT    0
+#define BUZZER_ON()   PORTB |= _BV(BUZZER_BIT)
+#define BUZZER_OFF()  PORTB &= (~_BV(BUZZER_BIT))
+
+#define RADIO_BIT  2
+#define RADIO_READ() (!!(PINB & (_BV(RADIO_BIT))))
+
+#define BUTTON_BIT 3
+#define BUTTON_IS_PRESSED()  (!(PINB & (_BV(BUTTON_BIT))))
+
+#define LED_BIT    4
+#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;
 
@@ -330,30 +345,24 @@ static uint8_t get_input(int i)
 #ifdef HOST_VERSION
        return !!x[i];
 #else
-       return !!(PINB & RADIO_BIT);
+       return RADIO_READ();
 #endif
 }
 
-static void set_output(int16_t i)
+#ifndef HOST_VERSION
+static void beep(void)
 {
-       int8_t detected = 0;
-
-       if (cpt_filter > 250)
-               detected = 1;
+       uint16_t i;
 
-#ifdef HOST_VERSION
-       printf("%d fil_fond=%d fil_harm1=%d fil_other=%d\n",
-              i, fil_fond, fil_harm1, fil_other);
-       printf("%d pow_fond=%d pow_harm1=%d pow_other=%d cpt_filter=%d detected=%d\n",
-              i, pow_fond, pow_harm1, pow_other, cpt_filter, detected);
-#else
-       /* when we receive, output a square signal at 625 hz */
-       if ((detected == 1) && (i & 4))
-               PORTB |= (1 << BUZ_BIT);
-       else
-               PORTB &= ~(1 << BUZ_BIT);
-#endif
+       for (i = 0; i < 1000; i++) {
+               if (i < 250 && (i & 1))
+                       BUZZER_ON();
+               else
+                       BUZZER_OFF();
+               wait_period();
+       }
 }
+#endif
 
 /*
   The CPU runs at 8Mhz.
@@ -364,11 +373,17 @@ static void set_output(int16_t 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__)
-       DDRB |= (1 << LED_BIT) | (1 << BUZ_BIT);
+       DDRB |= (1 << LED_BIT) | (1 << BUZZER_BIT);
+       PORTB |= (1 << BUTTON_BIT); /* pull up */
 #endif
 
        i = 0;
@@ -376,12 +391,42 @@ int main(void)
 
        while (1) {
 
+               /* wait until 200us is elapsed since previous call (5Khz) */
                wait_period();
+               detected = 0;
+
+#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);
@@ -396,10 +441,57 @@ int main(void)
                else if (cpt_filter > 0)
                        cpt_filter--;
 
-               set_output(i);
+               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",
+                      i, fil_fond, fil_harm1, fil_other);
+               printf("%d pow_fond=%d pow_harm1=%d pow_other=%d "
+                      "cpt_filter=%d detected=%d\n",
+                      i, pow_fond, pow_harm1, pow_other, cpt_filter, detected);
+#else
+               if (detected)
+                       LED_ON();
+               else
+                       LED_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