Repo for ESP32 Weather Station Development
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.7KB

  1. /*
  2. * display.c
  3. *
  4. * Created on: 19 Apr 2020
  5. * Author: Chris
  6. */
  7. #include "display.h"
  8. #include "tftspi.h"
  9. #include "tft.h"
  10. #define SPI_BUS TFT_VSPI_HOST
  11. typedef struct {
  12. int datetimeW;
  13. int datetimeA;
  14. int dateTimeLeft;
  15. int datetimeBaseline;
  16. int innenW;
  17. int aussenW;
  18. int locA;
  19. int tempW;
  20. int degCW;
  21. int tempA;
  22. int humW;
  23. int percentW;
  24. int humA;
  25. int pressW;
  26. int pressA;
  27. int hpaW;
  28. int datetimebarH;
  29. int margin;
  30. int borderVMargin;
  31. int innenBaseline;
  32. int aussenBaseline;
  33. int pressBaseline;
  34. int humBaseline;
  35. int tempBaseline;
  36. int unitMaxW;
  37. int unitW;
  38. int ioW;
  39. int unitLeft;
  40. int innenLeft;
  41. int aussenLeft;
  42. } layout_t;
  43. static layout_t layout;
  44. layout_t create_layout()
  45. {
  46. layout_t layout;
  47. int screenW = tft_width;
  48. int screenH = tft_height;
  49. layout.datetimebarH = 20; // Can be calculated too
  50. layout.margin = 5;
  51. layout.borderVMargin = 10;
  52. TFT_setFont(UBUNTU16_FONT, NULL);
  53. layout.datetimebarH = 20;
  54. layout.datetimeW = TFT_getStringWidth("25.12.2031 08:31");
  55. layout.datetimeA = TFT_getfontheight();
  56. layout.dateTimeLeft = (screenW - layout.datetimeW) / 2;
  57. layout.datetimeBaseline = (layout.datetimebarH - layout.datetimeA) / 2;
  58. layout.innenW = TFT_getStringWidth("Innen");
  59. layout.aussenW = TFT_getStringWidth("Außen");
  60. layout.locA = TFT_getfontheight();
  61. TFT_setFont(DEJAVU18_FONT, NULL);
  62. layout.tempW = TFT_getStringWidth("-35.2");
  63. layout.degCW = TFT_getStringWidth(" C");
  64. layout.tempA = TFT_getfontheight();
  65. TFT_setFont(UBUNTU16_FONT, NULL);
  66. layout.humW = TFT_getStringWidth("25.2");
  67. layout.percentW = TFT_getStringWidth("%");
  68. layout.humA = TFT_getfontheight();
  69. layout.pressW = TFT_getStringWidth("1281");
  70. layout.pressA = TFT_getfontheight();
  71. layout.hpaW = TFT_getStringWidth("hPa");
  72. layout.innenBaseline = layout.datetimebarH + layout.borderVMargin;
  73. layout.aussenBaseline = layout.innenBaseline;
  74. layout.pressBaseline = screenH - layout.borderVMargin - layout.pressA;
  75. layout.humBaseline = layout.pressBaseline - layout.pressA - layout.margin;
  76. layout.tempBaseline = (layout.humBaseline - layout.humA + layout.innenBaseline) / 2
  77. + 0.5*layout.tempA - 1;
  78. int unitMaxW = layout.degCW;
  79. if (layout.percentW > unitMaxW) unitMaxW = layout.percentW;
  80. if (layout.hpaW > unitMaxW) unitMaxW = layout.hpaW;
  81. layout.unitW = 2*layout.margin + unitMaxW;
  82. layout.ioW = (screenW - layout.unitW) / 2;
  83. layout.unitLeft = screenW - layout.unitW + layout.margin;
  84. layout.innenLeft = layout.margin;
  85. layout.aussenLeft = layout.innenLeft + layout.ioW;
  86. ESP_LOGI("main", "innen: %d x %d, aussen: %d x %d", layout.innenW,
  87. layout.locA, layout.aussenW, layout.locA);
  88. ESP_LOGI("main", "temps: %d x %d, unit: %d x %d", layout.tempW,
  89. layout.tempA, layout.degCW, layout.tempA);
  90. ESP_LOGI("main", "hum: %d x %d, unit: %d x %d", layout.humW, layout.humA,
  91. layout.percentW, layout.humA);
  92. ESP_LOGI("main", "press: %d x %d, unit: %d x %d", layout.pressW, layout.pressA,
  93. layout.hpaW, layout.pressA);
  94. ESP_LOGI("main", "Baselines - innen: %d, aussen: %d, press: %d, hum: %d, temp: %d",
  95. layout.innenBaseline, layout.aussenBaseline,
  96. layout.pressBaseline, layout.humBaseline, layout.tempBaseline);
  97. ESP_LOGI("main", "Width - units: %d, i/o: %d", layout.unitW, layout.ioW);
  98. ESP_LOGI("main", "Left - units: %d", layout.unitLeft);
  99. return layout;
  100. }
  101. void display_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
  102. int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw)
  103. {
  104. TFT_setFont(UBUNTU16_FONT, NULL);
  105. TFT_fillScreen(TFT_BLACK);
  106. tft_fg = TFT_WHITE;
  107. const color_t red = {0, 0, 255};
  108. const color_t green = {0, 255, 0};
  109. const color_t blue = {255, 0, 0};
  110. const color_t yellow = {255, 255, 0};
  111. TFT_print("25.12.2031 08:31", layout.dateTimeLeft, layout.datetimeBaseline);
  112. TFT_drawFastHLine(0, 20, 160, TFT_WHITE);
  113. TFT_print("Innen", layout.innenLeft, layout.innenBaseline);
  114. TFT_print("Aussen", layout.aussenLeft, layout.aussenBaseline);
  115. TFT_setFont(DEJAVU18_FONT, NULL);
  116. tft_fg = red;
  117. TFT_print("-42,2", layout.innenLeft, layout.tempBaseline);
  118. tft_fg = yellow;
  119. TFT_print("-35,2", layout.aussenLeft, layout.tempBaseline);
  120. tft_fg = TFT_WHITE;
  121. TFT_print(" C", layout.unitLeft, layout.tempBaseline);
  122. TFT_drawCircle(layout.unitLeft+3, layout.tempBaseline+3, 3, TFT_WHITE);
  123. TFT_setFont(UBUNTU16_FONT, NULL);
  124. TFT_print("25,2", layout.innenLeft, layout.humBaseline);
  125. TFT_print("42,2", layout.aussenLeft, layout.humBaseline);
  126. TFT_print("%", layout.unitLeft, layout.humBaseline);
  127. TFT_print("1288", layout.innenLeft, layout.pressBaseline);
  128. TFT_print("2563", layout.aussenLeft, layout.pressBaseline);
  129. TFT_print("hPa", layout.unitLeft, layout.pressBaseline);
  130. }
  131. esp_err_t init_display()
  132. {
  133. tft_max_rdclock = 4000000;
  134. TFT_PinsInit();
  135. spi_lobo_device_handle_t spi;
  136. spi_lobo_bus_config_t buscfg={
  137. .miso_io_num=PIN_NUM_MISO, // set SPI MISO pin
  138. .mosi_io_num=PIN_NUM_MOSI, // set SPI MOSI pin
  139. .sclk_io_num=PIN_NUM_CLK, // set SPI CLK pin
  140. .quadwp_io_num=-1,
  141. .quadhd_io_num=-1,
  142. .max_transfer_sz = 6*1024,
  143. };
  144. spi_lobo_device_interface_config_t devcfg={
  145. .clock_speed_hz=8000000,
  146. .mode=0,
  147. .spics_io_num=-1,
  148. .spics_ext_io_num=PIN_NUM_CS,
  149. .flags=LB_SPI_DEVICE_HALFDUPLEX,
  150. };
  151. esp_err_t ret;
  152. ret=spi_lobo_bus_add_device(SPI_BUS, &buscfg, &devcfg, &spi);
  153. if (ret != ESP_OK)
  154. {
  155. return ret;
  156. }
  157. tft_disp_spi = spi;
  158. TFT_display_init();
  159. tft_max_rdclock = find_rd_speed();
  160. spi_lobo_set_speed(spi, DEFAULT_SPI_CLOCK);
  161. tft_font_rotate = 0;
  162. tft_text_wrap = 0;
  163. tft_font_transparent = 0;
  164. tft_font_forceFixed = 0;
  165. tft_gray_scale = 0;
  166. TFT_setGammaCurve(DEFAULT_GAMMA_CURVE);
  167. TFT_setRotation(LANDSCAPE_FLIP);
  168. TFT_setFont(DEFAULT_FONT, NULL);
  169. TFT_resetclipwin();
  170. tft_image_debug = 0;
  171. layout = create_layout();
  172. return ESP_OK;
  173. }