dc8cfa1c34401f5ad4553937f2c05dc4f4ea7331
[oswald.git] / ui / oswald_main.c
1 #include "oswald.h"
2 #include "oswald_watch_faces.h"
3 #include "Fonts.h"
4 #include "LcdDisplay.h"
5
6 #include "oswald_main.h"
7
8 /*
9  * some variable defining our curent state
10  * these are globals in order to avoid having to pass pointers
11  * through function calls thus saving stack space
12  */
13 clock_state OswaldClk;
14 watch_state OswaldState;
15
16 void update_screen(void)
17 {
18         if (OswaldState.user_screendraw_func != NULL)
19                 OswaldState.user_screendraw_func();
20 }
21
22 void oswald_change_to_screen(screen_number screen)
23 {
24         // we spare the update if no change happened
25         if (OswaldState.screen != screen) {
26                 OswaldState.screen = screen;
27                 update_screen();
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_clock_state();
61         update_screen();
62 }
63
64 void draw_accel_screen(void)
65 {
66         lcd_clear_display();
67         SetFont(MetaWatch16);
68         WriteLcdString(2, 2, "X:");
69         WriteLcdString(20, 2, "123");
70         WriteLcdString(2, 18, "Z:");
71         WriteLcdString(20, 18, "123");
72         WriteLcdString(2, 34, "Y:");
73         WriteLcdString(20, 34, "123");
74 }
75
76 void idle_handle_user_buttons(watch_button button)
77 {
78 }
79
80 void oswald_handle_button_press(watch_button button)
81 {
82         switch (button) {
83                 case BUTTON_A:
84                 case BUTTON_B:
85                 case BUTTON_D:
86                 case BUTTON_E:
87                         if (OswaldState.user_button_func != NULL)
88                                 OswaldState.user_button_func(button);
89                         break;
90                 case BUTTON_C:
91                         // next screen
92                         OswaldState.screen++;
93                         if (OswaldState.screen >= LAST_SCREEN) {
94                                 OswaldState.screen = IDLE_SCREEN;
95                                 OswaldState.user_button_func = idle_handle_user_buttons;
96                                 OswaldState.user_screendraw_func = DrawLcdDigitalClock;
97                         } else {
98                                 switch (OswaldState.screen) {
99                                         case SETTING_DATETIME_SCREEN:
100                                                 OswaldState.user_button_func = idle_handle_user_buttons;
101                                                 OswaldState.user_screendraw_func = draw_accel_screen;
102                                                 break;
103                                         default:
104                                                 break;
105                                 };
106                         };
107                         update_screen();
108                         break;
109                 case BUTTON_F:
110                         // backlight on/off
111                         break;
112                 default:
113                         // should never get here
114                         break;
115         };
116 }
117
118 void oswald_init(void)
119 {
120         OswaldState.screen = IDLE_SCREEN;
121         OswaldState.idle_show_seconds = FALSE;
122         OswaldState.user_screendraw_func = DrawLcdDigitalClock;
123         OswaldState.user_button_func = idle_handle_user_buttons;
124 }
125