altos/telelcotwo: Add idle timeout
[fw/altos] / src / drivers / ao_event.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_event.h>
20
21 #define AO_EVENT_QUEUE  64
22
23 #define ao_event_queue_next(n)  (((n) + 1) & (AO_EVENT_QUEUE - 1))
24 #define ao_event_queue_prev(n)  (((n) - 1) & (AO_EVENT_QUEUE - 1))
25 #define ao_event_queue_empty()  (ao_event_queue_insert == ao_event_queue_remove)
26 #define ao_event_queue_full()   (ao_event_queue_next(ao_event_queue_insert) == ao_event_queue_remove)
27
28 struct ao_event ao_event_queue[AO_EVENT_QUEUE];
29 uint8_t         ao_event_queue_insert;
30 uint8_t         ao_event_queue_remove;
31
32
33 void
34 ao_event_get(struct ao_event *ev)
35 {
36         ao_arch_critical(
37                 while (ao_event_queue_empty())
38                         ao_sleep(&ao_event_queue);
39                 *ev = ao_event_queue[ao_event_queue_remove];
40                 ao_event_queue_remove = ao_event_queue_next(ao_event_queue_remove);
41                 );
42 }
43
44 uint8_t
45 ao_event_get_for(struct ao_event *ev, uint16_t timeout)
46 {
47         uint8_t empty = 1;
48         ao_arch_critical(
49                 while ((empty = ao_event_queue_empty()))
50                         if (ao_sleep_for(&ao_event_queue, timeout))
51                                 break;
52                 if (!empty) {
53                         *ev = ao_event_queue[ao_event_queue_remove];
54                         ao_event_queue_remove = ao_event_queue_next(ao_event_queue_remove);
55                 }
56                 );
57         return empty;
58 }
59
60 /* called with interrupts disabled */
61 void
62 ao_event_put_isr(uint8_t type, uint8_t unit, int32_t value)
63 {
64         if (!ao_event_queue_full()) {
65                 ao_event_queue[ao_event_queue_insert] = (struct ao_event) {
66                         .type = type,
67                         .unit = unit,
68                         .tick = ao_tick_count,
69                         .value = value
70                 };
71                 ao_event_queue_insert = ao_event_queue_next(ao_event_queue_insert);
72                 ao_wakeup(&ao_event_queue);
73         }
74 }
75
76 void
77 ao_event_put(uint8_t type, uint8_t unit, int32_t value)
78 {
79         ao_arch_critical(ao_event_put_isr(type, unit, value););
80 }