twali_system.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #define LILYGO_TWATCH_2020_V1
  2. #include <TTGO.h>
  3. #define TWALI_INTERNAL
  4. #include "twali_system_internal.h"
  5. #include "twali_power.h"
  6. #include "twali_screen.h"
  7. extern "C" {
  8. #include <string.h>
  9. static struct twali_system twali_system_singleton;
  10. static struct twali_system * twali_system_singleton_pointer = NULL;
  11. struct twali_power * twali_system_power(struct twali_system * s) {
  12. return s->power;
  13. }
  14. void twali_system_power_interrupt(void) {
  15. if (twali_system_singleton_pointer) {
  16. twali_power_interrupt(twali_system_singleton_pointer->power);
  17. }
  18. }
  19. struct twali_system * twali_system_make(void) {
  20. if (twali_system_singleton_pointer) {
  21. return twali_system_singleton_pointer;
  22. }
  23. twali_system_singleton.handle = TTGOClass::getWatch();
  24. twali_system_singleton.handle->begin();
  25. twali_system_singleton.handle->rtc->check();
  26. twali_system_singleton.handle->bl->adjust(150);
  27. twali_system_singleton.tft = twali_system_singleton.handle->eTFT;
  28. twali_system_singleton.tft->fillScreen(TFT_BLUE);
  29. twali_system_singleton.tft->setTextColor(TFT_WHITE, TFT_BLACK);
  30. twali_system_singleton.tft->setTextFont(8);
  31. twali_system_singleton.handle->openBL();
  32. twali_system_singleton.power = twali_power_make(&twali_system_singleton);
  33. twali_system_singleton.bus = { 0 };
  34. pinMode(AXP202_INT, INPUT);
  35. attachInterrupt(AXP202_INT, twali_system_power_interrupt, FALLING);
  36. twali_system_singleton_pointer = &twali_system_singleton;
  37. return twali_system_singleton_pointer;
  38. }
  39. void twali_system_start(struct twali_system * s) {
  40. twali_power_start_task(s->power);
  41. }
  42. void twali_system_update(struct twali_system * s) {
  43. vTaskDelay(10000);
  44. }
  45. void twali_system_wake_up(struct twali_system *s) {
  46. twali_system_publish_power(s, "POWER", twali_power_message_state_up);
  47. }
  48. int twali_system_publish_data(struct twali_system *s, const char * topic, void * mesg, size_t size) {
  49. return upubsub_publish_data(s->bus, topic, mesg, size);
  50. }
  51. int twali_system_publish_str(struct twali_system *s, const char * topic, char * str) {
  52. return upubsub_publish_str(s->bus, topic, str);
  53. }
  54. int twali_system_publish_message(struct twali_system *s, const char * topic, struct twali_message msg) {
  55. return upubsub_publish_type(s->bus, topic, msg);
  56. }
  57. int twali_system_publish_text(struct twali_system *s, const char * topic, const char * text) {
  58. struct twali_message msg = { twali_message_type_none };
  59. msg.message_type = twali_message_type_text;
  60. size_t len = strlen(text);
  61. /* truncate text */
  62. if (len >= sizeof(msg.body.text.bytes) - 1) {
  63. len = sizeof(msg.body.text.bytes) - 1;
  64. }
  65. strncpy(msg.body.text.bytes, text, len);
  66. msg.body.text.bytes[len] = '\0';
  67. return twali_system_publish_message(s, topic, msg);
  68. }
  69. int twali_system_publish_touch(struct twali_system *s, const char * topic, int x, int y) {
  70. struct twali_message msg = { twali_message_type_none };
  71. msg.message_type = twali_message_type_touch;
  72. msg.body.touch.x = x;
  73. msg.body.touch.y = y;
  74. return twali_system_publish_message(s, topic, msg);
  75. }
  76. int twali_system_publish_power(struct twali_system *s, const char * topic, enum twali_power_message_state state) {
  77. struct twali_message msg = { twali_message_type_none };
  78. msg.message_type = twali_message_type_power;
  79. msg.body.power.state = state;
  80. return twali_system_publish_message(s, topic, msg);
  81. }
  82. }