]> git.karo-electronics.de Git - oswald.git/blob - ui/oswald_main.c
Lot's of stuff...
[oswald.git] / ui / oswald_main.c
1 #include "oswald.h"
2 #include "oswald_watch_faces.h"
3 #include "oswald_screens.h"
4 #include "oswald_hal.h"
5
6 #include "embedvm.h"
7
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 alarm_clk OswaldAlarm;
17 watch_state OswaldState;
18 watch_screen OswaldScreens[SCREENS_END];
19 power_state OswaldPowerState;
20 u8t backlight_safety_off = 0;
21 char MainMessage[148];
22
23
24 void oswald_change_to_screen(screen_number screen_id)
25 {
26         // we spare the update if no change happened
27         if (OswaldState.screen_id != screen_id) {
28                 OswaldState.screen_id = screen_id;
29                 if ((OswaldState.screen->event_func != NULL) &&
30                     (OswaldState.screen->event_mask & EVENT_SCREEN_VISIBLE))
31                                 OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
32         }
33 }
34
35 void oswald_set_time(u8t hour, u8t minute, u8t second, boolean clk24hr)
36 {
37         OswaldClk.hour = hour;
38         OswaldClk.minute = minute;
39         OswaldClk.second = second;
40         OswaldClk.clk24hr = clk24hr;
41 }
42
43 void oswald_set_date(u8t day, u8t month, u16t year, boolean day_first)
44 {
45         OswaldClk.day = day;
46         OswaldClk.month = month;
47         OswaldClk.year = year;
48         OswaldClk.day_first = day_first;
49 }
50
51 static void update_clock_state (void)
52 {
53         hal_get_rtc(&OswaldClk);
54
55         /* check for pending alarm once per minute */
56         if (OswaldClk.second == 0) {
57                 if (OswaldClk.hour == OswaldAlarm.hour &&
58                         OswaldClk.minute == OswaldAlarm.minute &&
59                         ((1 << OswaldClk.wday) & OswaldAlarm.wday)) {
60                         OswaldState.screen->event_func(EVENT_SCREEN_DESTROY, NULL);
61                         OswaldState.screen_id = ALARM_SCREEN;
62                         OswaldState.screen = &OswaldScreens[OswaldState.screen_id];
63                         OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
64                 }
65         }
66 }
67
68 void oswald_one_second_tick(void)
69 {
70         /* update clock - should use RTC if available */
71         update_clock_state();
72
73         hal_get_power_state(&OswaldPowerState);
74         if (backlight_safety_off) {
75                 backlight_safety_off--;
76                 if (!backlight_safety_off)
77                         hal_lcd_set_backlight(FALSE);
78         }
79
80         /* wake-up screen if interested in the one-second-event */
81         if (OswaldState.screen->event_func != NULL &&
82             (OswaldState.screen->event_mask & EVENT_ONE_SEC_TIMER))
83                 OswaldState.screen->event_func(EVENT_ONE_SEC_TIMER, NULL);
84 }
85
86 void oswald_centisecond_tick(void)
87 {
88         if (OswaldState.screen->event_func != NULL &&
89             (OswaldState.screen->event_mask & EVENT_CS_TIMER))
90                 OswaldState.screen->event_func(EVENT_CS_TIMER, NULL);
91 }
92
93 void oswald_halfsecond_tick(void)
94 {
95         if (OswaldState.screen->event_func != NULL &&
96             (OswaldState.screen->event_mask & EVENT_HALF_SEC_TIMER))
97                 OswaldState.screen->event_func(EVENT_HALF_SEC_TIMER, NULL);
98 }
99
100 void oswald_handle_button_press(watch_button button)
101 {
102         switch (button) {
103                 case BUTTON_D:
104                         // backlight on/off
105                         if (hal_lcd_get_backlight()) {
106                                 hal_lcd_set_backlight(FALSE);
107                                 backlight_safety_off = 0;
108                         } else {
109                                 hal_lcd_set_backlight(TRUE);
110                                 backlight_safety_off = 2;
111                         }
112                         break;
113                 case BUTTON_A:
114                 case BUTTON_B:
115                 case BUTTON_E:
116                 case BUTTON_F:
117                         if (OswaldState.screen->event_func != NULL &&
118                             (OswaldState.screen->event_mask & EVENT_USER_BUTTONS))
119                                 OswaldState.screen->event_func(EVENT_USER_BUTTONS, &button);
120                         break;
121                 case BUTTON_C:
122                         OswaldState.screen->event_func(EVENT_SCREEN_DESTROY, NULL);
123                         // next screen
124                         OswaldState.screen_id++;
125                         if (OswaldState.screen_id >= LAST_SCREEN) {
126                                 OswaldState.screen_id = IDLE_SCREEN;
127                         };
128                         OswaldState.screen = &OswaldScreens[OswaldState.screen_id];
129                         OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
130                         break;
131                 default:
132                         // should never get here
133                         break;
134         };
135 }
136
137 void oswald_handle_accel_event(u8t x, u8t y, u8t z)
138 {
139         accel_data_t accel_data;
140
141         accel_data.x = x;
142         accel_data.y = y;
143         accel_data.z = z;
144
145         if (OswaldState.screen->event_func != NULL &&
146             (OswaldState.screen->event_mask & EVENT_ACCEL_UPDATE))
147                 OswaldState.screen->event_func(EVENT_ACCEL_UPDATE, &accel_data);
148 }
149
150 void oswald_handle_ambientlight_event(u8t light_level)
151 {
152         if (OswaldState.screen->event_func != NULL &&
153             (OswaldState.screen->event_mask & EVENT_AMBIENTLIGHT_UPDATE))
154                 OswaldState.screen->event_func(EVENT_AMBIENTLIGHT_UPDATE, &light_level);
155 }
156
157 void oswald_handle_comm_input(uint16_t mlen, const void *mdat)
158 {
159         char *icmd = (char *) mdat;
160
161         if (icmd[0] == '$') {
162                 if (strncmp(icmd, "$GRT", 4) == 0) { // get current RTC
163                         char rtime[16];
164                         snprintf(rtime, 16, "#RTC%02d%02d%02d\n", OswaldClk.hour, OswaldClk.minute, OswaldClk.second);
165                         hal_bluetooth_send_data(rtime, strlen(rtime));
166                 } else if (strncmp(icmd, "$SRT", 4) == 0) { // set current RTC
167                 } else if (strncmp(icmd, "$MSG", 4) == 0) { // message on main screen
168                         char *msg = (icmd+4);
169                         mlen -= 4;
170                         memset(MainMessage, 0, 148);
171                         strncpy(MainMessage, msg, (mlen > 147) ? 147 : mlen);
172                 } else if (strncmp(icmd, "$MCL", 4) == 0) { // clear message
173                         memset(MainMessage, 0, 148);
174                 } else if (strncmp(icmd, "$BAT", 4) == 0) { // clear message
175                         char rtime[16];
176                         snprintf(rtime, 16, "#BAT%d,%d\n", OswaldPowerState.charge_state, OswaldPowerState.percent);
177                         hal_bluetooth_send_data(rtime, strlen(rtime));
178                 }
179         }
180 }
181
182 void oswald_init(void)
183 {
184         OswaldScreens[IDLE_SCREEN].event_mask = EVENT_USER_BUTTONS | EVENT_ONE_SEC_TIMER;
185         OswaldScreens[IDLE_SCREEN].event_func = idle_handle_events;
186
187         OswaldScreens[ACCEL_DISPLAY_SCREEN].event_mask = EVENT_USER_BUTTONS | EVENT_ACCEL_UPDATE;
188         OswaldScreens[ACCEL_DISPLAY_SCREEN].event_func = accel_handle_events;
189
190         OswaldScreens[DATETIME_SETTING_SCREEN].event_mask = EVENT_USER_BUTTONS | EVENT_HALF_SEC_TIMER;
191         OswaldScreens[DATETIME_SETTING_SCREEN].event_func = datetime_setup_events;
192
193         OswaldScreens[ALARM_SETUP_SCREEN].event_mask = EVENT_USER_BUTTONS | EVENT_HALF_SEC_TIMER;
194         OswaldScreens[ALARM_SETUP_SCREEN].event_func = alarm_setup_events;
195
196         OswaldScreens[MENU_TEST_SCREEN].event_mask = EVENT_USER_BUTTONS;
197         OswaldScreens[MENU_TEST_SCREEN].event_func = test_menu_handle_events;
198
199         OswaldScreens[STOP_WATCH_SCREEN].event_mask = EVENT_USER_BUTTONS | EVENT_CS_TIMER;
200         OswaldScreens[STOP_WATCH_SCREEN].event_func = stop_watch_handle_events;
201
202         OswaldScreens[BLUETOOTH_SCREEN].event_mask = EVENT_USER_BUTTONS | EVENT_HALF_SEC_TIMER;
203         OswaldScreens[BLUETOOTH_SCREEN].event_func = bluetooth_screen_events;
204
205         OswaldScreens[ALARM_SCREEN].event_mask = EVENT_USER_BUTTONS | EVENT_HALF_SEC_TIMER;
206         OswaldScreens[ALARM_SCREEN].event_func = alarm_handle_events;
207
208         OswaldState.screen_id = IDLE_SCREEN;
209         OswaldState.screen = &OswaldScreens[OswaldState.screen_id];
210
211         if (OswaldState.screen->event_func != NULL)
212                 OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
213
214         OswaldAlarm.hour = 12;
215         OswaldAlarm.minute = 0;
216         OswaldAlarm.wday = 0x00;
217 }
218