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.
 
 
 

2345 rader
66 KiB

  1. /* EPD library
  2. *
  3. * Author: LoBo (loboris@gmail.com, loboris.github)
  4. *
  5. * Module supporting SPI ePaper displays
  6. */
  7. #include <stdio.h>
  8. #include <errno.h>
  9. #include <sys/stat.h>
  10. #include <string.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "esp_system.h"
  14. #include "time.h"
  15. #include <math.h>
  16. #include "rom/tjpgd.h"
  17. #include "esp_heap_alloc_caps.h"
  18. #include "EPD.h"
  19. #include "EPDspi.h"
  20. #include "rom/tjpgd.h"
  21. #define DEG_TO_RAD 0.01745329252
  22. #define RAD_TO_DEG 57.295779513
  23. #define deg_to_rad 0.01745329252 + 3.14159265359
  24. #define swap(a, b) { int16_t t = a; a = b; b = t; }
  25. #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
  26. #if !defined(max)
  27. #define max(A,B) ( (A) > (B) ? (A):(B))
  28. #endif
  29. #if !defined(min)
  30. #define min(A,B) ( (A) < (B) ? (A):(B))
  31. #endif
  32. //==========================================================================================
  33. // ==== Global variables ===================================================================
  34. //==========================================================================================
  35. uint8_t orientation; // current screen orientation
  36. uint16_t font_rotate; // current font font_rotate angle (0~395)
  37. uint8_t font_transparent; // if not 0 draw fonts transparent
  38. uint8_t font_forceFixed; // if not zero force drawing proportional fonts with fixed width
  39. uint8_t font_buffered_char;
  40. uint8_t font_line_space; // additional spacing between text lines; added to font height
  41. uint8_t text_wrap; // if not 0 wrap long text to the new line, else clip
  42. color_t _fg; // current foreground color for fonts
  43. color_t _bg; // current background for non transparent fonts
  44. dispWin_t dispWin; // display clip window
  45. float _angleOffset; // angle offset for arc, polygon and line by angle functions
  46. Font_t cfont; // Current font structure
  47. uint8_t image_debug;
  48. int EPD_X; // X position of the next character after EPD_print() function
  49. int EPD_Y; // Y position of the next character after EPD_print() function
  50. // =========================================================================================
  51. // Embedded fonts
  52. extern uint8_t tft_SmallFont[];
  53. extern uint8_t tft_DefaultFont[];
  54. extern uint8_t tft_Dejavu18[];
  55. extern uint8_t tft_Dejavu24[];
  56. extern uint8_t tft_Ubuntu16[];
  57. extern uint8_t tft_Comic24[];
  58. extern uint8_t tft_minya24[];
  59. extern uint8_t tft_tooney32[];
  60. // ==============================================================
  61. // ==== Set default values of global variables ==================
  62. uint8_t orientation = LANDSCAPE_0; // screen orientation
  63. uint16_t font_rotate = 0; // font rotation
  64. uint8_t font_transparent = 0;
  65. uint8_t font_forceFixed = 0;
  66. uint8_t text_wrap = 0; // character wrapping to new line
  67. color_t _fg = EPD_BLACK;
  68. color_t _bg = EPD_WHITE;
  69. float _angleOffset = DEFAULT_ANGLE_OFFSET;
  70. int EPD_X = 0;
  71. int EPD_Y = 0;
  72. dispWin_t dispWin = {
  73. .x1 = 0,
  74. .y1 = 0,
  75. .x2 = EPD_DISPLAY_WIDTH-1,
  76. .y2 = EPD_DISPLAY_HEIGHT-1,
  77. };
  78. Font_t cfont = {
  79. .font = tft_DefaultFont,
  80. .x_size = 0,
  81. .y_size = 0x0B,
  82. .offset = 0,
  83. .numchars = 95,
  84. .bitmap = 1,
  85. };
  86. uint8_t font_line_space = 0;
  87. uint8_t image_debug = 0;
  88. // ==============================================================
  89. typedef struct {
  90. uint8_t charCode;
  91. int adjYOffset;
  92. int width;
  93. int height;
  94. int xOffset;
  95. int xDelta;
  96. uint16_t dataPtr;
  97. } propFont;
  98. static dispWin_t dispWinTemp;
  99. static uint8_t *userfont = NULL;
  100. static int EPD_OFFSET = 0;
  101. static propFont fontChar;
  102. static float _arcAngleMax = DEFAULT_ARC_ANGLE_MAX;
  103. // ==============================================================================================================
  104. //----------------------------------------------
  105. static void drawPixel(int x, int y, uint8_t val)
  106. {
  107. if (orientation == LANDSCAPE_180) {
  108. x = _width - x - 1;
  109. y = _height - y - 1;
  110. }
  111. if (_gs) {
  112. val &= 0x0F;
  113. //if (gs_drawBuff[(y * _width) + x] != val) {
  114. gs_drawBuff[(y * _width) + x] = val;
  115. gs_used_shades |= (1<<val);
  116. //}
  117. }
  118. else {
  119. val &= 0x01;
  120. uint8_t buf_val = drawBuff[(x * (_height >> 3)) + (y>>3)];
  121. uint8_t new_val = buf_val;
  122. if (val) new_val &= (0x80 >> (y % 8)) ^ 0xFF;
  123. else new_val |= (0x80 >> (y % 8));
  124. //if (new_val != buf_val) drawBuff[(x * (_height>>3)) + (y>>3)] = new_val;
  125. drawBuff[(x * (_height>>3)) + (y>>3)] = new_val;
  126. }
  127. }
  128. //-------------------------------------------------------------------------
  129. static void EPD_pushColorRep(int x1, int y1, int x2, int y2, color_t color)
  130. {
  131. if (_gs == 0) color &= 0x01;
  132. else color &= 0x0F;
  133. for (int y=y1; y<=y2; y++) {
  134. for (int x = x1; x<=x2; x++){
  135. drawPixel(x, y, color);
  136. }
  137. }
  138. }
  139. /*
  140. //---------------------------------------------------------------------------------------
  141. static void copyBuff(int x1, int y1, int x2, int y2, uint8_t *src_buf, uint8_t *dest_buf)
  142. {
  143. if
  144. uint8_t buf_val1 = (src_buf[(x1 * (EPD_DISPLAY_HEIGHT>>3)) + (y1>>3)]) ;
  145. uint8_t val = 0x80 >> (y1 % 8);
  146. if (val) buf_val &= (0x80 >> (y % 8)) ^ 0xFF;
  147. else buf_val |= (0x80 >> (y % 8));
  148. drawBuff[(x * (EPD_DISPLAY_HEIGHT>>3)) + (y>>3)] = buf_val;
  149. }
  150. */
  151. // ==============================================================================================================
  152. // =========================================================================
  153. // ** All drawings are clipped to 'dispWin' **
  154. // ** All x,y coordinates in public functions are relative to clip window **
  155. // =========== : Public functions
  156. // ----------- : Local functions
  157. // =========================================================================
  158. // Compare two colors; return 0 if equal
  159. //============================================
  160. int EPD_compare_colors(color_t c1, color_t c2)
  161. {
  162. if (c1 != c2) return 1;
  163. return 0;
  164. }
  165. // draw color pixel on screen
  166. //-----------------------------------------------------------
  167. static void _drawPixel(int16_t x, int16_t y, color_t color) {
  168. if ((x < dispWin.x1) || (y < dispWin.y1) || (x > dispWin.x2) || (y > dispWin.y2)) return;
  169. drawPixel(x, y, color);
  170. }
  171. //=======================================================
  172. void EPD_drawPixel(int16_t x, int16_t y, color_t color) {
  173. _drawPixel(x+dispWin.x1, y+dispWin.y1, color);
  174. }
  175. //--------------------------------------------------------------------------
  176. static void _drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color) {
  177. // clipping
  178. if ((x < dispWin.x1) || (x > dispWin.x2) || (y > dispWin.y2)) return;
  179. if (y < dispWin.y1) {
  180. h -= (dispWin.y1 - y);
  181. y = dispWin.y1;
  182. }
  183. if (h < 0) h = 0;
  184. if ((y + h) > (dispWin.y2+1)) h = dispWin.y2 - y + 1;
  185. if (h == 0) h = 1;
  186. EPD_pushColorRep(x, y, x, y+h-1, color);
  187. }
  188. //--------------------------------------------------------------------------
  189. static void _drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color) {
  190. // clipping
  191. if ((y < dispWin.y1) || (x > dispWin.x2) || (y > dispWin.y2)) return;
  192. if (x < dispWin.x1) {
  193. w -= (dispWin.x1 - x);
  194. x = dispWin.x1;
  195. }
  196. if (w < 0) w = 0;
  197. if ((x + w) > (dispWin.x2+1)) w = dispWin.x2 - x + 1;
  198. if (w == 0) w = 1;
  199. EPD_pushColorRep(x, y, x+w-1, y, color);
  200. }
  201. //======================================================================
  202. void EPD_drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color) {
  203. _drawFastVLine(x+dispWin.x1, y+dispWin.y1, h, color);
  204. }
  205. //======================================================================
  206. void EPD_drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color) {
  207. _drawFastHLine(x+dispWin.x1, y+dispWin.y1, w, color);
  208. }
  209. // Bresenham's algorithm - thx wikipedia - speed enhanced by Bodmer this uses
  210. // the eficient FastH/V Line draw routine for segments of 2 pixels or more
  211. //----------------------------------------------------------------------------------
  212. static void _drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t color)
  213. {
  214. if (x0 == x1) {
  215. if (y0 <= y1) _drawFastVLine(x0, y0, y1-y0, color);
  216. else _drawFastVLine(x0, y1, y0-y1, color);
  217. return;
  218. }
  219. if (y0 == y1) {
  220. if (x0 <= x1) _drawFastHLine(x0, y0, x1-x0, color);
  221. else _drawFastHLine(x1, y0, x0-x1, color);
  222. return;
  223. }
  224. int steep = 0;
  225. if (abs(y1 - y0) > abs(x1 - x0)) steep = 1;
  226. if (steep) {
  227. swap(x0, y0);
  228. swap(x1, y1);
  229. }
  230. if (x0 > x1) {
  231. swap(x0, x1);
  232. swap(y0, y1);
  233. }
  234. int16_t dx = x1 - x0, dy = abs(y1 - y0);;
  235. int16_t err = dx >> 1, ystep = -1, xs = x0, dlen = 0;
  236. if (y0 < y1) ystep = 1;
  237. // Split into steep and not steep for FastH/V separation
  238. if (steep) {
  239. for (; x0 <= x1; x0++) {
  240. dlen++;
  241. err -= dy;
  242. if (err < 0) {
  243. err += dx;
  244. if (dlen == 1) _drawPixel(y0, xs, color);
  245. else _drawFastVLine(y0, xs, dlen, color);
  246. dlen = 0; y0 += ystep; xs = x0 + 1;
  247. }
  248. }
  249. if (dlen) _drawFastVLine(y0, xs, dlen, color);
  250. }
  251. else
  252. {
  253. for (; x0 <= x1; x0++) {
  254. dlen++;
  255. err -= dy;
  256. if (err < 0) {
  257. err += dx;
  258. if (dlen == 1) _drawPixel(xs, y0, color);
  259. else _drawFastHLine(xs, y0, dlen, color);
  260. dlen = 0; y0 += ystep; xs = x0 + 1;
  261. }
  262. }
  263. if (dlen) _drawFastHLine(xs, y0, dlen, color);
  264. }
  265. }
  266. //==============================================================================
  267. void EPD_drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t color)
  268. {
  269. _drawLine(x0+dispWin.x1, y0+dispWin.y1, x1+dispWin.x1, y1+dispWin.y1, color);
  270. }
  271. // fill a rectangle
  272. //--------------------------------------------------------------------------------
  273. static void _fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color) {
  274. // clipping
  275. if ((x >= dispWin.x2) || (y > dispWin.y2)) return;
  276. if (x < dispWin.x1) {
  277. w -= (dispWin.x1 - x);
  278. x = dispWin.x1;
  279. }
  280. if (y < dispWin.y1) {
  281. h -= (dispWin.y1 - y);
  282. y = dispWin.y1;
  283. }
  284. if (w < 0) w = 0;
  285. if (h < 0) h = 0;
  286. if ((x + w) > (dispWin.x2+1)) w = dispWin.x2 - x + 1;
  287. if ((y + h) > (dispWin.y2+1)) h = dispWin.y2 - y + 1;
  288. if (w == 0) w = 1;
  289. if (h == 0) h = 1;
  290. EPD_pushColorRep(x, y, x+w-1, y+h-1, color);
  291. }
  292. //============================================================================
  293. void EPD_fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color) {
  294. _fillRect(x+dispWin.x1, y+dispWin.y1, w, h, color);
  295. }
  296. //==================================
  297. void EPD_fillScreen(color_t color) {
  298. color &= 0x0F;
  299. memset(disp_buffer, ((color&1) ? 0 : 0xFF), _width * (_height/8));
  300. memset(gs_disp_buffer, color, _width * _height);
  301. gs_used_shades = 0;
  302. }
  303. //==================================
  304. void EPD_fillWindow(color_t color) {
  305. EPD_pushColorRep(dispWin.x1, dispWin.y1, dispWin.x2, dispWin.y2, 0xFF);
  306. }
  307. // ^^^============= Basics drawing functions ================================^^^
  308. // ================ Graphics drawing functions ==================================
  309. //-----------------------------------------------------------------------------------
  310. static void _drawRect(uint16_t x1,uint16_t y1,uint16_t w,uint16_t h, color_t color) {
  311. _drawFastHLine(x1,y1,w, color);
  312. _drawFastVLine(x1+w-1,y1,h, color);
  313. _drawFastHLine(x1,y1+h-1,w, color);
  314. _drawFastVLine(x1,y1,h, color);
  315. }
  316. //===============================================================================
  317. void EPD_drawRect(uint16_t x1,uint16_t y1,uint16_t w,uint16_t h, color_t color) {
  318. _drawRect(x1+dispWin.x1, y1+dispWin.y1, w, h, color);
  319. }
  320. //-------------------------------------------------------------------------------------------------
  321. static void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, color_t color)
  322. {
  323. int16_t f = 1 - r;
  324. int16_t ddF_x = 1;
  325. int16_t ddF_y = -2 * r;
  326. int16_t x = 0;
  327. int16_t y = r;
  328. while (x < y) {
  329. if (f >= 0) {
  330. y--;
  331. ddF_y += 2;
  332. f += ddF_y;
  333. }
  334. x++;
  335. ddF_x += 2;
  336. f += ddF_x;
  337. if (cornername & 0x4) {
  338. _drawPixel(x0 + x, y0 + y, color);
  339. _drawPixel(x0 + y, y0 + x, color);
  340. }
  341. if (cornername & 0x2) {
  342. _drawPixel(x0 + x, y0 - y, color);
  343. _drawPixel(x0 + y, y0 - x, color);
  344. }
  345. if (cornername & 0x8) {
  346. _drawPixel(x0 - y, y0 + x, color);
  347. _drawPixel(x0 - x, y0 + y, color);
  348. }
  349. if (cornername & 0x1) {
  350. _drawPixel(x0 - y, y0 - x, color);
  351. _drawPixel(x0 - x, y0 - y, color);
  352. }
  353. }
  354. }
  355. // Used to do circles and roundrects
  356. //----------------------------------------------------------------------------------------------------------------
  357. static void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, color_t color)
  358. {
  359. int16_t f = 1 - r;
  360. int16_t ddF_x = 1;
  361. int16_t ddF_y = -2 * r;
  362. int16_t x = 0;
  363. int16_t y = r;
  364. int16_t ylm = x0 - r;
  365. while (x < y) {
  366. if (f >= 0) {
  367. if (cornername & 0x1) _drawFastVLine(x0 + y, y0 - x, 2 * x + 1 + delta, color);
  368. if (cornername & 0x2) _drawFastVLine(x0 - y, y0 - x, 2 * x + 1 + delta, color);
  369. ylm = x0 - y;
  370. y--;
  371. ddF_y += 2;
  372. f += ddF_y;
  373. }
  374. x++;
  375. ddF_x += 2;
  376. f += ddF_x;
  377. if ((x0 - x) > ylm) {
  378. if (cornername & 0x1) _drawFastVLine(x0 + x, y0 - y, 2 * y + 1 + delta, color);
  379. if (cornername & 0x2) _drawFastVLine(x0 - x, y0 - y, 2 * y + 1 + delta, color);
  380. }
  381. }
  382. }
  383. // Draw a rounded rectangle
  384. //=============================================================================================
  385. void EPD_drawRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color)
  386. {
  387. x += dispWin.x1;
  388. y += dispWin.y1;
  389. // smarter version
  390. _drawFastHLine(x + r, y, w - 2 * r, color); // Top
  391. _drawFastHLine(x + r, y + h - 1, w - 2 * r, color); // Bottom
  392. _drawFastVLine(x, y + r, h - 2 * r, color); // Left
  393. _drawFastVLine(x + w - 1, y + r, h - 2 * r, color); // Right
  394. // draw four corners
  395. drawCircleHelper(x + r, y + r, r, 1, color);
  396. drawCircleHelper(x + w - r - 1, y + r, r, 2, color);
  397. drawCircleHelper(x + w - r - 1, y + h - r - 1, r, 4, color);
  398. drawCircleHelper(x + r, y + h - r - 1, r, 8, color);
  399. }
  400. // Fill a rounded rectangle
  401. //=============================================================================================
  402. void EPD_fillRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color)
  403. {
  404. x += dispWin.x1;
  405. y += dispWin.y1;
  406. // smarter version
  407. _fillRect(x + r, y, w - 2 * r, h, color);
  408. // draw four corners
  409. fillCircleHelper(x + w - r - 1, y + r, r, 1, h - 2 * r - 1, color);
  410. fillCircleHelper(x + r, y + r, r, 2, h - 2 * r - 1, color);
  411. }
  412. //-----------------------------------------------------------------------------------------------
  413. static void _drawLineByAngle(int16_t x, int16_t y, int16_t angle, uint16_t length, color_t color)
  414. {
  415. _drawLine(
  416. x,
  417. y,
  418. x + length * cos((angle + _angleOffset) * DEG_TO_RAD),
  419. y + length * sin((angle + _angleOffset) * DEG_TO_RAD), color);
  420. }
  421. //---------------------------------------------------------------------------------------------------------------
  422. static void _DrawLineByAngle(int16_t x, int16_t y, int16_t angle, uint16_t start, uint16_t length, color_t color)
  423. {
  424. _drawLine(
  425. x + start * cos((angle + _angleOffset) * DEG_TO_RAD),
  426. y + start * sin((angle + _angleOffset) * DEG_TO_RAD),
  427. x + (start + length) * cos((angle + _angleOffset) * DEG_TO_RAD),
  428. y + (start + length) * sin((angle + _angleOffset) * DEG_TO_RAD), color);
  429. }
  430. //===========================================================================================================
  431. void EPD_drawLineByAngle(uint16_t x, uint16_t y, uint16_t start, uint16_t len, uint16_t angle, color_t color)
  432. {
  433. x += dispWin.x1;
  434. y += dispWin.y1;
  435. if (start == 0) _drawLineByAngle(x, y, angle, len, color);
  436. else _DrawLineByAngle(x, y, angle, start, len, color);
  437. }
  438. // Draw a triangle
  439. //--------------------------------------------------------------------------------------------------------------------
  440. static void _drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color)
  441. {
  442. _drawLine(x0, y0, x1, y1, color);
  443. _drawLine(x1, y1, x2, y2, color);
  444. _drawLine(x2, y2, x0, y0, color);
  445. }
  446. //================================================================================================================
  447. void EPD_drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color)
  448. {
  449. x0 += dispWin.x1;
  450. y0 += dispWin.y1;
  451. x1 += dispWin.x1;
  452. y1 += dispWin.y1;
  453. x2 += dispWin.x1;
  454. y2 += dispWin.y1;
  455. _drawLine(x0, y0, x1, y1, color);
  456. _drawLine(x1, y1, x2, y2, color);
  457. _drawLine(x2, y2, x0, y0, color);
  458. }
  459. // Fill a triangle
  460. //--------------------------------------------------------------------------------------------------------------------
  461. static void _fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color)
  462. {
  463. int16_t a, b, y, last;
  464. // Sort coordinates by Y order (y2 >= y1 >= y0)
  465. if (y0 > y1) {
  466. swap(y0, y1); swap(x0, x1);
  467. }
  468. if (y1 > y2) {
  469. swap(y2, y1); swap(x2, x1);
  470. }
  471. if (y0 > y1) {
  472. swap(y0, y1); swap(x0, x1);
  473. }
  474. if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
  475. a = b = x0;
  476. if(x1 < a) a = x1;
  477. else if(x1 > b) b = x1;
  478. if(x2 < a) a = x2;
  479. else if(x2 > b) b = x2;
  480. _drawFastHLine(a, y0, b-a+1, color);
  481. return;
  482. }
  483. int16_t
  484. dx01 = x1 - x0,
  485. dy01 = y1 - y0,
  486. dx02 = x2 - x0,
  487. dy02 = y2 - y0,
  488. dx12 = x2 - x1,
  489. dy12 = y2 - y1;
  490. int32_t
  491. sa = 0,
  492. sb = 0;
  493. // For upper part of triangle, find scanline crossings for segments
  494. // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
  495. // is included here (and second loop will be skipped, avoiding a /0
  496. // error there), otherwise scanline y1 is skipped here and handled
  497. // in the second loop...which also avoids a /0 error here if y0=y1
  498. // (flat-topped triangle).
  499. if(y1 == y2) last = y1; // Include y1 scanline
  500. else last = y1-1; // Skip it
  501. for(y=y0; y<=last; y++) {
  502. a = x0 + sa / dy01;
  503. b = x0 + sb / dy02;
  504. sa += dx01;
  505. sb += dx02;
  506. /* longhand:
  507. a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  508. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  509. */
  510. if(a > b) swap(a,b);
  511. _drawFastHLine(a, y, b-a+1, color);
  512. }
  513. // For lower part of triangle, find scanline crossings for segments
  514. // 0-2 and 1-2. This loop is skipped if y1=y2.
  515. sa = dx12 * (y - y1);
  516. sb = dx02 * (y - y0);
  517. for(; y<=y2; y++) {
  518. a = x1 + sa / dy12;
  519. b = x0 + sb / dy02;
  520. sa += dx12;
  521. sb += dx02;
  522. /* longhand:
  523. a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  524. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  525. */
  526. if(a > b) swap(a,b);
  527. _drawFastHLine(a, y, b-a+1, color);
  528. }
  529. }
  530. //================================================================================================================
  531. void EPD_fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color)
  532. {
  533. _fillTriangle(
  534. x0 + dispWin.x1, y0 + dispWin.y1,
  535. x1 + dispWin.x1, y1 + dispWin.y1,
  536. x2 + dispWin.x1, y2 + dispWin.y1,
  537. color);
  538. }
  539. //====================================================================
  540. void EPD_drawCircle(int16_t x, int16_t y, int radius, color_t color) {
  541. x += dispWin.x1;
  542. y += dispWin.y1;
  543. int f = 1 - radius;
  544. int ddF_x = 1;
  545. int ddF_y = -2 * radius;
  546. int x1 = 0;
  547. int y1 = radius;
  548. _drawPixel(x, y + radius, color);
  549. _drawPixel(x, y - radius, color);
  550. _drawPixel(x + radius, y, color);
  551. _drawPixel(x - radius, y, color);
  552. while(x1 < y1) {
  553. if (f >= 0) {
  554. y1--;
  555. ddF_y += 2;
  556. f += ddF_y;
  557. }
  558. x1++;
  559. ddF_x += 2;
  560. f += ddF_x;
  561. _drawPixel(x + x1, y + y1, color);
  562. _drawPixel(x - x1, y + y1, color);
  563. _drawPixel(x + x1, y - y1, color);
  564. _drawPixel(x - x1, y - y1, color);
  565. _drawPixel(x + y1, y + x1, color);
  566. _drawPixel(x - y1, y + x1, color);
  567. _drawPixel(x + y1, y - x1, color);
  568. _drawPixel(x - y1, y - x1, color);
  569. }
  570. }
  571. //====================================================================
  572. void EPD_fillCircle(int16_t x, int16_t y, int radius, color_t color) {
  573. x += dispWin.x1;
  574. y += dispWin.y1;
  575. _drawFastVLine(x, y-radius, 2*radius+1, color);
  576. fillCircleHelper(x, y, radius, 3, 0, color);
  577. }
  578. //----------------------------------------------------------------------------------------------------------------
  579. static void _draw_ellipse_section(uint16_t x, uint16_t y, uint16_t x0, uint16_t y0, color_t color, uint8_t option)
  580. {
  581. // upper right
  582. if ( option & EPD_ELLIPSE_UPPER_RIGHT ) _drawPixel(x0 + x, y0 - y, color);
  583. // upper left
  584. if ( option & EPD_ELLIPSE_UPPER_LEFT ) _drawPixel(x0 - x, y0 - y, color);
  585. // lower right
  586. if ( option & EPD_ELLIPSE_LOWER_RIGHT ) _drawPixel(x0 + x, y0 + y, color);
  587. // lower left
  588. if ( option & EPD_ELLIPSE_LOWER_LEFT ) _drawPixel(x0 - x, y0 + y, color);
  589. }
  590. //=====================================================================================================
  591. void EPD_drawEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option)
  592. {
  593. x0 += dispWin.x1;
  594. y0 += dispWin.y1;
  595. uint16_t x, y;
  596. int32_t xchg, ychg;
  597. int32_t err;
  598. int32_t rxrx2;
  599. int32_t ryry2;
  600. int32_t stopx, stopy;
  601. rxrx2 = rx;
  602. rxrx2 *= rx;
  603. rxrx2 *= 2;
  604. ryry2 = ry;
  605. ryry2 *= ry;
  606. ryry2 *= 2;
  607. x = rx;
  608. y = 0;
  609. xchg = 1;
  610. xchg -= rx;
  611. xchg -= rx;
  612. xchg *= ry;
  613. xchg *= ry;
  614. ychg = rx;
  615. ychg *= rx;
  616. err = 0;
  617. stopx = ryry2;
  618. stopx *= rx;
  619. stopy = 0;
  620. while( stopx >= stopy ) {
  621. _draw_ellipse_section(x, y, x0, y0, color, option);
  622. y++;
  623. stopy += rxrx2;
  624. err += ychg;
  625. ychg += rxrx2;
  626. if ( 2*err+xchg > 0 ) {
  627. x--;
  628. stopx -= ryry2;
  629. err += xchg;
  630. xchg += ryry2;
  631. }
  632. }
  633. x = 0;
  634. y = ry;
  635. xchg = ry;
  636. xchg *= ry;
  637. ychg = 1;
  638. ychg -= ry;
  639. ychg -= ry;
  640. ychg *= rx;
  641. ychg *= rx;
  642. err = 0;
  643. stopx = 0;
  644. stopy = rxrx2;
  645. stopy *= ry;
  646. while( stopx <= stopy ) {
  647. _draw_ellipse_section(x, y, x0, y0, color, option);
  648. x++;
  649. stopx += ryry2;
  650. err += xchg;
  651. xchg += ryry2;
  652. if ( 2*err+ychg > 0 ) {
  653. y--;
  654. stopy -= rxrx2;
  655. err += ychg;
  656. ychg += rxrx2;
  657. }
  658. }
  659. }
  660. //-----------------------------------------------------------------------------------------------------------------------
  661. static void _draw_filled_ellipse_section(uint16_t x, uint16_t y, uint16_t x0, uint16_t y0, color_t color, uint8_t option)
  662. {
  663. // upper right
  664. if ( option & EPD_ELLIPSE_UPPER_RIGHT ) _drawFastVLine(x0+x, y0-y, y+1, color);
  665. // upper left
  666. if ( option & EPD_ELLIPSE_UPPER_LEFT ) _drawFastVLine(x0-x, y0-y, y+1, color);
  667. // lower right
  668. if ( option & EPD_ELLIPSE_LOWER_RIGHT ) _drawFastVLine(x0+x, y0, y+1, color);
  669. // lower left
  670. if ( option & EPD_ELLIPSE_LOWER_LEFT ) _drawFastVLine(x0-x, y0, y+1, color);
  671. }
  672. //=====================================================================================================
  673. void EPD_fillEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option)
  674. {
  675. x0 += dispWin.x1;
  676. y0 += dispWin.y1;
  677. uint16_t x, y;
  678. int32_t xchg, ychg;
  679. int32_t err;
  680. int32_t rxrx2;
  681. int32_t ryry2;
  682. int32_t stopx, stopy;
  683. rxrx2 = rx;
  684. rxrx2 *= rx;
  685. rxrx2 *= 2;
  686. ryry2 = ry;
  687. ryry2 *= ry;
  688. ryry2 *= 2;
  689. x = rx;
  690. y = 0;
  691. xchg = 1;
  692. xchg -= rx;
  693. xchg -= rx;
  694. xchg *= ry;
  695. xchg *= ry;
  696. ychg = rx;
  697. ychg *= rx;
  698. err = 0;
  699. stopx = ryry2;
  700. stopx *= rx;
  701. stopy = 0;
  702. while( stopx >= stopy ) {
  703. _draw_filled_ellipse_section(x, y, x0, y0, color, option);
  704. y++;
  705. stopy += rxrx2;
  706. err += ychg;
  707. ychg += rxrx2;
  708. if ( 2*err+xchg > 0 ) {
  709. x--;
  710. stopx -= ryry2;
  711. err += xchg;
  712. xchg += ryry2;
  713. }
  714. }
  715. x = 0;
  716. y = ry;
  717. xchg = ry;
  718. xchg *= ry;
  719. ychg = 1;
  720. ychg -= ry;
  721. ychg -= ry;
  722. ychg *= rx;
  723. ychg *= rx;
  724. err = 0;
  725. stopx = 0;
  726. stopy = rxrx2;
  727. stopy *= ry;
  728. while( stopx <= stopy ) {
  729. _draw_filled_ellipse_section(x, y, x0, y0, color, option);
  730. x++;
  731. stopx += ryry2;
  732. err += xchg;
  733. xchg += ryry2;
  734. if ( 2*err+ychg > 0 ) {
  735. y--;
  736. stopy -= rxrx2;
  737. err += ychg;
  738. ychg += rxrx2;
  739. }
  740. }
  741. }
  742. // ==== ARC DRAWING ===================================================================
  743. //---------------------------------------------------------------------------------------------------------------------------------
  744. static void _fillArcOffsetted(uint16_t cx, uint16_t cy, uint16_t radius, uint16_t thickness, float start, float end, color_t color)
  745. {
  746. //float sslope = (float)cos_lookup(start) / (float)sin_lookup(start);
  747. //float eslope = (float)cos_lookup(end) / (float)sin_lookup(end);
  748. float sslope = (cos(start/_arcAngleMax * 2 * PI) * _arcAngleMax) / (sin(start/_arcAngleMax * 2 * PI) * _arcAngleMax) ;
  749. float eslope = (cos(end/_arcAngleMax * 2 * PI) * _arcAngleMax) / (sin(end/_arcAngleMax * 2 * PI) * _arcAngleMax);
  750. if (end == 360) eslope = -1000000;
  751. int ir2 = (radius - thickness) * (radius - thickness);
  752. int or2 = radius * radius;
  753. for (int x = -radius; x <= radius; x++) {
  754. for (int y = -radius; y <= radius; y++) {
  755. int x2 = x * x;
  756. int y2 = y * y;
  757. if (
  758. (x2 + y2 < or2 && x2 + y2 >= ir2) &&
  759. (
  760. (y > 0 && start < 180 && x <= y * sslope) ||
  761. (y < 0 && start > 180 && x >= y * sslope) ||
  762. (y < 0 && start <= 180) ||
  763. (y == 0 && start <= 180 && x < 0) ||
  764. (y == 0 && start == 0 && x > 0)
  765. ) &&
  766. (
  767. (y > 0 && end < 180 && x >= y * eslope) ||
  768. (y < 0 && end > 180 && x <= y * eslope) ||
  769. (y > 0 && end >= 180) ||
  770. (y == 0 && end >= 180 && x < 0) ||
  771. (y == 0 && start == 0 && x > 0)
  772. )
  773. )
  774. _drawPixel(cx+x, cy+y, color);
  775. }
  776. }
  777. }
  778. //===========================================================================================================================
  779. 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)
  780. {
  781. cx += dispWin.x1;
  782. cy += dispWin.y1;
  783. if (th < 1) th = 1;
  784. if (th > r) th = r;
  785. int f = EPD_compare_colors(fillcolor, color);
  786. float astart = fmodf(start, _arcAngleMax);
  787. float aend = fmodf(end, _arcAngleMax);
  788. astart += _angleOffset;
  789. aend += _angleOffset;
  790. if (astart < 0) astart += (float)360;
  791. if (aend < 0) aend += (float)360;
  792. if (aend == 0) aend = (float)360;
  793. if (astart > aend) {
  794. _fillArcOffsetted(cx, cy, r, th, astart, _arcAngleMax, fillcolor);
  795. _fillArcOffsetted(cx, cy, r, th, 0, aend, fillcolor);
  796. if (f) {
  797. _fillArcOffsetted(cx, cy, r, 1, astart, _arcAngleMax, color);
  798. _fillArcOffsetted(cx, cy, r, 1, 0, aend, color);
  799. _fillArcOffsetted(cx, cy, r-th, 1, astart, _arcAngleMax, color);
  800. _fillArcOffsetted(cx, cy, r-th, 1, 0, aend, color);
  801. }
  802. }
  803. else {
  804. _fillArcOffsetted(cx, cy, r, th, astart, aend, fillcolor);
  805. if (f) {
  806. _fillArcOffsetted(cx, cy, r, 1, astart, aend, color);
  807. _fillArcOffsetted(cx, cy, r-th, 1, astart, aend, color);
  808. }
  809. }
  810. if (f) {
  811. _drawLine(cx + (r-th) * cos(astart * DEG_TO_RAD), cy + (r-th) * sin(astart * DEG_TO_RAD),
  812. cx + (r-1) * cos(astart * DEG_TO_RAD), cy + (r-1) * sin(astart * DEG_TO_RAD), color);
  813. _drawLine(cx + (r-th) * cos(aend * DEG_TO_RAD), cy + (r-th) * sin(aend * DEG_TO_RAD),
  814. cx + (r-1) * cos(aend * DEG_TO_RAD), cy + (r-1) * sin(aend * DEG_TO_RAD), color);
  815. }
  816. }
  817. //=============================================================================================================
  818. void EPD_drawPolygon(int cx, int cy, int sides, int diameter, color_t color, color_t fill, int rot, uint8_t th)
  819. {
  820. cx += dispWin.x1;
  821. cy += dispWin.y1;
  822. int deg = rot - _angleOffset;
  823. int f = EPD_compare_colors(fill, color);
  824. if (sides < MIN_POLIGON_SIDES) sides = MIN_POLIGON_SIDES; // This ensures the minimum side number
  825. if (sides > MAX_POLIGON_SIDES) sides = MAX_POLIGON_SIDES; // This ensures the maximum side number
  826. int Xpoints[sides], Ypoints[sides]; // Set the arrays based on the number of sides entered
  827. int rads = 360 / sides; // This equally spaces the points.
  828. for (int idx = 0; idx < sides; idx++) {
  829. Xpoints[idx] = cx + sin((float)(idx*rads + deg) * deg_to_rad) * diameter;
  830. Ypoints[idx] = cy + cos((float)(idx*rads + deg) * deg_to_rad) * diameter;
  831. }
  832. // Draw the polygon on the screen.
  833. if (f) {
  834. for(int idx = 0; idx < sides; idx++) {
  835. if((idx+1) < sides) _fillTriangle(cx,cy,Xpoints[idx],Ypoints[idx],Xpoints[idx+1],Ypoints[idx+1], fill);
  836. else _fillTriangle(cx,cy,Xpoints[idx],Ypoints[idx],Xpoints[0],Ypoints[0], fill);
  837. }
  838. }
  839. if (th) {
  840. for (int n=0; n<th; n++) {
  841. if (n > 0) {
  842. for (int idx = 0; idx < sides; idx++) {
  843. Xpoints[idx] = cx + sin((float)(idx*rads + deg) * deg_to_rad) * (diameter-n);
  844. Ypoints[idx] = cy + cos((float)(idx*rads + deg) * deg_to_rad) * (diameter-n);
  845. }
  846. }
  847. for(int idx = 0; idx < sides; idx++) {
  848. if( (idx+1) < sides)
  849. _drawLine(Xpoints[idx],Ypoints[idx],Xpoints[idx+1],Ypoints[idx+1], color); // draw the lines
  850. else
  851. _drawLine(Xpoints[idx],Ypoints[idx],Xpoints[0],Ypoints[0], color); // finishes the last line to close up the polygon.
  852. }
  853. }
  854. }
  855. }
  856. /*
  857. // Similar to the Polygon function.
  858. //=====================================================================================
  859. void EPD_drawStar(int cx, int cy, int diameter, color_t color, bool fill, float factor)
  860. {
  861. cx += dispWin.x1;
  862. cy += dispWin.y1;
  863. factor = constrain(factor, 1.0, 4.0);
  864. uint8_t sides = 5;
  865. uint8_t rads = 360 / sides;
  866. int Xpoints_O[sides], Ypoints_O[sides], Xpoints_I[sides], Ypoints_I[sides];//Xpoints_T[5], Ypoints_T[5];
  867. for(int idx = 0; idx < sides; idx++) {
  868. // makes the outer points
  869. Xpoints_O[idx] = cx + sin((float)(idx*rads + 72) * deg_to_rad) * diameter;
  870. Ypoints_O[idx] = cy + cos((float)(idx*rads + 72) * deg_to_rad) * diameter;
  871. // makes the inner points
  872. Xpoints_I[idx] = cx + sin((float)(idx*rads + 36) * deg_to_rad) * ((float)(diameter)/factor);
  873. // 36 is half of 72, and this will allow the inner and outer points to line up like a triangle.
  874. Ypoints_I[idx] = cy + cos((float)(idx*rads + 36) * deg_to_rad) * ((float)(diameter)/factor);
  875. }
  876. for(int idx = 0; idx < sides; idx++) {
  877. if((idx+1) < sides) {
  878. if(fill) {// this part below should be self explanatory. It fills in the star.
  879. _fillTriangle(cx,cy,Xpoints_I[idx],Ypoints_I[idx],Xpoints_O[idx],Ypoints_O[idx], color);
  880. _fillTriangle(cx,cy,Xpoints_O[idx],Ypoints_O[idx],Xpoints_I[idx+1],Ypoints_I[idx+1], color);
  881. }
  882. else {
  883. _drawLine(Xpoints_O[idx],Ypoints_O[idx],Xpoints_I[idx+1],Ypoints_I[idx+1], color);
  884. _drawLine(Xpoints_I[idx],Ypoints_I[idx],Xpoints_O[idx],Ypoints_O[idx], color);
  885. }
  886. }
  887. else {
  888. if(fill) {
  889. _fillTriangle(cx,cy,Xpoints_I[0],Ypoints_I[0],Xpoints_O[idx],Ypoints_O[idx], color);
  890. _fillTriangle(cx,cy,Xpoints_O[idx],Ypoints_O[idx],Xpoints_I[idx],Ypoints_I[idx], color);
  891. }
  892. else {
  893. _drawLine(Xpoints_O[idx],Ypoints_O[idx],Xpoints_I[idx],Ypoints_I[idx], color);
  894. _drawLine(Xpoints_I[0],Ypoints_I[0],Xpoints_O[idx],Ypoints_O[idx], color);
  895. }
  896. }
  897. }
  898. }
  899. */
  900. // ================ Font and string functions ==================================
  901. //--------------------------------------------------------
  902. static int load_file_font(const char * fontfile, int info)
  903. {
  904. int err = 0;
  905. char err_msg[256] = {'\0'};
  906. if (userfont != NULL) {
  907. free(userfont);
  908. userfont = NULL;
  909. }
  910. struct stat sb;
  911. // Open the file
  912. FILE *fhndl = fopen(fontfile, "r");
  913. if (!fhndl) {
  914. sprintf(err_msg, "Error opening font file '%s'", fontfile);
  915. err = 1;
  916. goto exit;
  917. }
  918. // Get file size
  919. if (stat(fontfile, &sb) != 0) {
  920. sprintf(err_msg, "Error getting font file size");
  921. err = 2;
  922. goto exit;
  923. }
  924. int fsize = sb.st_size;
  925. if (fsize < 30) {
  926. sprintf(err_msg, "Error getting font file size");
  927. err = 3;
  928. goto exit;
  929. }
  930. userfont = malloc(fsize+4);
  931. if (userfont == NULL) {
  932. sprintf(err_msg, "Font memory allocation error");
  933. fclose(fhndl);
  934. err = 4;
  935. goto exit;
  936. }
  937. int read = fread(userfont, 1, fsize, fhndl);
  938. fclose(fhndl);
  939. if (read != fsize) {
  940. sprintf(err_msg, "Font read error");
  941. err = 5;
  942. goto exit;
  943. }
  944. userfont[read] = 0;
  945. if (strstr((char *)(userfont+read-8), "RPH_font") == NULL) {
  946. sprintf(err_msg, "Font ID not found");
  947. err = 6;
  948. goto exit;
  949. }
  950. // Check size
  951. int size = 0;
  952. int numchar = 0;
  953. int width = userfont[0];
  954. int height = userfont[1];
  955. uint8_t first = 255;
  956. uint8_t last = 0;
  957. //int offst = 0;
  958. int pminwidth = 255;
  959. int pmaxwidth = 0;
  960. if (width != 0) {
  961. // Fixed font
  962. numchar = userfont[3];
  963. first = userfont[2];
  964. last = first + numchar - 1;
  965. size = ((width * height * numchar) / 8) + 4;
  966. }
  967. else {
  968. // Proportional font
  969. size = 4; // point at first char data
  970. uint8_t charCode;
  971. int charwidth;
  972. do {
  973. charCode = userfont[size];
  974. charwidth = userfont[size+2];
  975. if (charCode != 0xFF) {
  976. numchar++;
  977. if (charwidth != 0) size += ((((charwidth * userfont[size+3])-1) / 8) + 7);
  978. else size += 6;
  979. if (info) {
  980. if (charwidth > pmaxwidth) pmaxwidth = charwidth;
  981. if (charwidth < pminwidth) pminwidth = charwidth;
  982. if (charCode < first) first = charCode;
  983. if (charCode > last) last = charCode;
  984. }
  985. }
  986. else size++;
  987. } while ((size < (read-8)) && (charCode != 0xFF));
  988. }
  989. if (size != (read-8)) {
  990. sprintf(err_msg, "Font size error: found %d expected %d)", size, (read-8));
  991. err = 7;
  992. goto exit;
  993. }
  994. if (info) {
  995. if (width != 0) {
  996. printf("Fixed width font:\r\n size: %d width: %d height: %d characters: %d (%d~%d)",
  997. size, width, height, numchar, first, last);
  998. }
  999. else {
  1000. printf("Proportional font:\r\n size: %d width: %d~%d height: %d characters: %d (%d~%d)\n",
  1001. size, pminwidth, pmaxwidth, height, numchar, first, last);
  1002. }
  1003. }
  1004. exit:
  1005. if (err) {
  1006. if (userfont) {
  1007. free(userfont);
  1008. userfont = NULL;
  1009. }
  1010. if (info) printf("Error: %d [%s]\r\n", err, err_msg);
  1011. }
  1012. return err;
  1013. }
  1014. //------------------------------------------------
  1015. int compile_font_file(char *fontfile, uint8_t dbg)
  1016. {
  1017. int err = 0;
  1018. char err_msg[128] = {'\0'};
  1019. char outfile[128] = {'\0'};
  1020. size_t len;
  1021. struct stat sb;
  1022. FILE *ffd = NULL;
  1023. FILE *ffd_out = NULL;
  1024. char *sourcebuf = NULL;
  1025. len = strlen(fontfile);
  1026. // check here that filename end with ".c".
  1027. if ((len < 3) || (len > 125) || (strcmp(fontfile + len - 2, ".c") != 0)) {
  1028. sprintf(err_msg, "not a .c file");
  1029. err = 1;
  1030. goto exit;
  1031. }
  1032. sprintf(outfile, "%s", fontfile);
  1033. sprintf(outfile+strlen(outfile)-1, "fon");
  1034. // Open the source file
  1035. if (stat(fontfile, &sb) != 0) {
  1036. sprintf(err_msg, "Error opening source file '%s'", fontfile);
  1037. err = 2;
  1038. goto exit;
  1039. }
  1040. // Open the file
  1041. ffd = fopen(fontfile, "rb");
  1042. if (!ffd) {
  1043. sprintf(err_msg, "Error opening source file '%s'", fontfile);
  1044. err = 3;
  1045. goto exit;
  1046. }
  1047. // Open the font file
  1048. ffd_out= fopen(outfile, "wb");
  1049. if (!ffd_out) {
  1050. sprintf(err_msg, "error opening destination file");
  1051. err = 4;
  1052. goto exit;
  1053. }
  1054. // Get file size
  1055. int fsize = sb.st_size;
  1056. if (fsize <= 0) {
  1057. sprintf(err_msg, "source file size error");
  1058. err = 5;
  1059. goto exit;
  1060. }
  1061. sourcebuf = malloc(fsize+4);
  1062. if (sourcebuf == NULL) {
  1063. sprintf(err_msg, "memory allocation error");
  1064. err = 6;
  1065. goto exit;
  1066. }
  1067. char *fbuf = sourcebuf;
  1068. int rdsize = fread(fbuf, 1, fsize, ffd);
  1069. fclose(ffd);
  1070. ffd = NULL;
  1071. if (rdsize != fsize) {
  1072. sprintf(err_msg, "error reading from source file");
  1073. err = 7;
  1074. goto exit;
  1075. }
  1076. *(fbuf+rdsize) = '\0';
  1077. fbuf = strchr(fbuf, '{'); // beginning of font data
  1078. char *fend = strstr(fbuf, "};"); // end of font data
  1079. if ((fbuf == NULL) || (fend == NULL) || ((fend-fbuf) < 22)) {
  1080. sprintf(err_msg, "wrong source file format");
  1081. err = 8;
  1082. goto exit;
  1083. }
  1084. fbuf++;
  1085. *fend = '\0';
  1086. char hexstr[5] = {'\0'};
  1087. int lastline = 0;
  1088. fbuf = strstr(fbuf, "0x");
  1089. int size = 0;
  1090. char *nextline;
  1091. char *numptr;
  1092. int bptr = 0;
  1093. while ((fbuf != NULL) && (fbuf < fend) && (lastline == 0)) {
  1094. nextline = strchr(fbuf, '\n'); // beginning of the next line
  1095. if (nextline == NULL) {
  1096. nextline = fend-1;
  1097. lastline++;
  1098. }
  1099. else nextline++;
  1100. while (fbuf < nextline) {
  1101. numptr = strstr(fbuf, "0x");
  1102. if ((numptr == NULL) || ((fbuf+4) > nextline)) numptr = strstr(fbuf, "0X");
  1103. if ((numptr != NULL) && ((numptr+4) <= nextline)) {
  1104. fbuf = numptr;
  1105. if (bptr >= 128) {
  1106. // buffer full, write to file
  1107. if (fwrite(outfile, 1, 128, ffd_out) != 128) goto error;
  1108. bptr = 0;
  1109. size += 128;
  1110. }
  1111. memcpy(hexstr, fbuf, 4);
  1112. hexstr[4] = 0;
  1113. outfile[bptr++] = (uint8_t)strtol(hexstr, NULL, 0);
  1114. fbuf += 4;
  1115. }
  1116. else fbuf = nextline;
  1117. }
  1118. fbuf = nextline;
  1119. }
  1120. if (bptr > 0) {
  1121. size += bptr;
  1122. if (fwrite(outfile, 1, bptr, ffd_out) != bptr) goto error;
  1123. }
  1124. // write font ID
  1125. sprintf(outfile, "RPH_font");
  1126. if (fwrite(outfile, 1, 8, ffd_out) != 8) goto error;
  1127. // === Test compiled font ===
  1128. sprintf(outfile, "%s", fontfile);
  1129. sprintf(outfile+strlen(outfile)-1, "fon");
  1130. uint8_t *uf = userfont; // save userfont pointer
  1131. userfont = NULL;
  1132. if (load_file_font(outfile, 1) == 0) {
  1133. sprintf(err_msg, "Error compiling file!");
  1134. }
  1135. else {
  1136. free(userfont);
  1137. sprintf(err_msg, "File compiled successfully.");
  1138. }
  1139. userfont = uf; // restore userfont
  1140. goto exit;
  1141. error:
  1142. sprintf(err_msg, "error writing to destination file");
  1143. err = 9;
  1144. exit:
  1145. if (sourcebuf) free(sourcebuf);
  1146. if (ffd) fclose(ffd);
  1147. if (ffd_out) fclose(ffd_out);
  1148. if (dbg) printf("%s\r\n", err_msg);
  1149. return err;
  1150. }
  1151. // -----------------------------------------------------------------------------------------
  1152. // Individual Proportional Font Character Format:
  1153. // -----------------------------------------------------------------------------------------
  1154. // Character Code
  1155. // yOffset (start Y of visible pixels)
  1156. // Width (width of the visible pixels)
  1157. // Height (height of the visible pixels)
  1158. // xOffset (start X of visible pixels)
  1159. // xDelta (the distance to move the cursor. Effective width of the character.)
  1160. // Data[n]
  1161. // -----------------------------------------------------------------------------------------
  1162. //---------------------------------------------------------------------------------------------
  1163. // Character drawing rectangle is (0, 0) (xDelta-1, cfont.y_size-1)
  1164. // Character visible pixels rectangle is (xOffset, yOffset) (xOffset+Width-1, yOffset+Height-1)
  1165. //---------------------------------------------------------------------------------------------
  1166. //----------------------------------
  1167. void getFontCharacters(uint8_t *buf)
  1168. {
  1169. if (cfont.bitmap == 2) {
  1170. //For 7 segment font only characters 0,1,2,3,4,5,6,7,8,9, . , - , : , / are available.
  1171. for (uint8_t n=0; n < 11; n++) {
  1172. buf[n] = n + 0x30;
  1173. }
  1174. buf[11] = '.';
  1175. buf[12] = '-';
  1176. buf[13] = '/';
  1177. buf[14] = '\0';
  1178. return;
  1179. }
  1180. if (cfont.x_size > 0) {
  1181. for (uint8_t n=0; n < cfont.numchars; n++) {
  1182. buf[n] = cfont.offset + n;
  1183. }
  1184. buf[cfont.numchars] = '\0';
  1185. return;
  1186. }
  1187. uint16_t tempPtr = 4; // point at first char data
  1188. uint8_t cc, cw, ch, n;
  1189. n = 0;
  1190. cc = cfont.font[tempPtr++];
  1191. while (cc != 0xFF) {
  1192. cfont.numchars++;
  1193. tempPtr++;
  1194. cw = cfont.font[tempPtr++];
  1195. ch = cfont.font[tempPtr++];
  1196. tempPtr++;
  1197. tempPtr++;
  1198. if (cw != 0) {
  1199. // packed bits
  1200. tempPtr += (((cw * ch)-1) / 8) + 1;
  1201. }
  1202. buf[n++] = cc;
  1203. cc = cfont.font[tempPtr++];
  1204. }
  1205. buf[n] = '\0';
  1206. }
  1207. // Set max width & height of the proportional font
  1208. //-----------------------------
  1209. static void getMaxWidthHeight()
  1210. {
  1211. uint16_t tempPtr = 4; // point at first char data
  1212. uint8_t cc, cw, ch, cd, cy;
  1213. cfont.numchars = 0;
  1214. cfont.max_x_size = 0;
  1215. cc = cfont.font[tempPtr++];
  1216. while (cc != 0xFF) {
  1217. cfont.numchars++;
  1218. cy = cfont.font[tempPtr++];
  1219. cw = cfont.font[tempPtr++];
  1220. ch = cfont.font[tempPtr++];
  1221. tempPtr++;
  1222. cd = cfont.font[tempPtr++];
  1223. cy += ch;
  1224. if (cw > cfont.max_x_size) cfont.max_x_size = cw;
  1225. if (cd > cfont.max_x_size) cfont.max_x_size = cd;
  1226. if (ch > cfont.y_size) cfont.y_size = ch;
  1227. if (cy > cfont.y_size) cfont.y_size = cy;
  1228. if (cw != 0) {
  1229. // packed bits
  1230. tempPtr += (((cw * ch)-1) / 8) + 1;
  1231. }
  1232. cc = cfont.font[tempPtr++];
  1233. }
  1234. cfont.size = tempPtr;
  1235. }
  1236. // Return the Glyph data for an individual character in the proportional font
  1237. //------------------------------------
  1238. static uint8_t getCharPtr(uint8_t c) {
  1239. uint16_t tempPtr = 4; // point at first char data
  1240. do {
  1241. fontChar.charCode = cfont.font[tempPtr++];
  1242. if (fontChar.charCode == 0xFF) return 0;
  1243. fontChar.adjYOffset = cfont.font[tempPtr++];
  1244. fontChar.width = cfont.font[tempPtr++];
  1245. fontChar.height = cfont.font[tempPtr++];
  1246. fontChar.xOffset = cfont.font[tempPtr++];
  1247. fontChar.xOffset = fontChar.xOffset < 0x80 ? fontChar.xOffset : -(0xFF - fontChar.xOffset);
  1248. fontChar.xDelta = cfont.font[tempPtr++];
  1249. if (c != fontChar.charCode && fontChar.charCode != 0xFF) {
  1250. if (fontChar.width != 0) {
  1251. // packed bits
  1252. tempPtr += (((fontChar.width * fontChar.height)-1) / 8) + 1;
  1253. }
  1254. }
  1255. } while ((c != fontChar.charCode) && (fontChar.charCode != 0xFF));
  1256. fontChar.dataPtr = tempPtr;
  1257. if (c == fontChar.charCode) {
  1258. if (font_forceFixed > 0) {
  1259. // fix width & offset for forced fixed width
  1260. fontChar.xDelta = cfont.max_x_size;
  1261. fontChar.xOffset = (fontChar.xDelta - fontChar.width) / 2;
  1262. }
  1263. }
  1264. else return 0;
  1265. return 1;
  1266. }
  1267. /*
  1268. //-----------------------
  1269. static void _testFont() {
  1270. if (cfont.x_size) {
  1271. printf("FONT TEST: fixed font\r\n");
  1272. return;
  1273. }
  1274. uint16_t tempPtr = 4; // point at first char data
  1275. uint8_t c = 0x20;
  1276. for (c=0x20; c <0xFF; c++) {
  1277. fontChar.charCode = cfont.font[tempPtr++];
  1278. if (fontChar.charCode == 0xFF) break;
  1279. if (fontChar.charCode != c) {
  1280. printf("FONT TEST: last sequential char: %d, expected %d\r\n", fontChar.charCode, c);
  1281. break;
  1282. }
  1283. c = fontChar.charCode;
  1284. fontChar.adjYOffset = cfont.font[tempPtr++];
  1285. fontChar.width = cfont.font[tempPtr++];
  1286. fontChar.height = cfont.font[tempPtr++];
  1287. fontChar.xOffset = cfont.font[tempPtr++];
  1288. fontChar.xOffset = fontChar.xOffset < 0x80 ? fontChar.xOffset : -(0xFF - fontChar.xOffset);
  1289. fontChar.xDelta = cfont.font[tempPtr++];
  1290. if (fontChar.charCode != 0xFF) {
  1291. if (fontChar.width != 0) {
  1292. // packed bits
  1293. tempPtr += (((fontChar.width * fontChar.height)-1) / 8) + 1;
  1294. }
  1295. }
  1296. }
  1297. printf("FONT TEST: W=%d H=%d last char: %d [%c]; length: %d\r\n", cfont.max_x_size, cfont.y_size, c, c, tempPtr);
  1298. }
  1299. */
  1300. //===================================================
  1301. void EPD_setFont(uint8_t font, const char *font_file)
  1302. {
  1303. cfont.font = NULL;
  1304. if (font == FONT_7SEG) {
  1305. cfont.bitmap = 2;
  1306. cfont.x_size = 24;
  1307. cfont.y_size = 6;
  1308. cfont.offset = 0;
  1309. cfont.color = _fg;
  1310. }
  1311. else {
  1312. if (font == USER_FONT) {
  1313. if (load_file_font(font_file, 0) != 0) cfont.font = tft_DefaultFont;
  1314. else cfont.font = userfont;
  1315. }
  1316. else if (font == DEJAVU18_FONT) cfont.font = tft_Dejavu18;
  1317. else if (font == DEJAVU24_FONT) cfont.font = tft_Dejavu24;
  1318. else if (font == UBUNTU16_FONT) cfont.font = tft_Ubuntu16;
  1319. else if (font == COMIC24_FONT) cfont.font = tft_Comic24;
  1320. else if (font == MINYA24_FONT) cfont.font = tft_minya24;
  1321. else if (font == TOONEY32_FONT) cfont.font = tft_tooney32;
  1322. else if (font == SMALL_FONT) cfont.font = tft_SmallFont;
  1323. else cfont.font = tft_DefaultFont;
  1324. cfont.bitmap = 1;
  1325. cfont.x_size = cfont.font[0];
  1326. cfont.y_size = cfont.font[1];
  1327. if (cfont.x_size > 0) {
  1328. cfont.offset = cfont.font[2];
  1329. cfont.numchars = cfont.font[3];
  1330. cfont.size = cfont.x_size * cfont.y_size * cfont.numchars;
  1331. }
  1332. else {
  1333. cfont.offset = 4;
  1334. getMaxWidthHeight();
  1335. }
  1336. //_testFont();
  1337. }
  1338. }
  1339. // -----------------------------------------------------------------------------------------
  1340. // Individual Proportional Font Character Format:
  1341. // -----------------------------------------------------------------------------------------
  1342. // Character Code
  1343. // yOffset (start Y of visible pixels)
  1344. // Width (width of the visible pixels)
  1345. // Height (height of the visible pixels)
  1346. // xOffset (start X of visible pixels)
  1347. // xDelta (the distance to move the cursor. Effective width of the character.)
  1348. // Data[n]
  1349. // -----------------------------------------------------------------------------------------
  1350. //---------------------------------------------------------------------------------------------
  1351. // Character drawing rectangle is (0, 0) (xDelta-1, cfont.y_size-1)
  1352. // Character visible pixels rectangle is (xOffset, yOffset) (xOffset+Width-1, yOffset+Height-1)
  1353. //---------------------------------------------------------------------------------------------
  1354. // print non-rotated proportional character
  1355. // character is already in fontChar
  1356. //----------------------------------------------
  1357. static int printProportionalChar(int x, int y) {
  1358. uint8_t ch = 0;
  1359. int i, j, char_width;
  1360. char_width = ((fontChar.width > fontChar.xDelta) ? fontChar.width : fontChar.xDelta);
  1361. int cx, cy;
  1362. if (!font_transparent) _fillRect(x, y, char_width+1, cfont.y_size, _bg);
  1363. // draw Glyph
  1364. uint8_t mask = 0x80;
  1365. for (j=0; j < fontChar.height; j++) {
  1366. for (i=0; i < fontChar.width; i++) {
  1367. if (((i + (j*fontChar.width)) % 8) == 0) {
  1368. mask = 0x80;
  1369. ch = cfont.font[fontChar.dataPtr++];
  1370. }
  1371. if ((ch & mask) !=0) {
  1372. cx = (uint16_t)(x+fontChar.xOffset+i);
  1373. cy = (uint16_t)(y+j+fontChar.adjYOffset);
  1374. _drawPixel(cx, cy, _fg);
  1375. }
  1376. mask >>= 1;
  1377. }
  1378. }
  1379. return char_width;
  1380. }
  1381. // non-rotated fixed width character
  1382. //----------------------------------------------
  1383. static void printChar(uint8_t c, int x, int y) {
  1384. uint8_t i, j, ch, fz, mask;
  1385. uint16_t k, temp, cx, cy;
  1386. // fz = bytes per char row
  1387. fz = cfont.x_size/8;
  1388. if (cfont.x_size % 8) fz++;
  1389. // get character position in buffer
  1390. temp = ((c-cfont.offset)*((fz)*cfont.y_size))+4;
  1391. if (!font_transparent) _fillRect(x, y, cfont.x_size, cfont.y_size, _bg);
  1392. for (j=0; j<cfont.y_size; j++) {
  1393. for (k=0; k < fz; k++) {
  1394. ch = cfont.font[temp+k];
  1395. mask=0x80;
  1396. for (i=0; i<8; i++) {
  1397. if ((ch & mask) !=0) {
  1398. cx = (uint16_t)(x+i+(k*8));
  1399. cy = (uint16_t)(y+j);
  1400. _drawPixel(cx, cy, _fg);
  1401. }
  1402. mask >>= 1;
  1403. }
  1404. }
  1405. temp += (fz);
  1406. }
  1407. }
  1408. // print rotated proportional character
  1409. // character is already in fontChar
  1410. //---------------------------------------------------
  1411. static int rotatePropChar(int x, int y, int offset) {
  1412. uint8_t ch = 0;
  1413. double radian = font_rotate * DEG_TO_RAD;
  1414. float cos_radian = cos(radian);
  1415. float sin_radian = sin(radian);
  1416. uint8_t mask = 0x80;
  1417. for (int j=0; j < fontChar.height; j++) {
  1418. for (int i=0; i < fontChar.width; i++) {
  1419. if (((i + (j*fontChar.width)) % 8) == 0) {
  1420. mask = 0x80;
  1421. ch = cfont.font[fontChar.dataPtr++];
  1422. }
  1423. int newX = (int)(x + (((offset + i) * cos_radian) - ((j+fontChar.adjYOffset)*sin_radian)));
  1424. int newY = (int)(y + (((j+fontChar.adjYOffset) * cos_radian) + ((offset + i) * sin_radian)));
  1425. if ((ch & mask) != 0) _drawPixel(newX,newY,_fg);
  1426. else if (!font_transparent) _drawPixel(newX,newY,_bg);
  1427. mask >>= 1;
  1428. }
  1429. }
  1430. return fontChar.xDelta+1;
  1431. }
  1432. // rotated fixed width character
  1433. //--------------------------------------------------------
  1434. static void rotateChar(uint8_t c, int x, int y, int pos) {
  1435. uint8_t i,j,ch,fz,mask;
  1436. uint16_t temp;
  1437. int newx,newy;
  1438. double radian = font_rotate*0.0175;
  1439. float cos_radian = cos(radian);
  1440. float sin_radian = sin(radian);
  1441. int zz;
  1442. if( cfont.x_size < 8 ) fz = cfont.x_size;
  1443. else fz = cfont.x_size/8;
  1444. temp=((c-cfont.offset)*((fz)*cfont.y_size))+4;
  1445. for (j=0; j<cfont.y_size; j++) {
  1446. for (zz=0; zz<(fz); zz++) {
  1447. ch = cfont.font[temp+zz];
  1448. mask = 0x80;
  1449. for (i=0; i<8; i++) {
  1450. newx=(int)(x+(((i+(zz*8)+(pos*cfont.x_size))*cos_radian)-((j)*sin_radian)));
  1451. newy=(int)(y+(((j)*cos_radian)+((i+(zz*8)+(pos*cfont.x_size))*sin_radian)));
  1452. if ((ch & mask) != 0) _drawPixel(newx,newy,_fg);
  1453. else if (!font_transparent) _drawPixel(newx,newy,_bg);
  1454. mask >>= 1;
  1455. }
  1456. }
  1457. temp+=(fz);
  1458. }
  1459. // calculate x,y for the next char
  1460. EPD_X = (int)(x + ((pos+1) * cfont.x_size * cos_radian));
  1461. EPD_Y = (int)(y + ((pos+1) * cfont.x_size * sin_radian));
  1462. }
  1463. //----------------------
  1464. static int _7seg_width()
  1465. {
  1466. return (2 * (2 * cfont.y_size + 1)) + cfont.x_size;
  1467. }
  1468. //-----------------------
  1469. static int _7seg_height()
  1470. {
  1471. return (3 * (2 * cfont.y_size + 1)) + (2 * cfont.x_size);
  1472. }
  1473. // Returns the string width in pixels.
  1474. // Useful for positions strings on the screen.
  1475. //===============================
  1476. int EPD_getStringWidth(char* str)
  1477. {
  1478. int strWidth = 0;
  1479. if (cfont.bitmap == 2) strWidth = ((_7seg_width()+2) * strlen(str)) - 2; // 7-segment font
  1480. else if (cfont.x_size != 0) strWidth = strlen(str) * cfont.x_size; // fixed width font
  1481. else {
  1482. // calculate the width of the string of proportional characters
  1483. char* tempStrptr = str;
  1484. while (*tempStrptr != 0) {
  1485. if (getCharPtr(*tempStrptr++)) {
  1486. strWidth += (((fontChar.width > fontChar.xDelta) ? fontChar.width : fontChar.xDelta) + 1);
  1487. }
  1488. }
  1489. strWidth--;
  1490. }
  1491. return strWidth;
  1492. }
  1493. //===============================================
  1494. void EPD_clearStringRect(int x, int y, char *str)
  1495. {
  1496. int w = EPD_getStringWidth(str);
  1497. int h = EPD_getfontheight();
  1498. EPD_fillRect(x+dispWin.x1, y+dispWin.y1, w, h, _bg);
  1499. }
  1500. //==============================================================================
  1501. /**
  1502. * bit-encoded bar position of all digits' bcd segments
  1503. *
  1504. * 6
  1505. * +-----+
  1506. * 3 | . | 2
  1507. * +--5--+
  1508. * 1 | . | 0
  1509. * +--.--+
  1510. * 4
  1511. */
  1512. static const uint16_t font_bcd[] = {
  1513. 0x200, // 0010 0000 0000 // -
  1514. 0x080, // 0000 1000 0000 // .
  1515. 0x06C, // 0100 0110 1100 // /, degree
  1516. 0x05f, // 0000 0101 1111, // 0
  1517. 0x005, // 0000 0000 0101, // 1
  1518. 0x076, // 0000 0111 0110, // 2
  1519. 0x075, // 0000 0111 0101, // 3
  1520. 0x02d, // 0000 0010 1101, // 4
  1521. 0x079, // 0000 0111 1001, // 5
  1522. 0x07b, // 0000 0111 1011, // 6
  1523. 0x045, // 0000 0100 0101, // 7
  1524. 0x07f, // 0000 0111 1111, // 8
  1525. 0x07d, // 0000 0111 1101 // 9
  1526. 0x900 // 1001 0000 0000 // :
  1527. };
  1528. //-----------------------------------------------------------------------------------------------
  1529. static void barVert(int16_t x, int16_t y, int16_t w, int16_t l, color_t color, color_t outline) {
  1530. _fillTriangle(x+1, y+2*w, x+w, y+w+1, x+2*w-1, y+2*w, color);
  1531. _fillTriangle(x+1, y+2*w+l+1, x+w, y+3*w+l, x+2*w-1, y+2*w+l+1, color);
  1532. _fillRect(x, y+2*w+1, 2*w+1, l, color);
  1533. if (cfont.offset) {
  1534. _drawTriangle(x+1, y+2*w, x+w, y+w+1, x+2*w-1, y+2*w, outline);
  1535. _drawTriangle(x+1, y+2*w+l+1, x+w, y+3*w+l, x+2*w-1, y+2*w+l+1, outline);
  1536. _drawRect(x, y+2*w+1, 2*w+1, l, outline);
  1537. }
  1538. }
  1539. //----------------------------------------------------------------------------------------------
  1540. static void barHor(int16_t x, int16_t y, int16_t w, int16_t l, color_t color, color_t outline) {
  1541. _fillTriangle(x+2*w, y+2*w-1, x+w+1, y+w, x+2*w, y+1, color);
  1542. _fillTriangle(x+2*w+l+1, y+2*w-1, x+3*w+l, y+w, x+2*w+l+1, y+1, color);
  1543. _fillRect(x+2*w+1, y, l, 2*w+1, color);
  1544. if (cfont.offset) {
  1545. _drawTriangle(x+2*w, y+2*w-1, x+w+1, y+w, x+2*w, y+1, outline);
  1546. _drawTriangle(x+2*w+l+1, y+2*w-1, x+3*w+l, y+w, x+2*w+l+1, y+1, outline);
  1547. _drawRect(x+2*w+1, y, l, 2*w+1, outline);
  1548. }
  1549. }
  1550. //--------------------------------------------------------------------------------------------
  1551. static void _draw7seg(int16_t x, int16_t y, int8_t num, int16_t w, int16_t l, color_t color) {
  1552. /* TODO: clipping */
  1553. if (num < 0x2D || num > 0x3A) return;
  1554. int16_t c = font_bcd[num-0x2D];
  1555. int16_t d = 2*w+l+1;
  1556. // === Clear unused segments ===
  1557. /*
  1558. if (!(c & 0x001)) barVert(x+d, y+d, w, l, _bg, _bg);
  1559. if (!(c & 0x002)) barVert(x, y+d, w, l, _bg, _bg);
  1560. if (!(c & 0x004)) barVert(x+d, y, w, l, _bg, _bg);
  1561. if (!(c & 0x008)) barVert(x, y, w, l, _bg, _bg);
  1562. if (!(c & 0x010)) barHor(x, y+2*d, w, l, _bg, _bg);
  1563. if (!(c & 0x020)) barHor(x, y+d, w, l, _bg, _bg);
  1564. if (!(c & 0x040)) barHor(x, y, w, l, _bg, _bg);
  1565. if (!(c & 0x080)) {
  1566. // low point
  1567. _fillRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, _bg);
  1568. if (cfont.offset) _drawRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, _bg);
  1569. }
  1570. if (!(c & 0x100)) {
  1571. // down middle point
  1572. _fillRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, _bg);
  1573. if (cfont.offset) _drawRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, _bg);
  1574. }
  1575. if (!(c & 0x800)) {
  1576. // up middle point
  1577. _fillRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, _bg);
  1578. if (cfont.offset) _drawRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, _bg);
  1579. }
  1580. if (!(c & 0x200)) {
  1581. // middle, minus
  1582. _fillRect(x+2*w+1, y+d, l, 2*w+1, _bg);
  1583. if (cfont.offset) _drawRect(x+2*w+1, y+d, l, 2*w+1, _bg);
  1584. }
  1585. */
  1586. barVert(x+d, y+d, w, l, _bg, _bg);
  1587. barVert(x, y+d, w, l, _bg, _bg);
  1588. barVert(x+d, y, w, l, _bg, _bg);
  1589. barVert(x, y, w, l, _bg, _bg);
  1590. barHor(x, y+2*d, w, l, _bg, _bg);
  1591. barHor(x, y+d, w, l, _bg, _bg);
  1592. barHor(x, y, w, l, _bg, _bg);
  1593. _fillRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, _bg);
  1594. _drawRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, _bg);
  1595. _fillRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, _bg);
  1596. _drawRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, _bg);
  1597. _fillRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, _bg);
  1598. _drawRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, _bg);
  1599. _fillRect(x+2*w+1, y+d, l, 2*w+1, _bg);
  1600. _drawRect(x+2*w+1, y+d, l, 2*w+1, _bg);
  1601. // === Draw used segments ===
  1602. if (c & 0x001) barVert(x+d, y+d, w, l, color, cfont.color); // down right
  1603. if (c & 0x002) barVert(x, y+d, w, l, color, cfont.color); // down left
  1604. if (c & 0x004) barVert(x+d, y, w, l, color, cfont.color); // up right
  1605. if (c & 0x008) barVert(x, y, w, l, color, cfont.color); // up left
  1606. if (c & 0x010) barHor(x, y+2*d, w, l, color, cfont.color); // down
  1607. if (c & 0x020) barHor(x, y+d, w, l, color, cfont.color); // middle
  1608. if (c & 0x040) barHor(x, y, w, l, color, cfont.color); // up
  1609. if (c & 0x080) {
  1610. // low point
  1611. _fillRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, color);
  1612. if (cfont.offset) _drawRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, cfont.color);
  1613. }
  1614. if (c & 0x100) {
  1615. // down middle point
  1616. _fillRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, color);
  1617. if (cfont.offset) _drawRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, cfont.color);
  1618. }
  1619. if (c & 0x800) {
  1620. // up middle point
  1621. _fillRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, color);
  1622. if (cfont.offset) _drawRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, cfont.color);
  1623. }
  1624. if (c & 0x200) {
  1625. // middle, minus
  1626. _fillRect(x+2*w+1, y+d, l, 2*w+1, color);
  1627. if (cfont.offset) _drawRect(x+2*w+1, y+d, l, 2*w+1, cfont.color);
  1628. }
  1629. }
  1630. //==============================================================================
  1631. //======================================
  1632. void EPD_print(char *st, int x, int y) {
  1633. int stl, i, tmpw, tmph, fh;
  1634. uint8_t ch;
  1635. if (cfont.bitmap == 0) return; // wrong font selected
  1636. // ** Rotated strings cannot be aligned
  1637. if ((font_rotate != 0) && ((x <= CENTER) || (y <= CENTER))) return;
  1638. if ((x < LASTX) || (font_rotate == 0)) EPD_OFFSET = 0;
  1639. if ((x >= LASTX) && (x < LASTY)) x = EPD_X + (x-LASTX);
  1640. else if (x > CENTER) x += dispWin.x1;
  1641. if (y >= LASTY) y = EPD_Y + (y-LASTY);
  1642. else if (y > CENTER) y += dispWin.y1;
  1643. // ** Get number of characters in string to print
  1644. stl = strlen(st);
  1645. // ** Calculate CENTER, RIGHT or BOTTOM position
  1646. tmpw = EPD_getStringWidth(st); // string width in pixels
  1647. fh = cfont.y_size; // font height
  1648. if ((cfont.x_size != 0) && (cfont.bitmap == 2)) {
  1649. // 7-segment font
  1650. fh = (3 * (2 * cfont.y_size + 1)) + (2 * cfont.x_size); // 7-seg character height
  1651. }
  1652. if (x == RIGHT) x = dispWin.x2 - tmpw + dispWin.x1;
  1653. else if (x == CENTER) x = (((dispWin.x2 - dispWin.x1 + 1) - tmpw) / 2) + dispWin.x1;
  1654. if (y == BOTTOM) y = dispWin.y2 - fh + dispWin.y1;
  1655. else if (y==CENTER) y = (((dispWin.y2 - dispWin.y1 + 1) - (fh/2)) / 2) + dispWin.y1;
  1656. if (x < dispWin.x1) x = dispWin.x1;
  1657. if (y < dispWin.y1) y = dispWin.y1;
  1658. if ((x > dispWin.x2) || (y > dispWin.y2)) return;
  1659. EPD_X = x;
  1660. EPD_Y = y;
  1661. // ** Adjust y position
  1662. tmph = cfont.y_size; // font height
  1663. // for non-proportional fonts, char width is the same for all chars
  1664. tmpw = cfont.x_size;
  1665. if (cfont.x_size != 0) {
  1666. if (cfont.bitmap == 2) { // 7-segment font
  1667. tmpw = _7seg_width(); // character width
  1668. tmph = _7seg_height(); // character height
  1669. }
  1670. }
  1671. else EPD_OFFSET = 0; // fixed font; offset not needed
  1672. if (( EPD_Y + tmph - 1) > dispWin.y2) return;
  1673. int offset = EPD_OFFSET;
  1674. for (i=0; i<stl; i++) {
  1675. ch = st[i]; // get string character
  1676. if (ch == 0x0D) { // === '\r', erase to eol ====
  1677. if ((!font_transparent) && (font_rotate==0)) _fillRect( EPD_X, EPD_Y, dispWin.x2+1- EPD_X, tmph, _bg);
  1678. }
  1679. else if (ch == 0x0A) { // ==== '\n', new line ====
  1680. if (cfont.bitmap == 1) {
  1681. EPD_Y += tmph + font_line_space;
  1682. if ( EPD_Y > (dispWin.y2-tmph)) break;
  1683. EPD_X = dispWin.x1;
  1684. }
  1685. }
  1686. else { // ==== other characters ====
  1687. if (cfont.x_size == 0) {
  1688. // for proportional font get character data to 'fontChar'
  1689. if (getCharPtr(ch)) tmpw = fontChar.xDelta;
  1690. else continue;
  1691. }
  1692. // check if character can be displayed in the current line
  1693. if (( EPD_X+tmpw) > (dispWin.x2)) {
  1694. if (text_wrap == 0) break;
  1695. EPD_Y += tmph + font_line_space;
  1696. if ( EPD_Y > (dispWin.y2-tmph)) break;
  1697. EPD_X = dispWin.x1;
  1698. }
  1699. // Let's print the character
  1700. if (cfont.x_size == 0) {
  1701. // == proportional font
  1702. if (font_rotate == 0) EPD_X += printProportionalChar( EPD_X, EPD_Y) + 1;
  1703. else {
  1704. // rotated proportional font
  1705. offset += rotatePropChar(x, y, offset);
  1706. EPD_OFFSET = offset;
  1707. }
  1708. }
  1709. else {
  1710. if (cfont.bitmap == 1) {
  1711. // == fixed font
  1712. if ((ch < cfont.offset) || ((ch-cfont.offset) > cfont.numchars)) ch = cfont.offset;
  1713. if (font_rotate == 0) {
  1714. printChar(ch, EPD_X, EPD_Y);
  1715. EPD_X += tmpw;
  1716. }
  1717. else rotateChar(ch, x, y, i);
  1718. }
  1719. else if (cfont.bitmap == 2) {
  1720. // == 7-segment font ==
  1721. _draw7seg( EPD_X, EPD_Y, ch, cfont.y_size, cfont.x_size, _fg);
  1722. EPD_X += (tmpw + 2);
  1723. }
  1724. }
  1725. }
  1726. }
  1727. }
  1728. // ================ Service functions ==========================================
  1729. //=====================================================================
  1730. void EPD_setclipwin(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
  1731. {
  1732. dispWin.x1 = x1;
  1733. dispWin.y1 = y1;
  1734. dispWin.x2 = x2;
  1735. dispWin.y2 = y2;
  1736. if (dispWin.x2 >= EPD_DISPLAY_WIDTH) dispWin.x2 = EPD_DISPLAY_WIDTH-1;
  1737. if (dispWin.y2 >= EPD_DISPLAY_HEIGHT) dispWin.y2 = EPD_DISPLAY_HEIGHT-1;
  1738. if (dispWin.x1 > dispWin.x2) dispWin.x1 = dispWin.x2;
  1739. if (dispWin.y1 > dispWin.y2) dispWin.y1 = dispWin.y2;
  1740. }
  1741. //=====================
  1742. void EPD_resetclipwin()
  1743. {
  1744. dispWin.x2 = EPD_DISPLAY_WIDTH-1;
  1745. dispWin.y2 = EPD_DISPLAY_HEIGHT-1;
  1746. dispWin.x1 = 0;
  1747. dispWin.y1 = 0;
  1748. }
  1749. //==========================================================================
  1750. void set_7seg_font_atrib(uint8_t l, uint8_t w, int outline, color_t color) {
  1751. if (cfont.bitmap != 2) return;
  1752. if (l < 6) l = 6;
  1753. if (l > 40) l = 40;
  1754. if (w < 1) w = 1;
  1755. if (w > (l/2)) w = l/2;
  1756. if (w > 12) w = 12;
  1757. cfont.x_size = l;
  1758. cfont.y_size = w;
  1759. cfont.offset = outline;
  1760. cfont.color = color;
  1761. }
  1762. //==========================================
  1763. int EPD_getfontsize(int *width, int* height)
  1764. {
  1765. if (cfont.bitmap == 1) {
  1766. if (cfont.x_size != 0) *width = cfont.x_size; // fixed width font
  1767. else *width = cfont.max_x_size; // proportional font
  1768. *height = cfont.y_size;
  1769. }
  1770. else if (cfont.bitmap == 2) {
  1771. // 7-segment font
  1772. *width = _7seg_width();
  1773. *height = _7seg_height();
  1774. }
  1775. else {
  1776. *width = 0;
  1777. *height = 0;
  1778. return 0;
  1779. }
  1780. return 1;
  1781. }
  1782. //=====================
  1783. int EPD_getfontheight()
  1784. {
  1785. if (cfont.bitmap == 1) return cfont.y_size; // Bitmap font
  1786. else if (cfont.bitmap == 2) return _7seg_height(); // 7-segment font
  1787. return 0;
  1788. }
  1789. //====================
  1790. void EPD_saveClipWin()
  1791. {
  1792. dispWinTemp.x1 = dispWin.x1;
  1793. dispWinTemp.y1 = dispWin.y1;
  1794. dispWinTemp.x2 = dispWin.x2;
  1795. dispWinTemp.y2 = dispWin.y2;
  1796. }
  1797. //=======================
  1798. void EPD_restoreClipWin()
  1799. {
  1800. dispWin.x1 = dispWinTemp.x1;
  1801. dispWin.y1 = dispWinTemp.y1;
  1802. dispWin.x2 = dispWinTemp.x2;
  1803. dispWin.y2 = dispWinTemp.y2;
  1804. }
  1805. // ================ JPG SUPPORT ================================================
  1806. // RGB to GRAYSCALE constants
  1807. // 0.2989 0.5870 0.1140
  1808. #define GS_FACT_R 0.2989
  1809. #define GS_FACT_G 0.4870
  1810. #define GS_FACT_B 0.2140
  1811. // User defined device identifier
  1812. typedef struct {
  1813. FILE *fhndl; // File handler for input function
  1814. int x; // image top left point X position
  1815. int y; // image top left point Y position
  1816. uint8_t *membuff; // memory buffer containing the image
  1817. uint32_t bufsize; // size of the memory buffer
  1818. uint32_t bufptr; // memory buffer current position
  1819. } JPGIODEV;
  1820. // User defined call-back function to input JPEG data from file
  1821. //---------------------
  1822. static UINT tjd_input (
  1823. JDEC* jd, // Decompression object
  1824. BYTE* buff, // Pointer to the read buffer (NULL:skip)
  1825. UINT nd // Number of bytes to read/skip from input stream
  1826. )
  1827. {
  1828. int rb = 0;
  1829. // Device identifier for the session (5th argument of jd_prepare function)
  1830. JPGIODEV *dev = (JPGIODEV*)jd->device;
  1831. if (buff) { // Read nd bytes from the input strem
  1832. rb = fread(buff, 1, nd, dev->fhndl);
  1833. return rb; // Returns actual number of bytes read
  1834. }
  1835. else { // Remove nd bytes from the input stream
  1836. if (fseek(dev->fhndl, nd, SEEK_CUR) >= 0) return nd;
  1837. else return 0;
  1838. }
  1839. }
  1840. // User defined call-back function to input JPEG data from memory buffer
  1841. //-------------------------
  1842. static UINT tjd_buf_input (
  1843. JDEC* jd, // Decompression object
  1844. BYTE* buff, // Pointer to the read buffer (NULL:skip)
  1845. UINT nd // Number of bytes to read/skip from input stream
  1846. )
  1847. {
  1848. // Device identifier for the session (5th argument of jd_prepare function)
  1849. JPGIODEV *dev = (JPGIODEV*)jd->device;
  1850. if (!dev->membuff) return 0;
  1851. if (dev->bufptr >= (dev->bufsize + 2)) return 0; // end of stream
  1852. if ((dev->bufptr + nd) > (dev->bufsize + 2)) nd = (dev->bufsize + 2) - dev->bufptr;
  1853. if (buff) { // Read nd bytes from the input strem
  1854. memcpy(buff, dev->membuff + dev->bufptr, nd);
  1855. dev->bufptr += nd;
  1856. return nd; // Returns number of bytes read
  1857. }
  1858. else { // Remove nd bytes from the input stream
  1859. dev->bufptr += nd;
  1860. return nd;
  1861. }
  1862. }
  1863. // User defined call-back function to output RGB bitmap to display device
  1864. //----------------------
  1865. static UINT tjd_output (
  1866. JDEC* jd, // Decompression object of current session
  1867. void* bitmap, // Bitmap data to be output
  1868. JRECT* rect // Rectangular region to output
  1869. )
  1870. {
  1871. // Device identifier for the session (5th argument of jd_prepare function)
  1872. JPGIODEV *dev = (JPGIODEV*)jd->device;
  1873. // ** Put the rectangular into the display device **
  1874. int x;
  1875. int y;
  1876. int dleft, dtop, dright, dbottom;
  1877. BYTE *src = (BYTE*)bitmap;
  1878. int left = rect->left + dev->x;
  1879. int top = rect->top + dev->y;
  1880. int right = rect->right + dev->x;
  1881. int bottom = rect->bottom + dev->y;
  1882. if ((left > dispWin.x2) || (top > dispWin.y2)) return 1; // out of screen area, return
  1883. if ((right < dispWin.x1) || (bottom < dispWin.y1)) return 1;// out of screen area, return
  1884. if (left < dispWin.x1) dleft = dispWin.x1;
  1885. else dleft = left;
  1886. if (top < dispWin.y1) dtop = dispWin.y1;
  1887. else dtop = top;
  1888. if (right > dispWin.x2) dright = dispWin.x2;
  1889. else dright = right;
  1890. if (bottom > dispWin.y2) dbottom = dispWin.y2;
  1891. else dbottom = bottom;
  1892. if ((dleft > dispWin.x2) || (dtop > dispWin.y2)) return 1; // out of screen area, return
  1893. if ((dright < dispWin.x1) || (dbottom < dispWin.y1)) return 1; // out of screen area, return
  1894. uint32_t len = ((dright-dleft+1) * (dbottom-dtop+1)); // calculate length of data
  1895. float gs_clr = 0;
  1896. uint8_t rgb_color[3];
  1897. uint8_t last_lvl, i;
  1898. uint8_t pix;
  1899. if ((len > 0) && (len <= JPG_IMAGE_LINE_BUF_SIZE)) {
  1900. for (y = top; y <= bottom; y++) {
  1901. for (x = left; x <= right; x++) {
  1902. // Clip to display area
  1903. if ((x >= dleft) && (y >= dtop) && (x <= dright) && (y <= dbottom)) {
  1904. // Directly convert color to 4-bit gray scale
  1905. pix = 0;
  1906. pix |= ((*src++) >> 4) & 0x08;
  1907. pix |= ((*src++) >> 5) & 0x06;
  1908. pix |= ((*src++) >> 7);
  1909. pix ^= 0x0F;
  1910. /* Convert rgb color to gray scale
  1911. memcpy(rgb_color, src, 3);
  1912. src += 3;
  1913. gs_clr = (GS_FACT_R * rgb_color[0]) + (GS_FACT_G * rgb_color[1]) + (GS_FACT_B * rgb_color[2]);
  1914. if (gs_clr > 255) gs_clr = 255;
  1915. // Use only 4 bits & invert
  1916. //pix = ((uint8_t)gs_clr >> 4) ^ 0x0F;
  1917. pix = (uint8_t)gs_clr;
  1918. // Using gray scale lookup table
  1919. last_lvl = 0;
  1920. i = 0;
  1921. for (i=0; i<16; i++) {
  1922. if ((pix > last_lvl) && (pix <= lvl_buf_jpg[i])) {
  1923. pix = 15 - i;
  1924. last_lvl = lvl_buf[i];
  1925. break;
  1926. }
  1927. last_lvl = lvl_buf[i];
  1928. }
  1929. */
  1930. gs_disp_buffer[(y * EPD_DISPLAY_WIDTH) + x] = pix;
  1931. gs_used_shades |= (1 << pix);
  1932. }
  1933. else src += 3; // skip
  1934. }
  1935. }
  1936. }
  1937. else {
  1938. printf("Data size error: %d jpg: (%d,%d,%d,%d) disp: (%d,%d,%d,%d)\r\n", len, left,top,right,bottom, dleft,dtop,dright,dbottom);
  1939. return 0; // stop decompression
  1940. }
  1941. return 1; // Continue to decompression
  1942. }
  1943. // X & Y can be < 0 !
  1944. //=================================================================================
  1945. int EPD_jpg_image(int x, int y, uint8_t scale, char *fname, uint8_t *buf, int size)
  1946. {
  1947. JPGIODEV dev;
  1948. struct stat sb;
  1949. char *work = NULL; // Pointer to the working buffer (must be 4-byte aligned)
  1950. UINT sz_work = 3800; // Size of the working buffer (must be power of 2)
  1951. JDEC jd; // Decompression object (70 bytes)
  1952. JRESULT rc;
  1953. int res = -10;
  1954. if (fname == NULL) {
  1955. // image from buffer
  1956. dev.fhndl = NULL;
  1957. dev.membuff = buf;
  1958. dev.bufsize = size;
  1959. dev.bufptr = 0;
  1960. }
  1961. else {
  1962. // image from file
  1963. dev.membuff = NULL;
  1964. dev.bufsize = 0;
  1965. dev.bufptr = 0;
  1966. if (stat(fname, &sb) != 0) {
  1967. if (image_debug) printf("File error: %ss\r\n", strerror(errno));
  1968. res = -11;
  1969. goto exit;
  1970. }
  1971. dev.fhndl = fopen(fname, "r");
  1972. if (!dev.fhndl) {
  1973. if (image_debug) printf("Error opening file: %s\r\n", strerror(errno));
  1974. res = -12;
  1975. goto exit;
  1976. }
  1977. }
  1978. if (scale > 3) scale = 3;
  1979. work = malloc(sz_work);
  1980. if (work) {
  1981. if (dev.membuff) rc = jd_prepare(&jd, tjd_buf_input, (void *)work, sz_work, &dev);
  1982. else rc = jd_prepare(&jd, tjd_input, (void *)work, sz_work, &dev);
  1983. if (rc == JDR_OK) {
  1984. if (x == CENTER) x = ((dispWin.x2 - dispWin.x1 + 1 - (int)(jd.width >> scale)) / 2) + dispWin.x1;
  1985. else if (x == RIGHT) x = dispWin.x2 + 1 - (int)(jd.width >> scale);
  1986. if (y == CENTER) y = ((dispWin.y2 - dispWin.y1 + 1 - (int)(jd.height >> scale)) / 2) + dispWin.y1;
  1987. else if (y == BOTTOM) y = dispWin.y2 + 1 - (int)(jd.height >> scale);
  1988. if (x < ((dispWin.x2-1) * -1)) x = (dispWin.x2-1) * -1;
  1989. if (y < ((dispWin.y2-1)) * -1) y = (dispWin.y2-1) * -1;
  1990. if (x > (dispWin.x2-1)) x = dispWin.x2 - 1;
  1991. if (y > (dispWin.y2-1)) y = dispWin.y2-1;
  1992. dev.x = x;
  1993. dev.y = y;
  1994. // Start to decode the JPEG file
  1995. rc = jd_decomp(&jd, tjd_output, scale);
  1996. if (rc != JDR_OK) {
  1997. if (image_debug) printf("jpg decompression error %d\r\n", rc);
  1998. res = rc * -1;
  1999. }
  2000. res = 0;
  2001. if (image_debug) printf("Jpg size: %dx%d, position; %d,%d, scale: %d, bytes used: %d\r\n", jd.width, jd.height, x, y, scale, jd.sz_pool);
  2002. }
  2003. else {
  2004. if (image_debug) printf("jpg prepare error %d\r\n", rc);
  2005. res = rc * -1;
  2006. }
  2007. }
  2008. else {
  2009. if (image_debug) printf("work buffer allocation error\r\n");
  2010. res = -13;
  2011. }
  2012. exit:
  2013. if (work) free(work); // free work buffer
  2014. if (dev.fhndl) fclose(dev.fhndl); // close input file
  2015. return res;
  2016. }