display_task.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #if SK_DISPLAY
  2. #include "display_task.h"
  3. #include "semaphore_guard.h"
  4. #include "font/roboto_light_60.h"
  5. static const uint8_t LEDC_CHANNEL_LCD_BACKLIGHT = 0;
  6. DisplayTask::DisplayTask(const uint8_t task_core) : Task{"Display", 4048, 1, task_core} {
  7. knob_state_queue_ = xQueueCreate(1, sizeof(KnobState));
  8. assert(knob_state_queue_ != NULL);
  9. mutex_ = xSemaphoreCreateMutex();
  10. assert(mutex_ != NULL);
  11. }
  12. DisplayTask::~DisplayTask() {
  13. vQueueDelete(knob_state_queue_);
  14. vSemaphoreDelete(mutex_);
  15. }
  16. static void HSV_to_RGB(float h, float s, float v, uint8_t *r, uint8_t *g, uint8_t *b)
  17. {
  18. int i;
  19. float f,p,q,t;
  20. h = fmax(0.0, fmin(360.0, h));
  21. s = fmax(0.0, fmin(100.0, s));
  22. v = fmax(0.0, fmin(100.0, v));
  23. s /= 100;
  24. v /= 100;
  25. if(s == 0) {
  26. // Achromatic (grey)
  27. *r = *g = *b = round(v*255);
  28. return;
  29. }
  30. h /= 60; // sector 0 to 5
  31. i = floor(h);
  32. f = h - i; // factorial part of h
  33. p = v * (1 - s);
  34. q = v * (1 - s * f);
  35. t = v * (1 - s * (1 - f));
  36. switch(i) {
  37. case 0:
  38. *r = round(255*v);
  39. *g = round(255*t);
  40. *b = round(255*p);
  41. break;
  42. case 1:
  43. *r = round(255*q);
  44. *g = round(255*v);
  45. *b = round(255*p);
  46. break;
  47. case 2:
  48. *r = round(255*p);
  49. *g = round(255*v);
  50. *b = round(255*t);
  51. break;
  52. case 3:
  53. *r = round(255*p);
  54. *g = round(255*q);
  55. *b = round(255*v);
  56. break;
  57. case 4:
  58. *r = round(255*t);
  59. *g = round(255*p);
  60. *b = round(255*v);
  61. break;
  62. default: // case 5:
  63. *r = round(255*v);
  64. *g = round(255*p);
  65. *b = round(255*q);
  66. }
  67. }
  68. void DisplayTask::run() {
  69. tft_.begin();
  70. tft_.invertDisplay(1);
  71. tft_.setRotation(0);
  72. tft_.fillScreen(TFT_DARKGREEN);
  73. ledcSetup(LEDC_CHANNEL_LCD_BACKLIGHT, 5000, 16);
  74. ledcAttachPin(PIN_LCD_BACKLIGHT, LEDC_CHANNEL_LCD_BACKLIGHT);
  75. ledcWrite(LEDC_CHANNEL_LCD_BACKLIGHT, UINT16_MAX);
  76. spr_.setColorDepth(16);
  77. if (spr_.createSprite(TFT_WIDTH, TFT_HEIGHT) == nullptr) {
  78. Serial.println("ERROR: sprite allocation failed!");
  79. tft_.fillScreen(TFT_RED);
  80. } else {
  81. Serial.println("Sprite created!");
  82. tft_.fillScreen(TFT_PURPLE);
  83. }
  84. spr_.setTextColor(0xFFFF, TFT_BLACK);
  85. KnobState state;
  86. const int RADIUS = TFT_WIDTH / 2;
  87. const uint16_t FILL_COLOR = spr_.color565(90, 18, 151);
  88. const uint16_t DOT_COLOR = spr_.color565(80, 100, 200);
  89. int32_t pointer_center_x = TFT_WIDTH / 2;
  90. int32_t pointer_center_y = TFT_HEIGHT / 2;
  91. int32_t pointer_length_short = 10;
  92. int32_t pointer_length_long = TFT_WIDTH / 2 - 5;
  93. spr_.setTextDatum(CC_DATUM);
  94. spr_.setTextColor(TFT_WHITE);
  95. while(1) {
  96. if (xQueueReceive(knob_state_queue_, &state, portMAX_DELAY) == pdFALSE) {
  97. continue;
  98. }
  99. spr_.fillSprite(TFT_BLACK);
  100. if (state.config.num_positions > 1) {
  101. int32_t height = state.current_position * TFT_HEIGHT / (state.config.num_positions - 1);
  102. spr_.fillRect(0, TFT_HEIGHT - height, TFT_WIDTH, height, FILL_COLOR);
  103. }
  104. spr_.setFreeFont(&Roboto_Light_60);
  105. spr_.drawString(String() + state.current_position, TFT_WIDTH / 2, TFT_HEIGHT / 2 - VALUE_OFFSET, 1);
  106. spr_.setFreeFont(&DESCRIPTION_FONT);
  107. int32_t line_y = TFT_HEIGHT / 2 + DESCRIPTION_Y_OFFSET;
  108. char* start = state.config.descriptor;
  109. char* end = start + strlen(state.config.descriptor);
  110. while (start < end) {
  111. char* newline = strchr(start, '\n');
  112. if (newline == nullptr) {
  113. newline = end;
  114. }
  115. char buf[sizeof(state.config.descriptor)] = {};
  116. strncat(buf, start, min(sizeof(buf) - 1, (size_t)(newline - start)));
  117. spr_.drawString(String(buf), TFT_WIDTH / 2, line_y, 1);
  118. start = newline + 1;
  119. line_y += spr_.fontHeight(1);
  120. }
  121. float left_bound = PI / 2;
  122. if (state.config.num_positions > 0) {
  123. float range_radians = (state.config.num_positions - 1) * state.config.position_width_radians;
  124. left_bound = PI / 2 + range_radians / 2;
  125. float right_bound = PI / 2 - range_radians / 2;
  126. spr_.drawLine(TFT_WIDTH/2 + RADIUS * cosf(left_bound), TFT_HEIGHT/2 - RADIUS * sinf(left_bound), TFT_WIDTH/2 + (RADIUS - 10) * cosf(left_bound), TFT_HEIGHT/2 - (RADIUS - 10) * sinf(left_bound), TFT_WHITE);
  127. spr_.drawLine(TFT_WIDTH/2 + RADIUS * cosf(right_bound), TFT_HEIGHT/2 - RADIUS * sinf(right_bound), TFT_WIDTH/2 + (RADIUS - 10) * cosf(right_bound), TFT_HEIGHT/2 - (RADIUS - 10) * sinf(right_bound), TFT_WHITE);
  128. }
  129. if (DRAW_ARC) {
  130. spr_.drawCircle(TFT_WIDTH/2, TFT_HEIGHT/2, RADIUS, TFT_DARKGREY);
  131. }
  132. float adjusted_sub_position = state.sub_position_unit * state.config.position_width_radians;
  133. if (state.config.num_positions > 0) {
  134. if (state.current_position == 0 && state.sub_position_unit < 0) {
  135. adjusted_sub_position = -logf(1 - state.sub_position_unit * state.config.position_width_radians / 5 / PI * 180) * 5 * PI / 180;
  136. } else if (state.current_position == state.config.num_positions - 1 && state.sub_position_unit > 0) {
  137. adjusted_sub_position = logf(1 + state.sub_position_unit * state.config.position_width_radians / 5 / PI * 180) * 5 * PI / 180;
  138. }
  139. }
  140. float raw_angle = left_bound - state.current_position * state.config.position_width_radians;
  141. float adjusted_angle = raw_angle - adjusted_sub_position;
  142. if (state.config.num_positions > 0 && ((state.current_position == 0 && state.sub_position_unit < 0) || (state.current_position == state.config.num_positions - 1 && state.sub_position_unit > 0))) {
  143. spr_.fillCircle(TFT_WIDTH/2 + (RADIUS - 10) * cosf(raw_angle), TFT_HEIGHT/2 - (RADIUS - 10) * sinf(raw_angle), 5, DOT_COLOR);
  144. if (raw_angle < adjusted_angle) {
  145. for (float r = raw_angle; r <= adjusted_angle; r += 2 * PI / 180) {
  146. spr_.fillCircle(TFT_WIDTH/2 + (RADIUS - 10) * cosf(r), TFT_HEIGHT/2 - (RADIUS - 10) * sinf(r), 2, DOT_COLOR);
  147. }
  148. spr_.fillCircle(TFT_WIDTH/2 + (RADIUS - 10) * cosf(adjusted_angle), TFT_HEIGHT/2 - (RADIUS - 10) * sinf(adjusted_angle), 2, DOT_COLOR);
  149. } else {
  150. for (float r = raw_angle; r >= adjusted_angle; r -= 2 * PI / 180) {
  151. spr_.fillCircle(TFT_WIDTH/2 + (RADIUS - 10) * cosf(r), TFT_HEIGHT/2 - (RADIUS - 10) * sinf(r), 2, DOT_COLOR);
  152. }
  153. spr_.fillCircle(TFT_WIDTH/2 + (RADIUS - 10) * cosf(adjusted_angle), TFT_HEIGHT/2 - (RADIUS - 10) * sinf(adjusted_angle), 2, DOT_COLOR);
  154. }
  155. } else {
  156. spr_.fillCircle(TFT_WIDTH/2 + (RADIUS - 10) * cosf(adjusted_angle), TFT_HEIGHT/2 - (RADIUS - 10) * sinf(adjusted_angle), 5, DOT_COLOR);
  157. }
  158. spr_.pushSprite(0, 0);
  159. {
  160. SemaphoreGuard lock(mutex_);
  161. ledcWrite(LEDC_CHANNEL_LCD_BACKLIGHT, brightness_);
  162. }
  163. delay(2);
  164. }
  165. }
  166. QueueHandle_t DisplayTask::getKnobStateQueue() {
  167. return knob_state_queue_;
  168. }
  169. void DisplayTask::setBrightness(uint16_t brightness) {
  170. SemaphoreGuard lock(mutex_);
  171. brightness_ = brightness;
  172. }
  173. #endif