Browse Source

Working on apps with a screen. Make twali a possible platformio library.

Beoran 3 years ago
parent
commit
2d148d0480
10 changed files with 297 additions and 19 deletions
  1. 26 0
      library.json
  2. 32 0
      src/twali_app.c
  3. 44 0
      src/twali_app.h
  4. 44 0
      src/twali_message.h
  5. 36 1
      src/twali_system.cpp
  6. 5 0
      src/twali_system.h
  7. 1 0
      src/twali_system_internal.h
  8. 18 1
      src/twali_task.c
  9. 5 2
      src/twali_task.h
  10. 86 15
      src/upubsub.h

+ 26 - 0
library.json

@@ -0,0 +1,26 @@
+{
+    "name": "twali",
+    "keywords": "TWatch,Watch,TTGO,library",
+    "description": "Library for building watch firmware based on TTGO T-Watch development kit.",
+    "repository": {
+        "type": "git",
+        "url": "https://gitlab.com/beoran/twali.git"
+    },
+    "authors": [
+        {
+            "name": "beoran",
+            "email": "beoran@gNmOaSiPlA.Mcom",
+            "url": "https://gitlab.com/beoran/",
+            "maintainer": true
+        }
+    ],
+    "version": "0.0.1",
+    "frameworks": "arduino",
+    "platforms": "esp32",
+    "dependencies":[
+        {            
+            "name": "TTGO TWatch Library",
+            "version": "^1.3.0"
+        }
+    ]
+}

+ 32 - 0
src/twali_app.c

