luat_lib_eink.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 "epd.h"
  18. #include "epdpaint.h"
  19. #include "imagedata.h"
  20. #include "../qrcode/qrcode.h"
  21. #include <stdlib.h>
  22. // #include "u8g2.h"
  23. #define COLORED 0
  24. #define UNCOLORED 1
  25. #define LUAT_LOG_TAG "luat.eink"
  26. enum
  27. {
  28. font_opposansm8,
  29. font_opposansm10,
  30. font_opposansm12,
  31. font_opposansm16,
  32. font_opposansm18,
  33. font_opposansm20,
  34. font_opposansm22,
  35. font_opposansm24,
  36. font_opposansm32,
  37. font_opposansm12_chinese,
  38. font_opposansm16_chinese,
  39. font_opposansm24_chinese,
  40. font_opposansm32_chinese,
  41. };
  42. static uint32_t eink_str_color;
  43. // static EPD epd;
  44. static Paint paint;
  45. static unsigned char* frame_buffer = NULL;
  46. // #ifdef econf
  47. // #undef econf
  48. // #endif
  49. eink_conf_t econf = {0};
  50. #define Pin_BUSY (econf.busy_pin)
  51. #define Pin_RES (econf.res_pin)
  52. #define Pin_DC (econf.dc_pin)
  53. #define Pin_CS (econf.cs_pin)
  54. #define SPI_ID (econf.spi_id)
  55. /**
  56. 初始化eink
  57. @api eink.setup(full, spiid)
  58. @int 全屏刷新1,局部刷新0,默认是全屏刷新
  59. @int 所在的spi,默认是0
  60. @return boolean 成功返回true,否则返回false
  61. */
  62. static int l_eink_setup(lua_State *L) {
  63. int status;
  64. econf.full_mode = luaL_optinteger(L, 1, 1);
  65. econf.spi_id = luaL_optinteger(L, 2, 0);
  66. econf.busy_pin = luaL_optinteger(L, 3, 18);
  67. econf.res_pin = luaL_optinteger(L, 4, 7);
  68. econf.dc_pin = luaL_optinteger(L, 5, 9);
  69. econf.cs_pin = luaL_optinteger(L, 6, 16);
  70. if (frame_buffer != NULL) {
  71. lua_pushboolean(L, 1);
  72. return 0;
  73. }
  74. if (lua_type(L, 7) == LUA_TUSERDATA){
  75. LLOGD("luat_spi_device_send");
  76. econf.userdata = (luat_spi_device_t*)lua_touserdata(L, 3);
  77. econf.port = LUAT_EINK_SPI_DEVICE;
  78. luat_gpio_mode(Pin_BUSY, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  79. luat_gpio_mode(Pin_RES, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  80. luat_gpio_mode(Pin_DC, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  81. status = 0;
  82. }else{
  83. LLOGD("luat_spi_send");
  84. luat_spi_t spi_config = {0};
  85. spi_config.bandrate = 2000000U;//luaL_optinteger(L, 1, 2000000U); // 2000000U
  86. spi_config.id = SPI_ID;
  87. spi_config.cs = 255; // 默认无
  88. spi_config.CPHA = 0; // CPHA0
  89. spi_config.CPOL = 0; // CPOL0
  90. spi_config.dataw = 8; // 8bit
  91. spi_config.bit_dict = 1; // MSB=1, LSB=0
  92. spi_config.master = 1; // master=1,slave=0
  93. spi_config.mode = 1; // FULL=1, half=0
  94. //LLOGD("setup GPIO for epd");
  95. luat_gpio_mode(Pin_BUSY, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  96. luat_gpio_mode(Pin_RES, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  97. luat_gpio_mode(Pin_DC, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  98. luat_gpio_mode(Pin_CS, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  99. status = luat_spi_setup(&spi_config);
  100. }
  101. size_t epd_w = 0;
  102. size_t epd_h = 0;
  103. if(status == 0)
  104. {
  105. LLOGD("spi setup complete, now setup epd");
  106. if(econf.full_mode)
  107. status = EPD_Init(1, &epd_w, &epd_h);
  108. else
  109. status = EPD_Init(0, &epd_w, &epd_h);
  110. if (status != 0) {
  111. LLOGD("e-Paper init failed");
  112. return 0;
  113. }
  114. frame_buffer = (unsigned char*)luat_heap_malloc(epd_w * epd_h / 8);
  115. // Paint paint;
  116. Paint_Init(&paint, frame_buffer, epd_w, epd_h);
  117. Paint_Clear(&paint, UNCOLORED);
  118. }
  119. u8g2_SetFontMode(&(paint.luat_eink_u8g2), 0);
  120. u8g2_SetFontDirection(&(paint.luat_eink_u8g2), 0);
  121. //LLOGD("epd init complete");
  122. lua_pushboolean(L, 1);
  123. return 1;
  124. }
  125. /**
  126. 清除绘图缓冲区
  127. @api eink.clear()
  128. @return nil 无返回值,不会马上刷新到设备
  129. */
  130. static int l_eink_clear(lua_State *L)
  131. {
  132. int colored = luaL_optinteger(L, 1, 1);
  133. Paint_Clear(&paint, colored);
  134. return 0;
  135. }
  136. /**
  137. 设置窗口
  138. @api eink.setWin(width, height, rotate)
  139. @int width 宽度
  140. @int height 高度
  141. @int rotate 显示方向,0/1/2/3, 相当于旋转0度/90度/180度/270度
  142. @return nil 无返回值
  143. */
  144. static int l_eink_setWin(lua_State *L)
  145. {
  146. int width = luaL_checkinteger(L, 1);
  147. int height = luaL_checkinteger(L, 2);
  148. int rotate = luaL_checkinteger(L, 3);
  149. Paint_SetWidth(&paint, width);
  150. Paint_SetHeight(&paint, height);
  151. Paint_SetRotate(&paint, rotate);
  152. return 0;
  153. }
  154. /**
  155. 获取窗口信息
  156. @api eink.getWin()
  157. @return int width 宽
  158. @return int height 高
  159. @return int rotate 旋转方向
  160. */
  161. static int l_eink_getWin(lua_State *L)
  162. {
  163. int width = Paint_GetWidth(&paint);
  164. int height = Paint_GetHeight(&paint);
  165. int rotate = Paint_GetRotate(&paint);
  166. lua_pushinteger(L, width);
  167. lua_pushinteger(L, height);
  168. lua_pushinteger(L, rotate);
  169. return 3;
  170. }
  171. static uint8_t utf8_state;
  172. static uint16_t encoding;
  173. static uint16_t utf8_next(uint8_t b)
  174. {
  175. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  176. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  177. if ( utf8_state == 0 )
  178. {
  179. if ( b >= 0xfc ) /* 6 byte sequence */
  180. {
  181. utf8_state = 5;
  182. b &= 1;
  183. }
  184. else if ( b >= 0xf8 )
  185. {
  186. utf8_state = 4;
  187. b &= 3;
  188. }
  189. else if ( b >= 0xf0 )
  190. {
  191. utf8_state = 3;
  192. b &= 7;
  193. }
  194. else if ( b >= 0xe0 )
  195. {
  196. utf8_state = 2;
  197. b &= 15;
  198. }
  199. else if ( b >= 0xc0 )
  200. {
  201. utf8_state = 1;
  202. b &= 0x01f;
  203. }
  204. else
  205. {
  206. /* do nothing, just use the value as encoding */
  207. return b;
  208. }
  209. encoding = b;
  210. return 0x0fffe;
  211. }
  212. else
  213. {
  214. utf8_state--;
  215. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  216. encoding<<=6;
  217. b &= 0x03f;
  218. encoding |= b;
  219. if ( utf8_state != 0 )
  220. return 0x0fffe; /* nothing to do yet */
  221. }
  222. return encoding;
  223. }
  224. static void drawFastHLine(Paint* conf,int16_t x, int16_t y, int16_t len, uint16_t color){
  225. Paint_DrawHorizontalLine(conf,x, y, len,color);
  226. }
  227. static void drawFastVLine(Paint* conf,int16_t x, int16_t y, int16_t len, uint16_t color){
  228. Paint_DrawVerticalLine(conf,x, y, len,color);
  229. }
  230. static void u8g2_draw_hv_line(u8g2_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color){
  231. switch(dir)
  232. {
  233. case 0:
  234. drawFastHLine(&paint,x,y,len,color);
  235. break;
  236. case 1:
  237. drawFastVLine(&paint,x,y,len,color);
  238. break;
  239. case 2:
  240. drawFastHLine(&paint,x-len+1,y,len,color);
  241. break;
  242. case 3:
  243. drawFastVLine(&paint,x,y-len+1,len,color);
  244. break;
  245. }
  246. }
  247. static int16_t u8g2_add_vector_x(int16_t dx, int8_t x, int8_t y, uint8_t dir) U8X8_NOINLINE;
  248. static int16_t u8g2_add_vector_x(int16_t dx, int8_t x, int8_t y, uint8_t dir)
  249. {
  250. switch(dir)
  251. {
  252. case 0:
  253. dx += x;
  254. break;
  255. case 1:
  256. dx -= y;
  257. break;
  258. case 2:
  259. dx -= x;
  260. break;
  261. default:
  262. dx += y;
  263. break;
  264. }
  265. return dx;
  266. }
  267. static int16_t u8g2_add_vector_y(int16_t dy, int8_t x, int8_t y, uint8_t dir) U8X8_NOINLINE;
  268. static int16_t u8g2_add_vector_y(int16_t dy, int8_t x, int8_t y, uint8_t dir)
  269. {
  270. switch(dir)
  271. {
  272. case 0:
  273. dy += y;
  274. break;
  275. case 1:
  276. dy += x;
  277. break;
  278. case 2:
  279. dy -= y;
  280. break;
  281. default:
  282. dy -= x;
  283. break;
  284. }
  285. return dy;
  286. }
  287. static void u8g2_font_decode_len(u8g2_t *u8g2, uint8_t len, uint8_t is_foreground){
  288. uint8_t cnt; /* total number of remaining pixels, which have to be drawn */
  289. uint8_t rem; /* remaining pixel to the right edge of the glyph */
  290. uint8_t current; /* number of pixels, which need to be drawn for the draw procedure */
  291. /* current is either equal to cnt or equal to rem */
  292. /* local coordinates of the glyph */
  293. uint8_t lx,ly;
  294. /* target position on the screen */
  295. int16_t x, y;
  296. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  297. cnt = len;
  298. /* get the local position */
  299. lx = decode->x;
  300. ly = decode->y;
  301. for(;;){
  302. /* calculate the number of pixel to the right edge of the glyph */
  303. rem = decode->glyph_width;
  304. rem -= lx;
  305. /* calculate how many pixel to draw. This is either to the right edge */
  306. /* or lesser, if not enough pixel are left */
  307. current = rem;
  308. if ( cnt < rem )
  309. current = cnt;
  310. /* now draw the line, but apply the rotation around the glyph target position */
  311. //u8g2_font_decode_draw_pixel(u8g2, lx,ly,current, is_foreground);
  312. /* get target position */
  313. x = decode->target_x;
  314. y = decode->target_y;
  315. /* apply rotation */
  316. x = u8g2_add_vector_x(x, lx, ly, decode->dir);
  317. y = u8g2_add_vector_y(y, lx, ly, decode->dir);
  318. /* draw foreground and background (if required) */
  319. if ( current > 0 ) /* avoid drawing zero length lines, issue #4 */
  320. {
  321. if ( is_foreground )
  322. {
  323. u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, eink_str_color);
  324. }
  325. else if ( decode->is_transparent == 0 )
  326. {
  327. // u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, decode->bg_color);
  328. }
  329. }
  330. /* check, whether the end of the run length code has been reached */
  331. if ( cnt < rem )
  332. break;
  333. cnt -= rem;
  334. lx = 0;
  335. ly++;
  336. }
  337. lx += cnt;
  338. decode->x = lx;
  339. decode->y = ly;
  340. }
  341. static void u8g2_font_setup_decode(u8g2_t *u8g2, const uint8_t *glyph_data)
  342. {
  343. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  344. decode->decode_ptr = glyph_data;
  345. decode->decode_bit_pos = 0;
  346. /* 8 Nov 2015, this is already done in the glyph data search procedure */
  347. /*
  348. decode->decode_ptr += 1;
  349. decode->decode_ptr += 1;
  350. */
  351. decode->glyph_width = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_char_width);
  352. decode->glyph_height = u8g2_font_decode_get_unsigned_bits(decode,u8g2->font_info.bits_per_char_height);
  353. }
  354. static int8_t u8g2_font_decode_glyph(u8g2_t *u8g2, const uint8_t *glyph_data){
  355. uint8_t a, b;
  356. int8_t x, y;
  357. int8_t d;
  358. int8_t h;
  359. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  360. u8g2_font_setup_decode(u8g2, glyph_data);
  361. h = u8g2->font_decode.glyph_height;
  362. x = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_x);
  363. y = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_y);
  364. d = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_delta_x);
  365. if ( decode->glyph_width > 0 )
  366. {
  367. decode->target_x = u8g2_add_vector_x(decode->target_x, x, -(h+y), decode->dir);
  368. decode->target_y = u8g2_add_vector_y(decode->target_y, x, -(h+y), decode->dir);
  369. //u8g2_add_vector(&(decode->target_x), &(decode->target_y), x, -(h+y), decode->dir);
  370. /* reset local x/y position */
  371. decode->x = 0;
  372. decode->y = 0;
  373. /* decode glyph */
  374. for(;;){
  375. a = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_0);
  376. b = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_1);
  377. do{
  378. u8g2_font_decode_len(u8g2, a, 0);
  379. u8g2_font_decode_len(u8g2, b, 1);
  380. } while( u8g2_font_decode_get_unsigned_bits(decode, 1) != 0 );
  381. if ( decode->y >= h )
  382. break;
  383. }
  384. }
  385. return d;
  386. }
  387. const uint8_t *u8g2_font_get_glyph_data(u8g2_t *u8g2, uint16_t encoding);
  388. static int16_t u8g2_font_draw_glyph(u8g2_t *u8g2, int16_t x, int16_t y, uint16_t encoding){
  389. int16_t dx = 0;
  390. u8g2->font_decode.target_x = x;
  391. u8g2->font_decode.target_y = y;
  392. const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, encoding);
  393. if ( glyph_data != NULL ){
  394. dx = u8g2_font_decode_glyph(u8g2, glyph_data);
  395. }
  396. return dx;
  397. }
  398. /**
  399. 绘制字符串
  400. @api eink.print(x, y, str, colored, font)
  401. @int x坐标
  402. @int y坐标
  403. @string 字符串
  404. @int 默认是0
  405. @font 字体大小,默认12
  406. @return nil 无返回值
  407. */
  408. static int l_eink_print(lua_State *L)
  409. {
  410. size_t len;
  411. int x = luaL_checkinteger(L, 1);
  412. int y = luaL_checkinteger(L, 2);
  413. const char *str = luaL_checklstring(L, 3, &len);
  414. eink_str_color = luaL_optinteger(L, 4, 0);
  415. int font = luaL_optinteger(L, 5, 12);
  416. switch (font)
  417. {
  418. case font_opposansm8:
  419. LLOGI("font_opposansm8");
  420. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm8);
  421. lua_pushboolean(L, 1);
  422. break;
  423. case font_opposansm10:
  424. LLOGI("font_opposansm10");
  425. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm10);
  426. lua_pushboolean(L, 1);
  427. break;
  428. case font_opposansm12:
  429. LLOGI("font_opposansm12");
  430. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm12);
  431. lua_pushboolean(L, 1);
  432. break;
  433. case font_opposansm16:
  434. LLOGI("font_opposansm16");
  435. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm16);
  436. lua_pushboolean(L, 1);
  437. break;
  438. case font_opposansm18:
  439. LLOGI("font_opposansm18");
  440. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm18);
  441. lua_pushboolean(L, 1);
  442. break;
  443. case font_opposansm20:
  444. LLOGI("font_opposansm20");
  445. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm20);
  446. lua_pushboolean(L, 1);
  447. break;
  448. case font_opposansm22:
  449. LLOGI("font_opposansm22");
  450. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm22);
  451. lua_pushboolean(L, 1);
  452. break;
  453. case font_opposansm24:
  454. LLOGI("font_opposansm24");
  455. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm24);
  456. lua_pushboolean(L, 1);
  457. break;
  458. case font_opposansm32:
  459. LLOGI("font_opposansm32");
  460. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm32);
  461. lua_pushboolean(L, 1);
  462. break;
  463. #if defined USE_U8G2_OPPOSANSM12_CHINESE
  464. case font_opposansm12_chinese:
  465. LLOGI("font_opposansm12_chinese");
  466. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm12_chinese);
  467. lua_pushboolean(L, 1);
  468. break;
  469. #endif
  470. #if defined USE_U8G2_OPPOSANSM16_CHINESE
  471. case font_opposansm16_chinese:
  472. LLOGI("font_opposansm16_chinese");
  473. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm16_chinese);
  474. lua_pushboolean(L, 1);
  475. break;
  476. #endif
  477. #if defined USE_U8G2_OPPOSANSM24_CHINESE
  478. case font_opposansm24_chinese:
  479. LLOGI("font_opposansm24_chinese");
  480. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm24_chinese);
  481. lua_pushboolean(L, 1);
  482. break;
  483. #endif
  484. #if defined USE_U8G2_OPPOSANSM32_CHINESE
  485. case font_opposansm32_chinese:
  486. LLOGI("font_opposansm32_chinese");
  487. u8g2_SetFont(&(paint.luat_eink_u8g2), u8g2_font_opposansm32_chinese);
  488. lua_pushboolean(L, 1);
  489. break;
  490. #endif
  491. default:
  492. lua_pushboolean(L, 0);
  493. LLOGI("no font");
  494. break;
  495. }
  496. uint16_t e;
  497. int16_t delta, sum;
  498. utf8_state = 0;
  499. sum = 0;
  500. for(;;){
  501. e = utf8_next((uint8_t)*str);
  502. if ( e == 0x0ffff )
  503. break;
  504. str++;
  505. if ( e != 0x0fffe ){
  506. delta = u8g2_font_draw_glyph(&(paint.luat_eink_u8g2), x, y, e);
  507. switch(paint.luat_eink_u8g2.font_decode.dir){
  508. case 0:
  509. x += delta;
  510. break;
  511. case 1:
  512. y += delta;
  513. break;
  514. case 2:
  515. x -= delta;
  516. break;
  517. case 3:
  518. y -= delta;
  519. break;
  520. }
  521. sum += delta;
  522. }
  523. }
  524. return 0;
  525. }
  526. /**
  527. 将缓冲区图像输出到屏幕
  528. @api eink.show(x, y)
  529. @int x 输出的x坐标,默认0
  530. @int y 输出的y坐标,默认0
  531. @return nil 无返回值
  532. */
  533. static int l_eink_show(lua_State *L)
  534. {
  535. int x = luaL_optinteger(L, 1, 0);
  536. int y = luaL_optinteger(L, 2, 0);
  537. /* Display the frame_buffer */
  538. //EPD_SetFrameMemory(&epd, frame_buffer, x, y, Paint_GetWidth(&paint), Paint_GetHeight(&paint));
  539. //EPD_DisplayFrame(&epd);
  540. EPD_Clear();
  541. EPD_Display(frame_buffer, NULL);
  542. return 0;
  543. }
  544. /**
  545. 缓冲区绘制线
  546. @api eink.line(x, y, x2, y2, colored)
  547. @int 起点x坐标
  548. @int 起点y坐标
  549. @int 终点x坐标
  550. @int 终点y坐标
  551. @return nil 无返回值
  552. @usage
  553. eink.line(0, 0, 10, 20, 0)
  554. */
  555. static int l_eink_line(lua_State *L)
  556. {
  557. int x = luaL_checkinteger(L, 1);
  558. int y = luaL_checkinteger(L, 2);
  559. int x2 = luaL_checkinteger(L, 3);
  560. int y2 = luaL_checkinteger(L, 4);
  561. int colored = luaL_optinteger(L, 5, 0);
  562. Paint_DrawLine(&paint, x, y, x2, y2, colored);
  563. return 0;
  564. }
  565. /**
  566. 缓冲区绘制矩形
  567. @api eink.rect(x, y, x2, y2, colored, fill)
  568. @int 左上顶点x坐标
  569. @int 左上顶点y坐标
  570. @int 右下顶点x坐标
  571. @int 右下顶点y坐标
  572. @int 默认是0
  573. @int 是否填充,默认是0,不填充
  574. @return nil 无返回值
  575. @usage
  576. eink.rect(0, 0, 10, 20)
  577. eink.rect(0, 0, 10, 20, 1) -- Filled
  578. */
  579. static int l_eink_rect(lua_State *L)
  580. {
  581. int x = luaL_checkinteger(L, 1);
  582. int y = luaL_checkinteger(L, 2);
  583. int x2 = luaL_checkinteger(L, 3);
  584. int y2 = luaL_checkinteger(L, 4);
  585. int colored = luaL_optinteger(L, 5, 0);
  586. int fill = luaL_optinteger(L, 6, 0);
  587. if(fill)
  588. Paint_DrawFilledRectangle(&paint, x, y, x2, y2, colored);
  589. else
  590. Paint_DrawRectangle(&paint, x, y, x2, y2, colored);
  591. return 0;
  592. }
  593. /**
  594. 缓冲区绘制圆形
  595. @api eink.circle(x, y, radius, colored, fill)
  596. @int 圆心x坐标
  597. @int 圆心y坐标
  598. @int 半径
  599. @int 默认是0
  600. @int 是否填充,默认是0,不填充
  601. @return nil 无返回值
  602. @usage
  603. eink.circle(0, 0, 10)
  604. eink.circle(0, 0, 10, 1, 1) -- Filled
  605. */
  606. static int l_eink_circle(lua_State *L)
  607. {
  608. int x = luaL_checkinteger(L, 1);
  609. int y = luaL_checkinteger(L, 2);
  610. int radius = luaL_checkinteger(L, 3);
  611. int colored = luaL_optinteger(L, 4, 0);
  612. int fill = luaL_optinteger(L, 5, 0);
  613. if(fill)
  614. Paint_DrawFilledCircle(&paint, x, y, radius, colored);
  615. else
  616. Paint_DrawCircle(&paint, x, y, radius, colored);
  617. return 0;
  618. }
  619. /**
  620. 缓冲区绘制QRCode
  621. @api eink.qrcode(x, y, str, version)
  622. @int x坐标
  623. @int y坐标
  624. @string 二维码的内容
  625. @int 二维码版本号
  626. @return nil 无返回值
  627. */
  628. static int l_eink_qrcode(lua_State *L)
  629. {
  630. size_t len;
  631. int x = luaL_checkinteger(L, 1);
  632. int y = luaL_checkinteger(L, 2);
  633. const char* str = luaL_checklstring(L, 3, &len);
  634. int version = luaL_checkinteger(L, 4);
  635. // Create the QR code
  636. QRCode qrcode;
  637. uint8_t qrcodeData[qrcode_getBufferSize(version)];
  638. qrcode_initText(&qrcode, qrcodeData, version, 0, str);
  639. for(int i = 0; i < qrcode.size; i++)
  640. {
  641. for (int j = 0; j < qrcode.size; j++)
  642. {
  643. qrcode_getModule(&qrcode, j, i) ? Paint_DrawPixel(&paint, x+j, y+i, COLORED) : Paint_DrawPixel(&paint, x+j, y+i, UNCOLORED);
  644. }
  645. }
  646. return 0;
  647. }
  648. /**
  649. 缓冲区绘制电池
  650. @api eink.bat(x, y, bat)
  651. @int x坐标
  652. @int y坐标
  653. @int 电池电压,单位毫伏
  654. @return nil 无返回值
  655. */
  656. static int l_eink_bat(lua_State *L)
  657. {
  658. int x = luaL_checkinteger(L, 1);
  659. int y = luaL_checkinteger(L, 2);
  660. int bat = luaL_checkinteger(L, 3);
  661. int batnum = 0;
  662. // eink.rect(0, 3, 2, 6)
  663. // eink.rect(2, 0, 20, 9)
  664. // eink.rect(9, 1, 19, 8, 0, 1)
  665. if(bat < 4200 && bat > 4080)batnum = 100;
  666. if(bat < 4080 && bat > 4000)batnum = 90;
  667. if(bat < 4000 && bat > 3930)batnum = 80;
  668. if(bat < 3930 && bat > 3870)batnum = 70;
  669. if(bat < 3870 && bat > 3820)batnum = 60;
  670. if(bat < 3820 && bat > 3790)batnum = 50;
  671. if(bat < 3790 && bat > 3770)batnum = 40;
  672. if(bat < 3770 && bat > 3730)batnum = 30;
  673. if(bat < 3730 && bat > 3700)batnum = 20;
  674. if(bat < 3700 && bat > 3680)batnum = 15;
  675. if(bat < 3680 && bat > 3500)batnum = 10;
  676. if(bat < 3500 && bat > 2500)batnum = 5;
  677. batnum = 20 - (int)(batnum / 5) + 3;
  678. // w外框
  679. Paint_DrawRectangle(&paint, x+0, y+3, x+2, y+6, COLORED);
  680. Paint_DrawRectangle(&paint, x+2, y+0, x+23, y+9, COLORED);
  681. // 3 ~21 100 / 5
  682. Paint_DrawFilledRectangle(&paint, x+batnum, y+1, x+22, y+8, COLORED);
  683. return 0;
  684. }
  685. /**
  686. 缓冲区绘制天气图标
  687. @api eink.weather_icon(x, y, code)
  688. @int x坐标
  689. @int y坐标
  690. @int 天气代号
  691. @return nil 无返回值
  692. */
  693. static int l_eink_weather_icon(lua_State *L)
  694. {
  695. size_t len;
  696. int x = luaL_checkinteger(L, 1);
  697. int y = luaL_checkinteger(L, 2);
  698. int code = luaL_checkinteger(L, 3);
  699. const char* str = luaL_optlstring(L, 4, "nil", &len);
  700. const unsigned char * icon = gImage_999;
  701. if (strcmp(str, "xue") == 0)code = 401;
  702. if (strcmp(str, "lei") == 0)code = 302;
  703. if (strcmp(str, "shachen") == 0)code = 503;
  704. if (strcmp(str, "wu") == 0)code = 501;
  705. if (strcmp(str, "bingbao") == 0)code = 504;
  706. if (strcmp(str, "yun") == 0)code = 103;
  707. if (strcmp(str, "yu") == 0)code = 306;
  708. if (strcmp(str, "yin") == 0)code = 101;
  709. if (strcmp(str, "qing") == 0)code = 100;
  710. // xue、lei、shachen、wu、bingbao、yun、yu、yin、qing
  711. //if(code == 64)
  712. // for(int i = 0; i < 64; i++)
  713. // {
  714. // for (int j = 0; j < 8; j++)
  715. // {
  716. // for (int k = 0; k < 8; k++)
  717. // (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);
  718. // }
  719. // }
  720. switch (code)
  721. {
  722. case 100:icon = gImage_100;break;
  723. case 101:icon = gImage_101;break;
  724. case 102:icon = gImage_102;break;
  725. case 103:icon = gImage_103;break;
  726. case 104:icon = gImage_104;break;
  727. case 200:icon = gImage_200;break;
  728. case 201:icon = gImage_201;break;
  729. case 205:icon = gImage_205;break;
  730. case 208:icon = gImage_208;break;
  731. case 301:icon = gImage_301;break;
  732. case 302:icon = gImage_302;break;
  733. case 303:icon = gImage_303;break;
  734. case 304:icon = gImage_304;break;
  735. case 305:icon = gImage_305;break;
  736. case 306:icon = gImage_306;break;
  737. case 307:icon = gImage_307;break;
  738. case 308:icon = gImage_308;break;
  739. case 309:icon = gImage_309;break;
  740. case 310:icon = gImage_310;break;
  741. case 311:icon = gImage_311;break;
  742. case 312:icon = gImage_312;break;
  743. case 313:icon = gImage_313;break;
  744. case 400:icon = gImage_400;break;
  745. case 401:icon = gImage_401;break;
  746. case 402:icon = gImage_402;break;
  747. case 403:icon = gImage_403;break;
  748. case 404:icon = gImage_404;break;
  749. case 405:icon = gImage_405;break;
  750. case 406:icon = gImage_406;break;
  751. case 407:icon = gImage_407;break;
  752. case 500:icon = gImage_500;break;
  753. case 501:icon = gImage_501;break;
  754. case 502:icon = gImage_502;break;
  755. case 503:icon = gImage_503;break;
  756. case 504:icon = gImage_504;break;
  757. case 507:icon = gImage_507;break;
  758. case 508:icon = gImage_508;break;
  759. case 900:icon = gImage_900;break;
  760. case 901:icon = gImage_901;break;
  761. case 999:icon = gImage_999;break;
  762. default:
  763. break;
  764. }
  765. for(int i = 0; i < 48; i++)
  766. {
  767. for (int j = 0; j < 6; j++)
  768. {
  769. for (int k = 0; k < 8; k++)
  770. (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);
  771. }
  772. }
  773. return 0;
  774. }
  775. /**
  776. 设置墨水屏驱动型号
  777. @api eink.model(m)
  778. @int 型号名称, 例如 eink.model(eink.MODEL_1in54_V2)
  779. @return nil 无返回值
  780. */
  781. static int l_eink_model(lua_State *L) {
  782. EPD_Model(luaL_checkinteger(L, 1));
  783. return 0;
  784. }
  785. #ifdef LUAT_USE_GTFONT
  786. #include "GT5SLCD2E_1A.h"
  787. extern void gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int widt,unsigned int high,int(*point)(void*),void* userdata,int mode);
  788. extern void gtfont_draw_gray_hz(unsigned char *data,unsigned short x,unsigned short y,unsigned short w ,unsigned short h,unsigned char grade, unsigned char HB_par,int(*point)(void*,uint16_t, uint16_t, uint32_t),void* userdata,int mode);
  789. static int l_eink_draw_gtfont_gb2312(lua_State *L) {
  790. unsigned char buf[128];
  791. int len;
  792. int i = 0;
  793. uint8_t strhigh,strlow ;
  794. uint16_t str;
  795. const char *fontCode = luaL_checklstring(L, 1,&len);
  796. unsigned char size = luaL_checkinteger(L, 2);
  797. int x = luaL_checkinteger(L, 3);
  798. int y = luaL_checkinteger(L, 4);
  799. while ( i < len){
  800. strhigh = *fontCode;
  801. fontCode++;
  802. strlow = *fontCode;
  803. str = (strhigh<<8)|strlow;
  804. fontCode++;
  805. get_font(buf, 1, str, size, size, size);
  806. gtfont_draw_w(buf , x ,y , size , size,Paint_DrawPixel,&paint,1);
  807. x+=size;
  808. i+=2;
  809. }
  810. return 0;
  811. }
  812. static int l_eink_draw_gtfont_gb2312_gray(lua_State* L) {
  813. unsigned char buf[2048];
  814. int len;
  815. int i = 0;
  816. uint8_t strhigh,strlow ;
  817. uint16_t str;
  818. const char *fontCode = luaL_checklstring(L, 1,&len);
  819. unsigned char size = luaL_checkinteger(L, 2);
  820. unsigned char font_g = luaL_checkinteger(L, 3);
  821. int x = luaL_checkinteger(L, 4);
  822. int y = luaL_checkinteger(L, 5);
  823. while ( i < len){
  824. strhigh = *fontCode;
  825. fontCode++;
  826. strlow = *fontCode;
  827. str = (strhigh<<8)|strlow;
  828. fontCode++;
  829. get_font(buf, 1, str, size*font_g, size*font_g, size*font_g);
  830. Gray_Process(buf,size,size,font_g);
  831. gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,Paint_DrawPixel,&paint,1);
  832. x+=size;
  833. i+=2;
  834. }
  835. return 0;
  836. }
  837. #ifdef LUAT_USE_GTFONT_UTF8
  838. extern unsigned short unicodetogb2312 ( unsigned short chr);
  839. static int l_eink_draw_gtfont_utf8(lua_State *L) {
  840. unsigned char buf[128];
  841. int len;
  842. int i = 0;
  843. uint8_t strhigh,strlow ;
  844. uint16_t e,str;
  845. const char *fontCode = luaL_checklstring(L, 1,&len);
  846. unsigned char size = luaL_checkinteger(L, 2);
  847. int x = luaL_checkinteger(L, 3);
  848. int y = luaL_checkinteger(L, 4);
  849. for(;;){
  850. e = utf8_next((uint8_t)*fontCode);
  851. if ( e == 0x0ffff )
  852. break;
  853. fontCode++;
  854. if ( e != 0x0fffe ){
  855. uint16_t str = unicodetogb2312(e);
  856. get_font(buf, 1, str, size, size, size);
  857. gtfont_draw_w(buf , x ,y , size , size,Paint_DrawPixel,&paint,1);
  858. x+=size;
  859. }
  860. }
  861. return 0;
  862. }
  863. static int l_eink_draw_gtfont_utf8_gray(lua_State* L) {
  864. unsigned char buf[2048];
  865. int len;
  866. int i = 0;
  867. uint8_t strhigh,strlow ;
  868. uint16_t e,str;
  869. const char *fontCode = luaL_checklstring(L, 1,&len);
  870. unsigned char size = luaL_checkinteger(L, 2);
  871. unsigned char font_g = luaL_checkinteger(L, 3);
  872. int x = luaL_checkinteger(L, 4);
  873. int y = luaL_checkinteger(L, 5);
  874. for(;;){
  875. e = utf8_next((uint8_t)*fontCode);
  876. if ( e == 0x0ffff )
  877. break;
  878. fontCode++;
  879. if ( e != 0x0fffe ){
  880. uint16_t str = unicodetogb2312(e);
  881. get_font(buf, 1, str, size*font_g, size*font_g, size*font_g);
  882. Gray_Process(buf,size,size,font_g);
  883. gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,Paint_DrawPixel,&paint,1);
  884. x+=size;
  885. }
  886. }
  887. return 0;
  888. }
  889. #endif // LUAT_USE_GTFONT_UTF8
  890. #endif // LUAT_USE_GTFONT
  891. /*
  892. 绘制位图
  893. @api eink.bitmap(x, y, h, data, mode)
  894. @int X坐标
  895. @int y坐标
  896. @int 行数
  897. @int 位图数据,每一位代表一个像素
  898. @int 模式, 0值是否写入. mode=1代表写入, mode=0代表不写入, 默认mode=0
  899. @usage
  900. -- 在(10,10)为左上角,绘制 10x4 的位图
  901. eink.bitmap(10, 10, 10, string.char(0x20, 0xFF, 0xFF, 0xAF, 0xDE), 1)
  902. */
  903. static int l_eink_bitmap(lua_State *L) {
  904. int x = luaL_checkinteger(L, 1);
  905. int y = luaL_checkinteger(L, 2);
  906. int row = luaL_checkinteger(L, 3);
  907. size_t len;
  908. const char* data = luaL_checklstring(L, 4, &len);
  909. int mode = luaL_optinteger(L, 5, 0);
  910. int w = len * 8 / row; // 算出每行的bit数量
  911. uint8_t c = 0;
  912. for (size_t i = 0; i < row; i++) // 行
  913. {
  914. for (size_t j = 0; j < w; j++) // 列
  915. {
  916. c = data[(w*i + j)/8];
  917. if (c & ((1 << ((w*i + j)%8)))) {
  918. Paint_DrawPixel(&paint, x + j, y + row, 1);
  919. }
  920. else if (mode)
  921. Paint_DrawPixel(&paint, x + j, y + row, 0);
  922. }
  923. return 0;
  924. }
  925. }
  926. #include "rotable.h"
  927. static const rotable_Reg reg_eink[] =
  928. {
  929. { "setup", l_eink_setup, 0},
  930. { "clear", l_eink_clear, 0},
  931. { "setWin", l_eink_setWin, 0},
  932. { "getWin", l_eink_getWin, 0},
  933. { "print", l_eink_print, 0},
  934. { "show", l_eink_show, 0},
  935. { "rect", l_eink_rect, 0},
  936. { "circle", l_eink_circle, 0},
  937. { "line", l_eink_line, 0},
  938. { "qrcode", l_eink_qrcode, 0},
  939. { "bat", l_eink_bat, 0},
  940. { "weather_icon", l_eink_weather_icon, 0},
  941. { "model", l_eink_model, 0},
  942. { "bitmap", l_eink_bitmap, 0},
  943. #ifdef LUAT_USE_GTFONT
  944. { "drawGtfontGb2312", l_eink_draw_gtfont_gb2312, 0},
  945. { "drawGtfontGb2312Gray", l_eink_draw_gtfont_gb2312_gray, 0},
  946. #ifdef LUAT_USE_GTFONT_UTF8
  947. { "drawGtfontUtf8", l_eink_draw_gtfont_utf8, 0},
  948. { "drawGtfontUtf8Gray", l_eink_draw_gtfont_utf8_gray, 0},
  949. #endif // LUAT_USE_GTFONT_UTF8
  950. #endif // LUAT_USE_GTFONT
  951. { "MODEL_1in02d", NULL, MODEL_1in02d},
  952. { "MODEL_1in54", NULL, MODEL_1in54},
  953. { "MODEL_1in54_V2", NULL, MODEL_1in54_V2},
  954. { "MODEL_1in54b", NULL, MODEL_1in54b},
  955. { "MODEL_1in54b_V2", NULL, MODEL_1in54b_V2},
  956. { "MODEL_1in54c", NULL, MODEL_1in54c},
  957. { "MODEL_1in54f", NULL, MODEL_1in54f},
  958. { "MODEL_2in54b_V3", NULL, MODEL_2in13b_V3},
  959. { "MODEL_2in7", NULL, MODEL_2in7},
  960. { "MODEL_2in7b", NULL, MODEL_2in7b},
  961. { "MODEL_2in9", NULL, MODEL_2in9},
  962. { "MODEL_2in9_V2", NULL, MODEL_2in9_V2},
  963. { "MODEL_2in9bc", NULL, MODEL_2in9bc},
  964. { "MODEL_2in9b_V3", NULL, MODEL_2in9b_V3},
  965. { "MODEL_2in9d", NULL, MODEL_2in9d},
  966. { "MODEL_2in9f", NULL, MODEL_2in9f},
  967. { "MODEL_3in7", NULL, MODEL_3in7},
  968. { "font_opposansm8", NULL, font_opposansm8},
  969. { "font_opposansm10", NULL, font_opposansm10},
  970. { "font_opposansm12", NULL, font_opposansm12},
  971. { "font_opposansm16", NULL, font_opposansm16},
  972. { "font_opposansm18", NULL, font_opposansm18},
  973. { "font_opposansm20", NULL, font_opposansm20},
  974. { "font_opposansm22", NULL, font_opposansm22},
  975. { "font_opposansm24", NULL, font_opposansm24},
  976. { "font_opposansm32", NULL, font_opposansm32},
  977. { "font_opposansm12_chinese", NULL, font_opposansm12_chinese},
  978. { "font_opposansm16_chinese", NULL, font_opposansm16_chinese},
  979. { "font_opposansm24_chinese", NULL, font_opposansm24_chinese},
  980. { "font_opposansm32_chinese", NULL, font_opposansm32_chinese},
  981. { NULL, NULL, 0}
  982. };
  983. LUAMOD_API int luaopen_eink( lua_State *L ){
  984. luat_newlib(L, reg_eink);
  985. return 1;
  986. }