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.

288 lines
7.9KB

  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("Aussen");
  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. uint8_t interpolation(int32_t arg) {
  102. int32_t i32_val = arg * 256 / 1000;
  103. uint8_t u8_val = (uint8_t)i32_val;
  104. return u8_val;
  105. }
  106. color_t temp_color(int32_t temp_raw) {
  107. color_t col = { 0 };
  108. if (temp_raw >= 3500)
  109. col.r = 255;
  110. else if (temp_raw < 3500 && temp_raw > 2500) {
  111. int32_t diff = 3500 - temp_raw;
  112. col.r = 255;
  113. col.g = interpolation(diff);
  114. }
  115. else if (temp_raw == 2500) {
  116. col.r = 255;
  117. col.g = 255;
  118. }
  119. else if (temp_raw < 2500 && temp_raw > 1500) {
  120. int32_t diff = temp_raw - 1500;
  121. col.r = interpolation(diff);
  122. col.g = 255;
  123. }
  124. else if (temp_raw == 1500)
  125. col.g = 255;
  126. else if (temp_raw < 1500 && temp_raw > 500) {
  127. int32_t diff = 1500 - temp_raw;
  128. col.g = 255;
  129. col.b = interpolation(diff);
  130. }
  131. else if (temp_raw == 500) {
  132. col.g = 255;
  133. col.b = 255;
  134. }
  135. else if (temp_raw < 500 && temp_raw > -500) {
  136. int32_t diff = temp_raw + 500;
  137. col.g = interpolation(diff);
  138. col.b = 255;
  139. }
  140. else if (temp_raw == -500)
  141. col.b = 255;
  142. else if (temp_raw < -500 && temp_raw > -1500) {
  143. int32_t diff = -500 - temp_raw;
  144. col.r = interpolation(diff);
  145. col.b = 255;
  146. }
  147. else if (temp_raw <= -1500) {
  148. col.r = 255;
  149. col.b = 255;
  150. }
  151. return col;
  152. }
  153. int32_t last_temp_raw = 0;
  154. uint32_t last_pressure_raw = 0;
  155. uint32_t last_humidity_raw = 0;
  156. int32_t last_temp2_raw = 0;
  157. uint32_t last_pressure2_raw = 0;
  158. uint32_t last_humidity2_raw = 0;
  159. void display_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
  160. int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw)
  161. {
  162. // Calc temperature pre and post comma values
  163. int32_t temp_pre = temp_raw / 100;
  164. int32_t temp_post = (abs(temp_raw) % 100) / 10;
  165. char temp_str[12];
  166. sprintf(temp_str, "% 2.2d,%.1d", temp_pre, temp_post);
  167. int32_t temp2_pre = temp2_raw / 100;
  168. int32_t temp2_post = (abs(temp2_raw) % 100) / 10;
  169. char temp2_str[12];
  170. sprintf(temp2_str, "% 2.2d,%.1d", temp2_pre, temp2_post);
  171. // Calc humidity pre and post comma values
  172. uint32_t humid_pre = humidity_raw / 1024;
  173. uint32_t humid_post = (humidity_raw - humid_pre*1024) * 10 / 1024;
  174. char humid_str[12];
  175. sprintf(humid_str, "%2.2d,%.1d", humid_pre, humid_post);
  176. uint32_t humid2_pre = humidity2_raw / 1024;
  177. uint32_t humid2_post = (humidity2_raw - humid2_pre*1024) * 10 / 1024;
  178. char humid2_str[12];
  179. sprintf(humid2_str, "%2.2d,%.1d", humid2_pre, humid2_post);
  180. // Calc pressure values
  181. uint32_t press = pressure_raw / 100;
  182. char press_str[12];
  183. sprintf(press_str, "%d", press);
  184. uint32_t press2 = pressure2_raw / 100;
  185. char press2_str[12];
  186. sprintf(press2_str, "%d", press2);
  187. TFT_setFont(UBUNTU16_FONT, NULL);
  188. TFT_fillScreen(TFT_BLACK);
  189. tft_fg = TFT_WHITE;
  190. TFT_print("25.12.2031 08:31", layout.dateTimeLeft, layout.datetimeBaseline);
  191. TFT_drawFastHLine(0, 20, 160, TFT_WHITE);
  192. TFT_print("Innen", layout.innenLeft, layout.innenBaseline);
  193. TFT_print("Aussen", layout.aussenLeft, layout.aussenBaseline);
  194. TFT_setFont(DEJAVU18_FONT, NULL);
  195. tft_fg = temp_color(temp_raw);
  196. TFT_print(temp_str, layout.innenLeft, layout.tempBaseline);
  197. tft_fg = temp_color(temp2_raw);
  198. TFT_print(temp2_str, layout.aussenLeft, layout.tempBaseline);
  199. tft_fg = TFT_WHITE;
  200. TFT_print(" C", layout.unitLeft, layout.tempBaseline);
  201. TFT_drawCircle(layout.unitLeft+3, layout.tempBaseline+3, 3, TFT_WHITE);
  202. TFT_setFont(UBUNTU16_FONT, NULL);
  203. TFT_print(humid_str, layout.innenLeft, layout.humBaseline);
  204. TFT_print(humid2_str, layout.aussenLeft, layout.humBaseline);
  205. TFT_print("%", layout.unitLeft, layout.humBaseline);
  206. TFT_print(press_str, layout.innenLeft, layout.pressBaseline);
  207. TFT_print(press2_str, layout.aussenLeft, layout.pressBaseline);
  208. TFT_print("hPa", layout.unitLeft, layout.pressBaseline);
  209. }
  210. esp_err_t init_display()
  211. {
  212. tft_max_rdclock = 4000000;
  213. TFT_PinsInit();
  214. spi_lobo_device_handle_t spi;
  215. spi_lobo_bus_config_t buscfg={
  216. .miso_io_num=PIN_NUM_MISO,
  217. .mosi_io_num=PIN_NUM_MOSI,
  218. .sclk_io_num=PIN_NUM_CLK,
  219. .quadwp_io_num=-1,
  220. .quadhd_io_num=-1,
  221. .max_transfer_sz = 6*1024,
  222. };
  223. spi_lobo_device_interface_config_t devcfg={
  224. .clock_speed_hz=8000000,
  225. .mode=0,
  226. .spics_io_num=-1,
  227. .spics_ext_io_num=PIN_NUM_CS,
  228. .flags=LB_SPI_DEVICE_HALFDUPLEX,
  229. };
  230. esp_err_t ret;
  231. ret=spi_lobo_bus_add_device(SPI_BUS, &buscfg, &devcfg, &spi);
  232. if (ret != ESP_OK)
  233. {
  234. return ret;
  235. }
  236. tft_disp_spi = spi;
  237. TFT_display_init();
  238. tft_max_rdclock = find_rd_speed();
  239. spi_lobo_set_speed(spi, DEFAULT_SPI_CLOCK);
  240. tft_font_rotate = 0;
  241. tft_text_wrap = 0;
  242. tft_font_transparent = 0;
  243. tft_font_forceFixed = 0;
  244. tft_gray_scale = 0;
  245. TFT_setGammaCurve(DEFAULT_GAMMA_CURVE);
  246. TFT_setRotation(LANDSCAPE_FLIP);
  247. TFT_setFont(DEFAULT_FONT, NULL);
  248. TFT_resetclipwin();
  249. tft_image_debug = 0;
  250. layout = create_layout();
  251. return ESP_OK;
  252. }