twali_task.c 4.7 KB

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