move set_output() in main loop
[beacon-rx-433.git] / main.c
diff --git a/main.c b/main.c
index 5dd4f1d..35a86cf 100644 (file)
--- a/main.c
+++ b/main.c
 #endif
 
 /* port B */
 #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))
 
 static uint32_t bitfield;
 
 
 static uint32_t bitfield;
 
@@ -330,33 +340,7 @@ static uint8_t get_input(int i)
 #ifdef HOST_VERSION
        return !!x[i];
 #else
 #ifdef HOST_VERSION
        return !!x[i];
 #else
-       return !!(PINB & (1 << RADIO_BIT));
-#endif
-}
-
-static void set_output(int16_t i)
-{
-       int8_t detected = 0;
-
-       if (cpt_filter > 250)
-               detected = 1;
-
-#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
-       if (detected)
-               PORTB |= (1 << LED_BIT);
-       else
-               PORTB &= ~(1 << LED_BIT);
-
-       /* when we receive, output a square signal at 625 hz */
-       if ((detected == 1) && (i & 4))
-               PORTB |= (1 << BUZ_BIT);
-       else
-               PORTB &= ~(1 << BUZ_BIT);
+       return RADIO_READ();
 #endif
 }
 
 #endif
 }
 
@@ -369,11 +353,12 @@ static void set_output(int16_t i)
  */
 int main(void)
 {
  */
 int main(void)
 {
+       int8_t detected = 0;
        int16_t i;
 
        /* led and buzzer are outputs */
 #if defined(__AVR_ATtiny45__)
        int16_t i;
 
        /* led and buzzer are outputs */
 #if defined(__AVR_ATtiny45__)
-       DDRB |= (1 << LED_BIT) | (1 << BUZ_BIT);
+       DDRB |= (1 << LED_BIT) | (1 << BUZZER_BIT);
 #endif
 
        i = 0;
 #endif
 
        i = 0;
@@ -401,9 +386,30 @@ int main(void)
                else if (cpt_filter > 0)
                        cpt_filter--;
 
                else if (cpt_filter > 0)
                        cpt_filter--;
 
-               set_output(i);
+#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();
+
+               /* output a square signal at 625 hz if beacon is
+                * detected */
+               if ((detected == 1) && (i & 4))
+                       BUZZER_ON();
+               else
+                       BUZZER_OFF();
+
+#endif
 
                i ++;
 
                i ++;
+
 #ifdef HOST_VERSION
                if (i >= sizeof(x))
                        break;
 #ifdef HOST_VERSION
                if (i >= sizeof(x))
                        break;