No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

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