fix LED and button management
[beacon-tx-433.git] / main.c
diff --git a/main.c b/main.c
index 7aa50c8..200b67c 100644 (file)
--- a/main.c
+++ b/main.c
 #define BUZZER_ON()   PORTB |= _BV(BUZZER_BIT)
 #define BUZZER_OFF()  PORTB &= (~_BV(BUZZER_BIT))
 
-#define BUTTON_IS_PRESSED() 0
+#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))
 
 /* wait new period: timer unit is us: 250 -> 4 khz */
 static void wait_period(void)
@@ -58,28 +63,39 @@ int main(void)
        /* PORTB TX/DATA out*/
        DDRB |= (1 << TX_ENABLE_BIT) |
                (1 << TX_BIT) |
+               (1 << LED_BIT) |
                (1 << BUZZER_BIT);
+       /* pull up */
+       PORTB |= (1 << BUTTON_BIT);
 
        /* init timer */
        TCCR0A = 0;
        TCCR0B = (1 << CS01); /* clk/8 = 1Mhz */
 
        while (1) {
+               /* 4 Khz */
                wait_period();
 
                /* buzzer at 2 Khz after 10mns if not always on */
-               if (always_on || time_second > 600) {
-                       if ((time_period & 1) && (time_period < 500))
+               if (always_on || time_second > 60) {
+                       LED_OFF();
+                       /* every 2 secs */
+                       if (((time_second & 1) == 0) &&
+                           (time_period & 1) &&
+                           (time_period < 500))
                                BUZZER_ON();
                        else
                                BUZZER_OFF();
                }
-               else
+               else {
+                       LED_ON();
                        BUZZER_OFF();
+               }
 
-               /* radio at 500 Hz */
+               /* radio duty cycle is 500/4000 */
                if (time_period < 500) {
                        TX_ENABLE();
+                       /* at 500 Hz */
                        if (time_period & 4)
                                TX_ON();
                        else
@@ -97,10 +113,11 @@ int main(void)
 
                /* filter button */
                button = 0;
-               if (BUTTON_IS_PRESSED() && button_filter < 10)
+               if (BUTTON_IS_PRESSED() && button_filter < 10) {
                        button_filter++;
-               else if (BUTTON_IS_PRESSED() && button_filter == 10)
-                       button = 1;
+                       if (button_filter == 10)
+                               button = 1;
+               }
                else if (!BUTTON_IS_PRESSED())
                        button_filter = 0;