logo.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #include <TFT_eSPI.h>
  3. #include "config.h"
  4. // ---------------------------------------------------------------------------
  5. // Logo polygon data — design canvas 107 × 125 units
  6. // ---------------------------------------------------------------------------
  7. static const int kGreenPts[][2] = {
  8. {19, 92}, {19, 39}, {54, 66}, {107, 24},
  9. {107, 1}, {54, 42}, { 1, 2}, { 1, 107}
  10. };
  11. static const int kBluePts[][2] = {
  12. { 1, 125}, {31, 125}, {89, 79}, { 89, 107},
  13. { 76, 107}, {76, 125}, {107, 125}, {107, 42}
  14. };
  15. static constexpr int LOGO_N = 8;
  16. static constexpr float LOGO_SCALE = 1.6f;
  17. static constexpr int LOGO_X_OFF = 34;
  18. static constexpr int LOGO_Y_OFF = 20;
  19. static inline int lsx(int x) { return LOGO_X_OFF + (int)(x * LOGO_SCALE); }
  20. static inline int lsy(int y) { return LOGO_Y_OFF + (int)(y * LOGO_SCALE); }
  21. // ---------------------------------------------------------------------------
  22. // RGB565 colour helpers
  23. // ---------------------------------------------------------------------------
  24. static inline uint16_t rgb565(uint8_t r, uint8_t g, uint8_t b) {
  25. return ((uint16_t)(r & 0xF8) << 8) |
  26. ((uint16_t)(g & 0xFC) << 3) |
  27. (b >> 3);
  28. }
  29. static inline uint16_t logoGreenAt(float t) {
  30. return rgb565(0, (uint8_t)(191.0f * t), (uint8_t)(99.0f * t));
  31. }
  32. static inline uint16_t logoBlueAt(float t) {
  33. return rgb565((uint8_t)(17.0f * t), (uint8_t)(67.0f * t), (uint8_t)(120.0f * t));
  34. }
  35. // ---------------------------------------------------------------------------
  36. // Scanline polygon fill — operates in screen coordinates
  37. // ---------------------------------------------------------------------------
  38. static void fillPolygon(TFT_eSPI &tft, int pts[][2], int n, uint16_t colour) {
  39. int yMin = pts[0][1], yMax = pts[0][1];
  40. for (int i = 1; i < n; i++) {
  41. if (pts[i][1] < yMin) yMin = pts[i][1];
  42. if (pts[i][1] > yMax) yMax = pts[i][1];
  43. }
  44. int nodeX[16]; // 8-point polygon produces at most 8 intersections per line
  45. for (int y = yMin; y <= yMax; y++) {
  46. int nodes = 0;
  47. int j = n - 1;
  48. for (int i = 0; i < n; i++) {
  49. int yi = pts[i][1], yj = pts[j][1];
  50. if ((yi < y && yj >= y) || (yj < y && yi >= y)) {
  51. int dy = yj - yi;
  52. nodeX[nodes++] = pts[i][0] + (y - yi) * (pts[j][0] - pts[i][0]) / dy;
  53. }
  54. j = i;
  55. }
  56. // Insertion sort
  57. for (int a = 1; a < nodes; a++) {
  58. int key = nodeX[a], b = a - 1;
  59. while (b >= 0 && nodeX[b] > key) { nodeX[b + 1] = nodeX[b--]; }
  60. nodeX[b + 1] = key;
  61. }
  62. for (int a = 0; a + 1 < nodes; a += 2) {
  63. tft.drawFastHLine(nodeX[a], y, nodeX[a + 1] - nodeX[a] + 1, colour);
  64. }
  65. }
  66. }
  67. // Scale design-space polygon into screen-space once
  68. static void buildScaledLogo(int greenOut[][2], int blueOut[][2]) {
  69. for (int i = 0; i < LOGO_N; i++) {
  70. greenOut[i][0] = lsx(kGreenPts[i][0]);
  71. greenOut[i][1] = lsy(kGreenPts[i][1]);
  72. blueOut[i][0] = lsx(kBluePts[i][0]);
  73. blueOut[i][1] = lsy(kBluePts[i][1]);
  74. }
  75. }
  76. // ---------------------------------------------------------------------------
  77. // Animated logo — total duration ~10 s
  78. // 0.0 – 1.0 s green shape fades in
  79. // 1.0 – 2.0 s blue shape fades in, green holds solid
  80. // 2.0 – 10.0 s logo remains on screen
  81. // ---------------------------------------------------------------------------
  82. inline void drawLogo(TFT_eSPI &tft) {
  83. static int sGreen[LOGO_N][2];
  84. static int sBlue[LOGO_N][2];
  85. static bool scaled = false;
  86. if (!scaled) {
  87. buildScaledLogo(sGreen, sBlue);
  88. scaled = true;
  89. }
  90. tft.fillScreen(TFT_BLACK);
  91. // Phase 1: green fade-in
  92. for (int i = 0; i <= LOGO_FADE_STEPS; i++) {
  93. fillPolygon(tft, sGreen, LOGO_N, logoGreenAt(i / (float)LOGO_FADE_STEPS));
  94. delay(LOGO_FADE_STEP_MS);
  95. }
  96. // Phase 2: blue fade-in (green redrawn solid to prevent bleed-through)
  97. for (int i = 0; i <= LOGO_FADE_STEPS; i++) {
  98. fillPolygon(tft, sGreen, LOGO_N, logoGreenAt(1.0f));
  99. fillPolygon(tft, sBlue, LOGO_N, logoBlueAt(i / (float)LOGO_FADE_STEPS));
  100. delay(LOGO_FADE_STEP_MS);
  101. }
  102. // Phase 3: static hold
  103. delay(LOGO_STATIC_MS);
  104. }