Thu, 02 May 2019 11:52:36 +0200
Added tag rel-0.2.10 for changeset 5c92103c5e72
0 | 1 | /* TFT module |
2 | * | |
3 | * Author: LoBo (loboris@gmail.com, loboris.github) | |
4 | * | |
5 | * Module supporting SPI TFT displays based on ILI9341 & ILI9488 controllers | |
6 | */ | |
7 | ||
8 | #include <stdio.h> | |
9 | #include <errno.h> | |
10 | #include <sys/stat.h> | |
11 | #include <string.h> | |
12 | #include "freertos/FreeRTOS.h" | |
13 | #include "freertos/task.h" | |
14 | #include "esp_system.h" | |
15 | #include "tft.h" | |
16 | #include "time.h" | |
17 | #include <math.h> | |
18 | #include "rom/tjpgd.h" | |
19 | #include "esp_heap_caps.h" | |
20 | #include "tftspi.h" | |
21 | ||
22 | #include "vnc-server.h" | |
23 | ||
24 | #define DEG_TO_RAD 0.01745329252 | |
25 | #define RAD_TO_DEG 57.295779513 | |
26 | #define deg_to_rad 0.01745329252 + 3.14159265359 | |
27 | #define swap(a, b) { int16_t t = a; a = b; b = t; } | |
28 | #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) | |
29 | #if !defined(max) | |
30 | #define max(A,B) ( (A) > (B) ? (A):(B)) | |
31 | #endif | |
32 | #if !defined(min) | |
33 | #define min(A,B) ( (A) < (B) ? (A):(B)) | |
34 | #endif | |
35 | ||
36 | // Embedded fonts | |
37 | extern uint8_t tft_SmallFont[]; | |
38 | extern uint8_t tft_DefaultFont[]; | |
39 | extern uint8_t tft_Dejavu18[]; | |
40 | extern uint8_t tft_Dejavu24[]; | |
41 | extern uint8_t tft_Ubuntu16[]; | |
42 | extern uint8_t tft_Comic24[]; | |
43 | extern uint8_t tft_minya24[]; | |
44 | extern uint8_t tft_tooney32[]; | |
45 | extern uint8_t tft_def_small[]; | |
46 | ||
47 | // ==== Color definitions constants ============== | |
29 | 48 | const color_t TFT_BLACK = { 0, 0, 0 }; ///< Black |
49 | const color_t TFT_NAVY = { 0, 0, 128 }; ///< Navy blue | |
50 | const color_t TFT_DARKGREEN = { 0, 128, 0 }; ///< Dark green | |
51 | const color_t TFT_DARKCYAN = { 0, 128, 128 }; ///< Dark cyan | |
52 | const color_t TFT_MAROON = { 128, 0, 0 }; ///< Maroon red | |
53 | const color_t TFT_PURPLE = { 128, 0, 128 }; ///< Purple | |
54 | const color_t TFT_OLIVE = { 128, 128, 0 }; ///< Olive | |
55 | const color_t TFT_LIGHTGREY = { 192, 192, 192 }; ///< Light gray | |
56 | const color_t TFT_DARKGREY = { 128, 128, 128 }; ///< Dark gray | |
57 | const color_t TFT_BLUE = { 0, 0, 255 }; ///< Blue | |
58 | const color_t TFT_GREEN = { 0, 255, 0 }; ///< Green | |
59 | const color_t TFT_CYAN = { 0, 255, 255 }; ///< Cyan | |
60 | const color_t TFT_RED = { 255, 0, 0 }; ///< Red | |
61 | const color_t TFT_MAGENTA = { 255, 0, 255 }; ///< Magenta | |
62 | const color_t TFT_YELLOW = { 255, 255, 0 }; ///< Yellow | |
63 | const color_t TFT_WHITE = { 255, 255, 255 }; ///< White | |
64 | const color_t TFT_ORANGE = { 255, 164, 0 }; ///< Orange | |
65 | const color_t TFT_GREENYELLOW = { 172, 255, 44 }; ///< Green-yellow | |
66 | const color_t TFT_PINK = { 255, 192, 202 }; ///< Pink | |
0 | 67 | // =============================================== |
68 | ||
69 | // ============================================================== | |
70 | // ==== Set default values of global variables ================== | |
71 | uint8_t orientation = LANDSCAPE;// screen orientation | |
72 | uint16_t font_rotate = 0; // font rotation | |
73 | uint8_t font_transparent = 0; | |
74 | uint8_t font_forceFixed = 0; | |
75 | uint8_t text_wrap = 0; // character wrapping to new line | |
76 | color_t _fg = { 0, 255, 0}; | |
77 | color_t _bg = { 0, 0, 0}; | |
78 | uint8_t image_debug = 0; | |
79 | ||
80 | float _angleOffset = DEFAULT_ANGLE_OFFSET; | |
81 | ||
82 | int TFT_X = 0; | |
83 | int TFT_Y = 0; | |
84 | ||
85 | uint16_t tp_xleft = 300; | |
86 | uint16_t tp_xright = 3550; | |
87 | uint16_t tp_ytop = 3800; | |
88 | uint16_t tp_ybottom = 300; | |
89 | ||
90 | dispWin_t dispWin = { | |
91 | .x1 = 0, | |
92 | .y1 = 0, | |
93 | .x2 = DEFAULT_TFT_DISPLAY_WIDTH -1, | |
94 | .y2 = DEFAULT_TFT_DISPLAY_HEIGHT -1, | |
95 | }; | |
96 | ||
97 | Font cfont = { | |
98 | .font = tft_DefaultFont, | |
99 | .x_size = 0, | |
100 | .y_size = 0x0B, | |
101 | .offset = 0, | |
102 | .numchars = 95, | |
103 | .bitmap = 1, | |
104 | }; | |
105 | ||
106 | uint8_t font_buffered_char = 1; | |
107 | uint8_t font_line_space = 0; | |
108 | // ============================================================== | |
109 | ||
110 | ||
111 | typedef struct { | |
112 | uint8_t charCode; | |
113 | int adjYOffset; | |
114 | int width; | |
115 | int height; | |
116 | int xOffset; | |
117 | int xDelta; | |
118 | uint16_t dataPtr; | |
119 | } propFont; | |
120 | ||
121 | static dispWin_t dispWinTemp; | |
122 | ||
123 | static uint8_t *userfont = NULL; | |
124 | static int TFT_OFFSET = 0; | |
125 | static propFont fontChar; | |
126 | ||
127 | ||
128 | // ========================================================================= | |
129 | // ** All drawings are clipped to 'dispWin' ** | |
130 | // ** All x,y coordinates in public functions are relative to clip window ** | |
131 | // =========== : Public functions | |
132 | // ----------- : Local functions | |
133 | // ========================================================================= | |
134 | ||
135 | ||
136 | // draw color pixel on screen | |
137 | //------------------------------------------------------------------------ | |
138 | static void _drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel) { | |
139 | ||
140 | if ((x < dispWin.x1) || (y < dispWin.y1) || (x > dispWin.x2) || (y > dispWin.y2)) return; | |
141 | drawPixel(x, y, color, sel); | |
142 | VncDrawPixel(x, y, VNC_RGB2COL(color.r, color.g, color.b)); | |
143 | } | |
144 | ||
145 | //==================================================================== | |
146 | void TFT_drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel) { | |
147 | ||
148 | _drawPixel(x+dispWin.x1, y+dispWin.y1, color, sel); | |
149 | } | |
150 | ||
151 | //=========================================== | |
152 | color_t TFT_readPixel(int16_t x, int16_t y) { | |
153 | ||
154 | if ((x < dispWin.x1) || (y < dispWin.y1) || (x > dispWin.x2) || (y > dispWin.y2)) return TFT_BLACK; | |
155 | ||
156 | return readPixel(x, y); | |
157 | } | |
158 | ||
159 | //-------------------------------------------------------------------------- | |
160 | static void _drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color) { | |
161 | // clipping | |
162 | if ((x < dispWin.x1) || (x > dispWin.x2) || (y > dispWin.y2)) return; | |
163 | if (y < dispWin.y1) { | |
164 | h -= (dispWin.y1 - y); | |
165 | y = dispWin.y1; | |
166 | } | |
167 | if (h < 0) h = 0; | |
168 | if ((y + h) > (dispWin.y2+1)) h = dispWin.y2 - y + 1; | |
169 | if (h == 0) h = 1; | |
170 | TFT_pushColorRep(x, y, x, y+h-1, color, (uint32_t)h); | |
171 | VncDrawVertLine(x, y, y+h-1, VNC_RGB2COL(color.r, color.g, color.b)); | |
172 | } | |
173 | ||
174 | //-------------------------------------------------------------------------- | |
175 | static void _drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color) { | |
176 | // clipping | |
177 | if ((y < dispWin.y1) || (x > dispWin.x2) || (y > dispWin.y2)) return; | |
178 | if (x < dispWin.x1) { | |
179 | w -= (dispWin.x1 - x); | |
180 | x = dispWin.x1; | |
181 | } | |
182 | if (w < 0) w = 0; | |
183 | if ((x + w) > (dispWin.x2+1)) w = dispWin.x2 - x + 1; | |
184 | if (w == 0) w = 1; | |
185 | ||
186 | TFT_pushColorRep(x, y, x+w-1, y, color, (uint32_t)w); | |
187 | VncDrawHorzLine(x, x+w-1, y, VNC_RGB2COL(color.r, color.g, color.b)); | |
188 | } | |
189 | ||
190 | //====================================================================== | |
191 | void TFT_drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color) { | |
192 | _drawFastVLine(x+dispWin.x1, y+dispWin.y1, h, color); | |
193 | } | |
194 | ||
195 | //====================================================================== | |
196 | void TFT_drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color) { | |
197 | _drawFastHLine(x+dispWin.x1, y+dispWin.y1, w, color); | |
198 | } | |
199 | ||
200 | // Bresenham's algorithm - thx wikipedia - speed enhanced by Bodmer this uses | |
201 | // the eficient FastH/V Line draw routine for segments of 2 pixels or more | |
202 | //---------------------------------------------------------------------------------- | |
203 | static void _drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t color) | |
204 | { | |
205 | if (x0 == x1) { | |
206 | if (y0 <= y1) _drawFastVLine(x0, y0, y1-y0, color); | |
207 | else _drawFastVLine(x0, y1, y0-y1, color); | |
208 | return; | |
209 | } | |
210 | if (y0 == y1) { | |
211 | if (x0 <= x1) _drawFastHLine(x0, y0, x1-x0, color); | |
212 | else _drawFastHLine(x1, y0, x0-x1, color); | |
213 | return; | |
214 | } | |
215 | ||
216 | int steep = 0; | |
217 | if (abs(y1 - y0) > abs(x1 - x0)) steep = 1; | |
218 | if (steep) { | |
219 | swap(x0, y0); | |
220 | swap(x1, y1); | |
221 | } | |
222 | if (x0 > x1) { | |
223 | swap(x0, x1); | |
224 | swap(y0, y1); | |
225 | } | |
226 | ||
227 | int16_t dx = x1 - x0, dy = abs(y1 - y0); | |
228 | int16_t err = dx >> 1, ystep = -1, xs = x0, dlen = 0; | |
229 | ||
230 | if (y0 < y1) ystep = 1; | |
231 | ||
232 | // Split into steep and not steep for FastH/V separation | |
233 | if (steep) { | |
234 | for (; x0 <= x1; x0++) { | |
235 | dlen++; | |
236 | err -= dy; | |
237 | if (err < 0) { | |
238 | err += dx; | |
239 | if (dlen == 1) _drawPixel(y0, xs, color, 1); | |
240 | else _drawFastVLine(y0, xs, dlen, color); | |
241 | dlen = 0; y0 += ystep; xs = x0 + 1; | |
242 | } | |
243 | } | |
244 | if (dlen) _drawFastVLine(y0, xs, dlen, color); | |
245 | } | |
246 | else | |
247 | { | |
248 | for (; x0 <= x1; x0++) { | |
249 | dlen++; | |
250 | err -= dy; | |
251 | if (err < 0) { | |
252 | err += dx; | |
253 | if (dlen == 1) _drawPixel(xs, y0, color, 1); | |
254 | else _drawFastHLine(xs, y0, dlen, color); | |
255 | dlen = 0; y0 += ystep; xs = x0 + 1; | |
256 | } | |
257 | } | |
258 | if (dlen) _drawFastHLine(xs, y0, dlen, color); | |
259 | } | |
260 | } | |
261 | ||
262 | //============================================================================== | |
263 | void TFT_drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t color) | |
264 | { | |
265 | _drawLine(x0+dispWin.x1, y0+dispWin.y1, x1+dispWin.x1, y1+dispWin.y1, color); | |
266 | } | |
267 | ||
268 | // fill a rectangle | |
269 | //-------------------------------------------------------------------------------- | |
270 | static void _fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color) { | |
271 | // clipping | |
272 | if ((x >= dispWin.x2) || (y > dispWin.y2)) return; | |
273 | ||
274 | if (x < dispWin.x1) { | |
275 | w -= (dispWin.x1 - x); | |
276 | x = dispWin.x1; | |
277 | } | |
278 | if (y < dispWin.y1) { | |
279 | h -= (dispWin.y1 - y); | |
280 | y = dispWin.y1; | |
281 | } | |
282 | if (w < 0) w = 0; | |
283 | if (h < 0) h = 0; | |
284 | ||
285 | if ((x + w) > (dispWin.x2+1)) w = dispWin.x2 - x + 1; | |
286 | if ((y + h) > (dispWin.y2+1)) h = dispWin.y2 - y + 1; | |
287 | if (w == 0) w = 1; | |
288 | if (h == 0) h = 1; | |
289 | TFT_pushColorRep(x, y, x+w-1, y+h-1, color, (uint32_t)(h*w)); | |
290 | VncFillRect(x, y, x+w-1, y+h-1, VNC_RGB2COL(color.r, color.g, color.b)); | |
291 | } | |
292 | ||
293 | //============================================================================ | |
294 | void TFT_fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color) { | |
295 | _fillRect(x+dispWin.x1, y+dispWin.y1, w, h, color); | |
296 | } | |
297 | ||
298 | //================================== | |
299 | void TFT_fillScreen(color_t color) { | |
300 | TFT_pushColorRep(0, 0, _width-1, _height-1, color, (uint32_t)(_height*_width)); | |
301 | VncCls(VNC_RGB2COL(color.r, color.g, color.b)); | |
302 | } | |
303 | ||
304 | //================================== | |
305 | void TFT_fillWindow(color_t color) { | |
306 | TFT_pushColorRep(dispWin.x1, dispWin.y1, dispWin.x2, dispWin.y2, | |
307 | color, (uint32_t)((dispWin.x2-dispWin.x1+1) * (dispWin.y2-dispWin.y1+1))); | |
308 | VncFillRect(dispWin.x1, dispWin.y1, dispWin.x2, dispWin.y2, VNC_RGB2COL(color.r, color.g, color.b)); | |
309 | } | |
310 | ||
311 | // ^^^============= Basics drawing functions ================================^^^ | |
312 | ||
313 | ||
314 | // ================ Graphics drawing functions ================================== | |
315 | ||
316 | //----------------------------------------------------------------------------------- | |
317 | static void _drawRect(uint16_t x1,uint16_t y1,uint16_t w,uint16_t h, color_t color) { | |
318 | _drawFastHLine(x1,y1,w, color); | |
319 | _drawFastVLine(x1+w-1,y1,h, color); | |
320 | _drawFastHLine(x1,y1+h-1,w, color); | |
321 | _drawFastVLine(x1,y1,h, color); | |
322 | } | |
323 | ||
324 | //=============================================================================== | |
325 | void TFT_drawRect(uint16_t x1,uint16_t y1,uint16_t w,uint16_t h, color_t color) { | |
326 | _drawRect(x1+dispWin.x1, y1+dispWin.y1, w, h, color); | |
327 | } | |
328 | ||
329 | //------------------------------------------------------------------------------------------------- | |
330 | static void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, color_t color) | |
331 | { | |
332 | int16_t f = 1 - r; | |
333 | int16_t ddF_x = 1; | |
334 | int16_t ddF_y = -2 * r; | |
335 | int16_t x = 0; | |
336 | int16_t y = r; | |
337 | ||
338 | disp_select(); | |
339 | while (x < y) { | |
340 | if (f >= 0) { | |
341 | y--; | |
342 | ddF_y += 2; | |
343 | f += ddF_y; | |
344 | } | |
345 | x++; | |
346 | ddF_x += 2; | |
347 | f += ddF_x; | |
348 | if (cornername & 0x4) { | |
349 | _drawPixel(x0 + x, y0 + y, color, 0); | |
350 | _drawPixel(x0 + y, y0 + x, color, 0); | |
351 | } | |
352 | if (cornername & 0x2) { | |
353 | _drawPixel(x0 + x, y0 - y, color, 0); | |
354 | _drawPixel(x0 + y, y0 - x, color, 0); | |
355 | } | |
356 | if (cornername & 0x8) { | |
357 | _drawPixel(x0 - y, y0 + x, color, 0); | |
358 | _drawPixel(x0 - x, y0 + y, color, 0); | |
359 | } | |
360 | if (cornername & 0x1) { | |
361 | _drawPixel(x0 - y, y0 - x, color, 0); | |
362 | _drawPixel(x0 - x, y0 - y, color, 0); | |
363 | } | |
364 | } | |
365 | disp_deselect(); | |
366 | } | |
367 | ||
368 | // Used to do circles and roundrects | |
369 | //---------------------------------------------------------------------------------------------------------------- | |
370 | static void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, color_t color) | |
371 | { | |
372 | int16_t f = 1 - r; | |
373 | int16_t ddF_x = 1; | |
374 | int16_t ddF_y = -2 * r; | |
375 | int16_t x = 0; | |
376 | int16_t y = r; | |
377 | int16_t ylm = x0 - r; | |
378 | ||
379 | while (x < y) { | |
380 | if (f >= 0) { | |
381 | if (cornername & 0x1) _drawFastVLine(x0 + y, y0 - x, 2 * x + 1 + delta, color); | |
382 | if (cornername & 0x2) _drawFastVLine(x0 - y, y0 - x, 2 * x + 1 + delta, color); | |
383 | ylm = x0 - y; | |
384 | y--; | |
385 | ddF_y += 2; | |
386 | f += ddF_y; | |
387 | } | |
388 | x++; | |
389 | ddF_x += 2; | |
390 | f += ddF_x; | |
391 | ||
392 | if ((x0 - x) > ylm) { | |
393 | if (cornername & 0x1) _drawFastVLine(x0 + x, y0 - y, 2 * y + 1 + delta, color); | |
394 | if (cornername & 0x2) _drawFastVLine(x0 - x, y0 - y, 2 * y + 1 + delta, color); | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
399 | // Draw a rounded rectangle | |
400 | //============================================================================================= | |
401 | void TFT_drawRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color) | |
402 | { | |
403 | x += dispWin.x1; | |
404 | y += dispWin.y1; | |
405 | ||
406 | // smarter version | |
407 | _drawFastHLine(x + r, y, w - 2 * r, color); // Top | |
408 | _drawFastHLine(x + r, y + h - 1, w - 2 * r, color); // Bottom | |
409 | _drawFastVLine(x, y + r, h - 2 * r, color); // Left | |
410 | _drawFastVLine(x + w - 1, y + r, h - 2 * r, color); // Right | |
411 | ||
412 | // draw four corners | |
413 | drawCircleHelper(x + r, y + r, r, 1, color); | |
414 | drawCircleHelper(x + w - r - 1, y + r, r, 2, color); | |
415 | drawCircleHelper(x + w - r - 1, y + h - r - 1, r, 4, color); | |
416 | drawCircleHelper(x + r, y + h - r - 1, r, 8, color); | |
417 | } | |
418 | ||
419 | // Fill a rounded rectangle | |
420 | //============================================================================================= | |
421 | void TFT_fillRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color) | |
422 | { | |
423 | x += dispWin.x1; | |
424 | y += dispWin.y1; | |
425 | ||
426 | // smarter version | |
427 | _fillRect(x + r, y, w - 2 * r, h, color); | |
428 | ||
429 | // draw four corners | |
430 | fillCircleHelper(x + w - r - 1, y + r, r, 1, h - 2 * r - 1, color); | |
431 | fillCircleHelper(x + r, y + r, r, 2, h - 2 * r - 1, color); | |
432 | } | |
433 | ||
434 | ||
435 | ||
436 | ||
437 | //----------------------------------------------------------------------------------------------- | |
438 | ||
439 | // Draw a triangle | |
440 | //-------------------------------------------------------------------------------------------------------------------- | |
441 | static void _drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color) | |
442 | { | |
443 | _drawLine(x0, y0, x1, y1, color); | |
444 | _drawLine(x1, y1, x2, y2, color); | |
445 | _drawLine(x2, y2, x0, y0, color); | |
446 | } | |
447 | ||
448 | // Fill a triangle | |
449 | //-------------------------------------------------------------------------------------------------------------------- | |
450 | static void _fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color) | |
451 | { | |
452 | int16_t a, b, y, last; | |
453 | ||
454 | // Sort coordinates by Y order (y2 >= y1 >= y0) | |
455 | if (y0 > y1) { | |
456 | swap(y0, y1); swap(x0, x1); | |
457 | } | |
458 | if (y1 > y2) { | |
459 | swap(y2, y1); swap(x2, x1); | |
460 | } | |
461 | if (y0 > y1) { | |
462 | swap(y0, y1); swap(x0, x1); | |
463 | } | |
464 | ||
465 | if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing | |
466 | a = b = x0; | |
467 | if(x1 < a) a = x1; | |
468 | else if(x1 > b) b = x1; | |
469 | if(x2 < a) a = x2; | |
470 | else if(x2 > b) b = x2; | |
471 | _drawFastHLine(a, y0, b-a+1, color); | |
472 | return; | |
473 | } | |
474 | ||
475 | int16_t | |
476 | dx01 = x1 - x0, | |
477 | dy01 = y1 - y0, | |
478 | dx02 = x2 - x0, | |
479 | dy02 = y2 - y0, | |
480 | dx12 = x2 - x1, | |
481 | dy12 = y2 - y1; | |
482 | int32_t | |
483 | sa = 0, | |
484 | sb = 0; | |
485 | ||
486 | // For upper part of triangle, find scanline crossings for segments | |
487 | // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 | |
488 | // is included here (and second loop will be skipped, avoiding a /0 | |
489 | // error there), otherwise scanline y1 is skipped here and handled | |
490 | // in the second loop...which also avoids a /0 error here if y0=y1 | |
491 | // (flat-topped triangle). | |
492 | if(y1 == y2) last = y1; // Include y1 scanline | |
493 | else last = y1-1; // Skip it | |
494 | ||
495 | for(y=y0; y<=last; y++) { | |
496 | a = x0 + sa / dy01; | |
497 | b = x0 + sb / dy02; | |
498 | sa += dx01; | |
499 | sb += dx02; | |
500 | /* longhand: | |
501 | a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); | |
502 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); | |
503 | */ | |
504 | if(a > b) swap(a,b); | |
505 | _drawFastHLine(a, y, b-a+1, color); | |
506 | } | |
507 | ||
508 | // For lower part of triangle, find scanline crossings for segments | |
509 | // 0-2 and 1-2. This loop is skipped if y1=y2. | |
510 | sa = dx12 * (y - y1); | |
511 | sb = dx02 * (y - y0); | |
512 | for(; y<=y2; y++) { | |
513 | a = x1 + sa / dy12; | |
514 | b = x0 + sb / dy02; | |
515 | sa += dx12; | |
516 | sb += dx02; | |
517 | /* longhand: | |
518 | a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); | |
519 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); | |
520 | */ | |
521 | if(a > b) swap(a,b); | |
522 | _drawFastHLine(a, y, b-a+1, color); | |
523 | } | |
524 | } | |
525 | ||
526 | //==================================================================== | |
527 | void TFT_drawCircle(int16_t x, int16_t y, int radius, color_t color) { | |
528 | x += dispWin.x1; | |
529 | y += dispWin.y1; | |
530 | int f = 1 - radius; | |
531 | int ddF_x = 1; | |
532 | int ddF_y = -2 * radius; | |
533 | int x1 = 0; | |
534 | int y1 = radius; | |
535 | ||
536 | disp_select(); | |
537 | _drawPixel(x, y + radius, color, 0); | |
538 | _drawPixel(x, y - radius, color, 0); | |
539 | _drawPixel(x + radius, y, color, 0); | |
540 | _drawPixel(x - radius, y, color, 0); | |
541 | while(x1 < y1) { | |
542 | if (f >= 0) { | |
543 | y1--; | |
544 | ddF_y += 2; | |
545 | f += ddF_y; | |
546 | } | |
547 | x1++; | |
548 | ddF_x += 2; | |
549 | f += ddF_x; | |
550 | _drawPixel(x + x1, y + y1, color, 0); | |
551 | _drawPixel(x - x1, y + y1, color, 0); | |
552 | _drawPixel(x + x1, y - y1, color, 0); | |
553 | _drawPixel(x - x1, y - y1, color, 0); | |
554 | _drawPixel(x + y1, y + x1, color, 0); | |
555 | _drawPixel(x - y1, y + x1, color, 0); | |
556 | _drawPixel(x + y1, y - x1, color, 0); | |
557 | _drawPixel(x - y1, y - x1, color, 0); | |
558 | } | |
559 | disp_deselect(); | |
560 | } | |
561 | ||
562 | //==================================================================== | |
563 | void TFT_fillCircle(int16_t x, int16_t y, int radius, color_t color) { | |
564 | x += dispWin.x1; | |
565 | y += dispWin.y1; | |
566 | ||
567 | _drawFastVLine(x, y-radius, 2*radius+1, color); | |
568 | fillCircleHelper(x, y, radius, 3, 0, color); | |
569 | } | |
570 | ||
571 | ||
572 | // ================ Font and string functions ================================== | |
573 | ||
574 | //-------------------------------------------------------- | |
575 | static int load_file_font(const char * fontfile, int info) | |
576 | { | |
577 | int err = 0; | |
578 | char err_msg[256] = {'\0'}; | |
579 | ||
580 | if (userfont != NULL) { | |
581 | free(userfont); | |
582 | userfont = NULL; | |
583 | } | |
584 | ||
585 | struct stat sb; | |
586 | ||
587 | // Open the file | |
588 | FILE *fhndl = fopen(fontfile, "r"); | |
589 | if (!fhndl) { | |
590 | sprintf(err_msg, "Error opening font file '%s'", fontfile); | |
591 | err = 1; | |
592 | goto exit; | |
593 | } | |
594 | ||
595 | // Get file size | |
596 | if (stat(fontfile, &sb) != 0) { | |
597 | sprintf(err_msg, "Error getting font file size"); | |
598 | err = 2; | |
599 | goto exit; | |
600 | } | |
601 | int fsize = sb.st_size; | |
602 | if (fsize < 30) { | |
603 | sprintf(err_msg, "Error getting font file size"); | |
604 | err = 3; | |
605 | goto exit; | |
606 | } | |
607 | ||
608 | userfont = malloc(fsize+4); | |
609 | if (userfont == NULL) { | |
610 | sprintf(err_msg, "Font memory allocation error"); | |
611 | fclose(fhndl); | |
612 | err = 4; | |
613 | goto exit; | |
614 | } | |
615 | ||
616 | int read = fread(userfont, 1, fsize, fhndl); | |
617 | ||
618 | fclose(fhndl); | |
619 | ||
620 | if (read != fsize) { | |
621 | sprintf(err_msg, "Font read error"); | |
622 | err = 5; | |
623 | goto exit; | |
624 | } | |
625 | ||
626 | userfont[read] = 0; | |
627 | if (strstr((char *)(userfont+read-8), "RPH_font") == NULL) { | |
628 | sprintf(err_msg, "Font ID not found"); | |
629 | err = 6; | |
630 | goto exit; | |
631 | } | |
632 | ||
633 | // Check size | |
634 | int size = 0; | |
635 | int numchar = 0; | |
636 | int width = userfont[0]; | |
637 | int height = userfont[1]; | |
638 | uint8_t first = 255; | |
639 | uint8_t last = 0; | |
640 | //int offst = 0; | |
641 | int pminwidth = 255; | |
642 | int pmaxwidth = 0; | |
643 | ||
644 | if (width != 0) { | |
645 | // Fixed font | |
646 | numchar = userfont[3]; | |
647 | first = userfont[2]; | |
648 | last = first + numchar - 1; | |
649 | size = ((width * height * numchar) / 8) + 4; | |
650 | } | |
651 | else { | |
652 | // Proportional font | |
653 | size = 4; // point at first char data | |
654 | uint8_t charCode; | |
655 | int charwidth; | |
656 | ||
657 | do { | |
658 | charCode = userfont[size]; | |
659 | charwidth = userfont[size+2]; | |
660 | ||
661 | if (charCode != 0xFF) { | |
662 | numchar++; | |
663 | if (charwidth != 0) size += ((((charwidth * userfont[size+3])-1) / 8) + 7); | |
664 | else size += 6; | |
665 | ||
666 | if (info) { | |
667 | if (charwidth > pmaxwidth) pmaxwidth = charwidth; | |
668 | if (charwidth < pminwidth) pminwidth = charwidth; | |
669 | if (charCode < first) first = charCode; | |
670 | if (charCode > last) last = charCode; | |
671 | } | |
672 | } | |
673 | else size++; | |
674 | } while ((size < (read-8)) && (charCode != 0xFF)); | |
675 | } | |
676 | ||
677 | if (size != (read-8)) { | |
678 | sprintf(err_msg, "Font size error: found %d expected %d)", size, (read-8)); | |
679 | err = 7; | |
680 | goto exit; | |
681 | } | |
682 | ||
683 | if (info) { | |
684 | if (width != 0) { | |
685 | printf("Fixed width font:\r\n size: %d width: %d height: %d characters: %d (%d~%d)\n", | |
686 | size, width, height, numchar, first, last); | |
687 | } | |
688 | else { | |
689 | printf("Proportional font:\r\n size: %d width: %d~%d height: %d characters: %d (%d~%d)\n", | |
690 | size, pminwidth, pmaxwidth, height, numchar, first, last); | |
691 | } | |
692 | } | |
693 | ||
694 | exit: | |
695 | if (err) { | |
696 | if (userfont) { | |
697 | free(userfont); | |
698 | userfont = NULL; | |
699 | } | |
700 | if (info) printf("Error: %d [%s]\r\n", err, err_msg); | |
701 | } | |
702 | return err; | |
703 | } | |
704 | ||
705 | ||
706 | // ----------------------------------------------------------------------------------------- | |
707 | // Individual Proportional Font Character Format: | |
708 | // ----------------------------------------------------------------------------------------- | |
709 | // Character Code | |
710 | // yOffset (start Y of visible pixels) | |
711 | // Width (width of the visible pixels) | |
712 | // Height (height of the visible pixels) | |
713 | // xOffset (start X of visible pixels) | |
714 | // xDelta (the distance to move the cursor. Effective width of the character.) | |
715 | // Data[n] | |
716 | // ----------------------------------------------------------------------------------------- | |
717 | ||
718 | //--------------------------------------------------------------------------------------------- | |
719 | // Character drawing rectangle is (0, 0) (xDelta-1, cfont.y_size-1) | |
720 | // Character visible pixels rectangle is (xOffset, yOffset) (xOffset+Width-1, yOffset+Height-1) | |
721 | //--------------------------------------------------------------------------------------------- | |
722 | ||
723 | //---------------------------------- | |
724 | ||
725 | // Set max width & height of the proportional font | |
726 | //----------------------------- | |
727 | static void getMaxWidthHeight() | |
728 | { | |
729 | uint16_t tempPtr = 4; // point at first char data | |
730 | uint8_t cc, cw, ch, cd, cy; | |
731 | ||
732 | cfont.numchars = 0; | |
733 | cfont.max_x_size = 0; | |
734 | ||
735 | cc = cfont.font[tempPtr++]; | |
736 | while (cc != 0xFF) { | |
737 | cfont.numchars++; | |
738 | cy = cfont.font[tempPtr++]; | |
739 | cw = cfont.font[tempPtr++]; | |
740 | ch = cfont.font[tempPtr++]; | |
741 | tempPtr++; | |
742 | cd = cfont.font[tempPtr++]; | |
743 | cy += ch; | |
744 | if (cw > cfont.max_x_size) cfont.max_x_size = cw; | |
745 | if (cd > cfont.max_x_size) cfont.max_x_size = cd; | |
746 | if (ch > cfont.y_size) cfont.y_size = ch; | |
747 | if (cy > cfont.y_size) cfont.y_size = cy; | |
748 | if (cw != 0) { | |
749 | // packed bits | |
750 | tempPtr += (((cw * ch)-1) / 8) + 1; | |
751 | } | |
752 | cc = cfont.font[tempPtr++]; | |
753 | } | |
754 | cfont.size = tempPtr; | |
755 | } | |
756 | ||
757 | // Return the Glyph data for an individual character in the proportional font | |
758 | //------------------------------------ | |
759 | static uint8_t getCharPtr(uint8_t c) { | |
760 | uint16_t tempPtr = 4; // point at first char data | |
761 | ||
762 | do { | |
763 | fontChar.charCode = cfont.font[tempPtr++]; | |
764 | if (fontChar.charCode == 0xFF) return 0; | |
765 | ||
766 | fontChar.adjYOffset = cfont.font[tempPtr++]; | |
767 | fontChar.width = cfont.font[tempPtr++]; | |
768 | fontChar.height = cfont.font[tempPtr++]; | |
769 | fontChar.xOffset = cfont.font[tempPtr++]; | |
770 | fontChar.xOffset = fontChar.xOffset < 0x80 ? fontChar.xOffset : -(0xFF - fontChar.xOffset); | |
771 | fontChar.xDelta = cfont.font[tempPtr++]; | |
772 | ||
773 | if (c != fontChar.charCode && fontChar.charCode != 0xFF) { | |
774 | if (fontChar.width != 0) { | |
775 | // packed bits | |
776 | tempPtr += (((fontChar.width * fontChar.height)-1) / 8) + 1; | |
777 | } | |
778 | } | |
779 | } while ((c != fontChar.charCode) && (fontChar.charCode != 0xFF)); | |
780 | ||
781 | fontChar.dataPtr = tempPtr; | |
782 | if (c == fontChar.charCode) { | |
783 | if (font_forceFixed > 0) { | |
784 | // fix width & offset for forced fixed width | |
785 | fontChar.xDelta = cfont.max_x_size; | |
786 | fontChar.xOffset = (fontChar.xDelta - fontChar.width) / 2; | |
787 | } | |
788 | } | |
789 | else return 0; | |
790 | ||
791 | return 1; | |
792 | } | |
793 | ||
794 | ||
795 | //=================================================== | |
796 | void TFT_setFont(uint8_t font, const char *font_file) | |
797 | { | |
798 | cfont.font = NULL; | |
799 | ||
800 | if (font == FONT_7SEG) { | |
801 | cfont.bitmap = 2; | |
802 | cfont.x_size = 24; | |
803 | cfont.y_size = 6; | |
804 | cfont.offset = 0; | |
805 | cfont.color = _fg; | |
806 | } else { | |
807 | if (font == USER_FONT) { | |
808 | if (load_file_font(font_file, 0) != 0) | |
809 | cfont.font = tft_DefaultFont; | |
810 | else | |
811 | cfont.font = userfont; | |
812 | } else if (font == DEJAVU18_FONT) | |
813 | cfont.font = tft_Dejavu18; | |
814 | else if (font == DEJAVU24_FONT) | |
815 | cfont.font = tft_Dejavu24; | |
816 | else if (font == UBUNTU16_FONT) | |
817 | cfont.font = tft_Ubuntu16; | |
818 | else if (font == COMIC24_FONT) | |
819 | cfont.font = tft_Comic24; | |
820 | else if (font == MINYA24_FONT) | |
821 | cfont.font = tft_minya24; | |
822 | else if (font == TOONEY32_FONT) | |
823 | cfont.font = tft_tooney32; | |
824 | else if (font == SMALL_FONT) | |
825 | cfont.font = tft_SmallFont; | |
826 | else if (font == DEF_SMALL_FONT) | |
827 | cfont.font = tft_def_small; | |
828 | else | |
829 | cfont.font = tft_DefaultFont; | |
830 | ||
831 | cfont.bitmap = 1; | |
832 | cfont.x_size = cfont.font[0]; | |
833 | cfont.y_size = cfont.font[1]; | |
834 | if (cfont.x_size > 0) { | |
835 | cfont.offset = cfont.font[2]; | |
836 | cfont.numchars = cfont.font[3]; | |
837 | cfont.size = cfont.x_size * cfont.y_size * cfont.numchars; | |
838 | } else { | |
839 | cfont.offset = 4; | |
840 | getMaxWidthHeight(); | |
841 | } | |
842 | //_testFont(); | |
843 | } | |
844 | } | |
845 | ||
846 | ||
847 | ||
848 | // ----------------------------------------------------------------------------------------- | |
849 | // Individual Proportional Font Character Format: | |
850 | // ----------------------------------------------------------------------------------------- | |
851 | // Character Code | |
852 | // yOffset (start Y of visible pixels) | |
853 | // Width (width of the visible pixels) | |
854 | // Height (height of the visible pixels) | |
855 | // xOffset (start X of visible pixels) | |
856 | // xDelta (the distance to move the cursor. Effective width of the character.) | |
857 | // Data[n] | |
858 | // ----------------------------------------------------------------------------------------- | |
859 | //--------------------------------------------------------------------------------------------- | |
860 | // Character drawing rectangle is (0, 0) (xDelta-1, cfont.y_size-1) | |
861 | // Character visible pixels rectangle is (xOffset, yOffset) (xOffset+Width-1, yOffset+Height-1) | |
862 | //--------------------------------------------------------------------------------------------- | |
863 | ||
864 | // print non-rotated proportional character | |
865 | // character is already in fontChar | |
866 | //---------------------------------------------- | |
867 | static int printProportionalChar(int x, int y) { | |
868 | uint8_t ch = 0; | |
869 | int i, j, char_width; | |
870 | ||
871 | char_width = ((fontChar.width > fontChar.xDelta) ? fontChar.width : fontChar.xDelta); | |
872 | ||
873 | if ((font_buffered_char) && (!font_transparent)) { | |
874 | int len, bufPos; | |
875 | ||
876 | // === buffer Glyph data for faster sending === | |
877 | len = char_width * cfont.y_size; | |
878 | color_t *color_line = heap_caps_malloc(len*3, MALLOC_CAP_DMA); | |
879 | if (color_line) { | |
880 | // fill with background color | |
881 | for (int n = 0; n < len; n++) { | |
882 | color_line[n] = _bg; | |
883 | } | |
884 | // set character pixels to foreground color | |
885 | uint8_t mask = 0x80; | |
886 | for (j = 0; j < fontChar.height; j++) { | |
887 | for (i = 0; i < fontChar.width; i++) { | |
888 | if (((i + (j*fontChar.width)) % 8) == 0) { | |
889 | mask = 0x80; | |
890 | ch = cfont.font[fontChar.dataPtr++]; | |
891 | } | |
892 | if ((ch & mask) != 0) { | |
893 | // visible pixel | |
894 | bufPos = ((j + fontChar.adjYOffset) * char_width) + (fontChar.xOffset + i); // bufY + bufX | |
895 | color_line[bufPos] = _fg; | |
896 | VncDrawPixel(x + (fontChar.xOffset + i), y + (j + fontChar.adjYOffset), VNC_RGB2COL(_fg.r, _fg.g, _fg.b)); | |
897 | } else { | |
898 | VncDrawPixel(x + (fontChar.xOffset + i), y + (j + fontChar.adjYOffset), VNC_RGB2COL(_bg.r, _bg.g, _bg.b)); | |
899 | } | |
900 | mask >>= 1; | |
901 | } | |
902 | } | |
903 | // send to display in one transaction | |
904 | disp_select(); | |
905 | send_data(x, y, x+char_width-1, y+cfont.y_size-1, len, color_line); | |
906 | disp_deselect(); | |
907 | free(color_line); | |
908 | ||
909 | return char_width; | |
910 | } | |
911 | } | |
912 | ||
913 | int cx, cy; | |
914 | ||
915 | if (!font_transparent) | |
916 | _fillRect(x, y, char_width+1, cfont.y_size, _bg); | |
917 | ||
918 | // draw Glyph | |
919 | uint8_t mask = 0x80; | |
920 | disp_select(); | |
921 | for (j=0; j < fontChar.height; j++) { | |
922 | for (i=0; i < fontChar.width; i++) { | |
923 | if (((i + (j*fontChar.width)) % 8) == 0) { | |
924 | mask = 0x80; | |
925 | ch = cfont.font[fontChar.dataPtr++]; | |
926 | } | |
927 | ||
928 | if ((ch & mask) !=0) { | |
929 | cx = (uint16_t)(x+fontChar.xOffset+i); | |
930 | cy = (uint16_t)(y+j+fontChar.adjYOffset); | |
931 | _drawPixel(cx, cy, _fg, 0); | |
932 | } | |
933 | mask >>= 1; | |
934 | } | |
935 | } | |
936 | disp_deselect(); | |
937 | ||
938 | return char_width; | |
939 | } | |
940 | ||
941 | ||
942 | ||
943 | // non-rotated fixed width character | |
944 | //---------------------------------------------- | |
945 | static void printChar(uint8_t c, int x, int y) { | |
946 | uint8_t i, j, ch, fz, mask; | |
947 | uint16_t k, temp, cx, cy, len; | |
948 | ||
949 | // fz = bytes per char row | |
950 | fz = cfont.x_size/8; | |
951 | if (cfont.x_size % 8) | |
952 | fz++; | |
953 | ||
954 | // get character position in buffer | |
955 | temp = ((c-cfont.offset)*((fz)*cfont.y_size))+4; | |
956 | ||
957 | if ((font_buffered_char) && (!font_transparent)) { | |
958 | // === buffer Glyph data for faster sending === | |
959 | len = cfont.x_size * cfont.y_size; | |
960 | color_t *color_line = heap_caps_malloc(len*3, MALLOC_CAP_DMA); | |
961 | if (color_line) { | |
962 | // fill with background color | |
963 | for (int n = 0; n < len; n++) { | |
964 | color_line[n] = _bg; | |
965 | } | |
966 | // set character pixels to foreground color | |
967 | for (j = 0; j < cfont.y_size; j++) { | |
968 | for (k = 0; k < fz; k++) { | |
969 | ch = cfont.font[temp + k]; | |
970 | mask = 0x80; | |
971 | for (i = 0; i < 8; i++) { | |
972 | if ((ch & mask) !=0) { | |
973 | color_line[(j*cfont.x_size) + (i+(k*8))] = _fg; | |
974 | VncDrawPixel(x+i+(k*8), y+j, VNC_RGB2COL(_fg.r, _fg.g, _fg.b)); | |
975 | } else { | |
976 | VncDrawPixel(x+i+(k*8), y+j, VNC_RGB2COL(_bg.r, _bg.g, _bg.b)); | |
977 | } | |
978 | mask >>= 1; | |
979 | } | |
980 | } | |
981 | temp += (fz); | |
982 | } | |
983 | // send to display in one transaction | |
984 | disp_select(); | |
985 | send_data(x, y, x+cfont.x_size-1, y+cfont.y_size-1, len, color_line); | |
986 | disp_deselect(); | |
987 | free(color_line); | |
988 | ||
989 | return; | |
990 | } | |
991 | } | |
992 | ||
993 | if (!font_transparent) | |
994 | _fillRect(x, y, cfont.x_size, cfont.y_size, _bg); | |
995 | ||
996 | disp_select(); | |
997 | for (j = 0; j < cfont.y_size; j++) { | |
998 | for (k = 0; k < fz; k++) { | |
999 | ch = cfont.font[temp + k]; | |
1000 | mask = 0x80; | |
1001 | for (i = 0; i < 8; i++) { | |
1002 | if ((ch & mask) !=0) { | |
1003 | cx = (uint16_t)(x+i+(k*8)); | |
1004 | cy = (uint16_t)(y+j); | |
1005 | _drawPixel(cx, cy, _fg, 0); | |
1006 | } | |
1007 | mask >>= 1; | |
1008 | } | |
1009 | } | |
1010 | temp += (fz); | |
1011 | } | |
1012 | disp_deselect(); | |
1013 | } | |
1014 | ||
1015 | ||
1016 | ||
1017 | // print rotated proportional character | |
1018 | // character is already in fontChar | |
1019 | //--------------------------------------------------- | |
1020 | static int rotatePropChar(int x, int y, int offset) { | |
1021 | uint8_t ch = 0; | |
1022 | double radian = font_rotate * DEG_TO_RAD; | |
1023 | float cos_radian = cos(radian); | |
1024 | float sin_radian = sin(radian); | |
1025 | ||
1026 | uint8_t mask = 0x80; | |
1027 | disp_select(); | |
1028 | for (int j=0; j < fontChar.height; j++) { | |
1029 | for (int i=0; i < fontChar.width; i++) { | |
1030 | if (((i + (j*fontChar.width)) % 8) == 0) { | |
1031 | mask = 0x80; | |
1032 | ch = cfont.font[fontChar.dataPtr++]; | |
1033 | } | |
1034 | ||
1035 | int newX = (int)(x + (((offset + i) * cos_radian) - ((j+fontChar.adjYOffset)*sin_radian))); | |
1036 | int newY = (int)(y + (((j+fontChar.adjYOffset) * cos_radian) + ((offset + i) * sin_radian))); | |
1037 | ||
1038 | if ((ch & mask) != 0) _drawPixel(newX,newY,_fg, 0); | |
1039 | else if (!font_transparent) _drawPixel(newX,newY,_bg, 0); | |
1040 | ||
1041 | mask >>= 1; | |
1042 | } | |
1043 | } | |
1044 | disp_deselect(); | |
1045 | ||
1046 | return fontChar.xDelta+1; | |
1047 | } | |
1048 | ||
1049 | // rotated fixed width character | |
1050 | //-------------------------------------------------------- | |
1051 | static void rotateChar(uint8_t c, int x, int y, int pos) { | |
1052 | uint8_t i,j,ch,fz,mask; | |
1053 | uint16_t temp; | |
1054 | int newx,newy; | |
1055 | double radian = font_rotate*0.0175; | |
1056 | float cos_radian = cos(radian); | |
1057 | float sin_radian = sin(radian); | |
1058 | int zz; | |
1059 | ||
1060 | if( cfont.x_size < 8 ) fz = cfont.x_size; | |
1061 | else fz = cfont.x_size/8; | |
1062 | temp=((c-cfont.offset)*((fz)*cfont.y_size))+4; | |
1063 | ||
1064 | disp_select(); | |
1065 | for (j=0; j<cfont.y_size; j++) { | |
1066 | for (zz=0; zz<(fz); zz++) { | |
1067 | ch = cfont.font[temp+zz]; | |
1068 | mask = 0x80; | |
1069 | for (i=0; i<8; i++) { | |
1070 | newx=(int)(x+(((i+(zz*8)+(pos*cfont.x_size))*cos_radian)-((j)*sin_radian))); | |
1071 | newy=(int)(y+(((j)*cos_radian)+((i+(zz*8)+(pos*cfont.x_size))*sin_radian))); | |
1072 | ||
1073 | if ((ch & mask) != 0) _drawPixel(newx,newy,_fg, 0); | |
1074 | else if (!font_transparent) _drawPixel(newx,newy,_bg, 0); | |
1075 | mask >>= 1; | |
1076 | } | |
1077 | } | |
1078 | temp+=(fz); | |
1079 | } | |
1080 | disp_deselect(); | |
1081 | // calculate x,y for the next char | |
1082 | TFT_X = (int)(x + ((pos+1) * cfont.x_size * cos_radian)); | |
1083 | TFT_Y = (int)(y + ((pos+1) * cfont.x_size * sin_radian)); | |
1084 | } | |
1085 | ||
1086 | //---------------------- | |
1087 | static int _7seg_width() | |
1088 | { | |
1089 | return (2 * (2 * cfont.y_size + 1)) + cfont.x_size; | |
1090 | } | |
1091 | ||
1092 | //----------------------- | |
1093 | static int _7seg_height() | |
1094 | { | |
1095 | return (3 * (2 * cfont.y_size + 1)) + (2 * cfont.x_size); | |
1096 | } | |
1097 | ||
1098 | // Returns the string width in pixels. | |
1099 | // Useful for positions strings on the screen. | |
1100 | //=============================== | |
1101 | int TFT_getStringWidth(char* str) | |
1102 | { | |
1103 | int strWidth = 0; | |
1104 | ||
1105 | if (cfont.bitmap == 2) strWidth = ((_7seg_width()+2) * strlen(str)) - 2; // 7-segment font | |
1106 | else if (cfont.x_size != 0) strWidth = strlen(str) * cfont.x_size; // fixed width font | |
1107 | else { | |
1108 | // calculate the width of the string of proportional characters | |
1109 | char* tempStrptr = str; | |
1110 | while (*tempStrptr != 0) { | |
1111 | if (getCharPtr(*tempStrptr++)) { | |
1112 | strWidth += (((fontChar.width > fontChar.xDelta) ? fontChar.width : fontChar.xDelta) + 1); | |
1113 | } | |
1114 | } | |
1115 | strWidth--; | |
1116 | } | |
1117 | return strWidth; | |
1118 | } | |
1119 | ||
1120 | //=============================================== | |
1121 | void TFT_clearStringRect(int x, int y, char *str) | |
1122 | { | |
1123 | int w = TFT_getStringWidth(str); | |
1124 | int h = TFT_getfontheight(); | |
1125 | TFT_fillRect(x+dispWin.x1, y+dispWin.y1, w, h, _bg); | |
1126 | } | |
1127 | ||
1128 | //============================================================================== | |
1129 | /** | |
1130 | * bit-encoded bar position of all digits' bcd segments | |
1131 | * | |
1132 | * 6 | |
1133 | * +-----+ | |
1134 | * 3 | . | 2 | |
1135 | * +--5--+ | |
1136 | * 1 | . | 0 | |
1137 | * +--.--+ | |
1138 | * 4 | |
1139 | */ | |
1140 | static const uint16_t font_bcd[] = { | |
1141 | 0x200, // 0010 0000 0000 // - | |
1142 | 0x080, // 0000 1000 0000 // . | |
1143 | 0x06C, // 0100 0110 1100 // /, degree | |
1144 | 0x05f, // 0000 0101 1111, // 0 | |
1145 | 0x005, // 0000 0000 0101, // 1 | |
1146 | 0x076, // 0000 0111 0110, // 2 | |
1147 | 0x075, // 0000 0111 0101, // 3 | |
1148 | 0x02d, // 0000 0010 1101, // 4 | |
1149 | 0x079, // 0000 0111 1001, // 5 | |
1150 | 0x07b, // 0000 0111 1011, // 6 | |
1151 | 0x045, // 0000 0100 0101, // 7 | |
1152 | 0x07f, // 0000 0111 1111, // 8 | |
1153 | 0x07d, // 0000 0111 1101 // 9 | |
1154 | 0x900 // 1001 0000 0000 // : | |
1155 | }; | |
1156 | ||
1157 | //----------------------------------------------------------------------------------------------- | |
1158 | static void barVert(int16_t x, int16_t y, int16_t w, int16_t l, color_t color, color_t outline) { | |
1159 | _fillTriangle(x+1, y+2*w, x+w, y+w+1, x+2*w-1, y+2*w, color); | |
1160 | _fillTriangle(x+1, y+2*w+l+1, x+w, y+3*w+l, x+2*w-1, y+2*w+l+1, color); | |
1161 | _fillRect(x, y+2*w+1, 2*w+1, l, color); | |
1162 | if (cfont.offset) { | |
1163 | _drawTriangle(x+1, y+2*w, x+w, y+w+1, x+2*w-1, y+2*w, outline); | |
1164 | _drawTriangle(x+1, y+2*w+l+1, x+w, y+3*w+l, x+2*w-1, y+2*w+l+1, outline); | |
1165 | _drawRect(x, y+2*w+1, 2*w+1, l, outline); | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | //---------------------------------------------------------------------------------------------- | |
1170 | static void barHor(int16_t x, int16_t y, int16_t w, int16_t l, color_t color, color_t outline) { | |
1171 | _fillTriangle(x+2*w, y+2*w-1, x+w+1, y+w, x+2*w, y+1, color); | |
1172 | _fillTriangle(x+2*w+l+1, y+2*w-1, x+3*w+l, y+w, x+2*w+l+1, y+1, color); | |
1173 | _fillRect(x+2*w+1, y, l, 2*w+1, color); | |
1174 | if (cfont.offset) { | |
1175 | _drawTriangle(x+2*w, y+2*w-1, x+w+1, y+w, x+2*w, y+1, outline); | |
1176 | _drawTriangle(x+2*w+l+1, y+2*w-1, x+3*w+l, y+w, x+2*w+l+1, y+1, outline); | |
1177 | _drawRect(x+2*w+1, y, l, 2*w+1, outline); | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | //-------------------------------------------------------------------------------------------- | |
1182 | static void _draw7seg(int16_t x, int16_t y, int8_t num, int16_t w, int16_t l, color_t color) { | |
1183 | /* TODO: clipping */ | |
1184 | if (num < 0x2D || num > 0x3A) return; | |
1185 | ||
1186 | int16_t c = font_bcd[num-0x2D]; | |
1187 | int16_t d = 2*w+l+1; | |
1188 | ||
1189 | // === Clear unused segments === | |
1190 | if (!(c & 0x001)) barVert(x+d, y+d, w, l, _bg, _bg); | |
1191 | if (!(c & 0x002)) barVert(x, y+d, w, l, _bg, _bg); | |
1192 | if (!(c & 0x004)) barVert(x+d, y, w, l, _bg, _bg); | |
1193 | if (!(c & 0x008)) barVert(x, y, w, l, _bg, _bg); | |
1194 | if (!(c & 0x010)) barHor(x, y+2*d, w, l, _bg, _bg); | |
1195 | if (!(c & 0x020)) barHor(x, y+d, w, l, _bg, _bg); | |
1196 | if (!(c & 0x040)) barHor(x, y, w, l, _bg, _bg); | |
1197 | ||
1198 | if (!(c & 0x080)) { | |
1199 | // low point | |
1200 | _fillRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, _bg); | |
1201 | if (cfont.offset) _drawRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, _bg); | |
1202 | } | |
1203 | if (!(c & 0x100)) { | |
1204 | // down middle point | |
1205 | _fillRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, _bg); | |
1206 | if (cfont.offset) _drawRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, _bg); | |
1207 | } | |
1208 | if (!(c & 0x800)) { | |
1209 | // up middle point | |
1210 | _fillRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, _bg); | |
1211 | if (cfont.offset) _drawRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, _bg); | |
1212 | } | |
1213 | if (!(c & 0x200)) { | |
1214 | // middle, minus | |
1215 | _fillRect(x+2*w+1, y+d, l, 2*w+1, _bg); | |
1216 | if (cfont.offset) _drawRect(x+2*w+1, y+d, l, 2*w+1, _bg); | |
1217 | } | |
1218 | ||
1219 | // === Draw used segments === | |
1220 | if (c & 0x001) barVert(x+d, y+d, w, l, color, cfont.color); // down right | |
1221 | if (c & 0x002) barVert(x, y+d, w, l, color, cfont.color); // down left | |
1222 | if (c & 0x004) barVert(x+d, y, w, l, color, cfont.color); // up right | |
1223 | if (c & 0x008) barVert(x, y, w, l, color, cfont.color); // up left | |
1224 | if (c & 0x010) barHor(x, y+2*d, w, l, color, cfont.color); // down | |
1225 | if (c & 0x020) barHor(x, y+d, w, l, color, cfont.color); // middle | |
1226 | if (c & 0x040) barHor(x, y, w, l, color, cfont.color); // up | |
1227 | ||
1228 | if (c & 0x080) { | |
1229 | // low point | |
1230 | _fillRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, color); | |
1231 | if (cfont.offset) _drawRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, cfont.color); | |
1232 | } | |
1233 | if (c & 0x100) { | |
1234 | // down middle point | |
1235 | _fillRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, color); | |
1236 | if (cfont.offset) _drawRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, cfont.color); | |
1237 | } | |
1238 | if (c & 0x800) { | |
1239 | // up middle point | |
1240 | _fillRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, color); | |
1241 | if (cfont.offset) _drawRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, cfont.color); | |
1242 | } | |
1243 | if (c & 0x200) { | |
1244 | // middle, minus | |
1245 | _fillRect(x+2*w+1, y+d, l, 2*w+1, color); | |
1246 | if (cfont.offset) _drawRect(x+2*w+1, y+d, l, 2*w+1, cfont.color); | |
1247 | } | |
1248 | } | |
1249 | //============================================================================== | |
1250 | ||
1251 | //====================================== | |
1252 | void TFT_print(char *st, int x, int y) { | |
1253 | int stl, i, tmpw, tmph, fh; | |
1254 | uint8_t ch; | |
1255 | ||
1256 | if (cfont.bitmap == 0) return; // wrong font selected | |
1257 | ||
1258 | // ** Rotated strings cannot be aligned | |
1259 | if ((font_rotate != 0) && ((x <= CENTER) || (y <= CENTER))) return; | |
1260 | ||
1261 | if ((x < LASTX) || (font_rotate == 0)) TFT_OFFSET = 0; | |
1262 | ||
1263 | if ((x >= LASTX) && (x < LASTY)) x = TFT_X + (x-LASTX); | |
1264 | else if (x > CENTER) x += dispWin.x1; | |
1265 | ||
1266 | if (y >= LASTY) y = TFT_Y + (y-LASTY); | |
1267 | else if (y > CENTER) y += dispWin.y1; | |
1268 | ||
1269 | // ** Get number of characters in string to print | |
1270 | stl = strlen(st); | |
1271 | ||
1272 | // ** Calculate CENTER, RIGHT or BOTTOM position | |
1273 | tmpw = TFT_getStringWidth(st); // string width in pixels | |
1274 | fh = cfont.y_size; // font height | |
1275 | if ((cfont.x_size != 0) && (cfont.bitmap == 2)) { | |
1276 | // 7-segment font | |
1277 | fh = (3 * (2 * cfont.y_size + 1)) + (2 * cfont.x_size); // 7-seg character height | |
1278 | } | |
1279 | ||
1280 | if (x == RIGHT) x = dispWin.x2 - tmpw + dispWin.x1; | |
1281 | else if (x == CENTER) x = (((dispWin.x2 - dispWin.x1 + 1) - tmpw) / 2) + dispWin.x1; | |
1282 | ||
1283 | if (y == BOTTOM) y = dispWin.y2 - fh + dispWin.y1; | |
1284 | else if (y==CENTER) y = (((dispWin.y2 - dispWin.y1 + 1) - (fh/2)) / 2) + dispWin.y1; | |
1285 | ||
1286 | if (x < dispWin.x1) x = dispWin.x1; | |
1287 | if (y < dispWin.y1) y = dispWin.y1; | |
1288 | if ((x > dispWin.x2) || (y > dispWin.y2)) return; | |
1289 | ||
1290 | TFT_X = x; | |
1291 | TFT_Y = y; | |
1292 | ||
1293 | // ** Adjust y position | |
1294 | tmph = cfont.y_size; // font height | |
1295 | // for non-proportional fonts, char width is the same for all chars | |
1296 | tmpw = cfont.x_size; | |
1297 | if (cfont.x_size != 0) { | |
1298 | if (cfont.bitmap == 2) { // 7-segment font | |
1299 | tmpw = _7seg_width(); // character width | |
1300 | tmph = _7seg_height(); // character height | |
1301 | } | |
1302 | } | |
1303 | else TFT_OFFSET = 0; // fixed font; offset not needed | |
1304 | ||
1305 | if ((TFT_Y + tmph - 1) > dispWin.y2) return; | |
1306 | ||
1307 | int offset = TFT_OFFSET; | |
1308 | ||
1309 | for (i=0; i<stl; i++) { | |
1310 | ch = st[i]; // get string character | |
1311 | ||
1312 | if (ch == 0x0D) { // === '\r', erase to eol ==== | |
1313 | if ((!font_transparent) && (font_rotate==0)) _fillRect(TFT_X, TFT_Y, dispWin.x2+1-TFT_X, tmph, _bg); | |
1314 | } | |
1315 | ||
1316 | else if (ch == 0x0A) { // ==== '\n', new line ==== | |
1317 | if (cfont.bitmap == 1) { | |
1318 | TFT_Y += tmph + font_line_space; | |
1319 | if (TFT_Y > (dispWin.y2-tmph)) break; | |
1320 | TFT_X = dispWin.x1; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | else { // ==== other characters ==== | |
1325 | if (cfont.x_size == 0) { | |
1326 | // for proportional font get character data to 'fontChar' | |
1327 | if (getCharPtr(ch)) tmpw = fontChar.xDelta; | |
1328 | else continue; | |
1329 | } | |
1330 | ||
1331 | // check if character can be displayed in the current line | |
1332 | if ((TFT_X+tmpw) > (dispWin.x2)) { | |
1333 | if (text_wrap == 0) break; | |
1334 | TFT_Y += tmph + font_line_space; | |
1335 | if (TFT_Y > (dispWin.y2-tmph)) break; | |
1336 | TFT_X = dispWin.x1; | |
1337 | } | |
1338 | ||
1339 | // Let's print the character | |
1340 | if (cfont.x_size == 0) { | |
1341 | // == proportional font | |
1342 | if (font_rotate == 0) TFT_X += printProportionalChar(TFT_X, TFT_Y) + 1; | |
1343 | else { | |
1344 | // rotated proportional font | |
1345 | offset += rotatePropChar(x, y, offset); | |
1346 | TFT_OFFSET = offset; | |
1347 | } | |
1348 | } | |
1349 | else { | |
1350 | if (cfont.bitmap == 1) { | |
1351 | // == fixed font | |
1352 | if ((ch < cfont.offset) || ((ch-cfont.offset) > cfont.numchars)) ch = cfont.offset; | |
1353 | if (font_rotate == 0) { | |
1354 | printChar(ch, TFT_X, TFT_Y); | |
1355 | TFT_X += tmpw; | |
1356 | } | |
1357 | else rotateChar(ch, x, y, i); | |
1358 | } | |
1359 | else if (cfont.bitmap == 2) { | |
1360 | // == 7-segment font == | |
1361 | _draw7seg(TFT_X, TFT_Y, ch, cfont.y_size, cfont.x_size, _fg); | |
1362 | TFT_X += (tmpw + 2); | |
1363 | } | |
1364 | } | |
1365 | } | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | ||
1370 | // ================ Service functions ========================================== | |
1371 | ||
1372 | // Change the screen rotation. | |
1373 | // Input: m new rotation value (0 to 3) | |
1374 | //================================= | |
1375 | void TFT_setRotation(uint8_t rot) { | |
1376 | if (rot > 3) { | |
1377 | uint8_t madctl = (rot & 0xF8); // for testing, manually set MADCTL register | |
1378 | if (disp_select() == ESP_OK) { | |
1379 | disp_spi_transfer_cmd_data(TFT_MADCTL, &madctl, 1); | |
1380 | disp_deselect(); | |
1381 | } | |
1382 | } | |
1383 | else { | |
1384 | orientation = rot; | |
1385 | _tft_setRotation(rot); | |
1386 | } | |
1387 | ||
1388 | dispWin.x1 = 0; | |
1389 | dispWin.y1 = 0; | |
1390 | dispWin.x2 = _width-1; | |
1391 | dispWin.y2 = _height-1; | |
1392 | ||
1393 | TFT_fillScreen(_bg); | |
1394 | } | |
1395 | ||
1396 | ||
1397 | // Select gamma curve | |
1398 | // Input: gamma = 0~3 | |
1399 | //================================== | |
1400 | void TFT_setGammaCurve(uint8_t gm) { | |
1401 | uint8_t gamma_curve = 1 << (gm & 0x03); | |
1402 | disp_spi_transfer_cmd_data(TFT_CMD_GAMMASET, &gamma_curve, 1); | |
1403 | } | |
1404 | ||
1405 | ||
1406 | //===================================================================== | |
1407 | void TFT_setclipwin(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) | |
1408 | { | |
1409 | dispWin.x1 = x1; | |
1410 | dispWin.y1 = y1; | |
1411 | dispWin.x2 = x2; | |
1412 | dispWin.y2 = y2; | |
1413 | ||
1414 | if (dispWin.x2 >= _width) dispWin.x2 = _width-1; | |
1415 | if (dispWin.y2 >= _height) dispWin.y2 = _height-1; | |
1416 | if (dispWin.x1 > dispWin.x2) dispWin.x1 = dispWin.x2; | |
1417 | if (dispWin.y1 > dispWin.y2) dispWin.y1 = dispWin.y2; | |
1418 | } | |
1419 | ||
1420 | //===================== | |
1421 | void TFT_resetclipwin() | |
1422 | { | |
1423 | dispWin.x2 = _width-1; | |
1424 | dispWin.y2 = _height-1; | |
1425 | dispWin.x1 = 0; | |
1426 | dispWin.y1 = 0; | |
1427 | } | |
1428 | ||
1429 | //========================================================================== | |
1430 | void set_7seg_font_atrib(uint8_t l, uint8_t w, int outline, color_t color) { | |
1431 | if (cfont.bitmap != 2) return; | |
1432 | ||
1433 | if (l < 6) l = 6; | |
1434 | if (l > 40) l = 40; | |
1435 | if (w < 1) w = 1; | |
1436 | if (w > (l/2)) w = l/2; | |
1437 | if (w > 12) w = 12; | |
1438 | ||
1439 | cfont.x_size = l; | |
1440 | cfont.y_size = w; | |
1441 | cfont.offset = outline; | |
1442 | cfont.color = color; | |
1443 | } | |
1444 | ||
1445 | //========================================== | |
1446 | int TFT_getfontsize(int *width, int* height) | |
1447 | { | |
1448 | if (cfont.bitmap == 1) { | |
1449 | if (cfont.x_size != 0) *width = cfont.x_size; // fixed width font | |
1450 | else *width = cfont.max_x_size; // proportional font | |
1451 | *height = cfont.y_size; | |
1452 | } | |
1453 | else if (cfont.bitmap == 2) { | |
1454 | // 7-segment font | |
1455 | *width = _7seg_width(); | |
1456 | *height = _7seg_height(); | |
1457 | } | |
1458 | else { | |
1459 | *width = 0; | |
1460 | *height = 0; | |
1461 | return 0; | |
1462 | } | |
1463 | return 1; | |
1464 | } | |
1465 | ||
1466 | //===================== | |
1467 | int TFT_getfontheight() | |
1468 | { | |
1469 | if (cfont.bitmap == 1) return cfont.y_size; // Bitmap font | |
1470 | else if (cfont.bitmap == 2) return _7seg_height(); // 7-segment font | |
1471 | return 0; | |
1472 | } | |
1473 | ||
1474 | //==================== | |
1475 | void TFT_saveClipWin() | |
1476 | { | |
1477 | dispWinTemp.x1 = dispWin.x1; | |
1478 | dispWinTemp.y1 = dispWin.y1; | |
1479 | dispWinTemp.x2 = dispWin.x2; | |
1480 | dispWinTemp.y2 = dispWin.y2; | |
1481 | } | |
1482 | ||
1483 | //======================= | |
1484 | void TFT_restoreClipWin() | |
1485 | { | |
1486 | dispWin.x1 = dispWinTemp.x1; | |
1487 | dispWin.y1 = dispWinTemp.y1; | |
1488 | dispWin.x2 = dispWinTemp.x2; | |
1489 | dispWin.y2 = dispWinTemp.y2; | |
1490 | } | |
1491 | ||
1492 | ||
1493 | // ============= Touch panel functions ========================================= | |
1494 | ||
1495 | #if USE_TOUCH == TOUCH_TYPE_XPT2046 | |
1496 | //------------------------------------------------------- | |
1497 | static int tp_get_data_xpt2046(uint8_t type, int samples) | |
1498 | { | |
1499 | if (ts_spi == NULL) return 0; | |
1500 | ||
1501 | int n, result, val = 0; | |
1502 | uint32_t i = 0; | |
1503 | uint32_t vbuf[18]; | |
1504 | uint32_t minval, maxval, dif; | |
1505 | ||
1506 | if (samples < 3) samples = 1; | |
1507 | if (samples > 18) samples = 18; | |
1508 | ||
1509 | // one dummy read | |
1510 | result = touch_get_data(type); | |
1511 | ||
1512 | // read data | |
1513 | while (i < 10) { | |
1514 | minval = 5000; | |
1515 | maxval = 0; | |
1516 | // get values | |
1517 | for (n=0;n<samples;n++) { | |
1518 | result = touch_get_data(type); | |
1519 | if (result < 0) break; | |
1520 | ||
1521 | vbuf[n] = result; | |
1522 | if (result < minval) minval = result; | |
1523 | if (result > maxval) maxval = result; | |
1524 | } | |
1525 | if (result < 0) break; | |
1526 | dif = maxval - minval; | |
1527 | if (dif < 40) break; | |
1528 | i++; | |
1529 | } | |
1530 | if (result < 0) return -1; | |
1531 | ||
1532 | if (samples > 2) { | |
1533 | // remove one min value | |
1534 | for (n = 0; n < samples; n++) { | |
1535 | if (vbuf[n] == minval) { | |
1536 | vbuf[n] = 5000; | |
1537 | break; | |
1538 | } | |
1539 | } | |
1540 | // remove one max value | |
1541 | for (n = 0; n < samples; n++) { | |
1542 | if (vbuf[n] == maxval) { | |
1543 | vbuf[n] = 5000; | |
1544 | break; | |
1545 | } | |
1546 | } | |
1547 | for (n = 0; n < samples; n++) { | |
1548 | if (vbuf[n] < 5000) val += vbuf[n]; | |
1549 | } | |
1550 | val /= (samples-2); | |
1551 | } | |
1552 | else val = vbuf[0]; | |
1553 | ||
1554 | return val; | |
1555 | } | |
1556 | ||
1557 | //----------------------------------------------- | |
1558 | static int TFT_read_touch_xpt2046(int *x, int* y) | |
1559 | { | |
1560 | int res = 0, result = -1; | |
1561 | ||
1562 | if (spi_lobo_device_select(ts_spi, 0) != ESP_OK) | |
1563 | return 0; | |
1564 | ||
1565 | result = tp_get_data_xpt2046(0xB0, 3); // Z; pressure; touch detect | |
1566 | if (result <= 15) goto exit; // Z was 50, but near the origin it's just above 10. | |
1567 | // 26-6-2018 from 10 to 15. | |
1568 | ||
1569 | // touch panel pressed | |
1570 | result = tp_get_data_xpt2046(0xD0, 6); | |
1571 | if (result < 0) goto exit; | |
1572 | *x = result; | |
1573 | ||
1574 | result = tp_get_data_xpt2046(0x90, 6); | |
1575 | if (result < 0) goto exit; | |
1576 | *y = result; | |
1577 | res = 1; | |
1578 | ||
1579 | exit: | |
1580 | spi_lobo_device_deselect(ts_spi); | |
1581 | return res; | |
1582 | } | |
1583 | #endif | |
1584 | ||
1585 | //============================================= | |
1586 | int TFT_read_touch(int *x, int* y, uint8_t raw) | |
1587 | { | |
1588 | *x = 0; | |
1589 | *y = 0; | |
1590 | ||
1591 | if (ts_spi == NULL) | |
1592 | return 0; | |
1593 | #if USE_TOUCH == TOUCH_TYPE_NONE | |
1594 | return 0; | |
1595 | #else | |
1596 | int result = -1; | |
1597 | int X=0, Y=0; | |
1598 | ||
1599 | #if USE_TOUCH == TOUCH_TYPE_XPT2046 | |
1600 | result = TFT_read_touch_xpt2046(&X, &Y); | |
1601 | if (result == 0) | |
1602 | return 0; | |
1603 | #elif USE_TOUCH == TOUCH_TYPE_STMPE610 | |
1604 | uint32_t tp_calx = TP_CALX_STMPE610; | |
1605 | uint32_t tp_caly = TP_CALY_STMPE610; | |
1606 | uint16_t Xx, Yy, Z=0; | |
1607 | result = stmpe610_get_touch(&Xx, &Yy, &Z); | |
1608 | if (result == 0) return 0; | |
1609 | X = Xx; | |
1610 | Y = Yy; | |
1611 | #else | |
1612 | return 0; | |
1613 | #endif | |
1614 | ||
1615 | if (raw) { | |
1616 | *x = X; | |
1617 | *y = Y; | |
1618 | return 1; | |
1619 | } | |
1620 | ||
1621 | // Calibrate the result | |
1622 | int tmp; | |
1623 | int xleft = tp_xleft; //(tp_calx >> 16) & 0x3FFF; | |
1624 | int xright = tp_xright; //tp_calx & 0x3FFF; | |
1625 | int ytop = tp_ytop; //(tp_caly >> 16) & 0x3FFF; | |
1626 | int ybottom = tp_ybottom; //tp_caly & 0x3FFF; | |
1627 | ||
1628 | if (((xright - xleft) <= 0) || ((ytop - ybottom) <= 0)) | |
1629 | return 0; | |
1630 | ||
1631 | #if USE_TOUCH == TOUCH_TYPE_XPT2046 | |
1632 | // printf("Raw %dx%d ", X, Y); | |
1633 | // Received coordinates are always in portrait, origin left bottom. | |
1634 | int width = DEFAULT_TFT_DISPLAY_WIDTH; | |
1635 | int height = DEFAULT_TFT_DISPLAY_HEIGHT; | |
1636 | X = ((X - xleft) * width) / (xright - xleft); | |
1637 | Y = ((Y - ybottom) * height) / (ytop - ybottom); | |
1638 | ||
1639 | if (X < 0) | |
1640 | X = 0; | |
1641 | if (X > width-1) | |
1642 | X = width-1; | |
1643 | if (Y < 0) | |
1644 | Y = 0; | |
1645 | if (Y > height-1) | |
1646 | Y = height-1; | |
1647 | ||
1648 | switch (orientation) { | |
1649 | case PORTRAIT: | |
1650 | Y = height - Y - 1; | |
1651 | break; | |
1652 | case LANDSCAPE: | |
1653 | tmp = X; | |
1654 | X = height - Y - 1; | |
1655 | Y = width - tmp - 1; | |
1656 | break; | |
1657 | case PORTRAIT_FLIP: | |
1658 | X = width - X - 1; | |
1659 | break; | |
1660 | case LANDSCAPE_FLIP: | |
1661 | tmp = X; | |
1662 | X = Y; | |
1663 | Y = tmp; | |
1664 | break; | |
1665 | } | |
1666 | // printf("Cal %dx%d %dx%d XxY %dx%d\n", xleft, ybottom, xright, ytop, X, Y); | |
1667 | #elif USE_TOUCH == TOUCH_TYPE_STMPE610 | |
1668 | int width = _width; | |
1669 | int height = _height; | |
1670 | if (_width > _height) { | |
1671 | width = _height; | |
1672 | height = _width; | |
1673 | } | |
1674 | X = ((X - xleft) * width) / (xright - xleft); | |
1675 | Y = ((Y - ytop) * height) / (ybottom - ytop); | |
1676 | ||
1677 | if (X < 0) X = 0; | |
1678 | if (X > width-1) X = width-1; | |
1679 | if (Y < 0) Y = 0; | |
1680 | if (Y > height-1) Y = height-1; | |
1681 | ||
1682 | switch (orientation) { | |
1683 | case PORTRAIT_FLIP: | |
1684 | X = width - X - 1; | |
1685 | Y = height - Y - 1; | |
1686 | break; | |
1687 | case LANDSCAPE: | |
1688 | tmp = X; | |
1689 | X = Y; | |
1690 | Y = width - tmp -1; | |
1691 | break; | |
1692 | case LANDSCAPE_FLIP: | |
1693 | tmp = X; | |
1694 | X = height - Y -1; | |
1695 | Y = tmp; | |
1696 | break; | |
1697 | } | |
1698 | #endif | |
1699 | *x = X; | |
1700 | *y = Y; | |
1701 | return 1; | |
1702 | #endif | |
1703 | } | |
1704 |