Redesign to an event based handling
[oswald.git] / ui / oswald_main.c
1 #include "oswald.h"
2 #include "oswald_watch_faces.h"
3 #include "oswald_screens.h"
4 #if 0
5 #include "Fonts.h"
6 #include "LcdDisplay.h"
7 #endif
8 #include "oswald_main.h"
9
10 /*
11  * some variable defining our curent state
12  * these are globals in order to avoid having to pass pointers
13  * through function calls thus saving stack space
14  */
15 clock_state OswaldClk;
16 watch_state OswaldState;
17 watch_screen OswaldScreens[LAST_SCREEN];
18
19
20 void oswald_change_to_screen(screen_number screen_id)
21 {
22         // we spare the update if no change happened
23         if (OswaldState.screen_id != screen_id) {
24                 OswaldState.screen_id = screen_id;
25                 if ((OswaldState.screen->event_func != NULL) &&
26                     (OswaldState.screen->event_mask & EVENT_SCREEN_VISIBLE))
27                                 OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
28         }
29 }
30
31 void oswald_set_time(u8t hour, u8t minute, u8t second)
32 {
33         OswaldClk.hour = hour;
34         OswaldClk.minute = minute;
35         OswaldClk.second = second;
36 }
37
38 static void update_clock_state (void)
39 {
40         OswaldClk.second += 1;
41         if (OswaldClk.second > 59) {
42                 OswaldClk.second = 0;
43                 OswaldClk.minute += 1;
44         } else
45                 return;
46         if (OswaldClk.minute > 59) {
47                 OswaldClk.minute = 0;
48                 OswaldClk.hour += 1;
49         } else
50                 return;
51         if (OswaldClk.hour > 23) {
52                 OswaldClk.hour = 0;
53                 // day++
54         } else
55                 return;
56 }
57
58 void oswald_one_second_tick(void)
59 {
60         /* update our 'RTC' */
61         update_clock_state();
62
63         /* wake-up screen if interested in the one-second-event */
64         if (OswaldState.screen->event_func != NULL &&
65             (OswaldState.screen->event_mask & EVENT_ONE_SEC_TIMER))
66                 OswaldState.screen->event_func(EVENT_ONE_SEC_TIMER, NULL);
67         // oswald_update_screen();
68 }
69
70 void oswald_handle_button_press(watch_button button)
71 {
72         switch (button) {
73                 case BUTTON_A:
74                 case BUTTON_B:
75                 case BUTTON_D:
76                 case BUTTON_E:
77                         if (OswaldState.screen->event_func != NULL &&
78                             (OswaldState.screen->event_mask & EVENT_USER_BUTTONS))
79                                 OswaldState.screen->event_func(EVENT_USER_BUTTONS, &button);
80                         break;
81                 case BUTTON_C:
82                         // next screen
83                         OswaldState.screen_id++;
84                         if (OswaldState.screen_id >= LAST_SCREEN) {
85                                 OswaldState.screen_id = IDLE_SCREEN;
86                         };
87                         OswaldState.screen = &OswaldScreens[OswaldState.screen_id];
88                         //oswald_update_screen();
89                         OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
90                         break;
91                 case BUTTON_F:
92                         // backlight on/off
93                         break;
94                 default:
95                         // should never get here
96                         break;
97         };
98 }
99
100 void oswald_init(void)
101 {
102         OswaldScreens[IDLE_SCREEN].event_mask = EVENT_ONE_SEC_TIMER;
103         OswaldScreens[IDLE_SCREEN].event_func = idle_handle_events;
104
105         OswaldScreens[ACCEL_DISPLAY_SCREEN].event_mask = EVENT_USER_BUTTONS;
106         OswaldScreens[ACCEL_DISPLAY_SCREEN].event_func = accel_handle_events;
107         
108
109         OswaldScreens[DATETIME_SETTING_SCREEN].event_mask = EVENT_USER_BUTTONS | EVENT_ONE_SEC_TIMER;
110         OswaldScreens[DATETIME_SETTING_SCREEN].event_func = datetime_setup_events;
111
112         OswaldScreens[MENU_TEST_SCREEN].event_mask = EVENT_USER_BUTTONS;
113         OswaldScreens[MENU_TEST_SCREEN].event_func = test_menu_handle_events;
114
115         OswaldState.screen_id = IDLE_SCREEN;
116         OswaldState.screen = &OswaldScreens[OswaldState.screen_id];
117         OswaldState.idle_show_seconds = FALSE;
118
119         if (OswaldState.screen->event_func != NULL)
120                 OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
121 }
122