2 * High level TFT functions
3 * Author: LoBo 04/2017, https://github/loboris
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
54 extern Font tft_cfont; // Current font structure
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
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 // =========================================================================================
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
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
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
83 #define PI 3.14159265359
85 #define MIN_POLIGON_SIDES 3
86 #define MAX_POLIGON_SIDES 60
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;
109 // === Color invert constants ===
113 // === Special coordinates constants ===
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
130 #define DEF_SMALL_FONT 8
132 #define USER_FONT 10 // font will be read from file
136 // ===== PUBLIC FUNCTIONS =========================================================================
140 * Draw pixel at given x,y coordinates
143 * x: horizontal position
144 * y: vertical position
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
150 //-------------------------------------------------------------------
151 void TFT_drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel);
154 * Read pixel color value from display GRAM at given x,y coordinates
157 * x: horizontal position
158 * y: vertical position
163 //------------------------------------------
164 color_t TFT_readPixel(int16_t x, int16_t y);
167 * Draw vertical line at given x,y coordinates
170 * x: horizontal start position
171 * y: vertical start position
172 * h: line height in pixels
175 //---------------------------------------------------------------------
176 void TFT_drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color);
179 * Draw horizontal line at given x,y coordinates
182 * x: horizontal start position
183 * y: vertical start position
184 * w: line width in pixels
187 //---------------------------------------------------------------------
188 void TFT_drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color);
191 * Draw line on screen
194 * x0: horizontal start position
195 * y0: vertical start position
196 * x1: horizontal end position
197 * y1: vertical end position
200 //-------------------------------------------------------------------------------
201 void TFT_drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t color);
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)
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
217 //-----------------------------------------------------------------------------------------------------------
218 void TFT_drawLineByAngle(uint16_t x, uint16_t y, uint16_t start, uint16_t len, uint16_t angle, color_t color);
221 * Fill given rectangular screen region with color
224 * x: horizontal rect start position
225 * y: vertical rect start position
227 * h: rectangle height
230 //---------------------------------------------------------------------------
231 void TFT_fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color);
234 * Draw rectangle on screen
237 * x: horizontal rect start position
238 * y: vertical rect start position
240 * h: rectangle height
241 * color: rect line color
243 //------------------------------------------------------------------------------
244 void TFT_drawRect(uint16_t x1,uint16_t y1,uint16_t w,uint16_t h, color_t color);
247 * Draw rectangle with rounded corners on screen
250 * x: horizontal rect start position
251 * y: vertical rect start position
253 * h: rectangle height
255 * color: rectangle color
257 //----------------------------------------------------------------------------------------------
258 void TFT_drawRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color);
261 * Fill given rectangular screen region with rounded corners with color
264 * x: horizontal rect start position
265 * y: vertical rect start position
267 * h: rectangle height
271 //----------------------------------------------------------------------------------------------
272 void TFT_fillRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color);
275 * Fill the whole screen with color
280 //--------------------------------
281 void TFT_fillScreen(color_t color);
284 * Fill the current clip window with color
289 //---------------------------------
290 void TFT_fillWindow(color_t color);
293 * Draw triangle on screen
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
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);
308 * Fill triangular screen region with color
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
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);
323 * Draw circle on screen
326 * x: circle center x position
327 * y: circle center x position
329 * color: circle color
331 //-------------------------------------------------------------------
332 void TFT_drawCircle(int16_t x, int16_t y, int radius, color_t color);
335 * Fill circle on screen with color
338 * x: circle center x position
339 * y: circle center x position
341 * color: circle fill color
343 //-------------------------------------------------------------------
344 void TFT_fillCircle(int16_t x, int16_t y, int radius, color_t color);
347 * Draw ellipse on screen
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)
361 * color: circle color
363 //------------------------------------------------------------------------------------------------------
364 void TFT_drawEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option);
367 * Fill elliptical region on screen
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)
383 //------------------------------------------------------------------------------------------------------
384 void TFT_fillEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option);
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)
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
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);
407 * Draw polygon on screen
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
419 //--------------------------------------------------------------------------------------------------------------
420 void TFT_drawPolygon(int cx, int cy, int sides, int diameter, color_t color, color_t fill, int deg, uint8_t th);
423 //--------------------------------------------------------------------------------------
424 //void TFT_drawStar(int cx, int cy, int diameter, color_t color, bool fill, float factor);
428 * Set the font used for writing the text to display.
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 * ------------------------------------------------------------------------------------
436 * font: font number; use defined font names
437 * font_file: pointer to font file name; NULL for embeded fonts
439 //----------------------------------------------------
440 void TFT_setFont(uint8_t font, const char *font_file);
443 * Returns current font height & width in pixels.
446 * width: pointer to returned font width
447 * height: pointer to returned font height
449 //-------------------------------------------
450 int TFT_getfontsize(int *width, int *height);
454 * Returns current font height in pixels.
457 //----------------------
458 int TFT_getfontheight();
461 * Write text to display.
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
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)
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
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
487 //-------------------------------------------
488 void TFT_print(const char *st, int x, int y);
491 * Set attributes for 7 segment vector font
492 * == 7 segment font must be the current font to this function to have effect ==
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
501 //-------------------------------------------------------------------------
502 void set_7seg_font_atrib(uint8_t l, uint8_t w, int outline, color_t color);
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.
510 * x1,y1: upper left point of the clipping area
511 * x2,y2: bottom right point of the clipping area
514 //----------------------------------------------------------------------
515 void TFT_setclipwin(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
518 * Resets the clipping area to full screen (0,0),(_wodth,tft_height)
521 //----------------------
522 void TFT_resetclipwin();
525 * Save current clipping area to temporary variable
528 //---------------------
529 void TFT_saveClipWin();
532 * Restore current clipping area from temporary variable
535 //------------------------
536 void TFT_restoreClipWin();
539 * Set the screen rotation
540 * Also resets the clip window and clears the screen with current background color
543 * rot: 0~3; screen rotation; use defined rotation constants:
544 * PORTRAIT, LANDSCAPE, PORTRAIT_FLIP, LANDSCAPE_FLIP
547 //--------------------------------
548 void TFT_setRotation(uint8_t rot);
551 * Set inverted/normal colors
554 * mode: 0 or 1; use defined constants: INVERT_ON or INVERT_OFF
557 //-----------------------------------------
558 void TFT_invertDisplay(const uint8_t mode);
563 * gamma: gama curve, values 0~3
565 //=================================
566 void TFT_setGammaCurve(uint8_t gm);
569 * Compare two color structures
570 * Returns 0 if equal, 1 if not equal
573 * c1, c2: colors to be compared
575 //---------------------------------------------
576 int TFT_compare_colors(color_t c1, color_t c2);
579 * returns the string width in pixels.
580 * Useful for positions strings on the screen.
582 //--------------------------------------
583 int TFT_getStringWidth(const char *str);
587 * Fills the rectangle occupied by string with current background color
589 //------------------------------------------------------
590 void TFT_clearStringRect(int x, int y, const char *str);
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.
596 * The color structure that is returned by HSBtoRGB encodes the value of a color as R, G & B component
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
605 //----------------------------------------------------------
606 color_t HSBtoRGB(float _hue, float _sat, float _brightness);
609 * Decodes and displays JPG image
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.
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
626 //-----------------------------------------------------------------------------------------
627 void TFT_jpg_image(int x, int y, uint8_t scale, const char *fname, uint8_t *buf, int size);
630 * Decodes and displays BMP image
631 * Only uncompressed RGB 24-bit with no color space information BMP images can be displayed
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
643 //-------------------------------------------------------------------------------------------
644 int TFT_bmp_image(int x, int y, uint8_t scale, const char *fname, uint8_t *imgbuf, int size);
647 * Get the touch panel coordinates.
648 * The coordinates are adjusted to screen tft_orientation if raw=0
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
656 * 0 if touch panel is not touched; x=y=0
657 * 1 if touch panel is touched; x&y are the valid coordinates
659 //----------------------------------------------
660 int TFT_read_touch(int *x, int* y, uint8_t raw);
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
669 * fontfile: pointer to c source font file name; must have .c extension
670 * dbg: if set to 1, prints debug information
677 //-------------------------------------------------------
678 int compile_font_file(const char *fontfile, uint8_t dbg);
681 * Get all font's characters to buffer
683 void getFontCharacters(uint8_t *buf);