fix LED and button management
[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_BIT 3
36 #define BUTTON_IS_PRESSED() (!(PINB & _BV(BUTTON_BIT)))
37
38 #define LED_BIT 4
39 #define LED_ON()       PORTB |= _BV(LED_BIT)
40 #define LED_OFF()      PORTB &= (~_BV(LED_BIT))
41
42 /* wait new period: timer unit is us: 250 -> 4 khz */
43 static void wait_period(void)
44 {
45         static uint8_t prev = 0;
46         uint8_t cur, diff;
47
48         do {
49                 cur = TCNT0;
50                 diff = cur - prev;
51         } while (diff < 250);
52         prev = cur;
53 }
54
55 int main(void)
56 {
57         uint8_t button_filter = 0;
58         uint16_t time_period = 0;
59         uint32_t time_second = 0;
60         uint8_t always_on = 1;
61         uint8_t button = 0;
62
63         /* PORTB TX/DATA out*/
64         DDRB |= (1 << TX_ENABLE_BIT) |
65                 (1 << TX_BIT) |
66                 (1 << LED_BIT) |
67                 (1 << BUZZER_BIT);
68         /* pull up */
69         PORTB |= (1 << BUTTON_BIT);
70
71         /* init timer */
72         TCCR0A = 0;
73         TCCR0B = (1 << CS01); /* clk/8 = 1Mhz */
74
75         while (1) {
76                 /* 4 Khz */
77                 wait_period();
78
79                 /* buzzer at 2 Khz after 10mns if not always on */
80                 if (always_on || time_second > 60) {
81                         LED_OFF();
82                         /* every 2 secs */
83                         if (((time_second & 1) == 0) &&
84                             (time_period & 1) &&
85                             (time_period < 500))
86                                 BUZZER_ON();
87                         else
88                                 BUZZER_OFF();
89                 }
90                 else {
91                         LED_ON();
92                         BUZZER_OFF();
93                 }
94
95                 /* radio duty cycle is 500/4000 */
96                 if (time_period < 500) {
97                         TX_ENABLE();
98                         /* at 500 Hz */
99                         if (time_period & 4)
100                                 TX_ON();
101                         else
102                                 TX_OFF();
103                 }
104                 else
105                         TX_DISABLE();
106
107                 /* process next time vars */
108                 time_period++;
109                 if (time_period >= 4000) {
110                         time_period = 0;
111                         time_second ++;
112                 }
113
114                 /* filter button */
115                 button = 0;
116                 if (BUTTON_IS_PRESSED() && button_filter < 10) {
117                         button_filter++;
118                         if (button_filter == 10)
119                                 button = 1;
120                 }
121                 else if (!BUTTON_IS_PRESSED())
122                         button_filter = 0;
123
124                 /* change mode if button is pressed */
125                 if (button) {
126                         time_period = 0;
127                         time_second = 0;
128                         always_on = !always_on;
129                 }
130         }
131
132         return 0;
133 }