luat_lib_eink.c 47 KB

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