+
+/*
+ * Stop Watch
+ */
+
+typedef struct {
+ u8t hr;
+ u8t min;
+ u8t sec;
+ u8t csec;
+ u8t lapse_hr;
+ u8t lapse_min;
+ u8t lapse_sec;
+ u8t lapse_csec;
+ boolean running;
+} stopwatch_data_t;
+static stopwatch_data_t stopwatch_screen = { 0, 0, 0, 0, 0, 0, 0, 0, FALSE };
+
+
+static void update_stop_watch_screen(stopwatch_data_t *sdata)
+{
+ char tstr[16];
+ SetFont(MetaWatchMonospaced10);
+
+ snprintf(tstr, 16, "%02d:%02d:%02d.%1d", sdata->hr, sdata->min, sdata->sec, sdata->csec / 10);
+ WriteLcdString(5, 40, tstr);
+ snprintf(tstr, 16, "%02d:%02d:%02d.%02d", sdata->lapse_hr, sdata->lapse_min, sdata->lapse_sec, sdata->lapse_csec);
+ WriteLcdString(5, 60, tstr);
+
+ hal_lcd_update_display();
+}
+
+static void draw_stop_watch_screen(stopwatch_data_t *sdata)
+{
+ oswald_draw_bitmap(36, 0, stopwatch_icon_width, stopwatch_icon_height, stopwatch_icon_bits);
+ oswald_draw_bitmap(81, 6, startstopbutton_icon_width, startstopbutton_icon_height, startstopbutton_icon_bits);
+ oswald_draw_bitmap(81, 38, lapsebutton_icon_width, lapsebutton_icon_height, lapsebutton_icon_bits);
+
+ update_stop_watch_screen(sdata);
+}
+
+static void handle_stop_watch_buttons(watch_button button)
+{
+ switch (button) {
+ case BUTTON_A: // start/stop
+ if (stopwatch_screen.running) {
+ hal_disable_centisecond_timer();
+ stopwatch_screen.running = FALSE;
+ } else {
+ hal_enable_centisecond_timer();
+ stopwatch_screen.running = TRUE;
+ }
+ break;
+ case BUTTON_B: // lapse
+ stopwatch_screen.lapse_hr = stopwatch_screen.hr;
+ stopwatch_screen.lapse_min = stopwatch_screen.min;
+ stopwatch_screen.lapse_sec = stopwatch_screen.sec;
+ stopwatch_screen.lapse_csec = stopwatch_screen.csec;
+ break;
+ case BUTTON_F: // reset
+ stopwatch_screen.hr = 0;
+ stopwatch_screen.min = 0;
+ stopwatch_screen.sec = 0;
+ stopwatch_screen.csec = 0;
+ stopwatch_screen.lapse_hr = 0;
+ stopwatch_screen.lapse_min = 0;
+ stopwatch_screen.lapse_sec = 0;
+ stopwatch_screen.lapse_csec = 0;
+ break;
+ default:
+ break;
+ }
+}
+
+void stop_watch_handle_events(u16t event, void *data)
+{
+ switch (event) {
+ case EVENT_USER_BUTTONS:
+ dbg_out("button event %d\n", *(int *)data);
+ handle_stop_watch_buttons(*(watch_button *)data);
+ update_stop_watch_screen(&stopwatch_screen);
+ break;
+ case EVENT_SCREEN_VISIBLE:
+ hal_lcd_clear_display();
+ draw_stop_watch_screen(&stopwatch_screen);
+ break;
+ case EVENT_SCREEN_DESTROY:
+ hal_disable_centisecond_timer();
+ stopwatch_screen.running = FALSE;
+ break;
+ case EVENT_CS_TIMER:
+ stopwatch_screen.csec++;
+ if (stopwatch_screen.csec > 99) {
+ stopwatch_screen.csec = 0;
+ stopwatch_screen.sec++;
+ };
+ if (stopwatch_screen.sec > 59) {
+ stopwatch_screen.sec = 0;
+ stopwatch_screen.min++;
+ };
+ if (stopwatch_screen.min > 59) {
+ stopwatch_screen.min = 0;
+ stopwatch_screen.hr++;
+ };
+ if (stopwatch_screen.hr > 59) {
+ stopwatch_screen.hr = 0;
+ };
+ if (stopwatch_screen.csec % 10 == 0)
+ update_stop_watch_screen(&stopwatch_screen);
+ break;
+ default:
+ break;
+ };
+}
+
+
+/*
+ * when alarm is fired
+ */
+void draw_alarm_screen(void)
+{
+ hal_lcd_clear_display();
+
+// SetFont(MetaWatch16);
+// WriteLcdString(2, 2, "ALARM !");
+ oswald_draw_bitmap(36, 20, alarm_icon_width, alarm_icon_height, alarm_icon_bits);
+
+
+ hal_lcd_update_display();
+}
+
+void alarm_handle_events(u16t event, void *data)
+{
+ switch (event) {
+ case EVENT_SCREEN_VISIBLE:
+ draw_alarm_screen();
+ hal_enable_halfsecond_timer();
+ hal_vibration_set_state(TRUE);
+ break;
+ case EVENT_SCREEN_DESTROY:
+ hal_disable_halfsecond_timer();
+ hal_lcd_set_backlight(FALSE);
+ hal_vibration_set_state(FALSE);
+ break;
+ case EVENT_USER_BUTTONS:
+ dbg_out("button event %d\n", *(int *)data);
+ // hal_lcd_set_backlight(FALSE);
+ break;
+ case EVENT_HALF_SEC_TIMER:
+ hal_lcd_set_backlight(!hal_lcd_get_backlight());
+ hal_vibration_set_state(!hal_vibration_get_state());
+ dbg_out("timer\n");
+ break;
+ default:
+ break;
+ };
+}
+
+
+/*
+ * Bluetooth screen
+ */
+typedef struct {
+ u8t pos;
+ boolean bt_en;
+ boolean on;
+} bluetooth_data_t;
+static bluetooth_data_t bluetooth_screen = {
+ 0,
+ FALSE,
+ TRUE
+};
+
+void draw_bluetooth_screen(bluetooth_data_t *sdata)
+{
+ char bstr[20];
+ uint8_t *bd_addr;
+
+ hal_lcd_clear_display();
+
+ oswald_draw_bitmap(36, 0, Bluetooth_icon_width, Bluetooth_icon_height, Bluetooth_icon_bits);
+
+ oswald_draw_bitmap(81, 6, upbutton_icon_width, upbutton_icon_height, upbutton_icon_bits);
+ oswald_draw_bitmap(81, 38, downbutton_icon_width, downbutton_icon_height, downbutton_icon_bits);
+
+ SetFont(MetaWatch5);
+ WriteLcdString(2, 30, "Enable:");
+ if ((sdata->pos == 0 && sdata->on) || sdata->pos != 0) {
+ WriteLcdCharacter(45, 30, bluetooth_screen.bt_en ? 'x' : '_');
+ }
+ WriteLcdString(2, 39, "State:");
+ switch (hal_bluetooth_get_state()) {
+ case BLUETOOTH_OFF:
+ WriteLcdString(45, 39, "off");
+ break;
+ case BLUETOOTH_ON:
+ WriteLcdString(45, 39, "on");
+ break;
+ case BLUETOOTH_CONNECTED:
+ WriteLcdString(45, 39, "conn.");
+ break;
+ default:
+ break;
+ };
+ if (hal_bluetooth_get_state() >= BLUETOOTH_ON) {
+ bd_addr = hal_bluetooth_get_local_bdaddr();
+ snprintf(bstr, 20, "%02x:%02x:%02x:%02x:%02x:%02x", bd_addr[5], bd_addr[4], bd_addr[3], bd_addr[2], bd_addr[1], bd_addr[0]);
+ WriteLcdString(2, 48, bstr);
+ } else {
+ }
+ WriteLcdString(2, 57, "Visible:");
+ if ((sdata->pos == 1 && sdata->on) || sdata->pos != 1) {
+ WriteLcdCharacter(45, 57, hal_bluetooth_get_visible() ? 'x' : '_');
+ }
+
+ hal_lcd_update_display();
+}
+
+void bluetooth_handle_updown(u8t pos, s8t incr)
+{
+ switch (pos) {
+ case 0:
+ if (hal_bluetooth_get_state() >= BLUETOOTH_ON) {
+ hal_bluetooth_set_state(BLUETOOTH_OFF);
+ bluetooth_screen.bt_en = FALSE;
+ } else {
+ hal_bluetooth_set_state(BLUETOOTH_ON);
+ bluetooth_screen.bt_en = TRUE;
+ }
+ break;
+ case 1:
+ if (hal_bluetooth_get_state() >= BLUETOOTH_ON && !hal_bluetooth_get_visible()) {
+ hal_bluetooth_set_visible(TRUE);
+ } else {
+ hal_bluetooth_set_visible(FALSE);
+ }
+ break;
+ case 2:
+ break;
+ case 3:
+ break;
+ case 4:
+ break;
+ case 5:
+ break;
+ case 6:
+ break;
+ case 7:
+ break;
+ case 8:
+ break;
+ default:
+ break;
+ };
+}
+
+void handle_bluetooth_buttons(watch_button button, bluetooth_data_t *sdata)
+{
+ switch (button) {
+ case BUTTON_A:
+ bluetooth_handle_updown(sdata->pos, 1);
+ break;
+ case BUTTON_B:
+ bluetooth_handle_updown(sdata->pos, -1);
+ break;
+ case BUTTON_F:
+ sdata->pos++;
+ sdata->pos %= 2;
+ break;
+ default:
+ break;
+ }
+ draw_bluetooth_screen(sdata);
+}
+
+void bluetooth_screen_events(u16t event, void *data)
+{
+ switch (event) {
+ case EVENT_SCREEN_VISIBLE:
+ bluetooth_screen.pos = 0;
+ bluetooth_screen.bt_en = (hal_bluetooth_get_state() > 0);
+ draw_bluetooth_screen(&bluetooth_screen);
+ hal_enable_halfsecond_timer();
+ break;
+ case EVENT_SCREEN_DESTROY:
+ hal_disable_halfsecond_timer();
+ break;
+ case EVENT_USER_BUTTONS:
+ dbg_out("button event %d\n", *(int *)data);
+ handle_bluetooth_buttons(*(watch_button *)data, &bluetooth_screen);
+ break;
+ case EVENT_HALF_SEC_TIMER:
+ if (bluetooth_screen.on)
+ bluetooth_screen.on = FALSE;
+ else
+ bluetooth_screen.on = TRUE;
+ draw_bluetooth_screen(&bluetooth_screen);
+ break;
+ default:
+ break;
+ };
+}
+