luat_lib_eink.c 31 KB

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