Browse Source

Work on event system.

Beoran 3 years ago
parent
commit
9fd57d6c06
6 changed files with 100 additions and 19 deletions
  1. 7 0
      src/twali_screen.cpp
  2. 10 3
      src/twali_system.cpp
  3. 4 9
      src/twali_system.h
  4. 77 5
      src/twali_task.c
  5. 1 0
      src/twali_task.h
  6. 1 2
      src/upubsub.h

+ 7 - 0
src/twali_screen.cpp

@@ -1,4 +1,8 @@
 
+/*
+#include <TFT_eSPI>
+
+
 struct twali_screen {
     int x; 
     int y; 
@@ -319,3 +323,6 @@ void twali_screen_draw_line(struct twali_screen * s, int32_t x, int32_t y, int32
 
     uint8_t  decoderState = 0;   // UTF8 decoder state        - not for user access
     uint16_t decoderBuffer;      // Unicode code-point buffer - not for user access
+
+*/
+

+ 10 - 3
src/twali_system.cpp

@@ -39,6 +39,7 @@ struct twali_system * twali_system_make(void) {
     twali_system_singleton.tft->setTextFont(8);
     twali_system_singleton.handle->openBL();  
     twali_system_singleton.power  = twali_power_make(&twali_system_singleton);
+    twali_system_singleton.bus = { 0 };
     pinMode(AXP202_INT, INPUT);
     attachInterrupt(AXP202_INT, twali_system_power_interrupt, FALLING);
 
@@ -47,17 +48,23 @@ struct twali_system * twali_system_make(void) {
 }
 
 void twali_system_start(struct twali_system * s) {
-      twali_power_start_task(s->power);
+    twali_power_start_task(s->power);
 }
 
 void twali_system_update(struct twali_system * s) {
     vTaskDelay(10000);
 }
 
+void twali_system_wake_up(struct twali_system *s) {
+    twali_system_publish_str(s, "POWER", (char *)"ON");
+}
 
+int twali_system_publish_data(struct twali_system *s, const char * topic, void * mesg, size_t size) {
+    return upubsub_publish_data(s->bus, topic, mesg, size);
+}
 
-void twali_system_wake_up(struct twali_system *s) {
-    // TODO
+int twali_system_publish_str(struct twali_system *s, const char * topic, char * str) {
+    return upubsub_publish_str(s->bus, topic, str);
 }
 
 }

+ 4 - 9
src/twali_system.h

@@ -5,6 +5,8 @@
 extern "C" {
 #endif
 
+#include "upubsub.h"
+
 struct twali_system;
 struct twali_power;
 struct twali_task;
@@ -18,15 +20,8 @@ void twali_system_update(struct twali_system * s);
 
 void twali_system_wake_up(struct twali_system *s);
 
-struct upubsub_listener * upubsub_subscribe_listener(struct upubsub * u, struct upubsub_listener l);
-struct upubsub_listener* upubsub_subscribe(struct upubsub * u, const char * topic, void * data, upubsub_listen_func * listen);
-int upubsub_publish_message(struct upubsub u, struct upubsub_message m);
-int upubsub_publish_data(struct upubsub u, const char * topic, void * data, size_t size);
-int upubsub_publish_str(struct upubsub u, const char * topic, char * str);
-int upubsub_unsubscribe_listener(struct upubsub * u, struct upubsub_listener * l);
-
-void twali_system_publish_data(struct twali_system *s, const char * topic, void * mesg, size_t size);
-void twali_system_publish_str(struct twali_system *s, const char * topic, char * str);
+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);
 
 struct upubsub_listener * twali_system_subscribe_listener(struct twali_system *s, 
 const char * topic, void * data, upubsub_listen_func * listen);

+ 77 - 5
src/twali_task.c

@@ -6,10 +6,21 @@
 #include "freertos/FreeRTOS.h"
 #include "freertos/queue.h"
 #include "freertos/task.h"
+#include <stdio.h>
 
 #define UPUBSUB_IMPLEMENTATION
 #include "upubsub.h"
 
+#ifndef TWALI_TASK_QUEUE_SIZE
+#define TWALI_TASK_QUEUE_SIZE 32
+#endif
+
+#ifndef TWALI_TASK_TOPICS_COUNT
+#define TWALI_TASK_TOPICS_COUNT 32
+#endif
+
+
+
 struct twali_task {
     struct twali_system * system;
     struct twali_task   * parent;
@@ -19,18 +30,33 @@ struct twali_task {
     void *                data;
     int                   low_power_delay;
     int                   delay;
+    int                   done;
+    void                  (*message)(struct twali_task *t, struct upubsub_message m);
     void                  (*update)(struct twali_task *t);
+    const char          * topics[TWALI_TASK_TOPICS_COUNT];
+    struct upubsub_listener * listeners[TWALI_TASK_TOPICS_COUNT];
 };
 
 void twali_task_task(void * object) {
     struct twali_task * t = object;
     struct twali_power * p = twali_system_power(t->system);
     
-    for (;;) {
+    do {
+        struct upubsub_message mesg;
         /* 
         bool update_display = false;
         unsigned int delay_time = 0;
         */
+        
+        if( xQueueReceive(t->queue,
+                         &(mesg),
+                         ( TickType_t ) 10 ) == pdPASS ) {
+            if (t->message) {
+                t->message(t, mesg);
+            } else {
+                printf("Task %s received message topic %s\n", t->name, mesg.topic);
+            }
+        }
 
         if (twali_power_low(p) && (t->low_power_delay > 0)) {
             vTaskDelay(t->low_power_delay / portTICK_PERIOD_MS);
@@ -42,15 +68,61 @@ void twali_task_task(void * object) {
             t->update(t);
         }    
         vTaskDelay(t->delay / portTICK_PERIOD_MS);
-  }
-  printf("deleting task %s\n", t->name);
-  vTaskDelete(NULL);
+  } while (!t->done);
+  twali_task_stop(t);
+}
+
+int task_upubsub_listen_func(struct upubsub_listener *l, struct upubsub_message m) {
+    struct twali_task *t = l->data;
+    return (int)xQueueSend(t->queue, &m, 0);
+}
+
+struct upubsub_listener * twali_task_subscribe_topic(struct twali_task * t, const char * topic) {
+    return twali_system_subscribe_listener(t->system, topic, t, task_upubsub_listen_func);
+}
+
+int twali_task_subscribe_all_topics(struct twali_task *t) {
+    int i;
+    if (!t->system) {
+        return 0;
+    }
+    for (i = 0; i < TWALI_TASK_TOPICS_COUNT; i++) {
+        const char * topic = t->topics[i];
+        if (topic) {
+            t->listeners[i] = twali_task_subscribe_topic(t, topic);
+        } else {
+            t->listeners[i] = NULL;
+        }
+    }
+    return 0;
 }
 
 int twali_task_start(struct twali_task * t, int priority) {
     BaseType_t res;
     res = xTaskCreate(twali_task_task, t->name, 10000, (void *) t, priority, &t->task);
-    return res == pdPASS;
+    t->queue = xQueueCreate( (UBaseType_t) TWALI_TASK_QUEUE_SIZE,
+                             (UBaseType_t) sizeof(struct upubsub_message));
+    /*if (!t->system) {
+        t->system = twali_system_singleton_pointer;
+    }*/
+    if (!t->system) {
+        return 0;
+    }
+    twali_task_subscribe_all_topics(t);
+    return res == pdPASS && t->queue != 0;
+}
+
+int twali_task_stop(struct twali_task * t) {
+    printf("deleting task %s\n", t->name);
+    int i;
+    for (i = 0; i < TWALI_TASK_TOPICS_COUNT; i ++) {
+        if (t->listeners[i]) {
+            upubsub_unsubscribe(t->listeners[i]);
+        }
+    }
+    vQueueDelete(t->queue);
+    vTaskDelete(NULL);
+    return 0;
 }
 
 /*

+ 1 - 0
src/twali_task.h

@@ -9,5 +9,6 @@ struct twali_task * to,
 struct twali_message * msg);
 
 int twali_task_start(struct twali_task * t, int priority);
+int twali_task_stop(struct twali_task * t);
 
 #endif

+ 1 - 2
src/upubsub.h

@@ -90,7 +90,6 @@ struct bsearch_range_result bsearch_range (const void *key, const void *base, si
     struct bsearch_range_result res;
 	void *here;
 	int sign;
-    int found;    
     int botm = 0;
     int top = nel;    
     res.low = 0;
@@ -192,7 +191,7 @@ struct upubsub_listener* upubsub_subscribe(struct upubsub * u, const char * topi
 
 int upubsub_publish_message(struct upubsub u, struct upubsub_message m) {
 	int sent = 0;
-	size_t start, index;
+	size_t index;
 	struct upubsub_listener * found = NULL;
 	struct upubsub_listener key;
     struct bsearch_range_result ran;