ui.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #pragma once
  2. extern "C" {
  3. #include "main.h"
  4. #include "cmsis_os.h"
  5. #include "u8g2_support.h"
  6. #include "u8g2/u8g2.h"
  7. }
  8. #include "events.h"
  9. typedef struct {
  10. void* item;
  11. const char* name;
  12. } MenuItem;
  13. #include "vendor.h"
  14. typedef enum {
  15. LayoutActionUp,
  16. LayoutActionDown,
  17. LayoutActionLeft,
  18. LayoutActionRight,
  19. LayoutActionOk,
  20. LayoutActionBack,
  21. LayoutActionTimeout,
  22. LayoutActionUsbConnect,
  23. LayoutActionUsbDisconnect,
  24. LayoutActionEndOfCycle
  25. } LayoutAction;
  26. typedef struct {
  27. FlipperComponent* item;
  28. LayoutAction action;
  29. } ActionItem;
  30. void draw_text(const char* text, u8g2_t* u8g2, ScreenArea area);
  31. void draw_bitmap(const char* bitmap, u8g2_t* u8g2, ScreenArea area);
  32. class LayoutComponent: FlipperComponent {
  33. public:
  34. LayoutComponent(void (*draw_fn)(const char* text, u8g2_t* u8g2, ScreenArea area), ActionItem* actions, size_t actions_size, uint32_t timeout, const char* data) {
  35. this->data = data;
  36. this->actions = actions;
  37. this->actions_size = actions_size;
  38. this->timeout = timeout;
  39. this->draw_fn = draw_fn;
  40. this->dirty = true;
  41. this->wait_time = 0;
  42. }
  43. virtual void handle(Event* event, struct _Store* store, u8g2_t* u8g2, ScreenArea area);
  44. private:
  45. const char* data;
  46. ActionItem* actions;
  47. size_t actions_size;
  48. uint32_t timeout;
  49. void (*draw_fn)(const char* text, u8g2_t* u8g2, ScreenArea area);
  50. uint32_t wait_time;
  51. bool dirty;
  52. };
  53. typedef struct {
  54. uint32_t on_time;
  55. Color on_color;
  56. uint32_t off_time;
  57. Color off_color;
  58. } BlinkerComponentConfig;
  59. class BlinkerComponent: FlipperComponent {
  60. public:
  61. BlinkerComponent(
  62. void (*draw_fn)(const char* text, u8g2_t* u8g2, ScreenArea area),
  63. BlinkerComponentConfig config,
  64. const char* data
  65. ) {
  66. this->data = data;
  67. this->draw_fn = draw_fn;
  68. this->config = config;
  69. this->dirty = true;
  70. this->wait_time = 0;
  71. this->is_on = true;
  72. this->active = false;
  73. this->prev = NULL;
  74. }
  75. virtual void handle(Event* event, struct _Store* store, u8g2_t* u8g2, ScreenArea area);
  76. private:
  77. const char* data;
  78. BlinkerComponentConfig config;
  79. void (*draw_fn)(const char* text, u8g2_t* u8g2, ScreenArea area);
  80. uint32_t wait_time;
  81. bool is_on;
  82. bool active;
  83. bool dirty;
  84. FlipperComponent* prev;
  85. };
  86. typedef struct {
  87. uint32_t on_time;
  88. Color on_color;
  89. uint32_t off_time;
  90. Color off_color;
  91. } BlinkerComponentOnBtnConfig;
  92. class BlinkerComponentOnBtn: FlipperComponent {
  93. public:
  94. BlinkerComponentOnBtn(
  95. void (*draw_fn)(const char* text, u8g2_t* u8g2, ScreenArea area),
  96. BlinkerComponentOnBtnConfig config,
  97. const char* data
  98. ) {
  99. this->data = data;
  100. this->draw_fn = draw_fn;
  101. this->config = config;
  102. this->dirty = true;
  103. this->wait_time = 0;
  104. this->is_on = true;
  105. this->active = false;
  106. this->prev = NULL;
  107. }
  108. virtual void handle(Event* event, struct _Store* store, u8g2_t* u8g2, ScreenArea area);
  109. private:
  110. const char* data;
  111. BlinkerComponentOnBtnConfig config;
  112. void (*draw_fn)(const char* text, u8g2_t* u8g2, ScreenArea area);
  113. uint32_t wait_time;
  114. bool is_on;
  115. bool active;
  116. bool dirty;
  117. FlipperComponent* prev;
  118. };
  119. typedef enum {
  120. MenuEventUp,
  121. MenuEventDown
  122. } MenuEvent;
  123. class MenuCtx {
  124. public:
  125. size_t size;
  126. size_t current;
  127. uint8_t cursor;
  128. MenuItem* list;
  129. void handle(MenuEvent event);
  130. void reset();
  131. };
  132. void draw_menu(MenuCtx* ctx, u8g2_t* u8g2, ScreenArea area);
  133. class MenuComponent: FlipperComponent {
  134. public:
  135. MenuComponent(MenuItem* list, size_t size, const char* name) {
  136. this->ctx.size = size;
  137. this->ctx.current = 0;
  138. this->ctx.cursor = 0;
  139. this->ctx.list = list;
  140. this->name = name;
  141. this->prev = NULL;
  142. this->dirty = true;
  143. }
  144. const char* name;
  145. FlipperComponent* prev;
  146. MenuCtx ctx;
  147. bool dirty;
  148. virtual void handle(Event* event, struct _Store* store, u8g2_t* u8g2, ScreenArea area);
  149. };
  150. typedef struct {
  151. uint8_t x;
  152. uint8_t y;
  153. } Point;
  154. typedef struct {
  155. uint8_t width;
  156. uint8_t height;
  157. uint8_t margin_left;
  158. uint8_t margin_top;
  159. uint8_t padding_left;
  160. uint8_t padding_top;
  161. uint8_t background;
  162. uint8_t color;
  163. uint8_t* font;
  164. } Block;
  165. Point draw_block(u8g2_t* u8g2, const char* text, Block layout, uint8_t x, uint8_t y);