twali_task.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "twali_message.h"
  2. #include "twali_task.h"
  3. #include "twali_system.h"
  4. #include "twali_power.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/queue.h"
  7. #include "freertos/task.h"
  8. #include <stdio.h>
  9. #define UPUBSUB_IMPLEMENTATION
  10. #include "upubsub.h"
  11. #ifndef TWALI_TASK_QUEUE_SIZE
  12. #define TWALI_TASK_QUEUE_SIZE 32
  13. #endif
  14. #ifndef TWALI_TASK_TOPICS_COUNT
  15. #define TWALI_TASK_TOPICS_COUNT 32
  16. #endif
  17. struct twali_task {
  18. struct twali_system * system;
  19. struct twali_task * parent;
  20. struct twali_app * app;
  21. char * name;
  22. TaskHandle_t task;
  23. QueueHandle_t queue;
  24. void * data;
  25. int low_power_delay;
  26. int delay;
  27. int done;
  28. void (*message)(struct twali_task *t, struct upubsub_message m);
  29. void (*update)(struct twali_task *t);
  30. const char * topics[TWALI_TASK_TOPICS_COUNT];
  31. struct upubsub_listener * listeners[TWALI_TASK_TOPICS_COUNT];
  32. };
  33. struct twali_app * twali_task_app(struct twali_task * task) {
  34. if (!task) {
  35. return 0;
  36. }
  37. return task->app;
  38. }
  39. int twali_task_init(struct twali_task *t,
  40. char * name,
  41. struct twali_app * app,
  42. void * data,
  43. void (*message)(struct twali_task *t, struct upubsub_message m),
  44. void (*update)(struct twali_task *t)
  45. );
  46. void twali_task_task(void * object) {
  47. struct twali_task * t = object;
  48. struct twali_power * p = twali_system_power(t->system);
  49. t->done = 0;
  50. do {
  51. struct upubsub_message mesg;
  52. /*
  53. bool update_display = false;
  54. unsigned int delay_time = 0;
  55. */
  56. if( xQueueReceive(t->queue,
  57. &(mesg),
  58. ( TickType_t ) 10 ) == pdPASS ) {
  59. if (t->message) {
  60. t->message(t, mesg);
  61. } else {
  62. printf("Task %s received message topic %s\n", t->name, mesg.topic);
  63. }
  64. }
  65. if (twali_power_low(p) && (t->low_power_delay > 0)) {
  66. vTaskDelay(t->low_power_delay / portTICK_PERIOD_MS);
  67. continue;
  68. }
  69. /* twali_task_check_wake_time(t); */
  70. if (t->update) {
  71. t->update(t);
  72. }
  73. vTaskDelay(t->delay / portTICK_PERIOD_MS);
  74. } while (!t->done);
  75. twali_task_stop(t);
  76. }
  77. int task_upubsub_listen_func(struct upubsub_listener *l, struct upubsub_message m) {
  78. struct twali_task *t = l->data;
  79. return (int)xQueueSend(t->queue, &m, 0);
  80. }
  81. struct upubsub_listener * twali_task_subscribe_topic(struct twali_task * t, const char * topic) {
  82. return twali_system_subscribe_listener(t->system, topic, t, task_upubsub_listen_func);
  83. }
  84. int twali_task_subscribe_all_topics(struct twali_task *t) {
  85. int i;
  86. if (!t->system) {
  87. return 0;
  88. }
  89. for (i = 0; i < TWALI_TASK_TOPICS_COUNT; i++) {
  90. const char * topic = t->topics[i];
  91. if (topic) {
  92. t->listeners[i] = twali_task_subscribe_topic(t, topic);
  93. } else {
  94. t->listeners[i] = NULL;
  95. }
  96. }
  97. return 0;
  98. }
  99. int twali_task_start(struct twali_task * t, int priority) {
  100. BaseType_t res;
  101. res = xTaskCreate(twali_task_task, t->name, 10000, (void *) t, priority, &t->task);
  102. t->queue = xQueueCreate( (UBaseType_t) TWALI_TASK_QUEUE_SIZE,
  103. (UBaseType_t) sizeof(struct upubsub_message));
  104. /*if (!t->system) {
  105. t->system = twali_system_singleton_pointer;
  106. }*/
  107. if (!t->system) {
  108. return 0;
  109. }
  110. twali_task_subscribe_all_topics(t);
  111. return res == pdPASS && t->queue != 0;
  112. }
  113. int twali_task_stop(struct twali_task * t) {
  114. printf("deleting task %s\n", t->name);
  115. int i;
  116. for (i = 0; i < TWALI_TASK_TOPICS_COUNT; i ++) {
  117. if (t->listeners[i]) {
  118. upubsub_unsubscribe(t->listeners[i]);
  119. }
  120. }
  121. vQueueDelete(t->queue);
  122. vTaskDelete(NULL);
  123. return 0;
  124. }
  125. /*
  126. class Actor {
  127. public:
  128. static TTGOClass *watch;
  129. static TFT_eSPI *screen;
  130. static PowerManager * power;
  131. static TaskHandle_t display_task;
  132. // system accessors
  133. static void setPower(PowerManager * _power);
  134. static void setDisplayTask(TaskHandle_t _display_task);
  135. static void setWatch(TTGOClass * _watch);
  136. // setup and init
  137. bool needsInit();
  138. void setup();
  139. virtual void init() { }
  140. // run and display
  141. virtual void run() = 0;
  142. void execute(unsigned int & sleep_time, bool & display_update);
  143. virtual void display() = 0;
  144. virtual const uint32_t displayIdentifier() = 0;
  145. // low power considerations
  146. virtual const bool runDuringLowPower() { return false; }
  147. virtual const uint32_t delayDuringLowPower() { return 1000; }
  148. // sleep-wake
  149. static void systemWokeUp();
  150. void checkWakeTime();
  151. bool wakeUpRun();
  152. protected:
  153. unsigned int delay_time = 100;
  154. bool refresh_display = false;
  155. private:
  156. static unsigned int woke_up_at;
  157. unsigned int last_wake_up_tasked = 0;
  158. bool wake_up_run = true;
  159. bool inited = false;
  160. };
  161. void actorTask(void * object);
  162. void runActor(const char * name, Actor * object, int priority);
  163. */