Redesign to an event based handling
authorNils Faerber <nils.faerber@kernelconcepts.de>
Tue, 7 Aug 2012 22:09:29 +0000 (00:09 +0200)
committerNils Faerber <nils.faerber@kernelconcepts.de>
Tue, 7 Aug 2012 22:09:29 +0000 (00:09 +0200)
ui/Makefile.am
ui/oswald-ui.c
ui/oswald.h
ui/oswald_main.c
ui/oswald_main.h
ui/oswald_screens.c [new file with mode: 0644]
ui/oswald_screens.h [new file with mode: 0644]

index df3a186..d71ba35 100644 (file)
@@ -2,7 +2,7 @@ ACLOCAL_AMFLAGS = -I m4
 
 bin_PROGRAMS = oswald-gui
 
 
 bin_PROGRAMS = oswald-gui
 
-oswald_gui_SOURCES = oswald-ui.c LcdDisplay.c Fonts.c oswald_main.c oswald_watch_faces.c oswald_strings.c
+oswald_gui_SOURCES = oswald-ui.c LcdDisplay.c Fonts.c oswald_main.c oswald_watch_faces.c oswald_strings.c oswald_screens.c
 oswald_gui_CFLAGS = -g $(GTK_CFLAGS)
 oswald_gui_LDADD = $(GTK_LIBS)
 
 oswald_gui_CFLAGS = -g $(GTK_CFLAGS)
 oswald_gui_LDADD = $(GTK_LIBS)
 
index 27e8304..dbff4ef 100644 (file)
@@ -91,7 +91,7 @@ void button_A_clicked (GtkButton *button, gpointer user_data)
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
-       g_print("Button-A clicked\n");
+       // g_print("Button-A clicked\n");
        oswald_handle_button_press(BUTTON_A);
 }
 
        oswald_handle_button_press(BUTTON_A);
 }
 
@@ -99,7 +99,7 @@ void button_B_clicked (GtkButton *button, gpointer user_data)
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
-       g_print("Button-B clicked\n");
+       // g_print("Button-B clicked\n");
        oswald_handle_button_press(BUTTON_B);
 }
 
        oswald_handle_button_press(BUTTON_B);
 }
 
@@ -107,7 +107,7 @@ void button_C_clicked (GtkButton *button, gpointer user_data)
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
-       g_print("Button-C clicked\n");
+       // g_print("Button-C clicked\n");
        oswald_handle_button_press(BUTTON_C);
 }
 
        oswald_handle_button_press(BUTTON_C);
 }
 
@@ -115,7 +115,7 @@ void button_D_clicked (GtkButton *button, gpointer user_data)
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
-       g_print("Button-D clicked\n");
+       // g_print("Button-D clicked\n");
        oswald_handle_button_press(BUTTON_D);
 }
 
        oswald_handle_button_press(BUTTON_D);
 }
 
@@ -123,7 +123,7 @@ void button_E_clicked (GtkButton *button, gpointer user_data)
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
-       g_print("Button-E clicked\n");
+       // g_print("Button-E clicked\n");
        oswald_handle_button_press(BUTTON_E);
 }
 
        oswald_handle_button_press(BUTTON_E);
 }
 
@@ -131,7 +131,7 @@ void button_F_clicked (GtkButton *button, gpointer user_data)
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
 {
        oswald_ui *ui = (oswald_ui *)user_data;
 
-       g_print("Button-F clicked\n");
+       // g_print("Button-F clicked\n");
        oswald_handle_button_press(BUTTON_F);
 }
 
        oswald_handle_button_press(BUTTON_F);
 }
 
@@ -180,7 +180,7 @@ static void create_mainwin(oswald_ui *ui)
 
        btn = gtk_button_new_with_label(" E ");
        gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
 
        btn = gtk_button_new_with_label(" E ");
        gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
-       g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(button_D_clicked), ui);
+       g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(button_E_clicked), ui);
 
        btn = gtk_button_new_with_label(" F ");
        gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
 
        btn = gtk_button_new_with_label(" F ");
        gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
@@ -271,9 +271,10 @@ int main(int argc , char ** argv)
        gtk_init (&argc, &argv);
 
        create_mainwin(&ui);
        gtk_init (&argc, &argv);
 
        create_mainwin(&ui);
+       gtk_widget_realize(ui.mainwin);
 
 
-       oswald_init();
        oswald_set_time(mtime.tm_hour, mtime.tm_min, mtime.tm_sec);
        oswald_set_time(mtime.tm_hour, mtime.tm_min, mtime.tm_sec);
+       oswald_init();
 
        g_timeout_add_seconds(1, one_second_tmo_handler, &ui);
        // g_idle_add(app_idle_handler, &ui);
 
        g_timeout_add_seconds(1, one_second_tmo_handler, &ui);
        // g_idle_add(app_idle_handler, &ui);
