luat_lib_eink.c 47 KB

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