luat_lib_eink._c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. @module eink
  3. @summary 墨水屏操作库
  4. @version 1.0
  5. @date 2020.11.14
  6. */
  7. #include "luat_base.h"
  8. #include "luat_log.h"
  9. #include "luat_sys.h"
  10. #include "luat_msgbus.h"
  11. #include "luat_timer.h"
  12. #include "luat_malloc.h"
  13. #include "luat_spi.h"
  14. #include "luat_gpio.h"
  15. #include "epd1in54.h"
  16. // #include "epd2in9.h"
  17. #include "epdif.h"
  18. #include "epdpaint.h"
  19. #include "imagedata.h"
  20. #include "qrcode.h"
  21. #include <stdlib.h>
  22. #include "u8g2.h"
  23. #define Pin_BUSY (18)
  24. #define Pin_RES (7)
  25. #define Pin_DC (9)
  26. #define Pin_CS (16)
  27. #define SPI_ID (0)
  28. #define COLORED 0
  29. #define UNCOLORED 1
  30. #define LUAT_LOG_TAG "luat.eink"
  31. static EPD epd;
  32. static Paint paint;
  33. static unsigned char* frame_buffer;
  34. /**
  35. 初始化eink
  36. @api eink.setup(full, spiid)
  37. @int 全屏刷新1,局部刷新0,默认是全屏刷新
  38. @int 所在的spi,默认是0
  39. @return boolean 成功返回true,否则返回false
  40. */
  41. static int l_eink_setup(lua_State *L) {
  42. int num = luaL_optinteger(L, 1, 1);
  43. int spi_id = luaL_optinteger(L, 2, 0);
  44. if (frame_buffer != NULL) {
  45. lua_pushboolean(L, 1);
  46. return 0;
  47. }
  48. luat_spi_t spi_config = {0};
  49. spi_config.bandrate = 2000000U;//luaL_optinteger(L, 1, 2000000U); // 2000000U
  50. spi_config.id = spi_id;
  51. spi_config.cs = 255; // 默认无
  52. spi_config.CPHA = 0; // CPHA0
  53. spi_config.CPOL = 0; // CPOL0
  54. spi_config.dataw = 8; // 8bit
  55. spi_config.bit_dict = 1; // MSB=1, LSB=0
  56. spi_config.master = 1; // master=1,slave=0
  57. spi_config.mode = 1; // FULL=1, half=0
  58. //LLOGD("setup GPIO for epd");
  59. // TODO 事实上无法配置
  60. luat_gpio_mode(luaL_optinteger(L, 3, Pin_BUSY), Luat_GPIO_INPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  61. luat_gpio_mode(luaL_optinteger(L, 4, Pin_RES), Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  62. luat_gpio_mode(luaL_optinteger(L, 5, Pin_DC), Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  63. luat_gpio_mode(luaL_optinteger(L, 6, Pin_CS), Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  64. //LLOGD("spi setup>>>");
  65. int status = luat_spi_setup(&spi_config);
  66. //LLOGD("spi setup<<<");
  67. if(status == 0)
  68. {
  69. LLOGD("spi setup complete, now setup epd");
  70. if(num)
  71. status = EPD_Init(&epd, lut_full_update);
  72. else
  73. status = EPD_Init(&epd, lut_partial_update);
  74. if (status != 0) {
  75. LLOGD("e-Paper init failed");
  76. return 0;
  77. }
  78. frame_buffer = (unsigned char*)luat_heap_malloc(EPD_WIDTH * EPD_HEIGHT / 8);
  79. // Paint paint;
  80. Paint_Init(&paint, frame_buffer, epd.width, epd.height);
  81. Paint_Clear(&paint, UNCOLORED);
  82. }
  83. //LLOGD("epd init complete");
  84. lua_pushboolean(L, 1);
  85. return 1;
  86. }
  87. /**
  88. 清除绘图缓冲区
  89. @api eink.clear()
  90. @return nil 无返回值,不会马上刷新到设备
  91. */
  92. static int l_eink_clear(lua_State *L)
  93. {
  94. int colored = luaL_optinteger(L, 1, 1);
  95. Paint_Clear(&paint, colored);
  96. return 0;
  97. }
  98. /**
  99. 设置窗口
  100. @api eink.setWin(width, height, rotate)
  101. @int width 宽度
  102. @int height 高度
  103. @int rotate 显示方向,0/1/2/3, 相当于旋转0度/90度/180度/270度
  104. @return nil 无返回值
  105. */
  106. static int l_eink_setWin(lua_State *L)
  107. {
  108. int width = luaL_checkinteger(L, 1);
  109. int height = luaL_checkinteger(L, 2);
  110. int rotate = luaL_checkinteger(L, 3);
  111. Paint_SetWidth(&paint, width);
  112. Paint_SetHeight(&paint, height);
  113. Paint_SetRotate(&paint, rotate);
  114. return 0;
  115. }
  116. /**
  117. 获取窗口信息
  118. @api eink.getWin()
  119. @return int width 宽
  120. @return int height 高
  121. @return int rotate 旋转方向
  122. */
  123. static int l_eink_getWin(lua_State *L)
  124. {
  125. int width = Paint_GetWidth(&paint);
  126. int height = Paint_GetHeight(&paint);
  127. int rotate = Paint_GetRotate(&paint);
  128. lua_pushinteger(L, width);
  129. lua_pushinteger(L, height);
  130. lua_pushinteger(L, rotate);
  131. return 3;
  132. }
  133. /**
  134. 绘制字符串(仅ASCII)
  135. @api eink.print(x, y, str, colored, font)
  136. @int x坐标
  137. @int y坐标
  138. @string 字符串
  139. @int 默认是0
  140. @font 字体大小,默认12
  141. @return nil 无返回值
  142. */
  143. static int l_eink_print(lua_State *L)
  144. {
  145. size_t len;
  146. int x = luaL_checkinteger(L, 1);
  147. int y = luaL_checkinteger(L, 2);
  148. const char *str = luaL_checklstring(L, 3, &len);
  149. int colored = luaL_optinteger(L, 4, 0);
  150. int font = luaL_optinteger(L, 5, 12);
  151. switch (font)
  152. {
  153. case 8:
  154. Paint_DrawStringAt(&paint, x, y, str, &Font8, colored);
  155. break;
  156. case 12:
  157. Paint_DrawStringAt(&paint, x, y, str, &Font12, colored);
  158. break;
  159. case 16:
  160. Paint_DrawStringAt(&paint, x, y, str, &Font16, colored);
  161. break;
  162. case 20:
  163. Paint_DrawStringAt(&paint, x, y, str, &Font20, colored);
  164. break;
  165. case 24:
  166. Paint_DrawStringAt(&paint, x, y, str, &Font24, colored);
  167. break;
  168. default:
  169. break;
  170. }
  171. return 0;
  172. }
  173. // void u8g2_read_font_info(u8g2_font_info_t *font_info, const uint8_t *font);
  174. uint8_t luat_u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  175. const uint8_t *u8g2_font_get_glyph_data(u8g2_t *u8g2, uint16_t encoding);
  176. uint8_t * font_pix_find(uint16_t e, int font_pix_find);
  177. /**
  178. 绘制字符串,支持中文
  179. @api eink.printcn(x, y, str, colored, font)
  180. @int x坐标
  181. @int y坐标
  182. @string 字符串
  183. @int 默认是0
  184. @font 字体大小,默认12
  185. @return nil 无返回值
  186. */
  187. static int l_eink_printcn(lua_State *L)
  188. {
  189. size_t len;
  190. int x = luaL_checkinteger(L, 1);
  191. int y = luaL_checkinteger(L, 2);
  192. const char *str = luaL_checklstring(L, 3, &len);
  193. int colored = luaL_optinteger(L, 4, 0);
  194. int font_size = luaL_optinteger(L, 5, 12);
  195. //LLOGD("printcn font_size %d len %d", font_size, len);
  196. // 解码数据 TODO 使用无u8g2的方案
  197. u8g2_t *u8g2 = luat_heap_malloc(sizeof(u8g2_t));
  198. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_byte_sw_i2c, luat_u8x8_gpio_and_delay);
  199. u8g2_InitDisplay(u8g2);
  200. u8g2_SetPowerSave(u8g2, 0);
  201. u8g2->u8x8.next_cb = u8x8_utf8_next;
  202. uint16_t e;
  203. u8g2_uint_t delta, sum;
  204. u8x8_utf8_init(u8g2_GetU8x8(u8g2));
  205. sum = 0;
  206. uint8_t* str2 = (uint8_t*)str;
  207. for(;;)
  208. {
  209. e = u8g2->u8x8.next_cb(u8g2_GetU8x8(u8g2), (uint8_t)*str2);
  210. //LLOGD("chinese >> 0x%04X", e);
  211. if ( e == 0x0ffff )
  212. break;
  213. str2++;
  214. // if (e != 0x0fffe && e < 0x007e) {
  215. // char ch[2] = {e, 0};
  216. // if (font_size == 16)
  217. // Paint_DrawStringAt(&paint, x, y, ch, &Font16, colored);
  218. // else if (font_size == 24)
  219. // Paint_DrawStringAt(&paint, x, y, ch, &Font24, colored);
  220. // x += font_size;
  221. // }
  222. // else
  223. if ( e != 0x0fffe )
  224. {
  225. //delta = u8g2_DrawGlyph(u8g2, x, y, e);
  226. uint8_t * f = font_pix_find(e, font_size);
  227. if (f != NULL) {
  228. // 当前仅支持16p和24p
  229. int datalen = font_size == 16 ? 32 : 72;
  230. int xlen = font_size == 16 ? 2 : 3;
  231. //LLOGD("found FONT DATA 0x%04X datalen=%d xlen=%d", e, datalen, xlen);
  232. for (size_t i = 0; i < datalen; )
  233. {
  234. for (size_t k = 0; k < xlen; k++)
  235. {
  236. uint8_t pix = f[i];
  237. //LLOGD("pix data %02X x+j=%d y+j/2=%d", pix, x, y+i/2);
  238. for (size_t j = 0; j < 8; j++)
  239. {
  240. if ((pix >> (7-j)) & 0x01) {
  241. Paint_DrawPixel(&paint, x+j+k*8, y+i/xlen, colored);
  242. }
  243. }
  244. i++;
  245. }
  246. }
  247. }
  248. else {
  249. LLOGD("NOT found FONT DATA 0x%04X", e);
  250. }
  251. if (e <= 0x7E)
  252. x += font_size / 2;
  253. else
  254. {
  255. x += font_size;
  256. }
  257. }
  258. }
  259. luat_heap_free(u8g2);
  260. return 0;
  261. }
  262. /**
  263. 将缓冲区图像输出到屏幕
  264. @api eink.show(x, y)
  265. @int x 输出的x坐标,默认0
  266. @int y 输出的y坐标,默认0
  267. @return nil 无返回值
  268. */
  269. static int l_eink_show(lua_State *L)
  270. {
  271. int x = luaL_optinteger(L, 1, 0);
  272. int y = luaL_optinteger(L, 2, 0);
  273. /* Display the frame_buffer */
  274. EPD_SetFrameMemory(&epd, frame_buffer, x, y, Paint_GetWidth(&paint), Paint_GetHeight(&paint));
  275. EPD_DisplayFrame(&epd);
  276. return 0;
  277. }
  278. /**
  279. 缓冲区绘制线
  280. @api eink.line(x, y, x2, y2, colored)
  281. @int 起点x坐标
  282. @int 起点y坐标
  283. @int 终点x坐标
  284. @int 终点y坐标
  285. @return nil 无返回值
  286. @usage
  287. eink.line(0, 0, 10, 20, 0)
  288. */
  289. static int l_eink_line(lua_State *L)
  290. {
  291. int x = luaL_checkinteger(L, 1);
  292. int y = luaL_checkinteger(L, 2);
  293. int x2 = luaL_checkinteger(L, 3);
  294. int y2 = luaL_checkinteger(L, 4);
  295. int colored = luaL_optinteger(L, 5, 0);
  296. Paint_DrawLine(&paint, x, y, x2, y2, colored);
  297. return 0;
  298. }
  299. /**
  300. 缓冲区绘制矩形
  301. @api eink.rect(x, y, x2, y2, colored, fill)
  302. @int 左上顶点x坐标
  303. @int 左上顶点y坐标
  304. @int 右下顶点x坐标
  305. @int 右下顶点y坐标
  306. @int 默认是0
  307. @int 是否填充,默认是0,不填充
  308. @return nil 无返回值
  309. @usage
  310. eink.rect(0, 0, 10, 20)
  311. eink.rect(0, 0, 10, 20, 1) -- Filled
  312. */
  313. static int l_eink_rect(lua_State *L)
  314. {
  315. int x = luaL_checkinteger(L, 1);
  316. int y = luaL_checkinteger(L, 2);
  317. int x2 = luaL_checkinteger(L, 3);
  318. int y2 = luaL_checkinteger(L, 4);
  319. int colored = luaL_optinteger(L, 5, 0);
  320. int fill = luaL_optinteger(L, 6, 0);
  321. if(fill)
  322. Paint_DrawFilledRectangle(&paint, x, y, x2, y2, colored);
  323. else
  324. Paint_DrawRectangle(&paint, x, y, x2, y2, colored);
  325. return 0;
  326. }
  327. /**
  328. 缓冲区绘制圆形
  329. @api eink.circle(x, y, radius, colored, fill)
  330. @int 圆心x坐标
  331. @int 圆心y坐标
  332. @int 半径
  333. @int 默认是0
  334. @int 是否填充,默认是0,不填充
  335. @return nil 无返回值
  336. @usage
  337. eink.circle(0, 0, 10)
  338. eink.circle(0, 0, 10, 1, 1) -- Filled
  339. */
  340. static int l_eink_circle(lua_State *L)
  341. {
  342. int x = luaL_checkinteger(L, 1);
  343. int y = luaL_checkinteger(L, 2);
  344. int radius = luaL_checkinteger(L, 3);
  345. int colored = luaL_optinteger(L, 4, 0);
  346. int fill = luaL_optinteger(L, 5, 0);
  347. if(fill)
  348. Paint_DrawFilledCircle(&paint, x, y, radius, colored);
  349. else
  350. Paint_DrawCircle(&paint, x, y, radius, colored);
  351. return 0;
  352. }
  353. /**
  354. 缓冲区绘制QRCode
  355. @api eink.qrcode(x, y, str, version)
  356. @int x坐标
  357. @int y坐标
  358. @string 二维码的内容
  359. @int 二维码版本号
  360. @return nil 无返回值
  361. */
  362. static int l_eink_qrcode(lua_State *L)
  363. {
  364. size_t len;
  365. int x = luaL_checkinteger(L, 1);
  366. int y = luaL_checkinteger(L, 2);
  367. const char* str = luaL_checklstring(L, 3, &len);
  368. int version = luaL_checkinteger(L, 4);
  369. // Create the QR code
  370. QRCode qrcode;
  371. uint8_t qrcodeData[qrcode_getBufferSize(version)];
  372. qrcode_initText(&qrcode, qrcodeData, version, 0, str);
  373. for(int i = 0; i < qrcode.size; i++)
  374. {
  375. for (int j = 0; j < qrcode.size; j++)
  376. {
  377. qrcode_getModule(&qrcode, j, i) ? Paint_DrawPixel(&paint, x+j, y+i, COLORED) : Paint_DrawPixel(&paint, x+j, y+i, UNCOLORED);
  378. }
  379. }
  380. return 0;
  381. }
  382. /**
  383. 缓冲区绘制电池
  384. @api eink.bat(x, y, bat)
  385. @int x坐标
  386. @int y坐标
  387. @int 电池电压,单位毫伏
  388. @return nil 无返回值
  389. */
  390. static int l_eink_bat(lua_State *L)
  391. {
  392. int x = luaL_checkinteger(L, 1);
  393. int y = luaL_checkinteger(L, 2);
  394. int bat = luaL_checkinteger(L, 3);
  395. int batnum = 0;
  396. // eink.rect(0, 3, 2, 6)
  397. // eink.rect(2, 0, 20, 9)
  398. // eink.rect(9, 1, 19, 8, 0, 1)
  399. if(bat < 4200 && bat > 4080)batnum = 100;
  400. if(bat < 4080 && bat > 4000)batnum = 90;
  401. if(bat < 4000 && bat > 3930)batnum = 80;
  402. if(bat < 3930 && bat > 3870)batnum = 70;
  403. if(bat < 3870 && bat > 3820)batnum = 60;
  404. if(bat < 3820 && bat > 3790)batnum = 50;
  405. if(bat < 3790 && bat > 3770)batnum = 40;
  406. if(bat < 3770 && bat > 3730)batnum = 30;
  407. if(bat < 3730 && bat > 3700)batnum = 20;
  408. if(bat < 3700 && bat > 3680)batnum = 15;
  409. if(bat < 3680 && bat > 3500)batnum = 10;
  410. if(bat < 3500 && bat > 2500)batnum = 5;
  411. batnum = 20 - (int)(batnum / 5) + 3;
  412. // w外框
  413. Paint_DrawRectangle(&paint, x+0, y+3, x+2, y+6, COLORED);
  414. Paint_DrawRectangle(&paint, x+2, y+0, x+23, y+9, COLORED);
  415. // 3 ~21 100 / 5
  416. Paint_DrawFilledRectangle(&paint, x+batnum, y+1, x+22, y+8, COLORED);
  417. return 0;
  418. }
  419. /**
  420. 缓冲区绘制天气图标
  421. @api eink.weather_icon(x, y, code)
  422. @int x坐标
  423. @int y坐标
  424. @int 天气代号
  425. @return nil 无返回值
  426. */
  427. static int l_eink_weather_icon(lua_State *L)
  428. {
  429. size_t len;
  430. int x = luaL_checkinteger(L, 1);
  431. int y = luaL_checkinteger(L, 2);
  432. int code = luaL_checkinteger(L, 3);
  433. const char* str = luaL_optlstring(L, 4, "nil", &len);
  434. const unsigned char * icon = gImage_999;
  435. if (strcmp(str, "xue") == 0)code = 401;
  436. if (strcmp(str, "lei") == 0)code = 302;
  437. if (strcmp(str, "shachen") == 0)code = 503;
  438. if (strcmp(str, "wu") == 0)code = 501;
  439. if (strcmp(str, "bingbao") == 0)code = 504;
  440. if (strcmp(str, "yun") == 0)code = 103;
  441. if (strcmp(str, "yu") == 0)code = 306;
  442. if (strcmp(str, "yin") == 0)code = 101;
  443. if (strcmp(str, "qing") == 0)code = 100;
  444. // xue、lei、shachen、wu、bingbao、yun、yu、yin、qing
  445. //if(code == 64)
  446. // for(int i = 0; i < 64; i++)
  447. // {
  448. // for (int j = 0; j < 8; j++)
  449. // {
  450. // for (int k = 0; k < 8; k++)
  451. // (gImage_103[i*8+j] << k )& 0x80 ? Paint_DrawPixel(&paint, x+j*8+k, y+i, COLORED) : Paint_DrawPixel(&paint, x+j*8+k, y+i, UNCOLORED);
  452. // }
  453. // }
  454. switch (code)
  455. {
  456. case 100:icon = gImage_100;break;
  457. case 101:icon = gImage_101;break;
  458. case 102:icon = gImage_102;break;
  459. case 103:icon = gImage_103;break;
  460. case 104:icon = gImage_104;break;
  461. case 200:icon = gImage_200;break;
  462. case 201:icon = gImage_201;break;
  463. case 205:icon = gImage_205;break;
  464. case 208:icon = gImage_208;break;
  465. case 301:icon = gImage_301;break;
  466. case 302:icon = gImage_302;break;
  467. case 303:icon = gImage_303;break;
  468. case 304:icon = gImage_304;break;
  469. case 305:icon = gImage_305;break;
  470. case 306:icon = gImage_306;break;
  471. case 307:icon = gImage_307;break;
  472. case 308:icon = gImage_308;break;
  473. case 309:icon = gImage_309;break;
  474. case 310:icon = gImage_310;break;
  475. case 311:icon = gImage_311;break;
  476. case 312:icon = gImage_312;break;
  477. case 313:icon = gImage_313;break;
  478. case 400:icon = gImage_400;break;
  479. case 401:icon = gImage_401;break;
  480. case 402:icon = gImage_402;break;
  481. case 403:icon = gImage_403;break;
  482. case 404:icon = gImage_404;break;
  483. case 405:icon = gImage_405;break;
  484. case 406:icon = gImage_406;break;
  485. case 407:icon = gImage_407;break;
  486. case 500:icon = gImage_500;break;
  487. case 501:icon = gImage_501;break;
  488. case 502:icon = gImage_502;break;
  489. case 503:icon = gImage_503;break;
  490. case 504:icon = gImage_504;break;
  491. case 507:icon = gImage_507;break;
  492. case 508:icon = gImage_508;break;
  493. case 900:icon = gImage_900;break;
  494. case 901:icon = gImage_901;break;
  495. case 999:icon = gImage_999;break;
  496. default:
  497. break;
  498. }
  499. for(int i = 0; i < 48; i++)
  500. {
  501. for (int j = 0; j < 6; j++)
  502. {
  503. for (int k = 0; k < 8; k++)
  504. (icon[i*6+j] << k )& 0x80 ? Paint_DrawPixel(&paint, x+j*8+k, y+i, COLORED) : Paint_DrawPixel(&paint, x+j*8+k, y+i, UNCOLORED);
  505. }
  506. }
  507. return 0;
  508. }
  509. static int l_eink_model(lua_State *L) {
  510. if (lua_gettop(L) > 0) {
  511. EPD_Model(luaL_checkinteger(L, 1));
  512. }
  513. return 0;
  514. }
  515. #include "rotable.h"
  516. static const rotable_Reg reg_eink[] =
  517. {
  518. { "model", l_eink_model, 0},
  519. { "setup", l_eink_setup, 0},
  520. { "clear", l_eink_clear, 0},
  521. { "setWin", l_eink_setWin, 0},
  522. { "getWin", l_eink_getWin, 0},
  523. { "print", l_eink_print, 0},
  524. { "printcn", l_eink_printcn, 0},
  525. { "show", l_eink_show, 0},
  526. { "rect", l_eink_rect, 0},
  527. { "circle", l_eink_circle, 0},
  528. { "line", l_eink_line, 0},
  529. { "qrcode", l_eink_qrcode, 0},
  530. { "bat", l_eink_bat, 0},
  531. { "weather_icon", l_eink_weather_icon, 0},
  532. { NULL, NULL, 0}
  533. };
  534. // LUAMOD_API int luaopen_eink( lua_State *L ){
  535. // rotable_newlib(L, reg_eink);
  536. // return 1;
  537. // }