tx-beacon: full-featured version
[beacon-tx-433.git] / main.c
diff --git a/main.c b/main.c
index 9c2a25f..7aa50c8 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
 /*
- *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
+ *  Copyright 2013 Olivier Matz <zer0@droids-corp.org>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- *  Revision : $Id: main.c,v 1.4.6.3 2007-05-28 12:55:48 zer0 Exp $
- *
  */
 
 #include <aversive.h>
 #include <aversive/wait.h>
 
-#include <stdio.h>
-#include <string.h>
+#define TX_ENABLE_BIT 2
+#define TX_ENABLE()   PORTB |= _BV(TX_ENABLE_BIT)
+#define TX_DISABLE()  PORTB &= (~_BV(TX_ENABLE_BIT))
+
+#define TX_BIT 1
+#define TX_ON()       PORTB |= _BV(TX_BIT)
+#define TX_OFF()      PORTB &= (~_BV(TX_BIT))
 
-#define TX_ENABLE_PORT 2
-#define DATA_PORT 1
-#define BUZZER_PORT 0
+#define BUZZER_BIT 0
+#define BUZZER_ON()   PORTB |= _BV(BUZZER_BIT)
+#define BUZZER_OFF()  PORTB &= (~_BV(BUZZER_BIT))
 
-#define TX_ON()       sbi(PORTB, TX_ENABLE_PORT)
-#define TX_OFF()      cbi(PORTB, TX_ENABLE_PORT)
+#define BUTTON_IS_PRESSED() 0
 
-#define DATA_ON()       sbi(PORTB, DATA_PORT)
-#define DATA_OFF()      cbi(PORTB, DATA_PORT)
+/* wait new period: timer unit is us: 250 -> 4 khz */
+static void wait_period(void)
+{
+       static uint8_t prev = 0;
+       uint8_t cur, diff;
+
+       do {
+               cur = TCNT0;
+               diff = cur - prev;
+       } while (diff < 250);
+       prev = cur;
+}
 
 int main(void)
 {
-       int i;
+       uint8_t button_filter = 0;
+       uint16_t time_period = 0;
+       uint32_t time_second = 0;
+       uint8_t always_on = 1;
+       uint8_t button = 0;
 
        /* PORTB TX/DATA out*/
-       DDRB |= (1 << TX_ENABLE_PORT) |
-               (1 << DATA_PORT) |
-               (1 << BUZZER_PORT);
+       DDRB |= (1 << TX_ENABLE_BIT) |
+               (1 << TX_BIT) |
+               (1 << BUZZER_BIT);
 
+       /* init timer */
+       TCCR0A = 0;
+       TCCR0B = (1 << CS01); /* clk/8 = 1Mhz */
 
        while (1) {
-               PORTB &= ~(1 << TX_ENABLE_PORT);
-               wait_ms(500);
-               PORTB |= (1 << TX_ENABLE_PORT);
-               for (i=0;i<100; i++) {
-                       PORTB |= (1 << DATA_PORT);
-                       PORTB |= (1 << BUZZER_PORT);
-                       _delay_us(1000);
-                       PORTB &= ~(1 << DATA_PORT);
-                       PORTB &= ~(1 << BUZZER_PORT);
-                       _delay_us(1000);
+               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))
+                               BUZZER_ON();
+                       else
+                               BUZZER_OFF();
+               }
+               else
+                       BUZZER_OFF();
+
+               /* radio at 500 Hz */
+               if (time_period < 500) {
+                       TX_ENABLE();
+                       if (time_period & 4)
+                               TX_ON();
+                       else
+                               TX_OFF();
+               }
+               else
+                       TX_DISABLE();
+
+               /* process next time vars */
+               time_period++;
+               if (time_period >= 4000) {
+                       time_period = 0;
+                       time_second ++;
+               }
+
+               /* filter button */
+               button = 0;
+               if (BUTTON_IS_PRESSED() && button_filter < 10)
+                       button_filter++;
+               else if (BUTTON_IS_PRESSED() && button_filter == 10)
+                       button = 1;
+               else if (!BUTTON_IS_PRESSED())
+                       button_filter = 0;
+
+               /* change mode if button is pressed */
+               if (button) {
+                       time_period = 0;
+                       time_second = 0;
+                       always_on = !always_on;
                }
        }