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