luat_lib_eink.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. /*
  2. @module eink
  3. @summary 墨水屏操作库
  4. @version 1.0
  5. @date 2020.11.14
  6. @demo eink
  7. @tag LUAT_USE_EINK
  8. */
  9. #include "luat_base.h"
  10. #include "luat_sys.h"
  11. #include "luat_msgbus.h"
  12. #include "luat_timer.h"
  13. #include "luat_malloc.h"
  14. #include "luat_spi.h"
  15. #include "luat_gpio.h"
  16. #include "epd.h"
  17. #include "epdpaint.h"
  18. #include "imagedata.h"
  19. #include "qrcodegen.h"
  20. #include <stdlib.h>
  21. #include "u8g2_luat_fonts.h"
  22. #include "luat_zbuff.h"
  23. #include "luat_log.h"
  24. #ifndef LUAT_LOG_TAG
  25. #define LUAT_LOG_TAG "eink"
  26. #endif
  27. int8_t u8g2_font_decode_get_signed_bits(u8g2_font_decode_t *f, uint8_t cnt);
  28. uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt);
  29. #define COLORED 0
  30. #define UNCOLORED 1
  31. eink_conf_t econf = {0};
  32. static int check_init(void) {
  33. if (econf.ctxs[0] == NULL) {
  34. LLOGW("eink NOT init yet");
  35. return 0;
  36. }
  37. return 1;
  38. }
  39. /*
  40. eink显示屏初始化
  41. @api eink.init(tp, args,spi_device)
  42. @number eink类型,当前支持:https://wiki.luatos.com/api/eink.html#id1
  43. @table 附加参数,与具体设备有关:<br>pin_busy(busy)<br>port:spi端口,例如0,1,2...如果为device方式则为"device"<br>pin_dc:eink数据/命令选择引脚<br>pin_rst:eink复位引脚
  44. @userdata spi设备,当port = "device"时有效
  45. @usage
  46. -- 初始化spi0的eink.MODEL_4in2bc) 注意:eink初始化之前需要先初始化spi
  47. spi_eink = spi.deviceSetup(0,20,0,0,8,20000000,spi.MSB,1,1)
  48. log.info("eink.init",
  49. eink.init(eink.MODEL_4in2bc,{port = "device",pin_dc = 17, pin_pwr = 7,pin_rst = 19,direction = 2,w = 160,h = 80,xoffset = 1,yoffset = 26},spi_eink))
  50. */
  51. static int l_eink_init(lua_State* L) {
  52. if (lua_type(L, 3) == LUA_TUSERDATA){
  53. // 如果是SPI Device模式, 就可能出现变量为local, 从而在某个时间点被GC掉的可能性
  54. econf.eink_spi_device = (luat_spi_device_t*)lua_touserdata(L, 3);
  55. lua_pushvalue(L, 3);
  56. // 所以, 直接引用之外, 再加上强制引用, 避免被GC
  57. // 鉴于LCD不太可能重复初始化, 引用也没什么问题
  58. econf.eink_spi_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  59. econf.port = LUAT_EINK_SPI_DEVICE;
  60. }
  61. if (econf.async){
  62. luat_rtos_task_create(&econf.eink_task_handle, 512, 50, "eink", EPD_Task, NULL, 0);
  63. luat_rtos_queue_create(&econf.eink_queue_handle, 5, sizeof(uint8_t));
  64. }
  65. EPD_Model(luaL_checkinteger(L, 1));
  66. if (lua_gettop(L) > 1) {
  67. lua_settop(L, 2); // 丢弃多余的参数
  68. lua_pushstring(L, "port");
  69. int port = lua_gettable(L, 2);
  70. if (econf.port == LUAT_EINK_SPI_DEVICE && port ==LUA_TNUMBER) {
  71. LLOGE("port is not device but find luat_spi_device_t");
  72. goto end;
  73. }else if (econf.port != LUAT_EINK_SPI_DEVICE && LUA_TSTRING == port){
  74. LLOGE("port is device but not find luat_spi_device_t");
  75. goto end;
  76. }else if (LUA_TNUMBER == port) {
  77. econf.port = luaL_checkinteger(L, -1);
  78. }else if (LUA_TSTRING == port){
  79. econf.port = LUAT_EINK_SPI_DEVICE;
  80. }
  81. lua_pop(L, 1);
  82. lua_pushstring(L, "pin_dc");
  83. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  84. econf.pin_dc = luaL_checkinteger(L, -1);
  85. }
  86. lua_pop(L, 1);
  87. lua_pushstring(L, "pin_busy");
  88. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  89. econf.pin_busy = luaL_checkinteger(L, -1);
  90. }
  91. lua_pop(L, 1);
  92. lua_pushstring(L, "pin_rst");
  93. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  94. econf.pin_rst = luaL_checkinteger(L, -1);
  95. }
  96. lua_pop(L, 1);
  97. lua_pushstring(L, "mode");
  98. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  99. econf.full_mode = luaL_optinteger(L, -1, 1);
  100. }
  101. lua_pop(L, 1);
  102. lua_pushstring(L, "pin_cs");
  103. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  104. econf.pin_cs = luaL_checkinteger(L, -1);
  105. luat_gpio_mode(econf.pin_cs, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  106. }
  107. lua_pop(L, 1);
  108. }
  109. luat_gpio_mode(econf.pin_busy, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  110. luat_gpio_mode(econf.pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  111. luat_gpio_mode(econf.pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  112. size_t epd_w = 0;
  113. size_t epd_h = 0;
  114. size_t colors = 0;
  115. int status = EPD_Init(econf.full_mode, &epd_w, &epd_h, &colors);
  116. if (status != 0) {
  117. LLOGD("e-Paper init failed");
  118. return 0;
  119. }
  120. if (colors > 2) {
  121. LLOGE("only 2 color eink supported yet");
  122. return 0;
  123. }
  124. for (size_t i = 0; i < colors; i++){
  125. econf.ctxs[i] = luat_heap_malloc( sizeof(eink_ctx_t) + (epd_w * epd_h + 7) / 8);
  126. if (econf.ctxs[i] == NULL) {
  127. LLOGE("out of memory when malloc buff for eink");
  128. for (size_t j = 0; j < i - 1; j++)
  129. {
  130. luat_heap_free(econf.ctxs[i]);
  131. econf.ctxs[i] = NULL;
  132. }
  133. return 0;
  134. }
  135. Paint_Init(&econf.ctxs[i]->paint, econf.ctxs[i]->fb, epd_w, epd_h);
  136. Paint_Clear(&econf.ctxs[i]->paint, UNCOLORED);
  137. econf.ctxs[i]->paint.inited = 1;
  138. }
  139. u8g2_SetFont(&(econf.luat_eink_u8g2), u8g2_font_opposansm8);
  140. u8g2_SetFontMode(&(econf.luat_eink_u8g2), 0);
  141. u8g2_SetFontDirection(&(econf.luat_eink_u8g2), 0);
  142. lua_pushboolean(L, 1);
  143. return 1;
  144. end:
  145. lua_pushboolean(L, 0);
  146. return 1;
  147. }
  148. /**
  149. 初始化eink
  150. @api eink.setup(full, spiid, pin_busy, pin_reset, pin_dc, pin_cs)
  151. @int 全屏刷新0,局部刷新1,默认是全屏刷新
  152. @int 所在的spi,默认是0
  153. @int Busy 忙信号管脚
  154. @int Reset 复位管脚
  155. @int DC 数据命令选择管脚
  156. @int CS 使能管脚
  157. @return boolean 成功返回true,否则返回false
  158. */
  159. static int l_eink_setup(lua_State *L) {
  160. int status = 0;
  161. econf.full_mode = luaL_optinteger(L, 1, 1);
  162. econf.port = luaL_optinteger(L, 2, 0);
  163. econf.pin_busy = luaL_checkinteger(L, 3);
  164. econf.pin_rst = luaL_checkinteger(L, 4);
  165. econf.pin_dc = luaL_checkinteger(L, 5);
  166. econf.pin_cs = luaL_checkinteger(L, 6);
  167. luat_gpio_mode(econf.pin_busy, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  168. luat_gpio_mode(econf.pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  169. luat_gpio_mode(econf.pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  170. if (lua_type(L, 7) == LUA_TUSERDATA){
  171. //LLOGD("luat_spi_device_send");
  172. econf.eink_spi_device = (luat_spi_device_t*)lua_touserdata(L, 3);
  173. econf.port = LUAT_EINK_SPI_DEVICE;
  174. status = 0;
  175. }else{
  176. luat_gpio_mode(econf.pin_cs, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  177. }
  178. if (status != 0) {
  179. LLOGD("spi setup fail, eink init fail");
  180. return 0;
  181. }
  182. size_t epd_w = 0;
  183. size_t epd_h = 0;
  184. size_t colors = 0;
  185. if(status == 0)
  186. {
  187. if(econf.full_mode)
  188. status = EPD_Init(1, &epd_w, &epd_h, &colors);
  189. else
  190. status = EPD_Init(0, &epd_w, &epd_h, &colors);
  191. if (status != 0) {
  192. LLOGD("e-Paper init failed");
  193. return 0;
  194. }
  195. LLOGD("spi setup complete, now setup epd");
  196. if (colors > 2) {
  197. LLOGE("only 2 color eink supported yet");
  198. return 0;
  199. }
  200. for (size_t i = 0; i < colors; i++)
  201. {
  202. econf.ctxs[i] = luat_heap_malloc( sizeof(eink_ctx_t) + (epd_w * epd_h + 7) / 8);
  203. if (econf.ctxs[i] == NULL) {
  204. LLOGE("out of memory when malloc buff for eink");
  205. for (size_t j = 0; j < i - 1; j++)
  206. {
  207. luat_heap_free(econf.ctxs[i]);
  208. econf.ctxs[i] = NULL;
  209. }
  210. return 0;
  211. }
  212. Paint_Init(&econf.ctxs[i]->paint, econf.ctxs[i]->fb, epd_w, epd_h);
  213. Paint_Clear(&econf.ctxs[i]->paint, UNCOLORED);
  214. econf.ctxs[i]->paint.inited = 1;
  215. }
  216. }
  217. u8g2_SetFont(&(econf.luat_eink_u8g2), u8g2_font_opposansm8);
  218. u8g2_SetFontMode(&(econf.luat_eink_u8g2), 0);
  219. u8g2_SetFontDirection(&(econf.luat_eink_u8g2), 0);
  220. //paint.inited = 1;
  221. //LLOGD("epd init complete");
  222. lua_pushboolean(L, 1);
  223. return 1;
  224. }
  225. /**
  226. 进入休眠模式,再次使用时需要重新初始化
  227. @api eink.sleep()
  228. */
  229. static int l_eink_sleep(lua_State *L)
  230. {
  231. EPD_Sleep();
  232. lua_pushboolean(L, 1);
  233. return 1;
  234. }
  235. /**
  236. 清除绘图缓冲区,默认不会马上刷新到设备
  237. @api eink.clear(color, force)
  238. @number color 可选,默认1。刷屏颜色
  239. @bool force 可选,默认false。如果为true则马上清屏
  240. @return nil 无返回值
  241. */
  242. static int l_eink_clear(lua_State *L)
  243. {
  244. int colored = luaL_optinteger(L, 1, 1);
  245. int no_clear = lua_toboolean(L, 2);
  246. if (econf.async){
  247. econf.idp = luat_pushcwait(L);
  248. if (check_init() == 0) {
  249. lua_pushinteger(L,0);
  250. luat_pushcwait_error(L,1);
  251. return 1;
  252. }
  253. Paint_Clear(&econf.ctxs[econf.ctx_index]->paint, colored);
  254. if (!no_clear){
  255. uint8_t event = EPD_CLEAR;
  256. luat_rtos_queue_send(econf.eink_queue_handle, &event, sizeof(uint8_t), 0);
  257. }
  258. }else{
  259. if (check_init() == 0) {
  260. return 0;
  261. }
  262. Paint_Clear(&econf.ctxs[econf.ctx_index]->paint, colored);
  263. if(!no_clear)
  264. EPD_Clear();
  265. lua_pushboolean(L, 1);
  266. }
  267. return 1;
  268. }
  269. /**
  270. 设置窗口
  271. @api eink.setWin(width, height, rotate)
  272. @int width 宽度
  273. @int height 高度
  274. @int rotate 显示方向,0/1/2/3, 相当于旋转0度/90度/180度/270度
  275. @return nil 无返回值
  276. */
  277. static int l_eink_setWin(lua_State *L)
  278. {
  279. int width = luaL_checkinteger(L, 1);
  280. int height = luaL_checkinteger(L, 2);
  281. int rotate = luaL_checkinteger(L, 3);
  282. if (check_init() == 0)
  283. return 0;
  284. Paint_SetWidth(&econf.ctxs[econf.ctx_index]->paint, width);
  285. Paint_SetHeight(&econf.ctxs[econf.ctx_index]->paint, height);
  286. Paint_SetRotate(&econf.ctxs[econf.ctx_index]->paint, rotate);
  287. return 0;
  288. }
  289. /**
  290. 获取窗口信息
  291. @api eink.getWin()
  292. @return int width 宽
  293. @return int height 高
  294. @return int rotate 旋转方向
  295. */
  296. static int l_eink_getWin(lua_State *L)
  297. {
  298. if (check_init() == 0)
  299. return 0;
  300. int width = Paint_GetWidth(&econf.ctxs[econf.ctx_index]->paint);
  301. int height = Paint_GetHeight(&econf.ctxs[econf.ctx_index]->paint);
  302. int rotate = Paint_GetRotate(&econf.ctxs[econf.ctx_index]->paint);
  303. lua_pushinteger(L, width);
  304. lua_pushinteger(L, height);
  305. lua_pushinteger(L, rotate);
  306. return 3;
  307. }
  308. static uint8_t utf8_state;
  309. static uint16_t encoding;
  310. static uint16_t utf8_next(uint8_t b)
  311. {
  312. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  313. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  314. if ( utf8_state == 0 )
  315. {
  316. if ( b >= 0xfc ) /* 6 byte sequence */
  317. {
  318. utf8_state = 5;
  319. b &= 1;
  320. }
  321. else if ( b >= 0xf8 )
  322. {
  323. utf8_state = 4;
  324. b &= 3;
  325. }
  326. else if ( b >= 0xf0 )
  327. {
  328. utf8_state = 3;
  329. b &= 7;
  330. }
  331. else if ( b >= 0xe0 )
  332. {
  333. utf8_state = 2;
  334. b &= 15;
  335. }
  336. else if ( b >= 0xc0 )
  337. {
  338. utf8_state = 1;
  339. b &= 0x01f;
  340. }
  341. else
  342. {
  343. /* do nothing, just use the value as encoding */
  344. return b;
  345. }
  346. encoding = b;
  347. return 0x0fffe;
  348. }
  349. else
  350. {
  351. utf8_state--;
  352. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  353. encoding<<=6;
  354. b &= 0x03f;
  355. encoding |= b;
  356. if ( utf8_state != 0 )
  357. return 0x0fffe; /* nothing to do yet */
  358. }
  359. return encoding;
  360. }
  361. static void drawFastHLine(Paint* conf,int16_t x, int16_t y, int16_t len, uint16_t color){
  362. Paint_DrawHorizontalLine(conf,x, y, len,color);
  363. }
  364. static void drawFastVLine(Paint* conf,int16_t x, int16_t y, int16_t len, uint16_t color){
  365. Paint_DrawVerticalLine(conf,x, y, len,color);
  366. }
  367. static void u8g2_draw_hv_line(u8g2_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color){
  368. switch(dir)
  369. {
  370. case 0:
  371. drawFastHLine(&econf.ctxs[econf.ctx_index]->paint,x,y,len,color);
  372. break;
  373. case 1:
  374. drawFastVLine(&econf.ctxs[econf.ctx_index]->paint,x,y,len,color);
  375. break;
  376. case 2:
  377. drawFastHLine(&econf.ctxs[econf.ctx_index]->paint,x-len+1,y,len,color);
  378. break;
  379. case 3:
  380. drawFastVLine(&econf.ctxs[econf.ctx_index]->paint,x,y-len+1,len,color);
  381. break;
  382. }
  383. }
  384. static void u8g2_font_decode_len(u8g2_t *u8g2, uint8_t len, uint8_t is_foreground){
  385. uint8_t cnt; /* total number of remaining pixels, which have to be drawn */
  386. uint8_t rem; /* remaining pixel to the right edge of the glyph */
  387. uint8_t current; /* number of pixels, which need to be drawn for the draw procedure */
  388. /* current is either equal to cnt or equal to rem */
  389. /* local coordinates of the glyph */
  390. uint8_t lx,ly;
  391. /* target position on the screen */
  392. int16_t x, y;
  393. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  394. cnt = len;
  395. /* get the local position */
  396. lx = decode->x;
  397. ly = decode->y;
  398. for(;;){
  399. /* calculate the number of pixel to the right edge of the glyph */
  400. rem = decode->glyph_width;
  401. rem -= lx;
  402. /* calculate how many pixel to draw. This is either to the right edge */
  403. /* or lesser, if not enough pixel are left */
  404. current = rem;
  405. if ( cnt < rem )
  406. current = cnt;
  407. /* now draw the line, but apply the rotation around the glyph target position */
  408. //u8g2_font_decode_draw_pixel(u8g2, lx,ly,current, is_foreground);
  409. /* get target position */
  410. x = decode->target_x;
  411. y = decode->target_y;
  412. /* apply rotation */
  413. x = u8g2_add_vector_x(x, lx, ly, decode->dir);
  414. y = u8g2_add_vector_y(y, lx, ly, decode->dir);
  415. /* draw foreground and background (if required) */
  416. if ( current > 0 ) /* avoid drawing zero length lines, issue #4 */
  417. {
  418. if ( is_foreground )
  419. {
  420. u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, econf.ctxs[econf.ctx_index]->str_color);
  421. }
  422. else if ( decode->is_transparent == 0 )
  423. {
  424. // u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, decode->bg_color);
  425. }
  426. }
  427. /* check, whether the end of the run length code has been reached */
  428. if ( cnt < rem )
  429. break;
  430. cnt -= rem;
  431. lx = 0;
  432. ly++;
  433. }
  434. lx += cnt;
  435. decode->x = lx;
  436. decode->y = ly;
  437. }
  438. static void u8g2_font_setup_decode(u8g2_t *u8g2, const uint8_t *glyph_data)
  439. {
  440. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  441. decode->decode_ptr = glyph_data;
  442. decode->decode_bit_pos = 0;
  443. /* 8 Nov 2015, this is already done in the glyph data search procedure */
  444. /*
  445. decode->decode_ptr += 1;
  446. decode->decode_ptr += 1;
  447. */
  448. decode->glyph_width = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_char_width);
  449. decode->glyph_height = u8g2_font_decode_get_unsigned_bits(decode,u8g2->font_info.bits_per_char_height);
  450. }
  451. static int8_t u8g2_font_decode_glyph(u8g2_t *u8g2, const uint8_t *glyph_data){
  452. uint8_t a, b;
  453. int8_t x, y;
  454. int8_t d;
  455. int8_t h;
  456. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  457. u8g2_font_setup_decode(u8g2, glyph_data);
  458. h = u8g2->font_decode.glyph_height;
  459. x = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_x);
  460. y = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_y);
  461. d = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_delta_x);
  462. if ( decode->glyph_width > 0 )
  463. {
  464. decode->target_x = u8g2_add_vector_x(decode->target_x, x, -(h+y), decode->dir);
  465. decode->target_y = u8g2_add_vector_y(decode->target_y, x, -(h+y), decode->dir);
  466. //u8g2_add_vector(&(decode->target_x), &(decode->target_y), x, -(h+y), decode->dir);
  467. /* reset local x/y position */
  468. decode->x = 0;
  469. decode->y = 0;
  470. /* decode glyph */
  471. for(;;){
  472. a = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_0);
  473. b = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_1);
  474. do{
  475. u8g2_font_decode_len(u8g2, a, 0);
  476. u8g2_font_decode_len(u8g2, b, 1);
  477. } while( u8g2_font_decode_get_unsigned_bits(decode, 1) != 0 );
  478. if ( decode->y >= h )
  479. break;
  480. }
  481. }
  482. return d;
  483. }
  484. const uint8_t *u8g2_font_get_glyph_data(u8g2_t *u8g2, uint16_t encoding);
  485. static int16_t u8g2_font_draw_glyph(u8g2_t *u8g2, int16_t x, int16_t y, uint16_t encoding){
  486. int16_t dx = 0;
  487. u8g2->font_decode.target_x = x;
  488. u8g2->font_decode.target_y = y;
  489. const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, encoding);
  490. if ( glyph_data != NULL ){
  491. dx = u8g2_font_decode_glyph(u8g2, glyph_data);
  492. }
  493. return dx;
  494. }
  495. /**
  496. 设置字体
  497. @api eink.setFont(font)
  498. @userdata 字体
  499. @usage
  500. -- 设置为字体,对之后的print有效
  501. eink.setFont(eink.font_opposansm12_chinese)
  502. */
  503. static int l_eink_set_font(lua_State *L) {
  504. if (check_init() == 0) {
  505. return 0;
  506. }
  507. if (!lua_islightuserdata(L, 1)) {
  508. LLOGE("only font pointer is allow");
  509. return 0;
  510. }
  511. const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
  512. if (ptr == NULL) {
  513. LLOGE("only font pointer is allow");
  514. return 0;
  515. }
  516. u8g2_SetFont(&(econf.luat_eink_u8g2), ptr);
  517. lua_pushboolean(L, 1);
  518. return 1;
  519. }
  520. /**
  521. 绘制字符串
  522. @api eink.print(x, y, str, colored)
  523. @int x坐标
  524. @int y坐标
  525. @string 字符串
  526. @int 颜色, 可以是0或者1, 默认是0
  527. @return nil 无返回值
  528. @usage
  529. -- 先设置字体, 然后写字
  530. -- 可用字体取决于具体的固件, 如果没有你想要的大小,可以云编译一份自定义固件
  531. -- font_opposansm8_chinese
  532. -- font_opposansm10_chinese
  533. -- font_opposansm12_chinese
  534. -- font_opposansm14_chinese
  535. -- font_opposansm16_chinese
  536. eink.setFont(eink.font_opposansm12_chinese)
  537. eink.print(10, 20, "LuatOS")
  538. */
  539. static int l_eink_print(lua_State *L)
  540. {
  541. size_t len;
  542. int x = luaL_checkinteger(L, 1);
  543. int y = luaL_checkinteger(L, 2);
  544. const char *str = luaL_checklstring(L, 3, &len);
  545. if (check_init() == 0) {
  546. return 0;
  547. }
  548. econf.ctxs[econf.ctx_index]->str_color = luaL_optinteger(L, 4, 0);
  549. uint16_t e;
  550. int16_t delta, sum;
  551. utf8_state = 0;
  552. sum = 0;
  553. for(;;){
  554. e = utf8_next((uint8_t)*str);
  555. if ( e == 0x0ffff )
  556. break;
  557. str++;
  558. if ( e != 0x0fffe ){
  559. delta = u8g2_font_draw_glyph(&(econf.luat_eink_u8g2), x, y, e);
  560. switch(econf.luat_eink_u8g2.font_decode.dir){
  561. case 0:
  562. x += delta;
  563. break;
  564. case 1:
  565. y += delta;
  566. break;
  567. case 2:
  568. x -= delta;
  569. break;
  570. case 3:
  571. y -= delta;
  572. break;
  573. }
  574. sum += delta;
  575. }
  576. }
  577. return 0;
  578. }
  579. /**
  580. 将缓冲区图像输出到屏幕
  581. @api eink.show(x, y, noClear)
  582. @int x 输出的x坐标,默认0
  583. @int y 输出的y坐标,默认0
  584. @bool 可选,默认false。如果为true则不进行清屏,直接刷上新内容
  585. @return nil 无返回值
  586. */
  587. static int l_eink_show(lua_State *L)
  588. {
  589. // int x = luaL_optinteger(L, 1, 0);
  590. // int y = luaL_optinteger(L, 2, 0);
  591. int no_clear = lua_toboolean(L, 3);
  592. /* Display the frame_buffer */
  593. //EPD_SetFrameMemory(&epd, frame_buffer, x, y, Paint_GetWidth(&econf.ctxs[econf.ctx_index]->paint), Paint_GetHeight(&econf.ctxs[econf.ctx_index]->paint));
  594. //EPD_DisplayFrame(&epd);
  595. if (econf.async){
  596. econf.idp = luat_pushcwait(L);
  597. if (check_init() == 0) {
  598. lua_pushinteger(L,0);
  599. luat_pushcwait_error(L,1);
  600. return 1;
  601. }
  602. uint8_t event = EPD_SHOW;
  603. if (!no_clear){
  604. event |= EPD_CLEAR;
  605. }
  606. luat_rtos_queue_send(econf.eink_queue_handle, &event, sizeof(uint8_t), 0);
  607. }else{
  608. if (check_init() == 0) {
  609. return 0;
  610. }
  611. if(!no_clear)
  612. EPD_Clear();
  613. if (econf.ctxs[1] == NULL)
  614. EPD_Display(econf.ctxs[0]->fb, NULL);
  615. else
  616. EPD_Display(econf.ctxs[0]->fb, econf.ctxs[1]->fb);
  617. lua_pushboolean(L, 1);
  618. }
  619. return 1;
  620. }
  621. /**
  622. 直接输出数据到屏幕,支持双色数据
  623. @api eink.draw(buff, buff2, noclear)
  624. @userdata zbuff指针
  625. @userdata zbuff指针
  626. @bool 可选,默认false。如果为true则不进行清屏,直接刷上新内容
  627. @return nil 无返回值
  628. */
  629. static int l_eink_draw(lua_State *L)
  630. {
  631. luat_zbuff_t* buff = tozbuff(L);
  632. luat_zbuff_t* buff2 = buff;
  633. int no_clear = lua_toboolean(L, 3);
  634. if (lua_isuserdata(L, 2)) {
  635. buff2 = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  636. }
  637. if (econf.async){
  638. econf.idp = luat_pushcwait(L);
  639. if (check_init() == 0) {
  640. lua_pushinteger(L,0);
  641. luat_pushcwait_error(L,1);
  642. return 1;
  643. }
  644. uint8_t event = EPD_DRAW;
  645. if (!no_clear){
  646. event |= EPD_CLEAR;
  647. }
  648. luat_rtos_queue_send(econf.eink_queue_handle, &event, sizeof(uint8_t), 0);
  649. }else{
  650. if (check_init() == 0) {
  651. return 0;
  652. }
  653. if(!no_clear)
  654. EPD_Clear();
  655. EPD_Display(buff->addr, buff2->addr);
  656. lua_pushboolean(L, 1);
  657. }
  658. return 1;
  659. }
  660. /**
  661. 缓冲区绘制线
  662. @api eink.line(x, y, x2, y2, colored)
  663. @int 起点x坐标
  664. @int 起点y坐标
  665. @int 终点x坐标
  666. @int 终点y坐标
  667. @return nil 无返回值
  668. @usage
  669. eink.line(0, 0, 10, 20, 0)
  670. */
  671. static int l_eink_line(lua_State *L)
  672. {
  673. int x = luaL_checkinteger(L, 1);
  674. int y = luaL_checkinteger(L, 2);
  675. int x2 = luaL_checkinteger(L, 3);
  676. int y2 = luaL_checkinteger(L, 4);
  677. int colored = luaL_optinteger(L, 5, 0);
  678. if (check_init() == 0) {
  679. return 0;
  680. }
  681. Paint_DrawLine(&econf.ctxs[econf.ctx_index]->paint, x, y, x2, y2, colored);
  682. return 0;
  683. }
  684. /**
  685. 缓冲区绘制矩形
  686. @api eink.rect(x, y, x2, y2, colored, fill)
  687. @int 左上顶点x坐标
  688. @int 左上顶点y坐标
  689. @int 右下顶点x坐标
  690. @int 右下顶点y坐标
  691. @int 默认是0
  692. @int 是否填充,默认是0,不填充
  693. @return nil 无返回值
  694. @usage
  695. eink.rect(0, 0, 10, 20)
  696. eink.rect(0, 0, 10, 20,0, 1) -- Filled
  697. */
  698. static int l_eink_rect(lua_State *L)
  699. {
  700. int x = luaL_checkinteger(L, 1);
  701. int y = luaL_checkinteger(L, 2);
  702. int x2 = luaL_checkinteger(L, 3);
  703. int y2 = luaL_checkinteger(L, 4);
  704. int colored = luaL_optinteger(L, 5, 0);
  705. int fill = luaL_optinteger(L, 6, 0);
  706. if (check_init() == 0) {
  707. return 0;
  708. }
  709. if(fill)
  710. Paint_DrawFilledRectangle(&econf.ctxs[econf.ctx_index]->paint, x, y, x2, y2, colored);
  711. else
  712. Paint_DrawRectangle(&econf.ctxs[econf.ctx_index]->paint, x, y, x2, y2, colored);
  713. return 0;
  714. }
  715. /**
  716. 缓冲区绘制圆形
  717. @api eink.circle(x, y, radius, colored, fill)
  718. @int 圆心x坐标
  719. @int 圆心y坐标
  720. @int 半径
  721. @int 默认是0
  722. @int 是否填充,默认是0,不填充
  723. @return nil 无返回值
  724. @usage
  725. eink.circle(0, 0, 10)
  726. eink.circle(0, 0, 10, 1, 1) -- Filled
  727. */
  728. static int l_eink_circle(lua_State *L)
  729. {
  730. int x = luaL_checkinteger(L, 1);
  731. int y = luaL_checkinteger(L, 2);
  732. int radius = luaL_checkinteger(L, 3);
  733. int colored = luaL_optinteger(L, 4, 0);
  734. int fill = luaL_optinteger(L, 5, 0);
  735. if (check_init() == 0) {
  736. return 0;
  737. }
  738. if(fill)
  739. Paint_DrawFilledCircle(&econf.ctxs[econf.ctx_index]->paint, x, y, radius, colored);
  740. else
  741. Paint_DrawCircle(&econf.ctxs[econf.ctx_index]->paint, x, y, radius, colored);
  742. return 0;
  743. }
  744. /**
  745. 缓冲区绘制QRCode
  746. @api eink.qrcode(x, y, str, size)
  747. @int x坐标
  748. @int y坐标
  749. @string 二维码的内容
  750. @int 可选,显示大小,不可小于21,默认21
  751. @return nil 无返回值
  752. */
  753. static int l_eink_qrcode(lua_State *L)
  754. {
  755. size_t len;
  756. int x = luaL_checkinteger(L, 1);
  757. int y = luaL_checkinteger(L, 2);
  758. const char* text = luaL_checklstring(L, 3, &len);
  759. int size = luaL_optinteger(L, 4,21);
  760. if (check_init() == 0) {
  761. return 0;
  762. }
  763. uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  764. uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  765. if (qrcode == NULL || tempBuffer == NULL) {
  766. if (qrcode)
  767. luat_heap_free(qrcode);
  768. if (tempBuffer)
  769. luat_heap_free(tempBuffer);
  770. LLOGE("qrcode out of memory");
  771. return 0;
  772. }
  773. bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_MEDIUM,
  774. qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
  775. if (ok){
  776. int qr_size = qrcodegen_getSize(qrcode);
  777. int scale = size / qr_size ;
  778. if (!scale)scale = 1;
  779. int margin = (size - qr_size * scale) / 2;
  780. x+=margin;
  781. y+=margin;
  782. for (int j = 0; j < qr_size; j++) {
  783. for (int i = 0; i < qr_size; i++) {
  784. if (qrcodegen_getModule(qrcode, i, j)){
  785. Paint_DrawFilledRectangle(&econf.ctxs[econf.ctx_index]->paint,x+i*scale,y+j*scale,x+(i+1)*scale,y+(j+1)*scale,COLORED);
  786. }
  787. }
  788. }
  789. }else{
  790. LLOGE("qrcodegen_encodeText %d",ok);
  791. }
  792. if (qrcode)
  793. luat_heap_free(qrcode);
  794. if (tempBuffer)
  795. luat_heap_free(tempBuffer);
  796. return 0;
  797. }
  798. /**
  799. 缓冲区绘制电池
  800. @api eink.bat(x, y, bat)
  801. @int x坐标
  802. @int y坐标
  803. @int 电池电压,单位毫伏
  804. @return nil 无返回值
  805. */
  806. static int l_eink_bat(lua_State *L)
  807. {
  808. int x = luaL_checkinteger(L, 1);
  809. int y = luaL_checkinteger(L, 2);
  810. int bat = luaL_checkinteger(L, 3);
  811. int batnum = 0;
  812. if (bat > 4200){
  813. LLOGE("The bat is greater than 4200");
  814. }
  815. if(bat > 4080)batnum = 100;
  816. if(bat <= 4080 && bat > 4000)batnum = 90;
  817. if(bat <= 4000 && bat > 3930)batnum = 80;
  818. if(bat <= 3930 && bat > 3870)batnum = 70;
  819. if(bat <= 3870 && bat > 3820)batnum = 60;
  820. if(bat <= 3820 && bat > 3790)batnum = 50;
  821. if(bat <= 3790 && bat > 3770)batnum = 40;
  822. if(bat <= 3770 && bat > 3730)batnum = 30;
  823. if(bat <= 3730 && bat > 3700)batnum = 20;
  824. if(bat <= 3700 && bat > 3680)batnum = 15;
  825. if(bat <= 3680 && bat > 3500)batnum = 10;
  826. if(bat <= 3500 && bat > 2500)batnum = 5;
  827. batnum = 20 - (int)(batnum / 5) + 3;
  828. if (check_init() == 0) {
  829. return 0;
  830. }
  831. // w外框
  832. Paint_DrawRectangle(&econf.ctxs[econf.ctx_index]->paint, x+0, y+3, x+2, y+6, COLORED);
  833. Paint_DrawRectangle(&econf.ctxs[econf.ctx_index]->paint, x+2, y+0, x+23, y+9, COLORED);
  834. // 3 ~21 100 / 5
  835. Paint_DrawFilledRectangle(&econf.ctxs[econf.ctx_index]->paint, x+batnum, y+1, x+22, y+8, COLORED);
  836. return 0;
  837. }
  838. /**
  839. 缓冲区绘制天气图标
  840. @api eink.weather_icon(x, y, code)
  841. @int x坐标
  842. @int y坐标
  843. @int 天气代号
  844. @return nil 无返回值
  845. */
  846. static int l_eink_weather_icon(lua_State *L)
  847. {
  848. size_t len;
  849. int x = luaL_checkinteger(L, 1);
  850. int y = luaL_checkinteger(L, 2);
  851. int code = luaL_checkinteger(L, 3);
  852. const char* str = luaL_optlstring(L, 4, "nil", &len);
  853. const unsigned char * icon = gImage_999;
  854. if (strcmp(str, "xue") == 0)code = 401;
  855. if (strcmp(str, "lei") == 0)code = 302;
  856. if (strcmp(str, "shachen") == 0)code = 503;
  857. if (strcmp(str, "wu") == 0)code = 501;
  858. if (strcmp(str, "bingbao") == 0)code = 504;
  859. if (strcmp(str, "yun") == 0)code = 103;
  860. if (strcmp(str, "yu") == 0)code = 306;
  861. if (strcmp(str, "yin") == 0)code = 101;
  862. if (strcmp(str, "qing") == 0)code = 100;
  863. // xue、lei、shachen、wu、bingbao、yun、yu、yin、qing
  864. //if(code == 64)
  865. // for(int i = 0; i < 64; i++)
  866. // {
  867. // for (int j = 0; j < 8; j++)
  868. // {
  869. // for (int k = 0; k < 8; k++)
  870. // (gImage_103[i*8+j] << k )& 0x80 ? Paint_DrawPixel(&econf.ctxs[econf.ctx_index]->paint, x+j*8+k, y+i, COLORED) : Paint_DrawPixel(&econf.ctxs[econf.ctx_index]->paint, x+j*8+k, y+i, UNCOLORED);
  871. // }
  872. // }
  873. if (check_init() == 0) {
  874. return 0;
  875. }
  876. switch (code)
  877. {
  878. case 100:icon = gImage_100;break;
  879. case 101:icon = gImage_101;break;
  880. case 102:icon = gImage_102;break;
  881. case 103:icon = gImage_103;break;
  882. case 104:icon = gImage_104;break;
  883. case 200:icon = gImage_200;break;
  884. case 201:icon = gImage_201;break;
  885. case 205:icon = gImage_205;break;
  886. case 208:icon = gImage_208;break;
  887. case 301:icon = gImage_301;break;
  888. case 302:icon = gImage_302;break;
  889. case 303:icon = gImage_303;break;
  890. case 304:icon = gImage_304;break;
  891. case 305:icon = gImage_305;break;
  892. case 306:icon = gImage_306;break;
  893. case 307:icon = gImage_307;break;
  894. case 308:icon = gImage_308;break;
  895. case 309:icon = gImage_309;break;
  896. case 310:icon = gImage_310;break;
  897. case 311:icon = gImage_311;break;
  898. case 312:icon = gImage_312;break;
  899. case 313:icon = gImage_313;break;
  900. case 400:icon = gImage_400;break;
  901. case 401:icon = gImage_401;break;
  902. case 402:icon = gImage_402;break;
  903. case 403:icon = gImage_403;break;
  904. case 404:icon = gImage_404;break;
  905. case 405:icon = gImage_405;break;
  906. case 406:icon = gImage_406;break;
  907. case 407:icon = gImage_407;break;
  908. case 500:icon = gImage_500;break;
  909. case 501:icon = gImage_501;break;
  910. case 502:icon = gImage_502;break;
  911. case 503:icon = gImage_503;break;
  912. case 504:icon = gImage_504;break;
  913. case 507:icon = gImage_507;break;
  914. case 508:icon = gImage_508;break;
  915. case 900:icon = gImage_900;break;
  916. case 901:icon = gImage_901;break;
  917. case 999:icon = gImage_999;break;
  918. default:
  919. break;
  920. }
  921. for(int i = 0; i < 48; i++)
  922. {
  923. for (int j = 0; j < 6; j++)
  924. {
  925. for (int k = 0; k < 8; k++)
  926. (icon[i*6+j] << k )& 0x80 ? Paint_DrawPixel(&econf.ctxs[econf.ctx_index]->paint, x+j*8+k, y+i, COLORED) : Paint_DrawPixel(&econf.ctxs[econf.ctx_index]->paint, x+j*8+k, y+i, UNCOLORED);
  927. }
  928. }
  929. return 0;
  930. }
  931. /**
  932. 设置墨水屏驱动型号
  933. @api eink.model(m)
  934. @int 型号名称, 例如 eink.model(eink.MODEL_1in54_V2)
  935. @return nil 无返回值
  936. */
  937. static int l_eink_model(lua_State *L) {
  938. EPD_Model(luaL_checkinteger(L, 1));
  939. return 0;
  940. }
  941. #ifdef LUAT_USE_GTFONT
  942. #include "GT5SLCD2E_1A.h"
  943. 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);
  944. 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);
  945. static int l_eink_draw_gtfont_gb2312(lua_State *L) {
  946. unsigned char buf[128];
  947. int len;
  948. int i = 0;
  949. uint8_t strhigh,strlow ;
  950. uint16_t str;
  951. const char *fontCode = luaL_checklstring(L, 1,&len);
  952. unsigned char size = luaL_checkinteger(L, 2);
  953. int x = luaL_checkinteger(L, 3);
  954. int y = luaL_checkinteger(L, 4);
  955. while ( i < len){
  956. strhigh = *fontCode;
  957. fontCode++;
  958. strlow = *fontCode;
  959. str = (strhigh<<8)|strlow;
  960. fontCode++;
  961. get_font(buf, 1, str, size, size, size);
  962. gtfont_draw_w(buf , x ,y , size , size,Paint_DrawPixel,&econf.ctxs[econf.ctx_index]->paint,1);
  963. x+=size;
  964. i+=2;
  965. }
  966. return 0;
  967. }
  968. static int l_eink_draw_gtfont_gb2312_gray(lua_State* L) {
  969. unsigned char buf[2048];
  970. int len;
  971. int i = 0;
  972. uint8_t strhigh,strlow ;
  973. uint16_t str;
  974. const char *fontCode = luaL_checklstring(L, 1,&len);
  975. unsigned char size = luaL_checkinteger(L, 2);
  976. unsigned char font_g = luaL_checkinteger(L, 3);
  977. int x = luaL_checkinteger(L, 4);
  978. int y = luaL_checkinteger(L, 5);
  979. while ( i < len){
  980. strhigh = *fontCode;
  981. fontCode++;
  982. strlow = *fontCode;
  983. str = (strhigh<<8)|strlow;
  984. fontCode++;
  985. get_font(buf, 1, str, size*font_g, size*font_g, size*font_g);
  986. Gray_Process(buf,size,size,font_g);
  987. gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,Paint_DrawPixel,&econf.ctxs[econf.ctx_index]->paint,1);
  988. x+=size;
  989. i+=2;
  990. }
  991. return 0;
  992. }
  993. #ifdef LUAT_USE_GTFONT_UTF8
  994. extern unsigned short unicodetogb2312 ( unsigned short chr);
  995. static int l_eink_draw_gtfont_utf8(lua_State *L) {
  996. unsigned char buf[128];
  997. int len;
  998. int i = 0;
  999. uint8_t strhigh,strlow ;
  1000. uint16_t e,str;
  1001. const char *fontCode = luaL_checklstring(L, 1,&len);
  1002. unsigned char size = luaL_checkinteger(L, 2);
  1003. int x = luaL_checkinteger(L, 3);
  1004. int y = luaL_checkinteger(L, 4);
  1005. for(;;){
  1006. e = utf8_next((uint8_t)*fontCode);
  1007. if ( e == 0x0ffff )
  1008. break;
  1009. fontCode++;
  1010. if ( e != 0x0fffe ){
  1011. uint16_t str = unicodetogb2312(e);
  1012. get_font(buf, 1, str, size, size, size);
  1013. gtfont_draw_w(buf , x ,y , size , size,Paint_DrawPixel,&econf.ctxs[econf.ctx_index]->paint,1);
  1014. x+=size;
  1015. }
  1016. }
  1017. return 0;
  1018. }
  1019. static int l_eink_draw_gtfont_utf8_gray(lua_State* L) {
  1020. unsigned char buf[2048];
  1021. int len;
  1022. int i = 0;
  1023. uint8_t strhigh,strlow ;
  1024. uint16_t e,str;
  1025. const char *fontCode = luaL_checklstring(L, 1,&len);
  1026. unsigned char size = luaL_checkinteger(L, 2);
  1027. unsigned char font_g = luaL_checkinteger(L, 3);
  1028. int x = luaL_checkinteger(L, 4);
  1029. int y = luaL_checkinteger(L, 5);
  1030. for(;;){
  1031. e = utf8_next((uint8_t)*fontCode);
  1032. if ( e == 0x0ffff )
  1033. break;
  1034. fontCode++;
  1035. if ( e != 0x0fffe ){
  1036. uint16_t str = unicodetogb2312(e);
  1037. get_font(buf, 1, str, size*font_g, size*font_g, size*font_g);
  1038. Gray_Process(buf,size,size,font_g);
  1039. gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,Paint_DrawPixel,&econf.ctxs[econf.ctx_index]->paint,1);
  1040. x+=size;
  1041. }
  1042. }
  1043. return 0;
  1044. }
  1045. #endif // LUAT_USE_GTFONT_UTF8
  1046. #endif // LUAT_USE_GTFONT
  1047. static void eink_DrawHXBM(uint16_t x, uint16_t y, uint16_t len, const uint8_t *b){
  1048. uint8_t mask = 1;
  1049. if (check_init() == 0) {
  1050. return;
  1051. }
  1052. while(len > 0) {
  1053. if ( *b & mask ) drawFastHLine(&econf.ctxs[econf.ctx_index]->paint, x, y, 1,COLORED);
  1054. else drawFastVLine(&econf.ctxs[econf.ctx_index]->paint, x, y, 1,UNCOLORED);
  1055. x++;
  1056. mask <<= 1;
  1057. if ( mask == 0 ){
  1058. mask = 1;
  1059. b++;
  1060. }
  1061. len--;
  1062. }
  1063. }
  1064. /*
  1065. 绘制位图
  1066. @api eink.drawXbm(x, y, w, h, data)
  1067. @int X坐标
  1068. @int y坐标
  1069. @int 位图宽
  1070. @int 位图高
  1071. @int 位图数据,每一位代表一个像素
  1072. @usage
  1073. -- 取模使用PCtoLCD2002软件即可
  1074. -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
  1075. eink.drawXbm(0, 0, 16,16, string.char(
  1076. 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
  1077. 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
  1078. ))
  1079. */
  1080. static int l_eink_drawXbm(lua_State *L){
  1081. int x = luaL_checkinteger(L, 1);
  1082. int y = luaL_checkinteger(L, 2);
  1083. int w = luaL_checkinteger(L, 3);
  1084. int h = luaL_checkinteger(L, 4);
  1085. size_t len = 0;
  1086. const char* data = luaL_checklstring(L, 5, &len);
  1087. if (h < 1) return 0; // 行数必须大于0
  1088. if (len*8/h < w) return 0; // 起码要填满一行
  1089. if (len < h*w/8) return 0;
  1090. if (check_init() == 0) {
  1091. return 0;
  1092. }
  1093. uint8_t blen = 0;
  1094. blen = w;
  1095. blen += 7;
  1096. blen >>= 3;
  1097. while( h > 0 ){
  1098. eink_DrawHXBM(x, y, w, (const uint8_t*)data);
  1099. data += blen;
  1100. y++;
  1101. h--;
  1102. }
  1103. lua_pushboolean(L, 1);
  1104. return 1;
  1105. }
  1106. /*
  1107. 切换颜色画板, 适合多色墨水屏
  1108. @api eink.setCtx(index)
  1109. @int 颜色索引, 黑色为0, 红色为1
  1110. @usage
  1111. -- 仅适合多色墨水屏, 对单色墨水屏(只有黑白)的无效
  1112. eink.setCtx(1)
  1113. -- 切换后, 所有drawXXX都会操作在指定颜色的画板
  1114. */
  1115. static int l_eink_set_ctx(lua_State *L) {
  1116. int index = luaL_checkinteger(L, 1);
  1117. if (index < 0 || index > 1) {
  1118. LLOGE("invaild ctx index %d", index);
  1119. return 0;
  1120. }
  1121. if (econf.ctxs[index] == NULL) {
  1122. LLOGE("invaild ctx index %d", index);
  1123. return 0;
  1124. }
  1125. econf.ctx_index = index;
  1126. lua_pushboolean(L, 1);
  1127. return 1;
  1128. }
  1129. /*
  1130. 异步方式,使用此方式需要先 require("sysplus") 之后 eink.clear().wait() eink.show().wait()进行刷屏
  1131. @api eink.async(index)
  1132. @int 1 使用异步
  1133. @usage
  1134. eink.async(1)
  1135. spi_eink = spi.deviceSetup(spi_id,pin_cs,0,0,8,20*1000*1000,spi.MSB,1,1)
  1136. eink.init(eink.MODEL_1in54,
  1137. {port = "device",pin_dc = pin_dc, pin_busy = pin_busy,pin_rst = pin_reset},
  1138. spi_eink)
  1139. eink.setWin(200, 200, 0)
  1140. sys.wait(100)
  1141. log.info("e-paper 1.54", "Testing Go")
  1142. eink.print(30, 20, "LuatOS-AIR780E",0x00)
  1143. eink.show().wait()
  1144. log.info("e-paper 1.54", "Testing End")
  1145. */
  1146. static int l_eink_async(lua_State *L) {
  1147. econf.async = luaL_checkinteger(L, 1);
  1148. return 0;
  1149. }
  1150. #include "rotable2.h"
  1151. static const rotable_Reg_t reg_eink[] =
  1152. {
  1153. { "async", ROREG_FUNC(l_eink_async)},
  1154. { "init", ROREG_FUNC(l_eink_init)},
  1155. { "setup", ROREG_FUNC(l_eink_setup)},
  1156. { "sleep", ROREG_FUNC(l_eink_sleep)},
  1157. { "clear", ROREG_FUNC(l_eink_clear)},
  1158. { "setWin", ROREG_FUNC(l_eink_setWin)},
  1159. { "getWin", ROREG_FUNC(l_eink_getWin)},
  1160. { "print", ROREG_FUNC(l_eink_print)},
  1161. { "show", ROREG_FUNC(l_eink_show)},
  1162. { "rect", ROREG_FUNC(l_eink_rect)},
  1163. { "circle", ROREG_FUNC(l_eink_circle)},
  1164. { "line", ROREG_FUNC(l_eink_line)},
  1165. { "setFont", ROREG_FUNC(l_eink_set_font)},
  1166. { "qrcode", ROREG_FUNC(l_eink_qrcode)},
  1167. { "bat", ROREG_FUNC(l_eink_bat)},
  1168. { "weather_icon", ROREG_FUNC(l_eink_weather_icon)},
  1169. { "model", ROREG_FUNC(l_eink_model)},
  1170. { "drawXbm", ROREG_FUNC(l_eink_drawXbm)},
  1171. { "draw", ROREG_FUNC(l_eink_draw)},
  1172. { "setCtx", ROREG_FUNC(l_eink_set_ctx)},
  1173. #ifdef LUAT_USE_GTFONT
  1174. { "drawGtfontGb2312", ROREG_FUNC(l_eink_draw_gtfont_gb2312)},
  1175. { "drawGtfontGb2312Gray", ROREG_FUNC(l_eink_draw_gtfont_gb2312_gray)},
  1176. #ifdef LUAT_USE_GTFONT_UTF8
  1177. { "drawGtfontUtf8", ROREG_FUNC(l_eink_draw_gtfont_utf8)},
  1178. { "drawGtfontUtf8Gray", ROREG_FUNC(l_eink_draw_gtfont_utf8_gray)},
  1179. #endif // LUAT_USE_GTFONT_UTF8
  1180. #endif // LUAT_USE_GTFONT
  1181. //@const MODEL_1in02d number 1.02寸d
  1182. { "MODEL_1in02d", ROREG_INT(MODEL_1in02d)},
  1183. //@const MODEL_1in54 number 1.54寸
  1184. { "MODEL_1in54", ROREG_INT(MODEL_1in54)},
  1185. //@const MODEL_1in54_V2 number 1.54寸_V2
  1186. { "MODEL_1in54_V2", ROREG_INT(MODEL_1in54_V2)},
  1187. //@const MODEL_1in54b number 1.54寸b
  1188. { "MODEL_1in54b", ROREG_INT(MODEL_1in54b)},
  1189. //@const MODEL_1in54b_V2 number 1.54寸b_V2
  1190. { "MODEL_1in54b_V2", ROREG_INT(MODEL_1in54b_V2)},
  1191. //@const MODEL_1in54_V3 number 1.54寸_V3
  1192. { "MODEL_1in54_V3", ROREG_INT(MODEL_1in54_V3)},
  1193. //@const MODEL_1in54c number 1.54寸c
  1194. { "MODEL_1in54c", ROREG_INT(MODEL_1in54c)},
  1195. //@const MODEL_2in13 number 2.13寸
  1196. { "MODEL_2in13", ROREG_INT(MODEL_2in13)},
  1197. //@const MODEL_2in13bc number 2.13寸bc
  1198. { "MODEL_2in13bc", ROREG_INT(MODEL_2in13bc)},
  1199. //@const MODEL_2in13d number 2.13寸d
  1200. { "MODEL_2in13d", ROREG_INT(MODEL_2in13d)},
  1201. //@const MODEL_2in13_V2 number 2.13寸V2
  1202. { "MODEL_2in13_V2", ROREG_INT(MODEL_2in13_V2)},
  1203. //@const MODEL_2in54b_V3 number 2.13寸b_V3
  1204. { "MODEL_2in13b_V3", ROREG_INT(MODEL_2in13b_V3)},
  1205. //@const MODEL_2in66 number 2.66寸
  1206. { "MODEL_2in66", ROREG_INT(MODEL_2in66)},
  1207. //@const MODEL_2in66b number 2.66寸b
  1208. { "MODEL_2in66b", ROREG_INT(MODEL_2in66b)},
  1209. //@const MODEL_2in7 number 2.7寸
  1210. { "MODEL_2in7", ROREG_INT(MODEL_2in7)},
  1211. //@const MODEL_2in7b number 2.7寸b
  1212. { "MODEL_2in7b", ROREG_INT(MODEL_2in7b)},
  1213. //@const MODEL_2in9 number 2.9寸
  1214. { "MODEL_2in9", ROREG_INT(MODEL_2in9)},
  1215. //@const MODEL_2in9_V2 number 2.9寸_V2
  1216. { "MODEL_2in9_V2", ROREG_INT(MODEL_2in9_V2)},
  1217. //@const MODEL_2in9bc number 2.9寸bc
  1218. { "MODEL_2in9bc", ROREG_INT(MODEL_2in9bc)},
  1219. //@const MODEL_2in9b_V3 number 2.9寸b_V3
  1220. { "MODEL_2in9b_V3", ROREG_INT(MODEL_2in9b_V3)},
  1221. //@const MODEL_2in9d number 2.9寸d
  1222. { "MODEL_2in9d", ROREG_INT(MODEL_2in9d)},
  1223. //@const MODEL_3in7 number 3.7寸
  1224. { "MODEL_3in7", ROREG_INT(MODEL_3in7)},
  1225. //@const MODEL_4in2 number 4.2寸
  1226. { "MODEL_4in2", ROREG_INT(MODEL_4in2)},
  1227. //@const MODEL_4in2bc number 4.2寸b
  1228. { "MODEL_4in2bc", ROREG_INT(MODEL_4in2bc)},
  1229. //@const MODEL_4in2b_V2 number 4.2寸V2
  1230. { "MODEL_4in2b_V2", ROREG_INT(MODEL_4in2b_V2)},
  1231. //@const MODEL_5in65f number 5.65寸f
  1232. { "MODEL_5in65f", ROREG_INT(MODEL_5in65f)},
  1233. //@const MODEL_5in83 number 5.83寸
  1234. { "MODEL_5in83", ROREG_INT(MODEL_5in83)},
  1235. //@const MODEL_5in83bc number 5.83寸bc
  1236. { "MODEL_5in83bc", ROREG_INT(MODEL_5in83bc)},
  1237. //@const MODEL_5in83_V2 number 5.83寸V2
  1238. { "MODEL_5in83_V2", ROREG_INT(MODEL_5in83_V2)},
  1239. //@const MODEL_5in83b_V2 number 5.83寸bV2
  1240. { "MODEL_5in83b_V2", ROREG_INT(MODEL_5in83b_V2)},
  1241. //@const MODEL_7in5 number 7.5寸
  1242. { "MODEL_7in5", ROREG_INT(MODEL_7in5)},
  1243. //@const MODEL_7in5_HD number 7.5寸HD
  1244. { "MODEL_7in5_HD", ROREG_INT(MODEL_7in5_HD)},
  1245. //@const MODEL_7in5_V2 number 7.5寸V2
  1246. { "MODEL_7in5_V2", ROREG_INT(MODEL_7in5_V2)},
  1247. //@const MODEL_7in5bc number 7.5寸bc
  1248. { "MODEL_7in5bc", ROREG_INT(MODEL_7in5bc)},
  1249. //@const MODEL_7in5b_HD number 7.5寸b_HD
  1250. { "MODEL_7in5b_HD", ROREG_INT(MODEL_7in5b_HD)},
  1251. //@const MODEL_7in5b_V2 number 7.5寸b_V2
  1252. { "MODEL_7in5b_V2", ROREG_INT(MODEL_7in5b_V2)},
  1253. // 默认只带8号字体
  1254. //@const font_opposansm8 font 8号字体
  1255. { "font_opposansm8", ROREG_PTR((void*)u8g2_font_opposansm8)},
  1256. #ifdef USE_U8G2_OPPOSANSM_ENGLISH
  1257. //@const font_unifont_t_symbols font 符号字体
  1258. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  1259. //@const font_open_iconic_weather_6x_t font 天气字体
  1260. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  1261. //@const font_opposansm10 font 10号字体
  1262. //@const font_opposansm12 font 12号字体
  1263. //@const font_opposansm16 font 16号字体
  1264. //@const font_opposansm18 font 18号字体
  1265. //@const font_opposansm20 font 20号字体
  1266. //@const font_opposansm22 font 22号字体
  1267. //@const font_opposansm24 font 24号字体
  1268. //@const font_opposansm32 font 32号字体
  1269. { "font_opposansm10", ROREG_PTR((void*)u8g2_font_opposansm10)},
  1270. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  1271. { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
  1272. { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
  1273. { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
  1274. { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
  1275. { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
  1276. { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
  1277. #endif
  1278. #ifdef USE_U8G2_OPPOSANSM8_CHINESE
  1279. //@const font_opposansm8_chinese font 8号中文字体
  1280. { "font_opposansm8_chinese", ROREG_PTR((void*)u8g2_font_opposansm8_chinese)},
  1281. #endif
  1282. #ifdef USE_U8G2_OPPOSANSM10_CHINESE
  1283. //@const font_opposansm8_chinese font 10号中文字体
  1284. { "font_opposansm10_chinese", ROREG_PTR((void*)u8g2_font_opposansm10_chinese)},
  1285. #endif
  1286. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  1287. //@const font_opposansm12_chinese font 12号中文字体
  1288. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  1289. #endif
  1290. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  1291. //@const font_opposansm16_chinese font 16号中文字体
  1292. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  1293. #endif
  1294. #ifdef USE_U8G2_OPPOSANSM18_CHINESE
  1295. //@const font_opposansm18_chinese font 18号中文字体
  1296. { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
  1297. #endif
  1298. #ifdef USE_U8G2_OPPOSANSM20_CHINESE
  1299. //@const font_opposansm20_chinese font 20号中文字体
  1300. { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
  1301. #endif
  1302. #ifdef USE_U8G2_OPPOSANSM22_CHINESE
  1303. //@const font_opposansm22_chinese font 22号中文字体
  1304. { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
  1305. #endif
  1306. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  1307. //@const font_opposansm24_chinese font 24号中文字体
  1308. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  1309. #endif
  1310. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  1311. //@const font_opposansm32_chinese font 32号中文字体
  1312. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  1313. #endif
  1314. { NULL, ROREG_INT(0)}
  1315. };
  1316. LUAMOD_API int luaopen_eink( lua_State *L ){
  1317. luat_newlib2(L, reg_eink);
  1318. return 1;
  1319. }