Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

582 rader
18 KiB

  1. /*
  2. * High level EPD functions
  3. * Author: LoBo 06/2017, https://github/loboris
  4. *
  5. */
  6. #ifndef _EPD_H_
  7. #define _EPD_H_
  8. #include <stdlib.h>
  9. #include "EPDspi.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. typedef uint8_t color_t;
  14. typedef struct {
  15. uint16_t x1;
  16. uint16_t y1;
  17. uint16_t x2;
  18. uint16_t y2;
  19. } dispWin_t;
  20. typedef struct {
  21. uint8_t *font;
  22. uint8_t x_size;
  23. uint8_t y_size;
  24. uint8_t offset;
  25. uint16_t numchars;
  26. uint16_t size;
  27. uint8_t max_x_size;
  28. uint8_t bitmap;
  29. color_t color;
  30. } Font_t;
  31. //==========================================================================================
  32. // ==== Global variables ===================================================================
  33. //==========================================================================================
  34. extern uint8_t orientation; // current screen orientation
  35. extern uint16_t font_rotate; // current font font_rotate angle (0~395)
  36. extern uint8_t font_transparent; // if not 0 draw fonts transparent
  37. extern uint8_t font_forceFixed; // if not zero force drawing proportional fonts with fixed width
  38. extern uint8_t font_buffered_char;
  39. extern uint8_t font_line_space; // additional spacing between text lines; added to font height
  40. extern uint8_t font_x_space; // additional spacing between characters in x axis
  41. extern uint8_t text_wrap; // if not 0 wrap long text to the new line, else clip
  42. extern color_t _fg; // current foreground color for fonts
  43. extern color_t _bg; // current background for non transparent fonts
  44. extern dispWin_t dispWin; // display clip window
  45. extern float _angleOffset; // angle offset for arc, polygon and line by angle functions
  46. extern Font_t cfont; // Current font structure
  47. extern uint8_t image_debug;
  48. extern int EPD_X; // X position of the next character after EPD_print() function
  49. extern int EPD_Y; // Y position of the next character after EPD_print() function
  50. // =========================================================================================
  51. // Buffer is created during jpeg decode for sending data
  52. // Total size of the buffer is 2 * (JPG_IMAGE_LINE_BUF_SIZE * 3)
  53. // The size must be multiple of 256 bytes !!
  54. #define JPG_IMAGE_LINE_BUF_SIZE 512
  55. // --- Constants for ellipse function ---
  56. #define EPD_ELLIPSE_UPPER_RIGHT 0x01
  57. #define EPD_ELLIPSE_UPPER_LEFT 0x02
  58. #define EPD_ELLIPSE_LOWER_LEFT 0x04
  59. #define EPD_ELLIPSE_LOWER_RIGHT 0x08
  60. // Constants for Arc function
  61. // number representing the maximum angle (e.g. if 100, then if you pass in start=0 and end=50, you get a half circle)
  62. // this can be changed with setArcParams function at runtime
  63. #define DEFAULT_ARC_ANGLE_MAX 360
  64. // rotational offset in degrees defining position of value 0 (-90 will put it at the top of circle)
  65. // this can be changed with setAngleOffset function at runtime
  66. #define DEFAULT_ANGLE_OFFSET -90
  67. #define PI 3.14159265359
  68. #define MIN_POLIGON_SIDES 3
  69. #define MAX_POLIGON_SIDES 60
  70. // === Color names constants ===
  71. #define EPD_BLACK 15
  72. #define EPD_WHITE 0
  73. // === Color invert constants ===
  74. #define INVERT_ON 1
  75. #define INVERT_OFF 0
  76. // === Screen orientation constants ===
  77. #define LANDSCAPE_0 1
  78. #define LANDSCAPE_180 2
  79. // === Special coordinates constants ===
  80. #define CENTER -9003
  81. #define RIGHT -9004
  82. #define BOTTOM -9004
  83. #define LASTX 7000
  84. #define LASTY 8000
  85. // === Embedded fonts constants ===
  86. #define DEFAULT_FONT 0
  87. #define DEJAVU18_FONT 1
  88. #define DEJAVU24_FONT 2
  89. #define UBUNTU16_FONT 3
  90. #define COMIC24_FONT 4
  91. #define MINYA24_FONT 5
  92. #define TOONEY32_FONT 6
  93. #define SMALL_FONT 7
  94. #define FONT_7SEG 8
  95. #define USER_FONT 9 // font will be read from file
  96. // ===== PUBLIC FUNCTIONS =========================================================================
  97. /*
  98. * Draw pixel at given x,y coordinates
  99. *
  100. * Params:
  101. * x: horizontal position
  102. * y: vertical position
  103. * color: pixel color
  104. */
  105. //------------------------------------------------------
  106. void EPD_drawPixel(int16_t x, int16_t y, color_t color);
  107. /*
  108. * Read pixel color value from display GRAM at given x,y coordinates
  109. *
  110. * Params:
  111. * x: horizontal position
  112. * y: vertical position
  113. *
  114. * Returns:
  115. * pixel color at x,y
  116. */
  117. //------------------------------------------
  118. color_t EPD_readPixel(int16_t x, int16_t y);
  119. /*
  120. * Draw vertical line at given x,y coordinates
  121. *
  122. * Params:
  123. * x: horizontal start position
  124. * y: vertical start position
  125. * h: line height in pixels
  126. * color: line color
  127. */
  128. //---------------------------------------------------------------------
  129. void EPD_drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color);
  130. /*
  131. * Draw horizontal line at given x,y coordinates
  132. *
  133. * Params:
  134. * x: horizontal start position
  135. * y: vertical start position
  136. * w: line width in pixels
  137. * color: line color
  138. */
  139. //---------------------------------------------------------------------
  140. void EPD_drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color);
  141. /*
  142. * Draw line on screen
  143. *
  144. * Params:
  145. * x0: horizontal start position
  146. * y0: vertical start position
  147. * x1: horizontal end position
  148. * y1: vertical end position
  149. * color: line color
  150. */
  151. //-------------------------------------------------------------------------------
  152. void EPD_drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t color);
  153. /*
  154. * Draw line on screen from (x,y) point at given angle
  155. * Line drawing angle starts at lower right quadrant of the screen and is offseted by
  156. * '_angleOffset' global variable (default: -90 degrees)
  157. *
  158. * Params:
  159. * x: horizontal start position
  160. * y: vertical start position
  161. * start: start offset from (x,y)
  162. * len: length of the line
  163. * angle: line angle in degrees
  164. * color: line color
  165. */
  166. //-----------------------------------------------------------------------------------------------------------
  167. void EPD_drawLineByAngle(uint16_t x, uint16_t y, uint16_t start, uint16_t len, uint16_t angle, color_t color);
  168. /*
  169. * Fill given rectangular screen region with color
  170. *
  171. * Params:
  172. * x: horizontal rect start position
  173. * y: vertical rect start position
  174. * w: rectangle width
  175. * h: rectangle height
  176. * color: fill color
  177. */
  178. //---------------------------------------------------------------------------
  179. void EPD_fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color);
  180. /*
  181. * Draw rectangle on screen
  182. *
  183. * Params:
  184. * x: horizontal rect start position
  185. * y: vertical rect start position
  186. * w: rectangle width
  187. * h: rectangle height
  188. * color: rect line color
  189. */
  190. //------------------------------------------------------------------------------
  191. void EPD_drawRect(uint16_t x1,uint16_t y1,uint16_t w,uint16_t h, color_t color);
  192. /*
  193. * Draw rectangle with rounded corners on screen
  194. *
  195. * Params:
  196. * x: horizontal rect start position
  197. * y: vertical rect start position
  198. * w: rectangle width
  199. * h: rectangle height
  200. * r: corner radius
  201. * color: rectangle color
  202. */
  203. //----------------------------------------------------------------------------------------------
  204. void EPD_drawRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color);
  205. /*
  206. * Fill given rectangular screen region with rounded corners with color
  207. *
  208. * Params:
  209. * x: horizontal rect start position
  210. * y: vertical rect start position
  211. * w: rectangle width
  212. * h: rectangle height
  213. * r: corner radius
  214. * color: fill color
  215. */
  216. //----------------------------------------------------------------------------------------------
  217. void EPD_fillRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color);
  218. /*
  219. * Fill the whole screen with color
  220. *
  221. * Params:
  222. * color: fill color
  223. */
  224. //--------------------------------
  225. void EPD_fillScreen(color_t color);
  226. /*
  227. * Fill the current clip window with color
  228. *
  229. * Params:
  230. * color: fill color
  231. */
  232. //---------------------------------
  233. void EPD_fillWindow(color_t color);
  234. /*
  235. * Draw triangle on screen
  236. *
  237. * Params:
  238. * x0: first triangle point x position
  239. * y0: first triangle point y position
  240. * x0: second triangle point x position
  241. * y0: second triangle point y position
  242. * x0: third triangle point x position
  243. * y0: third triangle point y position
  244. * color: triangle color
  245. */
  246. //-----------------------------------------------------------------------------------------------------------------
  247. void EPD_drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color);
  248. /*
  249. * Fill triangular screen region with color
  250. *
  251. * Params:
  252. * x0: first triangle point x position
  253. * y0: first triangle point y position
  254. * x0: second triangle point x position
  255. * y0: second triangle point y position
  256. * x0: third triangle point x position
  257. * y0: third triangle point y position
  258. * color: fill color
  259. */
  260. //-----------------------------------------------------------------------------------------------------------------
  261. void EPD_fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color);
  262. /*
  263. * Draw circle on screen
  264. *
  265. * Params:
  266. * x: circle center x position
  267. * y: circle center x position
  268. * r: circle radius
  269. * color: circle color
  270. */
  271. //-------------------------------------------------------------------
  272. void EPD_drawCircle(int16_t x, int16_t y, int radius, color_t color);
  273. /*
  274. * Fill circle on screen with color
  275. *
  276. * Params:
  277. * x: circle center x position
  278. * y: circle center x position
  279. * r: circle radius
  280. * color: circle fill color
  281. */
  282. //-------------------------------------------------------------------
  283. void EPD_fillCircle(int16_t x, int16_t y, int radius, color_t color);
  284. /*
  285. * Draw ellipse on screen
  286. *
  287. * Params:
  288. * x0: ellipse center x position
  289. * y0: ellipse center x position
  290. * rx: ellipse horizontal radius
  291. * ry: ellipse vertical radius
  292. * option: drawing options, multiple options can be combined
  293. 1 (TFT_ELLIPSE_UPPER_RIGHT) draw upper right corner
  294. 2 (TFT_ELLIPSE_UPPER_LEFT) draw upper left corner
  295. 4 (TFT_ELLIPSE_LOWER_LEFT) draw lower left corner
  296. 8 (TFT_ELLIPSE_LOWER_RIGHT) draw lower right corner
  297. to draw the whole ellipse use option value 15 (1 | 2 | 4 | 8)
  298. *
  299. * color: circle color
  300. */
  301. //------------------------------------------------------------------------------------------------------
  302. void EPD_drawEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option);
  303. /*
  304. * Fill elliptical region on screen
  305. *
  306. * Params:
  307. * x0: ellipse center x position
  308. * y0: ellipse center x position
  309. * rx: ellipse horizontal radius
  310. * ry: ellipse vertical radius
  311. * option: drawing options, multiple options can be combined
  312. 1 (TFT_ELLIPSE_UPPER_RIGHT) fill upper right corner
  313. 2 (TFT_ELLIPSE_UPPER_LEFT) fill upper left corner
  314. 4 (TFT_ELLIPSE_LOWER_LEFT) fill lower left corner
  315. 8 (TFT_ELLIPSE_LOWER_RIGHT) fill lower right corner
  316. to fill the whole ellipse use option value 15 (1 | 2 | 4 | 8)
  317. *
  318. * color: fill color
  319. */
  320. //------------------------------------------------------------------------------------------------------
  321. void EPD_fillEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option);
  322. /*
  323. * Draw circle arc on screen
  324. * Arc drawing angle starts at lower right quadrant of the screen and is offseted by
  325. * '_angleOffset' global variable (default: -90 degrees)
  326. *
  327. * Params:
  328. * cx: arc center X position
  329. * cy: arc center Y position
  330. * th: thickness of the drawn arc
  331. * ry: arc vertical radius
  332. * start: arc start angle in degrees
  333. * end: arc end angle in degrees
  334. * color: arc outline color
  335. * fillcolor: arc fill color
  336. */
  337. //----------------------------------------------------------------------------------------------------------------------------
  338. void EPD_drawArc(uint16_t cx, uint16_t cy, uint16_t r, uint16_t th, float start, float end, color_t color, color_t fillcolor);
  339. /*
  340. * Draw polygon on screen
  341. *
  342. * Params:
  343. * cx: polygon center X position
  344. * cy: arc center Y position
  345. * sides: number of polygon sides; MAX_POLIGON_SIDES ~ MAX_POLIGON_SIDES (3 ~ 60)
  346. * diameter: diameter of the circle inside which the polygon is drawn
  347. * color: polygon outline color
  348. * fill: polygon fill color; if same as color, polygon is not filled
  349. * deg: polygon rotation angle; 0 ~ 360
  350. * th: thickness of the polygon outline
  351. */
  352. //--------------------------------------------------------------------------------------------------------------
  353. void EPD_drawPolygon(int cx, int cy, int sides, int diameter, color_t color, color_t fill, int deg, uint8_t th);
  354. //--------------------------------------------------------------------------------------
  355. //void EPD_drawStar(int cx, int cy, int diameter, color_t color, bool fill, float factor);
  356. /*
  357. * Set the font used for writing the text to display.
  358. *
  359. * ------------------------------------------------------------------------------------
  360. * For 7 segment font only characters 0,1,2,3,4,5,6,7,8,9, . , - , : , / are available.
  361. * Character ‘/‘ draws the degree sign.
  362. * ------------------------------------------------------------------------------------
  363. *
  364. * Params:
  365. * font: font number; use defined font names
  366. * font_file: pointer to font file name; NULL for embeded fonts
  367. */
  368. //----------------------------------------------------
  369. void EPD_setFont(uint8_t font, const char *font_file);
  370. /*
  371. * Returns current font height & width in pixels.
  372. *
  373. * Params:
  374. * width: pointer to returned font width
  375. * height: pointer to returned font height
  376. */
  377. //-------------------------------------------
  378. int EPD_getfontsize(int *width, int* height);
  379. /*
  380. * Returns current font height in pixels.
  381. *
  382. */
  383. //----------------------
  384. int EPD_getfontheight();
  385. /*
  386. * Write text to display.
  387. *
  388. * Rotation of the displayed text depends on 'font_rotate' variable (0~360)
  389. * if 'font_transparent' variable is set to 1, no background pixels will be printed
  390. *
  391. * If the text does not fit the screen width it will be clipped (if text_wrap=0),
  392. * or continued on next line (if text_wrap=1)
  393. *
  394. * Two special characters are allowed in strings:
  395. * ‘\r’ CR (0x0D), clears the display to EOL
  396. * ‘\n’ LF (ox0A), continues to the new line, x=0
  397. *
  398. * Params:
  399. * st: pointer to null terminated string to be printed
  400. * x: horizontal position of the upper left point in pixels
  401. * Special values can be entered:
  402. * CENTER, centers the text
  403. * RIGHT, right justifies the text
  404. * LASTX, continues from last X position; offset can be used: LASTX+n
  405. * y: vertical position of the upper left point in pixels
  406. * Special values can be entered:
  407. * CENTER, centers the text
  408. * BOTTOM, bottom justifies the text
  409. * LASTY, continues from last Y position; offset can be used: LASTY+n
  410. *
  411. */
  412. //-------------------------------------
  413. void EPD_print(char *st, int x, int y);
  414. /*
  415. * Set atributes for 7 segment vector font
  416. * == 7 segment font must be the current font to this function to have effect ==
  417. *
  418. * Params:
  419. * l: 6~40; distance between bars in pixels
  420. * w: 1~12, max l/2; bar width in pixels
  421. * outline: draw font outline if set to 1
  422. * color: font outline color, only used if outline=1
  423. *
  424. */
  425. //-------------------------------------------------------------------------
  426. void set_7seg_font_atrib(uint8_t l, uint8_t w, int outline, color_t color);
  427. /*
  428. * Sets the clipping area coordinates.
  429. * All writing to screen is clipped to that area.
  430. * Starting x & y in all functions will be adjusted to the clipping area.
  431. *
  432. * Params:
  433. * x1,y1: upper left point of the clipping area
  434. * x2,y2: bottom right point of the clipping area
  435. *
  436. */
  437. //----------------------------------------------------------------------
  438. void EPD_setclipwin(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
  439. /*
  440. * Resets the clipping area to full screen (0,0),(_wodth,_height)
  441. *
  442. */
  443. //----------------------
  444. void EPD_resetclipwin();
  445. /*
  446. * Save current clipping area to temporary variable
  447. *
  448. */
  449. //---------------------
  450. void EPD_saveClipWin();
  451. /*
  452. * Restore current clipping area from temporary variable
  453. *
  454. */
  455. //------------------------
  456. void EPD_restoreClipWin();
  457. /*
  458. * returns the string width in pixels.
  459. * Useful for positions strings on the screen.
  460. */
  461. //--------------------------------
  462. int EPD_getStringWidth(char* str);
  463. /*
  464. * Fills the rectangle occupied by string with current background color
  465. */
  466. void EPD_clearStringRect(int x, int y, char *str);
  467. /*
  468. * Frames the rectangle occupied by string with current foreground color
  469. */
  470. void EPD_frameStringRect(int x, int y, char *str);
  471. /*
  472. * Compile font c source file to .fnt file
  473. * which can be used in EPD_setFont() function to select external font
  474. * Created file have the same name as source file and extension .fnt
  475. *
  476. * Params:
  477. * fontfile: pointer to c source font file name; must have .c extension
  478. * dbg: if set to 1, prints debug information
  479. *
  480. * Returns:
  481. * 0 on success
  482. * err no on error
  483. *
  484. */
  485. //------------------------------------------------
  486. int compile_font_file(char *fontfile, uint8_t dbg);
  487. /*
  488. * Get all font's characters to buffer
  489. */
  490. void getFontCharacters(uint8_t *buf);
  491. /*
  492. * Decodes and displays JPG image. RGB colors are converted to 4-bit Gray scale
  493. * Limits:
  494. * Baseline only. Progressive and Lossless JPEG format are not supported.
  495. * Image size: Up to 65520 x 65520 pixels
  496. * Color space: YCbCr three components only. Gray scale image is not supported.
  497. * Sampling factor: 4:4:4, 4:2:2 or 4:2:0.
  498. *
  499. * Params:
  500. * x: image left position; constants CENTER & RIGHT can be used; negative value is accepted
  501. * y: image top position; constants CENTER & BOTTOM can be used; negative value is accepted
  502. * scale: image scale factor: 0~3; if scale>0, image is scaled by factor 1/(2^scale) (1/2, 1/4 or 1/8)
  503. * fname: pointer to the name of the file from which the image will be read
  504. * if set to NULL, image will be read from memory buffer pointed to by 'buf'
  505. * buf: pointer to the memory buffer from which the image will be read; used if fname=NULL
  506. * size: size of the memory buffer from which the image will be read; used if fname=NULL & buf!=NULL
  507. *
  508. */
  509. int EPD_jpg_image(int x, int y, uint8_t scale, char *fname, uint8_t *buf, int size);
  510. #ifdef __cplusplus
  511. }
  512. #endif
  513. #endif