2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 #define AO_EVENT_QUEUE 64
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)
29 * Whether a sequence of events from the same device should be collapsed
31 #define ao_event_can_collapse(type) ((type) == AO_EVENT_QUADRATURE)
33 struct ao_event ao_event_queue[AO_EVENT_QUEUE];
34 uint8_t ao_event_queue_insert;
35 uint8_t ao_event_queue_remove;
39 ao_event_get(struct ao_event *ev)
42 while (ao_event_queue_empty())
43 ao_sleep(&ao_event_queue);
44 *ev = ao_event_queue[ao_event_queue_remove];
45 ao_event_queue_remove = ao_event_queue_next(ao_event_queue_remove);
49 /* called with interrupts disabled */
51 ao_event_put_isr(uint8_t type, uint8_t unit, uint32_t value)
53 if (!ao_event_queue_full()) {
55 if (ao_event_can_collapse(type) && !ao_event_queue_empty()) {
56 uint8_t prev = ao_event_queue_prev(ao_event_queue_insert);
58 if (ao_event_queue[prev].type == type &&
59 ao_event_queue[prev].unit == unit)
60 ao_event_queue_insert = prev;
62 ao_event_queue[ao_event_queue_insert] = (struct ao_event) {
65 .tick = ao_tick_count,
68 ao_event_queue_insert = ao_event_queue_next(ao_event_queue_insert);
69 ao_wakeup(&ao_event_queue);
74 ao_event_put(uint8_t type, uint8_t unit, uint32_t value)
76 ao_arch_critical(ao_event_put_isr(type, unit, value););