Merge branch 'master' of gitlab:wintermute/hackover2013-badge-firmware
[hackover2013-badge-firmware.git] / badge / ui / event.c
1 #include "event.h"
2
3 #include <core/gpio/gpio.h>
4 #include <core/timer32/timer32.h>
5
6 #ifdef R0KET
7 #include <r0ketports.h>
8 #else
9 #include <badge/pinconfig.h>
10 #endif
11
12 // We depend on input being popped often, so no queue.
13 static badge_event_t volatile event_buffer;
14 static uint8_t volatile event_flag;
15
16 enum {
17 BADGE_EVENT_FLAG_INPUT = 1,
18 BADGE_EVENT_FLAG_TIMER = 2
19 };
20
21 uint8_t badge_input_raw(void) {
22 uint8_t result = 0;
23
24 #ifdef R0KET
25 if (gpioGetValue(RB_BTN3) == 0) { result |= BADGE_EVENT_KEY_DOWN ; }
26 if (gpioGetValue(RB_BTN2) == 0) { result |= BADGE_EVENT_KEY_UP ; }
27 if (gpioGetValue(RB_BTN4) == 0) { result |= BADGE_EVENT_KEY_CENTER; }
28 if (gpioGetValue(RB_BTN0) == 0) { result |= BADGE_EVENT_KEY_RIGHT ; }
29 if (gpioGetValue(RB_BTN1) == 0) { result |= BADGE_EVENT_KEY_LEFT ; }
30 if (gpioGetValue(RB_HB0 ) == 0) { result |= BADGE_EVENT_KEY_BTN_A ; }
31 if (gpioGetValue(RB_HB1 ) == 0) { result |= BADGE_EVENT_KEY_BTN_B ; }
32 #elif defined(HOB_REV2)
33 if (gpioGetValue(HOB_PORT(HOB_BTN_UP ), HOB_PIN(HOB_BTN_UP )) == 0) { result |= BADGE_EVENT_KEY_UP ; }
34 if (gpioGetValue(HOB_PORT(HOB_BTN_DOWN ), HOB_PIN(HOB_BTN_DOWN )) == 0) { result |= BADGE_EVENT_KEY_DOWN ; }
35 if (gpioGetValue(HOB_PORT(HOB_BTN_LEFT ), HOB_PIN(HOB_BTN_LEFT )) == 0) { result |= BADGE_EVENT_KEY_LEFT ; }
36 if (gpioGetValue(HOB_PORT(HOB_BTN_RIGHT ), HOB_PIN(HOB_BTN_RIGHT )) == 0) { result |= BADGE_EVENT_KEY_RIGHT ; }
37 if (gpioGetValue(HOB_PORT(HOB_BTN_A ), HOB_PIN(HOB_BTN_A )) == 0) { result |= BADGE_EVENT_KEY_BTN_A ; }
38 if (gpioGetValue(HOB_PORT(HOB_BTN_B ), HOB_PIN(HOB_BTN_B )) == 0) { result |= BADGE_EVENT_KEY_BTN_B ; }
39 #else
40 if (gpioGetValue(HOB_PORT(HOB_BTN_UP ), HOB_PIN(HOB_BTN_UP )) == 1) { result |= BADGE_EVENT_KEY_UP ; }
41 if (gpioGetValue(HOB_PORT(HOB_BTN_DOWN ), HOB_PIN(HOB_BTN_DOWN )) == 1) { result |= BADGE_EVENT_KEY_DOWN ; }
42 if (gpioGetValue(HOB_PORT(HOB_BTN_CENTER), HOB_PIN(HOB_BTN_CENTER)) == 1) { result |= BADGE_EVENT_KEY_CENTER; }
43 if (gpioGetValue(HOB_PORT(HOB_BTN_LEFT ), HOB_PIN(HOB_BTN_LEFT )) == 1) { result |= BADGE_EVENT_KEY_LEFT ; }
44 if (gpioGetValue(HOB_PORT(HOB_BTN_RIGHT ), HOB_PIN(HOB_BTN_RIGHT )) == 1) { result |= BADGE_EVENT_KEY_RIGHT ; }
45 if (gpioGetValue(HOB_PORT(HOB_BTN_A ), HOB_PIN(HOB_BTN_A )) == 1) { result |= BADGE_EVENT_KEY_BTN_A ; }
46 if (gpioGetValue(HOB_PORT(HOB_BTN_B ), HOB_PIN(HOB_BTN_B )) == 1) { result |= BADGE_EVENT_KEY_BTN_B ; }
47 #endif
48
49 return result;
50 }
51
52 uint8_t badge_event_current_input_state(void) {
53 return badge_event_new_input_state(event_buffer);
54 }
55
56 void badge_event_irq(void) {
57 uint8_t old_state = badge_event_current_input_state();
58 uint8_t new_state = badge_input_raw();
59
60 if(new_state != old_state) {
61 event_buffer = badge_event_new(BADGE_EVENT_USER_INPUT,
62 old_state,
63 new_state);
64 event_flag |= BADGE_EVENT_FLAG_INPUT;
65 }
66
67 event_flag |= BADGE_EVENT_FLAG_TIMER;
68 }
69
70 badge_event_t badge_event_wait(void) {
71 while(!event_flag)
72 ;
73
74 // User input takes precedence.
75 if(event_flag & BADGE_EVENT_FLAG_INPUT) {
76 badge_event_t event = event_buffer;
77 event_flag &= ~BADGE_EVENT_FLAG_INPUT;
78 return event;
79 }
80
81 uint8_t state = badge_event_current_input_state();
82 event_flag &= ~BADGE_EVENT_FLAG_TIMER;
83 return badge_event_new(BADGE_EVENT_GAME_TICK, state, state);
84 }
85
86 void badge_event_start(void) {
87 timer32Init(0, TIMER32_CCLK_10MS / 100 * 133);
88 timer32SetIntHandler(badge_event_irq);
89 timer32Enable(0);
90 }
91
92 void badge_event_stop (void) {
93 timer32Disable(0);
94 }
This page took 0.05717 seconds and 5 git commands to generate.