index ed7f54a..c49acec 100644 (file)
@@ -1,6 +1,13 @@
 #ifndef _OSWALD_H
 #define _OSWALD_H
 
 #ifndef _OSWALD_H
 #define _OSWALD_H
 
+#define DEBUG 1
+#ifdef DEBUG
+#define dbg_out( args... ) fprintf(stderr, args)
+#else
+#define dbg_out( args... ) do {} while (0)
+#endif
+
 typedef signed char s8t;
 typedef unsigned char u8t;
 typedef signed short s16t;
 typedef signed char s8t;
 typedef unsigned char u8t;
 typedef signed short s16t;
@@ -29,7 +36,9 @@ typedef struct {
 
 typedef enum {
        IDLE_SCREEN = 0,
 
 typedef enum {
        IDLE_SCREEN = 0,
-       SETTING_DATETIME_SCREEN,
+       ACCEL_DISPLAY_SCREEN,
+       DATETIME_SETTING_SCREEN,
+       MENU_TEST_SCREEN,
 //     SCREEN2_SCREEN,
 //     SCREEN3_SCREEN,
 //     APPLICATION_SCREEN,
 //     SCREEN2_SCREEN,
 //     SCREEN3_SCREEN,
 //     APPLICATION_SCREEN,
@@ -45,16 +54,31 @@ typedef enum {
        BUTTON_F,
 } watch_button;
 
        BUTTON_F,
 } watch_button;
 
+#define EVENT_SCREEN_VISIBLE           (1<<0)          // screen just became visible
+#define EVENT_SCREEN_DESTROY           (1<<1)          // screen is destroyed
+#define EVENT_ONE_SEC_TIMER            (1<<2)
+#define EVENT_MS_TIMER                 (1<<3)
+#define EVENT_USER_BUTTONS             (1<<4)
+#define EVENT_ACCEL_UPDATE             (1<<5)
+#define EVENT_AMBIENTLIGHT_UPDATE      (1<<6)
+#define EVENT_POWER_CHANGE             (1<<7)
+#define EVENT_COMMS                    (1<<8)
+
 typedef struct {
 typedef struct {
-       void (*button_func)(watch_button button);
+       u16t event_mask;                                // the event the screen wants to receive
+       void (*event_func)(u16t event, void *data);     // callback for events
+//     void (*button_func)(watch_button button);       // handles button presses
+//     void (*screendraw_func)(void);                  // callback for screen update
+//     void *user_data;
 } watch_screen;
 
 typedef struct {
 } watch_screen;
 
 typedef struct {
-       screen_number   screen;
+       screen_number   screen_id;
        // void (*draw_watchface_func)(boolean show_seconds);
        boolean idle_show_seconds;
        // void (*draw_watchface_func)(boolean show_seconds);
        boolean idle_show_seconds;
-       void (*user_button_func)(watch_button button);
-       void (*user_screendraw_func)(void);
+       watch_screen *screen;           // the current screen
+       // void (*user_button_func)(watch_button button);
+       // void (*user_screendraw_func)(void);
 } watch_state;
 
 #endif
 } watch_state;
 
 #endif
index dc8cfa1..90be0d0 100644 (file)
@@ -1,8 +1,10 @@
 #include "oswald.h"
 #include "oswald_watch_faces.h"
 #include "oswald.h"
 #include "oswald_watch_faces.h"
+#include "oswald_screens.h"
+#if 0
 #include "Fonts.h"
 #include "LcdDisplay.h"
 #include "Fonts.h"
 #include "LcdDisplay.h"
-
+#endif
 #include "oswald_main.h"
 
 /*
 #include "oswald_main.h"
 
 /*
  */
 clock_state OswaldClk;
 watch_state OswaldState;
  */
 clock_state OswaldClk;
 watch_state OswaldState;
+watch_screen OswaldScreens[LAST_SCREEN];
 
 
-void update_screen(void)
-{
-       if (OswaldState.user_screendraw_func != NULL)
-               OswaldState.user_screendraw_func();
-}
 
 
-void oswald_change_to_screen(screen_number screen)
+void oswald_change_to_screen(screen_number screen_id)
 {
        // we spare the update if no change happened
 {
        // we spare the update if no change happened
-       if (OswaldState.screen != screen) {
-               OswaldState.screen = screen;
-               update_screen();
+       if (OswaldState.screen_id != screen_id) {
+               OswaldState.screen_id = screen_id;
+               if ((OswaldState.screen->event_func != NULL) &&
+                   (OswaldState.screen->event_mask & EVENT_SCREEN_VISIBLE))
+                               OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
        }
 }
 
        }
 }
 
@@ -57,24 +57,14 @@ static void update_clock_state (void)
 
 void oswald_one_second_tick(void)
 {
 
 void oswald_one_second_tick(void)
 {
+       /* update our 'RTC' */
        update_clock_state();
        update_clock_state();
-       update_screen();
-}
 
 
-void draw_accel_screen(void)
-{
-       lcd_clear_display();
-       SetFont(MetaWatch16);
-       WriteLcdString(2, 2, "X:");
-       WriteLcdString(20, 2, "123");
-       WriteLcdString(2, 18, "Z:");
-       WriteLcdString(20, 18, "123");
-       WriteLcdString(2, 34, "Y:");
-       WriteLcdString(20, 34, "123");
-}
-
-void idle_handle_user_buttons(watch_button button)
-{
+       /* wake-up screen if interested in the one-second-event */
+       if (OswaldState.screen->event_func != NULL &&
+           (OswaldState.screen->event_mask & EVENT_ONE_SEC_TIMER))
+               OswaldState.screen->event_func(EVENT_ONE_SEC_TIMER, NULL);
+       // oswald_update_screen();
 }
 
 void oswald_handle_button_press(watch_button button)
 }
 
 void oswald_handle_button_press(watch_button button)
@@ -84,27 +74,19 @@ void oswald_handle_button_press(watch_button button)
                case BUTTON_B:
                case BUTTON_D:
                case BUTTON_E:
                case BUTTON_B:
                case BUTTON_D:
                case BUTTON_E:
-                       if (OswaldState.user_button_func != NULL)
-                               OswaldState.user_button_func(button);
+                       if (OswaldState.screen->event_func != NULL &&
+                           (OswaldState.screen->event_mask & EVENT_USER_BUTTONS))
+                               OswaldState.screen->event_func(EVENT_USER_BUTTONS, &button);
                        break;
                case BUTTON_C:
                        // next screen
                        break;
                case BUTTON_C:
                        // next screen
-                       OswaldState.screen++;
-                       if (OswaldState.screen >= LAST_SCREEN) {
-                               OswaldState.screen = IDLE_SCREEN;
-                               OswaldState.user_button_func = idle_handle_user_buttons;
-                               OswaldState.user_screendraw_func = DrawLcdDigitalClock;
-                       } else {
-                               switch (OswaldState.screen) {
-                                       case SETTING_DATETIME_SCREEN:
-                                               OswaldState.user_button_func = idle_handle_user_buttons;
-                                               OswaldState.user_screendraw_func = draw_accel_screen;
-                                               break;
-                                       default:
-                                               break;
-                               };
+                       OswaldState.screen_id++;
+                       if (OswaldState.screen_id >= LAST_SCREEN) {
+                               OswaldState.screen_id = IDLE_SCREEN;
                        };
                        };
-                       update_screen();
+                       OswaldState.screen = &OswaldScreens[OswaldState.screen_id];
+                       //oswald_update_screen();
+                       OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
                        break;
                case BUTTON_F:
                        // backlight on/off
                        break;
                case BUTTON_F:
                        // backlight on/off
@@ -117,9 +99,24 @@ void oswald_handle_button_press(watch_button button)
 
 void oswald_init(void)
 {
 
 void oswald_init(void)
 {
-       OswaldState.screen = IDLE_SCREEN;
+       OswaldScreens[IDLE_SCREEN].event_mask = EVENT_ONE_SEC_TIMER;
+       OswaldScreens[IDLE_SCREEN].event_func = idle_handle_events;
+
+       OswaldScreens[ACCEL_DISPLAY_SCREEN].event_mask = EVENT_USER_BUTTONS;
+       OswaldScreens[ACCEL_DISPLAY_SCREEN].event_func = accel_handle_events;
+       
+
+       OswaldScreens[DATETIME_SETTING_SCREEN].event_mask = EVENT_USER_BUTTONS | EVENT_ONE_SEC_TIMER;
+       OswaldScreens[DATETIME_SETTING_SCREEN].event_func = datetime_setup_events;
+
+       OswaldScreens[MENU_TEST_SCREEN].event_mask = EVENT_USER_BUTTONS;
+       OswaldScreens[MENU_TEST_SCREEN].event_func = test_menu_handle_events;
+
+       OswaldState.screen_id = IDLE_SCREEN;
+       OswaldState.screen = &OswaldScreens[OswaldState.screen_id];
        OswaldState.idle_show_seconds = FALSE;
        OswaldState.idle_show_seconds = FALSE;
-       OswaldState.user_screendraw_func = DrawLcdDigitalClock;
-       OswaldState.user_button_func = idle_handle_user_buttons;
+
+       if (OswaldState.screen->event_func != NULL)
+               OswaldState.screen->event_func(EVENT_SCREEN_VISIBLE, NULL);
 }
 
 }
 
index b812547..a11252d 100644 (file)
@@ -5,6 +5,7 @@
 
 extern clock_state OswaldClk;
 extern watch_state OswaldState;
 
 extern clock_state OswaldClk;
 extern watch_state OswaldState;
+extern watch_screen OswaldScreens[];
 
 /* gets triggered by OS timer function */
 void oswald_one_second_tick();
 
 /* gets triggered by OS timer function */
 void oswald_one_second_tick();
diff --git a/ui/oswald_screens.c b/ui/oswald_screens.c
new file mode 100644 (file)
index 0000000..b6158d8
--- /dev/null
@@ -0,0 +1,123 @@
+#include "oswald.h"
+#include "oswald_watch_faces.h"
+#include "Fonts.h"
+#include "LcdDisplay.h"
+
+#include "oswald_screens.h"
+
+void idle_handle_user_buttons(watch_button button)
+{
+}
+void idle_handle_events(u16t event, void *data)
+{
+       switch (event) {
+               case EVENT_ONE_SEC_TIMER:
+               case EVENT_SCREEN_VISIBLE:
+                       DrawLcdDigitalClock();
+                       break;
+               case EVENT_USER_BUTTONS:
+                       dbg_out("button event %d\n", *(int *)data);
+                       break;
+               default:
+                       break;
+       };
+}
+
+void draw_accel_screen(void)
+{
+       lcd_clear_display();
+       SetFont(MetaWatch16);
+       WriteLcdString(2, 2, "X:");
+       WriteLcdString(20, 2, "123");
+       WriteLcdString(2, 18, "Z:");
+       WriteLcdString(20, 18, "123");
+       WriteLcdString(2, 34, "Y:");
+       WriteLcdString(20, 34, "123");
+}
+void accel_handle_events(u16t event, void *data)
+{
+       switch (event) {
+               case EVENT_SCREEN_VISIBLE:
+                       draw_accel_screen();
+                       break;
+               case EVENT_USER_BUTTONS:
+                       dbg_out("button event %d\n", *(int *)data);
+                       break;
+               default:
+                       break;
+       };
+}
+
+void draw_datetime_setup_screen(void)
+{
+       lcd_clear_display();
+       SetFont(MetaWatch16);
+       WriteLcdString(2, 2, "Date / Time");
+       WriteLcdString(2, 18, "Setting");
+       WriteLcdString(2, 34, "22:39");
+       WriteLcdString(2, 50, "07.08.2012");
+}
+void datetime_setup_events(u16t event, void *data)
+{
+       switch (event) {
+               case EVENT_SCREEN_VISIBLE:
+                       draw_datetime_setup_screen();
+                       break;
+               case EVENT_USER_BUTTONS:
+                       dbg_out("button event %d\n", *(int *)data);
+                       break;
+               default:
+                       break;
+       };
+}
+
+static u8t test_menu_pos = 0;
+
+void draw_menu_test_screen(void)
+{
+       lcd_clear_display();
+       SetFont(MetaWatch16);
+       WriteLcdString(2, 2, "Menu");
+       SetFont(MetaWatch7);
+       WriteLcdString(2, 20, "Item 1");
+       WriteLcdString(2, 29, "Item 2");
+       WriteLcdString(2, 38, "Item 3");
+       WriteLcdString(2, 47, "Item 4");
+       WriteLcdString(2, 56, "Item 5");
+
+       WriteLcdString(50, 20+(9*test_menu_pos), "*");
+}
+
+static void handle_menu_user_buttons(watch_button button)
+{
+       switch (button) {
+               case BUTTON_A:
+                       test_menu_pos--;
+                       test_menu_pos%=5;
+                       break;
+               case BUTTON_B:
+                       test_menu_pos++;
+                       test_menu_pos%=5;
+                       break;
+               default:
+                       break;
+       }
+       draw_menu_test_screen();
+}
+
+void test_menu_handle_events(u16t event, void *data)
+{
+       switch (event) {
+               case EVENT_USER_BUTTONS:
+                       dbg_out("button event %d\n", *(int *)data);
+                       handle_menu_user_buttons(*(watch_button *)data);
+                       break;
+               case EVENT_SCREEN_VISIBLE:
+                       test_menu_pos = 0;
+                       draw_menu_test_screen();
+                       break;
+               default:
+                       break;
+       };
+}
+
diff --git a/ui/oswald_screens.h b/ui/oswald_screens.h
new file mode 100644 (file)
index 0000000..54ea831
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef _OSWALD_SCREENS_H
+#define _OSWALD_SCREENS_H
+
+void idle_handle_user_buttons(watch_button button);
+void idle_handle_events(u16t event, void *data);
+
+void draw_accel_screen(void);
+void accel_handle_events(u16t event, void *data);
+
+void draw_datetime_setup_screen(void);
+void datetime_setup_events(u16t event, void *data);
+
+void draw_menu_test_screen(void);
+void test_menu_handle_events(u16t event, void *data);
+
+#endif
+