Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / m5stack-tft / repo / components / tft / tft.h
1 /*
2  * High level TFT functions
3  * Author:  LoBo 04/2017, https://github/loboris
4  * 
5  */
6
7 #ifndef _TFT_H_
8 #define _TFT_H_
9
10 #include <stdlib.h>
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 #include "tftspi.h"
17
18 typedef struct {
19         uint16_t        x1;
20         uint16_t        y1;
21         uint16_t        x2;
22         uint16_t        y2;
23 } dispWin_t;
24
25 typedef struct {
26         uint8_t         *font;
27         uint8_t         x_size;
28         uint8_t         y_size;
29         uint8_t     offset;
30         uint16_t        numchars;
31     uint16_t    size;
32         uint8_t         max_x_size;
33     uint8_t     bitmap;
34         color_t     color;
35 } Font;
36
37
38 //==========================================================================================
39 // ==== Global variables ===================================================================
40 //==========================================================================================
41 extern uint8_t   tft_orientation;               // current screen tft_orientation
42 extern uint16_t  tft_font_rotate;       // current font tft_font_rotate angle (0~395)
43 extern uint8_t   tft_font_transparent;  // if not 0 draw fonts transparent
44 extern uint8_t   tft_font_forceFixed;   // if not zero force drawing proportional fonts with fixed width
45 extern uint8_t   tft_font_buffered_char;
46 extern uint8_t   tft_font_line_space;   // additional spacing between text lines; added to font height
47 extern uint8_t   tft_text_wrap;         // if not 0 wrap long text to the new line, else clip
48 extern color_t   tft_fg;                // current foreground color for fonts
49 extern color_t   tft_bg;                // current background for non transparent fonts
50 extern dispWin_t tft_dispWin;                   // display clip window
51 extern float      tft_angleOffset;              // angle offset for arc, polygon and line by angle functions
52 extern uint8_t    tft_image_debug;              // print debug messages during image decode if set to 1
53
54 extern Font tft_cfont;                                  // Current font structure
55
56 extern int      tft_x;                                  // X position of the next character after TFT_print() function
57 extern int      tft_y;                                  // Y position of the next character after TFT_print() function
58
59 extern uint32_t tft_tp_calx;                    // touch screen X calibration constant
60 extern uint32_t tft_tp_caly;                    // touch screen Y calibration constant
61 // =========================================================================================
62
63
64 // Buffer is created during jpeg decode for sending data
65 // Total size of the buffer is  2 * (JPG_IMAGE_LINE_BUF_SIZE * 3)
66 // The size must be multiple of 256 bytes !!
67 #define JPG_IMAGE_LINE_BUF_SIZE 512
68
69 // --- Constants for ellipse function ---
70 #define TFT_ELLIPSE_UPPER_RIGHT 0x01
71 #define TFT_ELLIPSE_UPPER_LEFT  0x02
72 #define TFT_ELLIPSE_LOWER_LEFT  0x04
73 #define TFT_ELLIPSE_LOWER_RIGHT 0x08
74
75 // Constants for Arc function
76 // number representing the maximum angle (e.g. if 100, then if you pass in start=0 and end=50, you get a half circle)
77 // this can be changed with setArcParams function at runtime
78 #define DEFAULT_ARC_ANGLE_MAX 360
79 // rotational offset in degrees defining position of value 0 (-90 will put it at the top of circle)
80 // this can be changed with setAngleOffset function at runtime
81 #define DEFAULT_ANGLE_OFFSET -90
82
83 #define PI 3.14159265359
84
85 #define MIN_POLIGON_SIDES       3
86 #define MAX_POLIGON_SIDES       60
87
88 // === Color names constants ===
89 extern const color_t TFT_BLACK;
90 extern const color_t TFT_NAVY;
91 extern const color_t TFT_DARKGREEN;
92 extern const color_t TFT_DARKCYAN;
93 extern const color_t TFT_MAROON;
94 extern const color_t TFT_PURPLE;
95 extern const color_t TFT_OLIVE;
96 extern const color_t TFT_LIGHTGREY;
97 extern const color_t TFT_DARKGREY;
98 extern const color_t TFT_BLUE;
99 extern const color_t TFT_GREEN;
100 extern const color_t TFT_CYAN;
101 extern const color_t TFT_RED;
102 extern const color_t TFT_MAGENTA;
103 extern const color_t TFT_YELLOW;
104 extern const color_t TFT_WHITE;
105 extern const color_t TFT_ORANGE;
106 extern const color_t TFT_GREENYELLOW;
107 extern const color_t TFT_PINK;
108
109 // === Color invert constants ===
110 #define INVERT_ON               1
111 #define INVERT_OFF              0
112
113 // === Special coordinates constants ===
114 #define CENTER  -9003
115 #define RIGHT   -9004
116 #define BOTTOM  -9004
117
118 #define LASTX   7000
119 #define LASTY   8000
120
121 // === Embedded fonts constants ===
122 #define DEFAULT_FONT    0
123 #define DEJAVU18_FONT   1
124 #define DEJAVU24_FONT   2
125 #define UBUNTU16_FONT   3
126 #define COMIC24_FONT    4
127 #define MINYA24_FONT    5
128 #define TOONEY32_FONT   6
129 #define SMALL_FONT              7
130 #define DEF_SMALL_FONT  8
131 #define FONT_7SEG               9
132 #define USER_FONT               10  // font will be read from file
133
134
135
136 // ===== PUBLIC FUNCTIONS =========================================================================
137
138
139 /*
140  * Draw pixel at given x,y coordinates
141  * 
142  * Params:
143  *       x: horizontal position
144  *       y: vertical position
145  *   color: pixel color
146  *     sel: if not 0 activate CS before and deactivat after sending pixel data to display
147  *          when sending multiple pixels it is faster to activate the CS first,
148  *          send all pixels an deactivate CS after all pixela was sent
149 */
150 //-------------------------------------------------------------------
151 void TFT_drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel);
152
153 /*
154  * Read pixel color value from display GRAM at given x,y coordinates
155  * 
156  * Params:
157  *       x: horizontal position
158  *       y: vertical position
159  * 
160  * Returns:
161  *      pixel color at x,y
162 */
163 //------------------------------------------
164 color_t TFT_readPixel(int16_t x, int16_t y);
165
166 /*
167  * Draw vertical line at given x,y coordinates
168  * 
169  * Params:
170  *       x: horizontal start position
171  *       y: vertical start position
172  *       h: line height in pixels
173  *   color: line color
174 */
175 //---------------------------------------------------------------------
176 void TFT_drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color);
177
178 /*
179  * Draw horizontal line at given x,y coordinates
180  * 
181  * Params:
182  *       x: horizontal start position
183  *       y: vertical start position
184  *       w: line width in pixels
185  *   color: line color
186 */
187 //---------------------------------------------------------------------
188 void TFT_drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color);
189
190 /*
191  * Draw line on screen
192  * 
193  * Params:
194  *       x0: horizontal start position
195  *       y0: vertical start position
196  *       x1: horizontal end position
197  *       y1: vertical end position
198  *   color: line color
199 */
200 //-------------------------------------------------------------------------------
201 void TFT_drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t color);
202
203
204 /*
205  * Draw line on screen from (x,y) point at given angle
206  * Line drawing angle starts at lower right quadrant of the screen and is offseted by
207  * 'tft_angleOffset' global variable (default: -90 degrees)
208  *
209  * Params:
210  *       x: horizontal start position
211  *       y: vertical start position
212  *   start: start offset from (x,y)
213  *     len: length of the line
214  *   angle: line angle in degrees
215  *   color: line color
216 */
217 //-----------------------------------------------------------------------------------------------------------
218 void TFT_drawLineByAngle(uint16_t x, uint16_t y, uint16_t start, uint16_t len, uint16_t angle, color_t color);
219
220 /*
221  * Fill given rectangular screen region with color
222  * 
223  * Params:
224  *       x: horizontal rect start position
225  *       y: vertical rect start position
226  *       w: rectangle width
227  *       h: rectangle height
228  *   color: fill color
229 */
230 //---------------------------------------------------------------------------
231 void TFT_fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color);
232
233 /*
234  * Draw rectangle on screen
235  * 
236  * Params:
237  *       x: horizontal rect start position
238  *       y: vertical rect start position
239  *       w: rectangle width
240  *       h: rectangle height
241  *   color: rect line color
242 */
243 //------------------------------------------------------------------------------
244 void TFT_drawRect(uint16_t x1,uint16_t y1,uint16_t w,uint16_t h, color_t color);
245
246 /*
247  * Draw rectangle with rounded corners on screen
248  * 
249  * Params:
250  *       x: horizontal rect start position
251  *       y: vertical rect start position
252  *       w: rectangle width
253  *       h: rectangle height
254  *       r: corner radius
255  *   color: rectangle color
256 */
257 //----------------------------------------------------------------------------------------------
258 void TFT_drawRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color);
259
260 /*
261  * Fill given rectangular screen region with rounded corners with color
262  * 
263  * Params:
264  *       x: horizontal rect start position
265  *       y: vertical rect start position
266  *       w: rectangle width
267  *       h: rectangle height
268  *       r: corner radius
269  *   color: fill color
270 */
271 //----------------------------------------------------------------------------------------------
272 void TFT_fillRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color);
273
274 /*
275  * Fill the whole screen with color
276  * 
277  * Params:
278  *   color: fill color
279 */
280 //--------------------------------
281 void TFT_fillScreen(color_t color);
282
283 /*
284  * Fill the current clip window with color
285  *
286  * Params:
287  *   color: fill color
288 */
289 //---------------------------------
290 void TFT_fillWindow(color_t color);
291
292 /*
293  * Draw triangle on screen
294  * 
295  * Params:
296  *       x0: first triangle point x position
297  *       y0: first triangle point y position
298  *       x0: second triangle point x position
299  *       y0: second triangle point y position
300  *       x0: third triangle point x position
301  *       y0: third triangle point y position
302  *   color: triangle color
303 */
304 //-----------------------------------------------------------------------------------------------------------------
305 void TFT_drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color);
306
307 /*
308  * Fill triangular screen region with color
309  * 
310  * Params:
311  *       x0: first triangle point x position
312  *       y0: first triangle point y position
313  *       x0: second triangle point x position
314  *       y0: second triangle point y position
315  *       x0: third triangle point x position
316  *       y0: third triangle point y position
317  *   color: fill color
318 */
319 //-----------------------------------------------------------------------------------------------------------------
320 void TFT_fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color);
321
322 /*
323  * Draw circle on screen
324  * 
325  * Params:
326  *       x: circle center x position
327  *       y: circle center x position
328  *       r: circle radius
329  *   color: circle color
330 */
331 //-------------------------------------------------------------------
332 void TFT_drawCircle(int16_t x, int16_t y, int radius, color_t color);
333
334 /*
335  * Fill circle on screen with color
336  * 
337  * Params:
338  *       x: circle center x position
339  *       y: circle center x position
340  *       r: circle radius
341  *   color: circle fill color
342 */
343 //-------------------------------------------------------------------
344 void TFT_fillCircle(int16_t x, int16_t y, int radius, color_t color);
345
346 /*
347  * Draw ellipse on screen
348  * 
349  * Params:
350  *       x0: ellipse center x position
351  *       y0: ellipse center x position
352  *       rx: ellipse horizontal radius
353  *       ry: ellipse vertical radius
354  *   option: drawing options, multiple options can be combined
355                 1 (TFT_ELLIPSE_UPPER_RIGHT) draw upper right corner
356                 2 (TFT_ELLIPSE_UPPER_LEFT)  draw upper left corner
357                 4 (TFT_ELLIPSE_LOWER_LEFT)  draw lower left corner
358                 8 (TFT_ELLIPSE_LOWER_RIGHT) draw lower right corner
359              to draw the whole ellipse use option value 15 (1 | 2 | 4 | 8)
360  * 
361  *   color: circle color
362 */
363 //------------------------------------------------------------------------------------------------------
364 void TFT_drawEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option);
365
366 /*
367  * Fill elliptical region on screen
368  * 
369  * Params:
370  *       x0: ellipse center x position
371  *       y0: ellipse center x position
372  *       rx: ellipse horizontal radius
373  *       ry: ellipse vertical radius
374  *   option: drawing options, multiple options can be combined
375                 1 (TFT_ELLIPSE_UPPER_RIGHT) fill upper right corner
376                 2 (TFT_ELLIPSE_UPPER_LEFT)  fill upper left corner
377                 4 (TFT_ELLIPSE_LOWER_LEFT)  fill lower left corner
378                 8 (TFT_ELLIPSE_LOWER_RIGHT) fill lower right corner
379              to fill the whole ellipse use option value 15 (1 | 2 | 4 | 8)
380  * 
381  *   color: fill color
382 */
383 //------------------------------------------------------------------------------------------------------
384 void TFT_fillEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option);
385
386
387 /*
388  * Draw circle arc on screen
389  * Arc drawing angle starts at lower right quadrant of the screen and is offseted by
390  * 'tft_angleOffset' global variable (default: -90 degrees)
391  *
392  * Params:
393  *        cx: arc center X position
394  *        cy: arc center Y position
395  *        th: thickness of the drawn arc
396  *        ry: arc vertical radius
397  *     start: arc start angle in degrees
398  *       end: arc end angle in degrees
399  *     color: arc outline color
400  * fillcolor: arc fill color
401 */
402 //----------------------------------------------------------------------------------------------------------------------------
403 void TFT_drawArc(uint16_t cx, uint16_t cy, uint16_t r, uint16_t th, float start, float end, color_t color, color_t fillcolor);
404
405
406 /*
407  * Draw polygon on screen
408  *
409  * Params:
410  *        cx: polygon center X position
411  *        cy: arc center Y position
412  *     sides: number of polygon sides; MAX_POLIGON_SIDES ~ MAX_POLIGON_SIDES (3 ~ 60)
413  *  diameter: diameter of the circle inside which the polygon is drawn
414  *     color: polygon outline color
415  *      fill: polygon fill color; if same as color, polygon is not filled
416  *       deg: polygon rotation angle; 0 ~ 360
417  *        th: thickness of the polygon outline
418 */
419 //--------------------------------------------------------------------------------------------------------------
420 void TFT_drawPolygon(int cx, int cy, int sides, int diameter, color_t color, color_t fill, int deg, uint8_t th);
421
422
423 //--------------------------------------------------------------------------------------
424 //void TFT_drawStar(int cx, int cy, int diameter, color_t color, bool fill, float factor);
425
426
427 /*
428  * Set the font used for writing the text to display.
429  *
430  * ------------------------------------------------------------------------------------
431  * For 7 segment font only characters 0,1,2,3,4,5,6,7,8,9, . , - , : , / are available.
432  *   Character â€˜/‘ draws the degree sign.
433  * ------------------------------------------------------------------------------------
434  *
435  * Params:
436  *                       font: font number; use defined font names
437  *              font_file: pointer to font file name; NULL for embeded fonts
438  */
439 //----------------------------------------------------
440 void TFT_setFont(uint8_t font, const char *font_file);
441
442 /*
443  * Returns current font height & width in pixels.
444  *
445  * Params:
446  *               width: pointer to returned font width
447  *              height: pointer to returned font height
448  */
449 //-------------------------------------------
450 int TFT_getfontsize(int *width, int *height);
451
452
453 /*
454  * Returns current font height in pixels.
455  *
456  */
457 //----------------------
458 int TFT_getfontheight();
459
460 /*
461  * Write text to display.
462  *
463  * Rotation of the displayed text depends on 'tft_font_rotate' variable (0~360)
464  * if 'tft_font_transparent' variable is set to 1, no background pixels will be printed
465  *
466  * If the text does not fit the screen width it will be clipped (if tft_text_wrap=0),
467  * or continued on next line (if tft_text_wrap=1)
468  *
469  * Two special characters are allowed in strings:
470  *              â€˜\r’ CR (0x0D), clears the display to EOL
471  *              â€˜\n’ LF (ox0A), continues to the new line, x=0
472  *
473  * Params:
474  *         st:  pointer to null terminated string to be printed
475  *              x:      horizontal position of the upper left point in pixels
476  *                              Special values can be entered:
477  *                                      CENTER, centers the text
478  *                                      RIGHT, right justifies the text
479  *                                      LASTX, continues from last X position; offset can be used: LASTX+n
480  *              y: vertical position of the upper left point in pixels
481  *                              Special values can be entered:
482  *                                      CENTER, centers the text
483  *                                      BOTTOM, bottom justifies the text
484  *                                      LASTY, continues from last Y position; offset can be used: LASTY+n
485  *
486  */
487 //-------------------------------------------
488 void TFT_print(const char *st, int x, int y);
489
490 /*
491  * Set attributes for 7 segment vector font
492  * == 7 segment font must be the current font to this function to have effect ==
493  *
494  * Params:
495  *                 l:   6~40; distance between bars in pixels
496  *                 w:   1~12, max l/2;  bar width in pixels
497  *   outline:   draw font outline if set to 1
498  *         color:       font outline color, only used if outline=1
499  *
500  */
501 //-------------------------------------------------------------------------
502 void set_7seg_font_atrib(uint8_t l, uint8_t w, int outline, color_t color);
503
504 /*
505  * Sets the clipping area coordinates.
506  * All writing to screen is clipped to that area.
507  * Starting x & y in all functions will be adjusted to the clipping area.
508  *
509  * Params:
510  *              x1,y1:  upper left point of the clipping area
511  *              x2,y2:  bottom right point of the clipping area
512  *
513  */
514 //----------------------------------------------------------------------
515 void TFT_setclipwin(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
516
517 /*
518  * Resets the clipping area to full screen (0,0),(_wodth,tft_height)
519  *
520  */
521 //----------------------
522 void TFT_resetclipwin();
523
524 /*
525  * Save current clipping area to temporary variable
526  *
527  */
528 //---------------------
529 void TFT_saveClipWin();
530
531 /*
532  * Restore current clipping area from temporary variable
533  *
534  */
535 //------------------------
536 void TFT_restoreClipWin();
537
538 /*
539  * Set the screen rotation
540  * Also resets the clip window and clears the screen with current background color
541  *
542  * Params:
543  *              rot:    0~3; screen rotation; use defined rotation constants:
544  *                              PORTRAIT, LANDSCAPE, PORTRAIT_FLIP, LANDSCAPE_FLIP
545  *
546  */
547 //--------------------------------
548 void TFT_setRotation(uint8_t rot);
549
550 /*
551  * Set inverted/normal colors
552  *
553  * Params:
554  *              mode:   0 or 1; use defined constants: INVERT_ON or INVERT_OFF
555  *
556  */
557 //-----------------------------------------
558 void TFT_invertDisplay(const uint8_t mode);
559
560 /*
561  * Select gamma curve
562  * Params:
563  *      gamma: gama curve, values 0~3
564  */
565 //=================================
566 void TFT_setGammaCurve(uint8_t gm);
567
568 /*
569  * Compare two color structures
570  * Returns 0 if equal, 1 if not equal
571  *
572  * Params:
573  *              c1, c2: colors to be compared
574  */
575 //---------------------------------------------
576 int TFT_compare_colors(color_t c1, color_t c2);
577
578 /*
579  * returns the string width in pixels.
580  * Useful for positions strings on the screen.
581  */
582 //--------------------------------------
583 int TFT_getStringWidth(const char *str);
584
585
586 /*
587  * Fills the rectangle occupied by string with current background color
588  */
589 //------------------------------------------------------
590 void TFT_clearStringRect(int x, int y, const char *str);
591
592 /*
593  * Converts the components of a color, as specified by the HSB model,
594  * to an equivalent set of values for the default RGB model.
595
596  * The color structure that is returned by HSBtoRGB encodes the value of a color as R, G & B component
597  *
598  * Params:
599  *                 _hue:        float;  any number, the floor of this number is subtracted from it to create a fraction between 0 and 1.
600  *                                                      This fractional number is then multiplied by 360 to produce the hue angle in the HSB color model.
601  *                 _sat:        float; 0 ~ 1.0
602  *      _brightness:    float; 0 ~ 1.0
603  *
604 */
605 //----------------------------------------------------------
606 color_t HSBtoRGB(float _hue, float _sat, float _brightness);
607
608 /*
609  * Decodes and displays JPG image
610  * Limits:
611  *              Baseline only. Progressive and Lossless JPEG format are not supported.
612  *              Image size: Up to 65520 x 65520 pixels
613  *              Color space: YCbCr three components only. Gray scale image is not supported.
614  *              Sampling factor: 4:4:4, 4:2:2 or 4:2:0.
615  *
616  * Params:
617  *       x: image left position; constants CENTER & RIGHT can be used; negative value is accepted
618  *       y: image top position;  constants CENTER & BOTTOM can be used; negative value is accepted
619  *   scale: image scale factor: 0~3; if scale>0, image is scaled by factor 1/(2^scale) (1/2, 1/4 or 1/8)
620  *   fname: pointer to the name of the file from which the image will be read
621  *              if set to NULL, image will be read from memory buffer pointed to by 'buf'
622  *     buf: pointer to the memory buffer from which the image will be read; used if fname=NULL
623  *    size: size of the memory buffer from which the image will be read; used if fname=NULL & buf!=NULL
624  *
625  */
626 //-----------------------------------------------------------------------------------------
627 void TFT_jpg_image(int x, int y, uint8_t scale, const char *fname, uint8_t *buf, int size);
628
629 /*
630  * Decodes and displays BMP image
631  * Only uncompressed RGB 24-bit with no color space information BMP images can be displayed
632  *
633  * Params:
634  *       x: image left position; constants CENTER & RIGHT can be used; negative value is accepted
635  *       y: image top position;  constants CENTER & BOTTOM can be used; negative value is accepted
636  *   scale: image scale factor: 0~7; if scale>0, image is scaled by factor 1/(scale+1)
637  *   fname: pointer to the name of the file from which the image will be read
638  *              if set to NULL, image will be read from memory buffer pointed to by 'imgbuf'
639  *  imgbuf: pointer to the memory buffer from which the image will be read; used if fname=NULL
640  *    size: size of the memory buffer from which the image will be read; used if fname=NULL & imgbuf!=NULL
641  *
642  */
643 //-------------------------------------------------------------------------------------------
644 int TFT_bmp_image(int x, int y, uint8_t scale, const char *fname, uint8_t *imgbuf, int size);
645
646 /*
647  * Get the touch panel coordinates.
648  * The coordinates are adjusted to screen tft_orientation if raw=0
649  *
650  * Params:
651  *              x: pointer to X coordinate
652  *              y: pointer to Y coordinate
653  *        raw: if 0 returns calibrated screen coordinates; if 1 returns raw touch controller coordinates
654  *
655  * Returns:
656  *              0 if touch panel is not touched; x=y=0
657  *              1 if touch panel is touched; x&y are the valid coordinates
658  */
659 //----------------------------------------------
660 int TFT_read_touch(int *x, int* y, uint8_t raw);
661
662
663 /*
664  * Compile font c source file to .fnt file
665  * which can be used in TFT_setFont() function to select external font
666  * Created file have the same name as source file and extension .fnt
667  *
668  * Params:
669  *              fontfile: pointer to c source font file name; must have .c extension
670  *                       dbg: if set to 1, prints debug information
671  *
672  * Returns:
673  *              0 on success
674  *              err no on error
675  *
676  */
677 //-------------------------------------------------------
678 int compile_font_file(const char *fontfile, uint8_t dbg);
679
680 /*
681  * Get all font's characters to buffer
682  */
683 void getFontCharacters(uint8_t *buf);
684
685 #endif
686
687 #ifdef __cplusplus
688 }
689 #endif