3bdb5d6e66d33dc27d568dd9790e4bf9b4dca670
[beacon-tx-433.git] / main.c
1 /*
2  *  Copyright 2013 Olivier Matz <zer0@droids-corp.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19
20 #include <aversive.h>
21 #include <aversive/wait.h>
22
23 #define TX_ENABLE_BIT 2
24 #define TX_ENABLE()   PORTB |= _BV(TX_ENABLE_BIT)
25 #define TX_DISABLE()  PORTB &= (~_BV(TX_ENABLE_BIT))
26
27 #define TX_BIT 1
28 #define TX_ON()       PORTB |= _BV(TX_BIT)
29 #define TX_OFF()      PORTB &= (~_BV(TX_BIT))
30
31 #define BUZZER_BIT 0
32 #define BUZZER_ON()   PORTB |= _BV(BUZZER_BIT)
33 #define BUZZER_OFF()  PORTB &= (~_BV(BUZZER_BIT))
34
35 #define BUTTON_IS_PRESSED() 0
36
37 /* wait new period: timer unit is us: 250 -> 4 khz */
38 static void wait_period(void)
39 {
40         static uint8_t prev = 0;
41         uint8_t cur, diff;
42
43         do {
44                 cur = TCNT0;
45                 diff = cur - prev;
46         } while (diff < 250);
47         prev = cur;
48 }
49
50 int main(void)
51 {
52         uint8_t button_filter = 0;
53         uint16_t time_period = 0;
54         uint32_t time_second = 0;
55         uint8_t always_on = 1;
56         uint8_t button = 0;
57
58         /* PORTB TX/DATA out*/
59         DDRB |= (1 << TX_ENABLE_BIT) |
60                 (1 << TX_BIT) |
61                 (1 << BUZZER_BIT);
62
63         /* init timer */
64         TCCR0A = 0;
65         TCCR0B = (1 << CS01); /* clk/8 = 1Mhz */
66
67         while (1) {
68                 /* 4 Khz */
69                 wait_period();
70
71                 /* buzzer at 2 Khz after 10mns if not always on */
72                 if (always_on || time_second > 600) {
73                         if ((time_period & 1) && (time_period < 500))
74                                 BUZZER_ON();
75                         else
76                                 BUZZER_OFF();
77                 }
78                 else
79                         BUZZER_OFF();
80
81                 /* radio duty cycle is 500/4000 */
82                 if (time_period < 500) {
83                         TX_ENABLE();
84                         /* at 500 Hz */
85                         if (time_period & 4)
86                                 TX_ON();
87                         else
88                                 TX_OFF();
89                 }
90                 else
91                         TX_DISABLE();
92
93                 /* process next time vars */
94                 time_period++;
95                 if (time_period >= 4000) {
96                         time_period = 0;
97                         time_second ++;
98                 }
99
100                 /* filter button */
101                 button = 0;
102                 if (BUTTON_IS_PRESSED() && button_filter < 10) {
103                         button_filter++;
104                         if (button_filter == 10)
105                                 button = 1;
106                 }
107                 else if (!BUTTON_IS_PRESSED())
108                         button_filter = 0;
109
110                 /* change mode if button is pressed */
111                 if (button) {
112                         time_period = 0;
113                         time_second = 0;
114                         always_on = !always_on;
115                 }
116         }
117
118         return 0;
119 }