@@ -0,0 +1,32 @@
+#include "twali_app.h"
+#include "twali_task.h"
+
+
+#include "lvgl/lvgl.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+void twali_app_update_task(struct twali_task * task) {
+    struct twali_app *app = twali_task_app(task);
+    if (task && app>app) {
+        app->update(app);
+    }
+}
+
+int twali_app_start(struct twali_app * a, int priority) {
+    twali_task_start(a->private.task, priority);
+}
+
+int twali_app_stop(struct twali_app * a) {
+    twali_task_stop(a->private.task);
+    a->private.task = 0;    
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+

+ 44 - 0
src/twali_app.h

@@ -0,0 +1,44 @@
+#ifndef TWALI_APP_H_INCLUDED
+#define TWALI_APP_H_INCLUDED
+
+#define TWALI_APP_MAIN_TASK 0
+
+#include "lvgl/lvgl.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+struct twali_task;
+
+enum twali_app_type {
+    /* This app has only one screen, and is always active. E.g. a clock face. */
+    twali_app_type_static = 1,
+    /* This app can be started and stopped. E.g. a step counter.
+     * If stopped, the app shows the start screen with the given icon. 
+     * If started it shows the active interface.
+     */
+    twali_app_type_dynamic = 2,
+};
+
+struct twali_app {        
+    const char * name;
+    const char * icon_path;
+    enum twali_app_type app_type;
+    void * data;
+    void  (*update)(struct twali_app *t);    
+    struct { 
+        struct twali_task * task;
+        lv_obj_t * screen;
+    } private;
+};
+
+int twali_app_start(struct twali_app * a, int priority);
+int twali_app_stop(struct twali_app * a);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 44 - 0
src/twali_message.h

@@ -0,0 +1,44 @@
+#ifndef TWALI_MESSAHE_H_INCLUDED
+#define TWALI_MESSAHE_H_INCLUDED
+
+
+enum twali_message_type {
+    twali_message_type_none  = 0,
+    twali_message_type_power = 1,
+    twali_message_type_touch = 2,
+    twali_message_type_text  = 3
+};
+
+enum twali_power_message_state {
+    twali_power_message_state_up    = 1,
+    twali_power_message_state_low   = 2,
+    twali_power_message_state_sleep = 3,
+    twali_power_message_state_off   = 4,
+};
+
+struct twali_power_message {
+    enum twali_power_message_state state;
+};
+
+struct twali_touch_message {
+    int x;
+    int y;
+};
+
+#define TWALI_TEXT_MESSAGE_SIZE 64
+struct twali_text_message {
+        char bytes[TWALI_TEXT_MESSAGE_SIZE];
+};
+
+struct twali_message {
+    enum twali_message_type message_type;
+    union {
+        struct twali_power_message power;
+        struct twali_touch_message touch;
+        struct twali_text_message text;
+    } body;
+};
+
+#define UPUBSUB_MESSAGE_TYPE struct twali_message
+
+#endif

+ 36 - 1
src/twali_system.cpp

@@ -10,6 +10,8 @@
 
 extern "C" {
 
+#include <string.h>
+
 static struct twali_system twali_system_singleton;
 static struct twali_system * twali_system_singleton_pointer = NULL;
 
@@ -56,7 +58,7 @@ void twali_system_update(struct twali_system * s) {
 }
 
 void twali_system_wake_up(struct twali_system *s) {
-    twali_system_publish_str(s, "POWER", (char *)"ON");
+    twali_system_publish_power(s, "POWER", twali_power_message_state_up);
 }
 
 int twali_system_publish_data(struct twali_system *s, const char * topic, void * mesg, size_t size) {
@@ -67,4 +69,37 @@ int twali_system_publish_str(struct twali_system *s, const char * topic, char *
     return upubsub_publish_str(s->bus, topic, str);
 }
 
+int twali_system_publish_message(struct twali_system *s, const char * topic, struct twali_message msg) {
+    return upubsub_publish_type(s->bus, topic, msg);
+}
+
+int twali_system_publish_text(struct twali_system *s, const char * topic, const char * text) {
+    struct twali_message msg = { twali_message_type_none };
+    msg.message_type = twali_message_type_text;
+    size_t len = strlen(text);
+    /* truncate text */
+    if (len >= sizeof(msg.body.text.bytes) - 1) {
+        len = sizeof(msg.body.text.bytes) - 1;
+    }
+    strncpy(msg.body.text.bytes, text, len);
+    msg.body.text.bytes[len] = '\0';
+    return twali_system_publish_message(s, topic, msg);
+}
+
+int twali_system_publish_touch(struct twali_system *s, const char * topic, int x, int y) {
+    struct twali_message msg = { twali_message_type_none };
+    msg.message_type = twali_message_type_touch;
+    msg.body.touch.x = x;
+    msg.body.touch.y = y;
+    return twali_system_publish_message(s, topic, msg);
+}
+
+int twali_system_publish_power(struct twali_system *s, const char * topic, enum twali_power_message_state state) {
+    struct twali_message msg = { twali_message_type_none };
+    msg.message_type = twali_message_type_power;
+    msg.body.power.state = state;
+    return twali_system_publish_message(s, topic, msg);
+}
+
+
 }

+ 5 - 0
src/twali_system.h

@@ -5,6 +5,7 @@
 extern "C" {
 #endif
 
+#include "twali_message.h"
 #include "upubsub.h"
 
 struct twali_system;
@@ -22,6 +23,10 @@ void twali_system_wake_up(struct twali_system *s);
 
 int twali_system_publish_data(struct twali_system *s, const char * topic, void * mesg, size_t size);
 int twali_system_publish_str(struct twali_system *s, const char * topic, char * str);
+int twali_system_publish_message(struct twali_system *s, const char * topic, struct twali_message m);
+int twali_system_publish_text(struct twali_system *s, const char * topic, const char * text);
+int twali_system_publish_touch(struct twali_system *s, const char * topic, int x, int y);
+int twali_system_publish_power(struct twali_system *s, const char * topic, enum twali_power_message_state state);
 
 struct upubsub_listener * twali_system_subscribe_listener(struct twali_system *s, 
 const char * topic, void * data, upubsub_listen_func * listen);

+ 1 - 0
src/twali_system_internal.h

@@ -14,6 +14,7 @@
 #include <TTGO.h>
 #endif
 
+#include "twali_message.h"
 #include "upubsub.h"
 #include "twali_system.h"
 #include "twali_power.h"

+ 18 - 1
src/twali_task.c

@@ -1,3 +1,4 @@
+#include "twali_message.h"
 #include "twali_task.h"
 
 #include "twali_system.h"
@@ -24,6 +25,7 @@
 struct twali_task {
     struct twali_system * system;
     struct twali_task   * parent;
+    struct twali_app    * app;
     char                * name;
     TaskHandle_t          task;
     QueueHandle_t         queue;
@@ -37,10 +39,25 @@ struct twali_task {
     struct upubsub_listener * listeners[TWALI_TASK_TOPICS_COUNT];
 };
 
+struct twali_app * twali_task_app(struct twali_task * task) {
+    if (!task) {
+        return 0;
+    }
+    return task->app;
+}
+
+int twali_task_init(struct twali_task *t, 
+    char * name, 
+    struct twali_app * app,
+    void * data, 
+    void  (*message)(struct twali_task *t, struct upubsub_message m),
+    void  (*update)(struct twali_task *t)
+);
+
 void twali_task_task(void * object) {
     struct twali_task * t = object;
     struct twali_power * p = twali_system_power(t->system);
-    
+    t->done = 0;
     do {
         struct upubsub_message mesg;
         /* 

+ 5 - 2
src/twali_task.h

@@ -1,8 +1,9 @@
-#ifndef TWALI_PROCESS_H_INCLUDED
-#define TWALI_PROCESS_H_INCLUDED
+#ifndef TWALI_TASK_H_INCLUDED
+#define TWALI_TASK_H_INCLUDED
 
 struct twali_task;
 struct twali_message;
+struct twali_app;
 
 int twali_task_send(struct twali_task * from, 
 struct twali_task * to, 
@@ -11,4 +12,6 @@ struct twali_message * msg);
 int twali_task_start(struct twali_task * t, int priority);
 int twali_task_stop(struct twali_task * t);
 
+struct twali_app * twali_task_app(struct twali_task * task);
+
 #endif

+ 86 - 15
src/upubsub.h

@@ -6,8 +6,24 @@
 #define UPUBSUB_LISTENER_AMOUNT 64
 #endif /* UPUBSUB_LISTENER_AMOUNT */
 
+#ifdef UPUBSUB_TEST
+
+struct upubsub_test_message_type {
+    char text[64];
+    int x;
+    int y;
+};
+
+#define UPUBSUB_MESSAGE_TYPE struct upubsub_test_message_type
+
+#endif
+
 #ifndef UPUBSUB_MESSAGE_SIZE
+#ifndef UPUBSUB_MESSAGE_TYPE
 #define UPUBSUB_MESSAGE_SIZE 64
+#else 
+#define UPUBSUB_MESSAGE_SIZE (sizeof(UPUBSUB_MESSAGE_TYPE))
+#endif
 #endif /* UPUBSUB_MESSAGE_SIZE */
 
 #ifndef UPUBSUB_TOPIC_AMOUNT
@@ -23,12 +39,23 @@ extern "C" {
 #include <string.h>
 
 
+enum upubsub_message_data_type {
+    upubsub_message_data_type_bytes = 1,
+    upubsub_message_data_type_ptr   = 2,
+    upubsub_message_data_type_typed = 3,
+};
+
 struct upubsub_message {
 	const char * topic;
-	size_t	  size;
-	uint8_t   copy[UPUBSUB_MESSAGE_SIZE];
-	int	  copy_ok;
-	void    * data;
+	size_t	     size;
+    enum upubsub_message_data_type data_type;
+    union {
+        uint8_t   bytes[UPUBSUB_MESSAGE_SIZE];
+        void    * ptr;
+#ifdef UPUBSUB_MESSAGE_TYPE
+        UPUBSUB_MESSAGE_TYPE typed;
+#endif
+    } data;
 };
 
 struct upubsub_listener;
@@ -62,6 +89,9 @@ int upubsub_publish_data(struct upubsub u, const char * topic, void * data, size
 int upubsub_publish_str(struct upubsub u, const char * topic, char * str);
 int upubsub_unsubscribe_listener(struct upubsub * u, struct upubsub_listener * l);
 
+#ifdef UPUBSUB_MESSAGE_TYPE
+int upubsub_publish_type(struct upubsub u, const char * topic, UPUBSUB_MESSAGE_TYPE m);
+#endif
 
 #ifdef __cplusplus
 }
@@ -211,16 +241,22 @@ int upubsub_publish_message(struct upubsub u, struct upubsub_message m) {
 	return sent;
 }
 
-int upubsub_publish_data(struct upubsub u, const char * topic, void * data, size_t size) {
-	struct upubsub_message m = { 0 };
-	m.data = data;
+struct upubsub_message upubsub_message_make(const char * topic, void * data, size_t size) {
+   	struct upubsub_message m = { 0 };
 	m.size = size;
-	m.copy_ok = 0;
-	if (m.size < UPUBSUB_MESSAGE_SIZE) {
-		memcpy(m.copy, data, m.size);
-		m.copy_ok = 1;
-	}
+    if (m.size <= UPUBSUB_MESSAGE_SIZE) {
+		memcpy(m.data.bytes, data, m.size);
+		m.data_type = upubsub_message_data_type_bytes;
+	} else {
+        m.data.ptr = data;
+        m.data_type = upubsub_message_data_type_ptr;
+    }
 	m.topic = topic;
+    return m;
+}
+
+int upubsub_publish_data(struct upubsub u, const char * topic, void * data, size_t size) {
+	struct upubsub_message m = upubsub_message_make(topic, data, size);
 	return upubsub_publish_message(u, m);
 }
 
@@ -229,6 +265,18 @@ int upubsub_publish_str(struct upubsub u, const char * topic, char * str) {
 }
 
 
+#ifdef UPUBSUB_MESSAGE_TYPE
+int upubsub_publish_type(struct upubsub u, const char * topic, UPUBSUB_MESSAGE_TYPE mt) {
+    struct upubsub_message m = { 0 };
+	m.size = sizeof(mt);
+    m.data.typed = mt;
+    m.data_type = upubsub_message_data_type_typed;
+	m.topic = topic;
+	return upubsub_publish_message(u, m);
+}
+#endif
+
+
 int upubsub_unsubscribe_listener(struct upubsub * u, struct upubsub_listener * l)  {
 	struct upubsub_listener * found = NULL;
 
@@ -258,7 +306,15 @@ int upubsub_unsubscribe(struct upubsub_listener * l)  {
 #include <stdio.h>
 
 int listen_print_str(struct upubsub_listener *l, struct upubsub_message m) {
-	printf("received on topic %s: data %s, %s\n", l->topic, (char*)(l->data), (char *)(m.data));
+	printf("received message on topic %s: data %s, %s\n", l->topic,
+        (char*)(l->data), (char *)(m.data.bytes));
+	return 0;
+}
+
+int listen_print_typed_str(struct upubsub_listener *l, struct upubsub_message m) {
+	printf("received typed message on topic %s: data %s, %s, %d, %d\n", l->topic, 
+        (char*)(l->data), (char *)(m.data.typed.text), 
+        m.data.typed.x, m.data.typed.y);
 	return 0;
 }
 
@@ -303,8 +359,9 @@ int main(void) {
 	l[1] = upubsub_subscribe(&pusu, "POWER" , "APP1", listen_print_str);
 	l[2] = upubsub_subscribe(&pusu, "SYSTEM", "APP2", listen_print_str);
 	l[3] = upubsub_subscribe(&pusu, "SYSTEM", "WIND", listen_print_str);
-    l[2] = upubsub_subscribe(&pusu, "SYSTEM", "APP3", listen_print_str);
-	l[3] = upubsub_subscribe(&pusu, "POWER", "APP4", listen_print_str);
+    l[4] = upubsub_subscribe(&pusu, "SYSTEM", "APP3", listen_print_str);
+	l[5] = upubsub_subscribe(&pusu, "POWER" , "APP4", listen_print_str);
+    l[6] = upubsub_subscribe(&pusu, "TYPED" , "APP5", listen_print_typed_str);
 
 
 	printf("Publishing...\n");
@@ -313,6 +370,13 @@ int main(void) {
 	upubsub_publish_str(pusu, "SYSTEM", "CLICK(7,8)");
 	upubsub_publish_str(pusu, "SYSTEM", "ROLL(9,5)");
 	upubsub_publish_str(pusu, "NULL", "IGNORE");
+    {
+        UPUBSUB_MESSAGE_TYPE typed = {0};
+        strcpy(typed.text, "TYPED");
+        typed.x = 10;
+        typed.y = 20;
+        upubsub_publish_type(pusu, "TYPED", typed);
+    }
 
 	printf("Removing topics...\n");
 	upubsub_unsubscribe(l[1]);
@@ -324,6 +388,13 @@ int main(void) {
 	upubsub_publish_str(pusu, "SYSTEM", "CLICK(7,8) 2");
 	upubsub_publish_str(pusu, "SYSTEM", "ROLL(9,5) 2");
 	upubsub_publish_str(pusu, "NULL", "IGNORE");
+    {
+        UPUBSUB_MESSAGE_TYPE typed = {0};
+        strcpy(typed.text, "TYPED");
+        typed.x = 30;
+        typed.y = 40;
+        upubsub_publish_type(pusu, "TYPED", typed);
+    }
 
 	return 0;
 }