luat_lib_lcd.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. /*
  2. @module lcd
  3. @summary lcd驱动模块
  4. @version 1.0
  5. @date 2021.06.16
  6. @demo lcd
  7. @tag LUAT_USE_LCD
  8. */
  9. #include "luat_base.h"
  10. #include "luat_lcd.h"
  11. #include "luat_mem.h"
  12. #include "luat_zbuff.h"
  13. #include "luat_fs.h"
  14. #include "luat_gpio.h"
  15. #define LUAT_LOG_TAG "lcd"
  16. #include "luat_log.h"
  17. #include "u8g2.h"
  18. #include "u8g2_luat_fonts.h"
  19. #include "luat_u8g2.h"
  20. #include "qrcodegen.h"
  21. int8_t u8g2_font_decode_get_signed_bits(u8g2_font_decode_t *f, uint8_t cnt);
  22. uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt);
  23. extern luat_color_t BACK_COLOR , FORE_COLOR ;
  24. extern const luat_lcd_opts_t lcd_opts_custom;
  25. typedef struct lcd_reg {
  26. const char *name;
  27. const luat_lcd_opts_t *lcd_opts;
  28. }lcd_reg_t;
  29. luat_lcd_opts_t lcd_opts_nv3052c = {
  30. .name = "nv3052c",
  31. };
  32. luat_lcd_opts_t lcd_opts_hx8282 = {
  33. .name = "hx8282",
  34. };
  35. luat_lcd_opts_t lcd_opts_st7701s = {
  36. .name = "st7701s",
  37. };
  38. luat_lcd_opts_t lcd_opts_st7701sn = {
  39. .name = "st7701sn",
  40. };
  41. static const lcd_reg_t lcd_regs[] = {
  42. {"custom", &lcd_opts_custom}, //0 固定为第零个
  43. {"st7735", &lcd_opts_st7735},
  44. {"st7735v", &lcd_opts_st7735v},
  45. {"st7735s", &lcd_opts_st7735s},
  46. {"st7789", &lcd_opts_st7789},
  47. {"st7796", &lcd_opts_st7796},
  48. {"gc9a01", &lcd_opts_gc9a01},
  49. {"gc9106l", &lcd_opts_gc9106l},
  50. {"gc9306x", &lcd_opts_gc9306x},
  51. {"gc9306", &lcd_opts_gc9306x}, //gc9306是gc9306x的别名
  52. {"ili9341", &lcd_opts_ili9341},
  53. {"ili9486", &lcd_opts_ili9486},
  54. {"nv3037", &lcd_opts_nv3037},
  55. {"h050iwv", &lcd_opts_h050iwv},
  56. {"jd9261t_inited", &lcd_opts_jd9261t_inited},
  57. {"nv3052c", &lcd_opts_nv3052c},
  58. {"hx8282", &lcd_opts_hx8282},
  59. {"st7701s", &lcd_opts_st7701s},
  60. {"st7701sn", &lcd_opts_st7701sn},
  61. {"", NULL} // 最后一个必须是空字符串
  62. };
  63. static luat_lcd_conf_t *default_conf = NULL;
  64. // static int dft_conf_lua_ref = 0;
  65. // 所有绘图相关的函数都应该调用本函数
  66. static void lcd_auto_flush(luat_lcd_conf_t *conf) {
  67. #ifndef LUAT_USE_LCD_SDL2
  68. if (conf == NULL || conf->buff == NULL || conf->auto_flush == 0)
  69. return;
  70. #endif
  71. luat_lcd_flush(conf);
  72. }
  73. luat_color_t lcd_str_fg_color,lcd_str_bg_color;
  74. luat_lcd_conf_t *l_lcd_get_default_conf(void) {return default_conf;}
  75. LUAT_WEAK void luat_lcd_IF_init(luat_lcd_conf_t* conf){}
  76. LUAT_WEAK int luat_lcd_init_in_service(luat_lcd_conf_t* conf){return -1;}
  77. /*
  78. lcd显示屏初始化
  79. @api lcd.init(tp, args, spi_dev, init_in_service)
  80. @string lcd类型,当前支持:<br>st7796<br>st7789<br>st7735<br>st7735v<br>st7735s<br>gc9a01<br>gc9106l<br>gc9306x<br>ili9486<br>custom
  81. @table 附加参数,与具体设备有关:<br>pin_pwr(背光)为可选项,可不设置<br>port:spi端口,例如0,1,2...如果为device方式则为"device"<br>pin_dc:lcd数据/命令选择引脚<br>pin_rst:lcd复位引脚<br>pin_pwr:lcd背光引脚 可选项,可不设置<br>direction:lcd屏幕方向 0:0° 1:180° 2:270° 3:90°<br>w:lcd 水平分辨率<br>h:lcd 竖直分辨率<br>xoffset:x偏移(不同屏幕ic 不同屏幕方向会有差异)<br>yoffset:y偏移(不同屏幕ic 不同屏幕方向会有差异)<br>direction0:0°方向命令,(不同屏幕ic会有差异)<br>direction90:90°方向命令,(不同屏幕ic会有差异)<br>direction180:180°方向命令,(不同屏幕ic会有差异)<br>direction270:270°方向命令,(不同屏幕ic会有差异) <br>sleepcmd:睡眠命令,默认0X10<br>wakecmd:唤醒命令,默认0X11 <br>interface_mode lcd模式,默认lcd.WIRE_4_BIT_8_INTERFACE_I
  82. @userdata spi设备,当port = "device"时有效
  83. @boolean 允许初始化在lcd service里运行,默认是false
  84. @usage
  85. -- 初始化spi0的st7735s 注意:lcd初始化之前需要先初始化spi
  86. spi_lcd = spi.deviceSetup(0,20,0,0,8,2000000,spi.MSB,1,1)
  87. log.info("lcd.init",
  88. lcd.init("st7735s",{port = "device",pin_dc = 17, pin_pwr = 7,pin_rst = 19,direction = 2,w = 160,h = 80,xoffset = 1,yoffset = 26},spi_lcd))
  89. */
  90. /*
  91. 此处为api新增设计
  92. rgb:
  93. lcd.init("st7796s",{port = "rgb",direction = 2,w = 160,h = 80,xoffset = 1,yoffset = 26}))
  94. 8080:
  95. lcd.init("st7796s",{port = "8080",direction = 2,w = 160,h = 80,xoffset = 1,yoffset = 26}))
  96. arm2d:
  97. lcd.init("st7796s",{port = "ARM2D",direction = 2,w = 160,h = 80,xoffset = 1,yoffset = 26}))
  98. dma2d:
  99. lcd.init("st7796s",{port = "DMA2D",direction = 2,w = 160,h = 80,xoffset = 1,yoffset = 26}))
  100. */
  101. static int l_lcd_init(lua_State* L) {
  102. int ret;
  103. size_t len = 0;
  104. uint8_t spi_device = 0;
  105. luat_lcd_conf_t *conf = luat_heap_malloc(sizeof(luat_lcd_conf_t));
  106. if (conf == NULL) {
  107. LLOGE("out of system memory!!!");
  108. return 0;
  109. }
  110. if (default_conf != NULL) {
  111. LLOGD("lcd was inited, skip");
  112. lua_pushboolean(L, 1);
  113. return 1;
  114. }
  115. #if defined LUAT_USE_LCD_SERVICE
  116. uint8_t init_in_service = 0;
  117. if (lua_isboolean(L, 4)) {
  118. init_in_service = lua_toboolean(L, 4);
  119. }
  120. #endif
  121. memset(conf, 0, sizeof(luat_lcd_conf_t)); // 填充0,保证无脏数据
  122. conf->bpp = 16;
  123. conf->lcd_cs_pin = LUAT_GPIO_NONE;
  124. conf->pin_dc = LUAT_GPIO_NONE;
  125. conf->pin_pwr = LUAT_GPIO_NONE;
  126. conf->interface_mode = LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_I;
  127. if (lua_type(L, 3) == LUA_TUSERDATA){
  128. // 如果是SPI Device模式, 就可能出现变量为local, 从而在某个时间点被GC掉的可能性
  129. conf->lcd_spi_device = (luat_spi_device_t*)lua_touserdata(L, 3);
  130. lua_pushvalue(L, 3);
  131. // 所以, 直接引用之外, 再加上强制引用, 避免被GC
  132. // 鉴于LCD不太可能重复初始化, 引用也没什么问题
  133. conf->lcd_spi_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  134. spi_device = 1;
  135. }
  136. const char* tp = luaL_checklstring(L, 1, &len);
  137. int16_t s_index = -1;//第几个屏幕,-1表示没匹配到
  138. for(int i = 0; i < 100; i++){
  139. if (strlen(lcd_regs[i].name) == 0)
  140. break;
  141. if(strcmp(lcd_regs[i].name,tp) == 0){
  142. s_index = i;
  143. break;
  144. }
  145. }
  146. if (s_index != -1) {
  147. LLOGD("ic support: %s",tp);
  148. if (lua_gettop(L) > 1) {
  149. conf->opts = (struct luat_lcd_opts *)lcd_regs[s_index].lcd_opts;
  150. lua_settop(L, 2); // 丢弃多余的参数
  151. lua_pushstring(L, "port");
  152. int port = lua_gettable(L, 2);
  153. if (LUA_TNUMBER == port) {
  154. conf->port = luaL_checkinteger(L, -1);
  155. }else if(LUA_TSTRING == port){
  156. size_t len;
  157. const char *lcd_port = luaL_checklstring(L, -1,&len);
  158. if(memcmp("device", lcd_port, len) == 0){
  159. conf->port = LUAT_LCD_SPI_DEVICE;
  160. }else if(memcmp("rgb", lcd_port, len) == 0){
  161. conf->port = LUAT_LCD_PORT_RGB;
  162. }else{
  163. LLOGE("port %s is not support ",lcd_port);
  164. goto end;
  165. }
  166. }
  167. if (spi_device == 1 && conf->port != LUAT_LCD_SPI_DEVICE) {
  168. LLOGE("port is not device but find luat_spi_device_t");
  169. goto end;
  170. }else if (spi_device == 0 && conf->port == LUAT_LCD_SPI_DEVICE){
  171. LLOGE("port is device but not find luat_spi_device_t");
  172. goto end;
  173. }
  174. lua_pop(L, 1);
  175. lua_pushstring(L, "pin_dc");
  176. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  177. conf->pin_dc = luaL_checkinteger(L, -1);
  178. }
  179. lua_pop(L, 1);
  180. lua_pushstring(L, "pin_pwr");
  181. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  182. conf->pin_pwr = luaL_checkinteger(L, -1);
  183. }
  184. lua_pop(L, 1);
  185. lua_pushstring(L, "pin_rst");
  186. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  187. conf->pin_rst = luaL_checkinteger(L, -1);
  188. }
  189. lua_pop(L, 1);
  190. lua_pushstring(L, "direction");
  191. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  192. conf->direction = luaL_checkinteger(L, -1);
  193. }
  194. lua_pop(L, 1);
  195. lua_pushstring(L, "direction0");
  196. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  197. conf->opts->direction0 = luaL_checkinteger(L, -1);
  198. }
  199. lua_pop(L, 1);
  200. lua_pushstring(L, "direction90");
  201. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  202. conf->opts->direction90 = luaL_checkinteger(L, -1);
  203. }
  204. lua_pop(L, 1);
  205. lua_pushstring(L, "direction180");
  206. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  207. conf->opts->direction180 = luaL_checkinteger(L, -1);
  208. }
  209. lua_pop(L, 1);
  210. lua_pushstring(L, "direction270");
  211. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  212. conf->opts->direction270 = luaL_checkinteger(L, -1);
  213. }
  214. lua_pop(L, 1);
  215. lua_pushstring(L, "w");
  216. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  217. conf->w = luaL_checkinteger(L, -1);
  218. }
  219. lua_pop(L, 1);
  220. lua_pushstring(L, "h");
  221. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  222. conf->h = luaL_checkinteger(L, -1);
  223. }
  224. lua_pop(L, 1);
  225. conf->buffer_size = (conf->w * conf->h) * sizeof(luat_color_t);
  226. lua_pushstring(L, "xoffset");
  227. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  228. conf->xoffset = luaL_checkinteger(L, -1);
  229. }
  230. lua_pop(L, 1);
  231. lua_pushstring(L, "yoffset");
  232. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  233. conf->yoffset = luaL_checkinteger(L, -1);
  234. }
  235. lua_pop(L, 1);
  236. lua_pushstring(L, "sleepcmd");
  237. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  238. conf->opts->sleep_cmd = luaL_checkinteger(L, -1);
  239. }
  240. lua_pop(L, 1);
  241. lua_pushstring(L, "wakecmd");
  242. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  243. conf->opts->wakeup_cmd = luaL_checkinteger(L, -1);
  244. }
  245. lua_pop(L, 1);
  246. lua_pushstring(L, "interface_mode");
  247. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  248. conf->interface_mode = luaL_checkinteger(L, -1);
  249. }
  250. lua_pushstring(L, "bus_speed");
  251. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  252. conf->bus_speed = luaL_checkinteger(L, -1);
  253. }
  254. lua_pushstring(L, "flush_rate");
  255. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  256. conf->flush_rate = luaL_checkinteger(L, -1);
  257. }
  258. lua_pushstring(L, "vfp");
  259. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  260. conf->vfp = luaL_checkinteger(L, -1);
  261. }
  262. lua_pushstring(L, "vbp");
  263. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  264. conf->vbp = luaL_checkinteger(L, -1);
  265. }
  266. lua_pushstring(L, "vs");
  267. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  268. conf->vs = luaL_checkinteger(L, -1);
  269. }
  270. lua_pop(L, 1);
  271. }
  272. if (s_index == 0){
  273. unsigned int cmd = 0;
  274. lua_pushstring(L, "initcmd");
  275. lua_gettable(L, 2);
  276. if (lua_istable(L, -1)) {
  277. conf->opts->init_cmds_len = lua_rawlen(L, -1);
  278. conf->opts->init_cmds = luat_heap_malloc( conf->opts->init_cmds_len * sizeof(uint16_t));
  279. for (size_t i = 1; i <= conf->opts->init_cmds_len; i++){
  280. lua_geti(L, -1, i);
  281. cmd = luaL_checkinteger(L, -1);
  282. conf->opts->init_cmds[i-1] = ((cmd >> 8) & 0xFF00) | (cmd & 0xFF);
  283. lua_pop(L, 1);
  284. }
  285. }else if(lua_isstring(L, -1)){
  286. size_t len;
  287. const char *fail_name = luaL_checklstring(L, -1, &len);
  288. FILE* fd = (FILE *)luat_fs_fopen(fail_name, "rb");
  289. conf->opts->init_cmds_len = 0;
  290. if (fd){
  291. #define INITCMD_BUFF_SIZE 128
  292. char init_cmd_buff[INITCMD_BUFF_SIZE] ;
  293. conf->opts->init_cmds = luat_heap_malloc(sizeof(uint16_t));
  294. while (1) {
  295. memset(init_cmd_buff, 0, INITCMD_BUFF_SIZE);
  296. int readline_len = luat_fs_readline(init_cmd_buff, INITCMD_BUFF_SIZE-1, fd);
  297. if (readline_len < 1)
  298. break;
  299. if (memcmp(init_cmd_buff, "#", 1)==0){
  300. continue;
  301. }
  302. char *token = strtok(init_cmd_buff, ",");
  303. if (sscanf(token,"%x",&cmd) < 1){
  304. continue;
  305. }
  306. conf->opts->init_cmds_len = conf->opts->init_cmds_len + 1;
  307. conf->opts->init_cmds = luat_heap_realloc(conf->opts->init_cmds,conf->opts->init_cmds_len * sizeof(uint16_t));
  308. conf->opts->init_cmds[conf->opts->init_cmds_len-1]=((cmd >> 8) & 0xFF00) | (cmd & 0xFF);;
  309. while( token != NULL ) {
  310. token = strtok(NULL, ",");
  311. if (sscanf(token,"%x",&cmd) < 1){
  312. break;
  313. }
  314. conf->opts->init_cmds_len = conf->opts->init_cmds_len + 1;
  315. conf->opts->init_cmds = luat_heap_realloc(conf->opts->init_cmds,conf->opts->init_cmds_len * sizeof(uint16_t));
  316. conf->opts->init_cmds[conf->opts->init_cmds_len-1]=((cmd >> 8) & 0xFF00) | (cmd & 0xFF);;
  317. }
  318. }
  319. conf->opts->init_cmds[conf->opts->init_cmds_len]= 0;
  320. luat_fs_fclose(fd);
  321. }else{
  322. LLOGE("init_cmd fail open error");
  323. }
  324. }
  325. lua_pop(L, 1);
  326. }
  327. // 默认自动flush,即使没有buff
  328. conf->auto_flush = 1;
  329. #ifdef LUAT_USE_LCD_SDL2
  330. extern const luat_lcd_opts_t lcd_opts_sdl2;
  331. conf->opts = &lcd_opts_sdl2;
  332. #endif
  333. if (conf->port == LUAT_LCD_HW_ID_0) luat_lcd_IF_init(conf);
  334. #if defined LUAT_USE_LCD_SERVICE
  335. if (init_in_service) {
  336. ret = luat_lcd_init_in_service(conf);
  337. } else
  338. #endif
  339. {
  340. ret = luat_lcd_init(conf);
  341. }
  342. if (ret != 0) {
  343. LLOGE("lcd init fail %d", ret);
  344. luat_heap_free(conf);
  345. lua_pushboolean(L, 0);
  346. return 0;
  347. }
  348. // 初始化OK, 配置额外的参数
  349. default_conf = conf;
  350. u8g2_SetFont(&(conf->luat_lcd_u8g2), u8g2_font_opposansm12);
  351. u8g2_SetFontMode(&(conf->luat_lcd_u8g2), 0);
  352. u8g2_SetFontDirection(&(conf->luat_lcd_u8g2), 0);
  353. lua_pushboolean(L, 1);
  354. return 1;
  355. }
  356. LLOGE("ic not support: %s",tp);
  357. end:
  358. lua_pushboolean(L, 0);
  359. luat_heap_free(conf);
  360. return 1;
  361. }
  362. /*
  363. 关闭lcd显示屏
  364. @api lcd.close()
  365. @usage
  366. -- 关闭lcd
  367. lcd.close()
  368. */
  369. static int l_lcd_close(lua_State* L) {
  370. int ret = luat_lcd_close(default_conf);
  371. lua_pushboolean(L, ret == 0 ? 1 : 0);
  372. return 1;
  373. }
  374. /*
  375. 开启lcd显示屏背光
  376. @api lcd.on()
  377. @usage
  378. -- 开启lcd显示屏背光
  379. lcd.on()
  380. */
  381. static int l_lcd_display_on(lua_State* L) {
  382. int ret = luat_lcd_display_on(default_conf);
  383. lua_pushboolean(L, ret == 0 ? 1 : 0);
  384. return 1;
  385. }
  386. /*
  387. 关闭lcd显示屏背光
  388. @api lcd.off()
  389. @usage
  390. -- 关闭lcd显示屏背光
  391. lcd.off()
  392. */
  393. static int l_lcd_display_off(lua_State* L) {
  394. int ret = luat_lcd_display_off(default_conf);
  395. lua_pushboolean(L, ret == 0 ? 1 : 0);
  396. return 1;
  397. }
  398. /*
  399. lcd睡眠
  400. @api lcd.sleep()
  401. @usage
  402. -- lcd睡眠
  403. lcd.sleep()
  404. */
  405. static int l_lcd_sleep(lua_State* L) {
  406. int ret = luat_lcd_sleep(default_conf);
  407. lua_pushboolean(L, ret == 0 ? 1 : 0);
  408. return 1;
  409. }
  410. /*
  411. lcd唤醒
  412. @api lcd.wakeup()
  413. @usage
  414. -- lcd唤醒
  415. lcd.wakeup()
  416. */
  417. static int l_lcd_wakeup(lua_State* L) {
  418. int ret = luat_lcd_wakeup(default_conf);
  419. lua_pushboolean(L, ret == 0 ? 1 : 0);
  420. return 1;
  421. }
  422. /*
  423. lcd反显
  424. @api lcd.invon()
  425. @usage
  426. -- lcd反显
  427. lcd.invon()
  428. */
  429. static int l_lcd_inv_on(lua_State* L) {
  430. int ret = luat_lcd_inv_on(default_conf);
  431. lua_pushboolean(L, ret == 0 ? 1 : 0);
  432. return 1;
  433. }
  434. /*
  435. lcd反显关闭
  436. @api lcd.invoff()
  437. @usage
  438. -- lcd反显关闭
  439. lcd.invoff()
  440. */
  441. static int l_lcd_inv_off(lua_State* L) {
  442. int ret = luat_lcd_inv_off(default_conf);
  443. lua_pushboolean(L, ret == 0 ? 1 : 0);
  444. return 1;
  445. }
  446. /*
  447. lcd命令
  448. @api lcd.cmd(cmd)
  449. @int cmd
  450. @usage
  451. -- lcd命令
  452. lcd.cmd(0x21)
  453. */
  454. static int l_lcd_write_cmd(lua_State* L) {
  455. int ret = lcd_write_cmd_data(default_conf,(uint8_t)luaL_checkinteger(L, 1), NULL, 0);
  456. lua_pushboolean(L, ret == 0 ? 1 : 0);
  457. return 1;
  458. }
  459. /*
  460. lcd数据
  461. @api lcd.data(data)
  462. @int data
  463. @usage
  464. -- lcd数据
  465. lcd.data(0x21)
  466. */
  467. static int l_lcd_write_data(lua_State* L) {
  468. int ret = lcd_write_data(default_conf,(const uint8_t)luaL_checkinteger(L, 1));
  469. lua_pushboolean(L, ret == 0 ? 1 : 0);
  470. return 1;
  471. }
  472. /*
  473. lcd颜色设置
  474. @api lcd.setColor(back,fore)
  475. @int 背景色
  476. @int 前景色
  477. @usage
  478. -- lcd颜色设置
  479. lcd.setColor(0xFFFF,0x0000)
  480. */
  481. static int l_lcd_set_color(lua_State* L) {
  482. luat_color_t back,fore;
  483. back = (luat_color_t)luaL_checkinteger(L, 1);
  484. fore = (luat_color_t)luaL_checkinteger(L, 2);
  485. int ret = luat_lcd_set_color(back, fore);
  486. lua_pushboolean(L, ret == 0 ? 1 : 0);
  487. return 1;
  488. }
  489. static int l_lcd_set_direction(lua_State* L) {
  490. int ret = luat_lcd_set_direction(default_conf, (uint8_t)luaL_checkinteger(L, 1));
  491. lua_pushboolean(L, ret == 0 ? 1 : 0);
  492. return 1;
  493. }
  494. /*
  495. lcd颜色填充
  496. @api lcd.draw(x1, y1, x2, y2,color)
  497. @int 左上边缘的X位置.
  498. @int 左上边缘的Y位置.
  499. @int 右下边缘的X位置.
  500. @int 右下边缘的Y位置.
  501. @string 字符串或zbuff对象
  502. @usage
  503. -- lcd颜色填充
  504. local buff = zbuff.create({201,1,16},0x001F)
  505. lcd.draw(20,30,220,30,buff)
  506. */
  507. static int l_lcd_draw(lua_State* L) {
  508. int16_t x1, y1, x2, y2;
  509. int ret;
  510. // luat_color_t *color = NULL;
  511. luat_zbuff_t *buff;
  512. x1 = luaL_checkinteger(L, 1);
  513. y1 = luaL_checkinteger(L, 2);
  514. x2 = luaL_checkinteger(L, 3);
  515. y2 = luaL_checkinteger(L, 4);
  516. if (lua_isinteger(L, 5)) {
  517. // color = (luat_color_t *)luaL_checkstring(L, 5);
  518. luat_color_t color = (luat_color_t)luaL_checkinteger(L, 1);
  519. ret = luat_lcd_draw(default_conf, x1, y1, x2, y2, &color);
  520. }
  521. else if (lua_isuserdata(L, 5)) {
  522. buff = luaL_checkudata(L, 5, LUAT_ZBUFF_TYPE);
  523. luat_color_t *color = (luat_color_t *)buff->addr;
  524. ret = luat_lcd_draw(default_conf, x1, y1, x2, y2, color);
  525. }
  526. else if(lua_isstring(L, 5)) {
  527. luat_color_t *color = (luat_color_t *)luaL_checkstring(L, 5);
  528. ret = luat_lcd_draw(default_conf, x1, y1, x2, y2, color);
  529. }
  530. else {
  531. return 0;
  532. }
  533. lcd_auto_flush(default_conf);
  534. // int ret = luat_lcd_draw(default_conf, x1, y1, x2, y2, color);
  535. lua_pushboolean(L, ret == 0 ? 1 : 0);
  536. return 1;
  537. }
  538. /*
  539. lcd清屏
  540. @api lcd.clear(color)
  541. @int 屏幕颜色 可选参数,默认背景色
  542. @usage
  543. -- lcd清屏
  544. lcd.clear()
  545. */
  546. static int l_lcd_clear(lua_State* L) {
  547. //size_t len = 0;
  548. luat_color_t color = BACK_COLOR;
  549. if (lua_gettop(L) > 0)
  550. color = (luat_color_t)luaL_checkinteger(L, 1);
  551. int ret = luat_lcd_clear(default_conf, color);
  552. lcd_auto_flush(default_conf);
  553. lua_pushboolean(L, ret == 0 ? 1 : 0);
  554. return 1;
  555. }
  556. /*
  557. lcd颜色填充
  558. @api lcd.fill(x1, y1, x2, y2,color)
  559. @int 左上边缘的X位置.
  560. @int 左上边缘的Y位置.
  561. @int 右下边缘的X位置,不含
  562. @int 右下边缘的Y位置,不含
  563. @int 绘画颜色 可选参数,默认背景色
  564. @usage
  565. -- lcd颜色填充
  566. lcd.fill(20,30,220,30,0x0000)
  567. */
  568. static int l_lcd_draw_fill(lua_State* L) {
  569. int16_t x1, y1, x2, y2;
  570. luat_color_t color = BACK_COLOR;
  571. x1 = luaL_checkinteger(L, 1);
  572. y1 = luaL_checkinteger(L, 2);
  573. x2 = luaL_checkinteger(L, 3);
  574. y2 = luaL_checkinteger(L, 4);
  575. if (lua_gettop(L) > 4)
  576. color = (luat_color_t)luaL_checkinteger(L, 5);
  577. int ret = luat_lcd_draw_fill(default_conf, x1, y1, x2, y2, color);
  578. lcd_auto_flush(default_conf);
  579. lua_pushboolean(L, ret == 0 ? 1 : 0);
  580. return 1;
  581. }
  582. /*
  583. 画一个点.
  584. @api lcd.drawPoint(x0,y0,color)
  585. @int 点的X位置.
  586. @int 点的Y位置.
  587. @int 绘画颜色 可选参数,默认前景色
  588. @usage
  589. lcd.drawPoint(20,30,0x001F)
  590. */
  591. static int l_lcd_draw_point(lua_State* L) {
  592. int16_t x, y;
  593. luat_color_t color = FORE_COLOR;
  594. x = luaL_checkinteger(L, 1);
  595. y = luaL_checkinteger(L, 2);
  596. if (lua_gettop(L) > 2)
  597. color = (luat_color_t)luaL_checkinteger(L, 3);
  598. int ret = luat_lcd_draw_point(default_conf, x, y, color);
  599. lcd_auto_flush(default_conf);
  600. lua_pushboolean(L, ret == 0 ? 1 : 0);
  601. return 1;
  602. }
  603. /*
  604. 在两点之间画一条线.
  605. @api lcd.drawLine(x0,y0,x1,y1,color)
  606. @int 第一个点的X位置.
  607. @int 第一个点的Y位置.
  608. @int 第二个点的X位置.
  609. @int 第二个点的Y位置.
  610. @int 绘画颜色 可选参数,默认前景色
  611. @usage
  612. lcd.drawLine(20,30,220,30,0x001F)
  613. */
  614. static int l_lcd_draw_line(lua_State* L) {
  615. int16_t x1, y1, x2, y2;
  616. luat_color_t color = FORE_COLOR;
  617. x1 = luaL_checkinteger(L, 1);
  618. y1 = luaL_checkinteger(L, 2);
  619. x2 = luaL_checkinteger(L, 3);
  620. y2 = luaL_checkinteger(L, 4);
  621. if (lua_gettop(L) > 4)
  622. color = (luat_color_t)luaL_checkinteger(L, 5);
  623. int ret = luat_lcd_draw_line(default_conf, x1, y1, x2, y2, color);
  624. lcd_auto_flush(default_conf);
  625. lua_pushboolean(L, ret == 0 ? 1 : 0);
  626. return 1;
  627. }
  628. /*
  629. 从x / y位置(左上边缘)开始绘制一个框
  630. @api lcd.drawRectangle(x0,y0,x1,y1,color)
  631. @int 左上边缘的X位置.
  632. @int 左上边缘的Y位置.
  633. @int 右下边缘的X位置.
  634. @int 右下边缘的Y位置.
  635. @int 绘画颜色 可选参数,默认前景色
  636. @usage
  637. lcd.drawRectangle(20,40,220,80,0x001F)
  638. */
  639. static int l_lcd_draw_rectangle(lua_State* L) {
  640. int16_t x1, y1, x2, y2;
  641. luat_color_t color = FORE_COLOR;
  642. x1 = luaL_checkinteger(L, 1);
  643. y1 = luaL_checkinteger(L, 2);
  644. x2 = luaL_checkinteger(L, 3);
  645. y2 = luaL_checkinteger(L, 4);
  646. if (lua_gettop(L) > 4)
  647. color = (luat_color_t)luaL_checkinteger(L, 5);
  648. int ret = luat_lcd_draw_rectangle(default_conf, x1, y1, x2, y2, color);
  649. lcd_auto_flush(default_conf);
  650. lua_pushboolean(L, ret == 0 ? 1 : 0);
  651. return 1;
  652. }
  653. /*
  654. 从x / y位置(圆心)开始绘制一个圆
  655. @api lcd.drawCircle(x0,y0,r,color)
  656. @int 圆心的X位置.
  657. @int 圆心的Y位置.
  658. @int 半径.
  659. @int 绘画颜色 可选参数,默认前景色
  660. @usage
  661. lcd.drawCircle(120,120,20,0x001F)
  662. */
  663. static int l_lcd_draw_circle(lua_State* L) {
  664. int16_t x0, y0, r;
  665. luat_color_t color = FORE_COLOR;
  666. x0 = luaL_checkinteger(L, 1);
  667. y0 = luaL_checkinteger(L, 2);
  668. r = luaL_checkinteger(L, 3);
  669. if (lua_gettop(L) > 3)
  670. color = (luat_color_t)luaL_checkinteger(L, 4);
  671. int ret = luat_lcd_draw_circle(default_conf, x0, y0, r, color);
  672. lcd_auto_flush(default_conf);
  673. lua_pushboolean(L, ret == 0 ? 1 : 0);
  674. return 1;
  675. }
  676. /*
  677. 缓冲区绘制QRCode
  678. @api lcd.drawQrcode(x, y, str, size)
  679. @int x坐标
  680. @int y坐标
  681. @string 二维码的内容
  682. @int 显示大小 (注意:二维码生成大小与要显示内容和纠错等级有关,生成版本为1-40(对应 21x21 - 177x177)的不定大小,如果和设置大小不同会自动在指定的区域中间显示二维码,如二维码未显示请查看日志提示)
  683. @return nil 无返回值
  684. */
  685. static int l_lcd_drawQrcode(lua_State *L)
  686. {
  687. size_t len;
  688. int x = luaL_checkinteger(L, 1);
  689. int y = luaL_checkinteger(L, 2);
  690. const char* text = luaL_checklstring(L, 3, &len);
  691. int size = luaL_checkinteger(L, 4);
  692. uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  693. uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  694. if (qrcode == NULL || tempBuffer == NULL) {
  695. if (qrcode)
  696. luat_heap_free(qrcode);
  697. if (tempBuffer)
  698. luat_heap_free(tempBuffer);
  699. LLOGE("qrcode out of memory");
  700. return 0;
  701. }
  702. bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_LOW,
  703. qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
  704. if (ok){
  705. int qr_size = qrcodegen_getSize(qrcode);
  706. if (size < qr_size){
  707. LLOGE("size must be greater than qr_size %d",qr_size);
  708. goto end;
  709. }
  710. int scale = size / qr_size ;
  711. if (!scale)scale = 1;
  712. int margin = (size - qr_size * scale) / 2;
  713. luat_lcd_draw_fill(default_conf,x,y,x+size,y+size,BACK_COLOR);
  714. x+=margin;
  715. y+=margin;
  716. for (int j = 0; j < qr_size; j++) {
  717. for (int i = 0; i < qr_size; i++) {
  718. if (qrcodegen_getModule(qrcode, i, j))
  719. luat_lcd_draw_fill(default_conf,x+i*scale,y+j*scale,x+(i+1)*scale,y+(j+1)*scale,FORE_COLOR);
  720. }
  721. }
  722. }else{
  723. LLOGE("qrcodegen_encodeText false");
  724. }
  725. end:
  726. if (qrcode)
  727. luat_heap_free(qrcode);
  728. if (tempBuffer)
  729. luat_heap_free(tempBuffer);
  730. lcd_auto_flush(default_conf);
  731. return 0;
  732. }
  733. static uint8_t utf8_state;
  734. static uint16_t encoding;
  735. static uint16_t utf8_next(uint8_t b)
  736. {
  737. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  738. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  739. if ( utf8_state == 0 )
  740. {
  741. if ( b >= 0xfc ) /* 6 byte sequence */
  742. {
  743. utf8_state = 5;
  744. b &= 1;
  745. }
  746. else if ( b >= 0xf8 )
  747. {
  748. utf8_state = 4;
  749. b &= 3;
  750. }
  751. else if ( b >= 0xf0 )
  752. {
  753. utf8_state = 3;
  754. b &= 7;
  755. }
  756. else if ( b >= 0xe0 )
  757. {
  758. utf8_state = 2;
  759. b &= 15;
  760. }
  761. else if ( b >= 0xc0 )
  762. {
  763. utf8_state = 1;
  764. b &= 0x01f;
  765. }
  766. else
  767. {
  768. /* do nothing, just use the value as encoding */
  769. return b;
  770. }
  771. encoding = b;
  772. return 0x0fffe;
  773. }
  774. else
  775. {
  776. utf8_state--;
  777. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  778. encoding<<=6;
  779. b &= 0x03f;
  780. encoding |= b;
  781. if ( utf8_state != 0 )
  782. return 0x0fffe; /* nothing to do yet */
  783. }
  784. return encoding;
  785. }
  786. static void u8g2_draw_hv_line(u8g2_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color){
  787. switch(dir)
  788. {
  789. case 0:
  790. luat_lcd_draw_hline(default_conf,x,y,len,color);
  791. break;
  792. case 1:
  793. luat_lcd_draw_vline(default_conf,x,y,len,color);
  794. break;
  795. case 2:
  796. luat_lcd_draw_hline(default_conf,x-len+1,y,len,color);
  797. break;
  798. case 3:
  799. luat_lcd_draw_vline(default_conf,x,y-len+1,len,color);
  800. break;
  801. }
  802. }
  803. static void u8g2_font_decode_len(u8g2_t *u8g2, uint8_t len, uint8_t is_foreground){
  804. uint8_t cnt; /* total number of remaining pixels, which have to be drawn */
  805. uint8_t rem; /* remaining pixel to the right edge of the glyph */
  806. uint8_t current; /* number of pixels, which need to be drawn for the draw procedure */
  807. /* current is either equal to cnt or equal to rem */
  808. /* local coordinates of the glyph */
  809. uint8_t lx,ly;
  810. /* target position on the screen */
  811. int16_t x, y;
  812. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  813. cnt = len;
  814. /* get the local position */
  815. lx = decode->x;
  816. ly = decode->y;
  817. for(;;){
  818. /* calculate the number of pixel to the right edge of the glyph */
  819. rem = decode->glyph_width;
  820. rem -= lx;
  821. /* calculate how many pixel to draw. This is either to the right edge */
  822. /* or lesser, if not enough pixel are left */
  823. current = rem;
  824. if ( cnt < rem )
  825. current = cnt;
  826. /* now draw the line, but apply the rotation around the glyph target position */
  827. //u8g2_font_decode_draw_pixel(u8g2, lx,ly,current, is_foreground);
  828. // printf("lx:%d,ly:%d,current:%d, is_foreground:%d \r\n",lx,ly,current, is_foreground);
  829. /* get target position */
  830. x = decode->target_x;
  831. y = decode->target_y;
  832. /* apply rotation */
  833. x = u8g2_add_vector_x(x, lx, ly, decode->dir);
  834. y = u8g2_add_vector_y(y, lx, ly, decode->dir);
  835. /* draw foreground and background (if required) */
  836. if ( current > 0 ) /* avoid drawing zero length lines, issue #4 */
  837. {
  838. if ( is_foreground )
  839. {
  840. u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, lcd_str_fg_color);
  841. }
  842. // else if ( decode->is_transparent == 0 )
  843. // {
  844. // u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, lcd_str_bg_color);
  845. // }
  846. }
  847. /* check, whether the end of the run length code has been reached */
  848. if ( cnt < rem )
  849. break;
  850. cnt -= rem;
  851. lx = 0;
  852. ly++;
  853. }
  854. lx += cnt;
  855. decode->x = lx;
  856. decode->y = ly;
  857. }
  858. static void u8g2_font_setup_decode(u8g2_t *u8g2, const uint8_t *glyph_data)
  859. {
  860. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  861. decode->decode_ptr = glyph_data;
  862. decode->decode_bit_pos = 0;
  863. /* 8 Nov 2015, this is already done in the glyph data search procedure */
  864. /*
  865. decode->decode_ptr += 1;
  866. decode->decode_ptr += 1;
  867. */
  868. decode->glyph_width = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_char_width);
  869. decode->glyph_height = u8g2_font_decode_get_unsigned_bits(decode,u8g2->font_info.bits_per_char_height);
  870. }
  871. static int8_t u8g2_font_decode_glyph(u8g2_t *u8g2, const uint8_t *glyph_data){
  872. uint8_t a, b;
  873. int8_t x, y;
  874. int8_t d;
  875. int8_t h;
  876. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  877. u8g2_font_setup_decode(u8g2, glyph_data);
  878. h = u8g2->font_decode.glyph_height;
  879. x = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_x);
  880. y = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_y);
  881. d = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_delta_x);
  882. if ( decode->glyph_width > 0 )
  883. {
  884. decode->target_x = u8g2_add_vector_x(decode->target_x, x, -(h+y), decode->dir);
  885. decode->target_y = u8g2_add_vector_y(decode->target_y, x, -(h+y), decode->dir);
  886. //u8g2_add_vector(&(decode->target_x), &(decode->target_y), x, -(h+y), decode->dir);
  887. /* reset local x/y position */
  888. decode->x = 0;
  889. decode->y = 0;
  890. /* decode glyph */
  891. for(;;){
  892. a = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_0);
  893. b = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_1);
  894. do{
  895. u8g2_font_decode_len(u8g2, a, 0);
  896. u8g2_font_decode_len(u8g2, b, 1);
  897. } while( u8g2_font_decode_get_unsigned_bits(decode, 1) != 0 );
  898. if ( decode->y >= h )
  899. break;
  900. }
  901. }
  902. return d;
  903. }
  904. const uint8_t *u8g2_font_get_glyph_data(u8g2_t *u8g2, uint16_t encoding);
  905. static int16_t u8g2_font_draw_glyph(u8g2_t *u8g2, int16_t x, int16_t y, uint16_t encoding){
  906. int16_t dx = 0;
  907. u8g2->font_decode.target_x = x;
  908. u8g2->font_decode.target_y = y;
  909. const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, encoding);
  910. if ( glyph_data != NULL ){
  911. dx = u8g2_font_decode_glyph(u8g2, glyph_data);
  912. }
  913. return dx;
  914. }
  915. extern void luat_u8g2_set_ascii_indentation(uint8_t value);
  916. /*
  917. 设置字体
  918. @api lcd.setFont(font, indentation)
  919. @int font lcd.font_XXX 请查阅常量表
  920. @int indentation, 等宽字体ascii右侧缩进0~127个pixel,等宽字体的ascii字符可能在右侧有大片空白,用户可以选择删除部分。留空或者超过127则直接删除右半边, 非等宽字体无效
  921. @usage
  922. -- 设置为字体,对之后的drawStr有效,调用lcd.drawStr前一定要先设置
  923. -- 若提示 "only font pointer is allow" , 则代表当前固件不含对应字体, 可使用云编译服务免费定制
  924. -- 云编译文档: https://wiki.luatos.com/develop/compile/Cloud_compilation.html
  925. -- lcd库的默认字体均以 lcd.font_ 开头
  926. lcd.setFont(lcd.font_opposansm12)
  927. lcd.drawStr(40,10,"drawStr")
  928. sys.wait(2000)
  929. lcd.setFont(lcd.font_opposansm12_chinese) -- 具体取值可参考api文档的常量表
  930. lcd.drawStr(40,40,"drawStr测试")
  931. */
  932. static int l_lcd_set_font(lua_State *L) {
  933. if (!lua_islightuserdata(L, 1)) {
  934. LLOGE("only font pointer is allow");
  935. return 0;
  936. }
  937. const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
  938. if (ptr == NULL) {
  939. LLOGE("only font pointer is allow");
  940. return 0;
  941. }
  942. luat_u8g2_set_ascii_indentation(0xff);
  943. u8g2_SetFont(&(default_conf->luat_lcd_u8g2), ptr);
  944. if (lua_isinteger(L, 2)) {
  945. int indentation = luaL_checkinteger(L, 2);
  946. luat_u8g2_set_ascii_indentation(indentation);
  947. }
  948. lua_pushboolean(L, 1);
  949. return 1;
  950. }
  951. /*
  952. 显示字符串
  953. @api lcd.drawStr(x,y,str,fg_color)
  954. @int x 横坐标
  955. @int y 竖坐标 注意:此(x,y)为左下起始坐标
  956. @string str 文件内容
  957. @int fg_color str颜色 注意:此参数可选,如不填写则使用之前设置的颜色,绘制只会绘制字体部分,背景需要自己清除
  958. @usage
  959. -- 显示之前先设置为中文字体,对之后的drawStr有效
  960. lcd.setFont(lcd.font_opposansm12)
  961. lcd.drawStr(40,10,"drawStr")
  962. sys.wait(2000)
  963. lcd.setFont(lcd.font_opposansm16_chinese)
  964. lcd.drawStr(40,40,"drawStr测试")
  965. */
  966. static int l_lcd_draw_str(lua_State* L) {
  967. int x, y;
  968. size_t sz;
  969. const uint8_t* data;
  970. x = luaL_checkinteger(L, 1);
  971. y = luaL_checkinteger(L, 2);
  972. data = (const uint8_t*)luaL_checklstring(L, 3, &sz);
  973. lcd_str_fg_color = (luat_color_t)luaL_optinteger(L, 4,FORE_COLOR);
  974. // lcd_str_bg_color = (uint32_t)luaL_optinteger(L, 5,BACK_COLOR);
  975. if (sz == 0)
  976. return 0;
  977. uint16_t e;
  978. int16_t delta;
  979. utf8_state = 0;
  980. for(;;){
  981. e = utf8_next((uint8_t)*data);
  982. if ( e == 0x0ffff )
  983. break;
  984. data++;
  985. if ( e != 0x0fffe ){
  986. delta = u8g2_font_draw_glyph(&(default_conf->luat_lcd_u8g2), x, y, e);
  987. if (e < 0x0080) delta = luat_u8g2_need_ascii_cut(delta);
  988. switch(default_conf->luat_lcd_u8g2.font_decode.dir){
  989. case 0:
  990. x += delta;
  991. break;
  992. case 1:
  993. y += delta;
  994. break;
  995. case 2:
  996. x -= delta;
  997. break;
  998. case 3:
  999. y -= delta;
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. lcd_auto_flush(default_conf);
  1005. return 0;
  1006. }
  1007. #ifdef LUAT_USE_GTFONT
  1008. #include "GT5SLCD2E_1A.h"
  1009. 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);
  1010. 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);
  1011. /*
  1012. 使用gtfont显示gb2312字符串
  1013. @api lcd.drawGtfontGb2312(str,size,x,y)
  1014. @string str 显示字符串
  1015. @int size 字体大小 (支持16-192号大小字体)
  1016. @int x 横坐标
  1017. @int y 竖坐标
  1018. @usage
  1019. -- 注意, gtfont是额外的字体芯片硬件, 需要外挂在SPI总线才能调用本函数的
  1020. lcd.drawGtfontGb2312("啊啊啊",32,0,0)
  1021. */
  1022. /*
  1023. 使用gtfont显示gbk字符串
  1024. @api lcd.drawGtfontGbk(str,size,x,y)
  1025. @string str 显示字符串
  1026. @int size 字体大小 (支持16-192号大小字体)
  1027. @int x 横坐标
  1028. @int y 竖坐标
  1029. @usage
  1030. -- 注意, gtfont是额外的字体芯片硬件, 需要外挂在SPI总线才能调用本函数的
  1031. lcd.drawGtfontGbk("啊啊啊",32,0,0)
  1032. */
  1033. static int l_lcd_draw_gtfont_gbk(lua_State *L) {
  1034. unsigned char buf[128];
  1035. size_t len;
  1036. int i = 0;
  1037. uint8_t strhigh,strlow ;
  1038. uint16_t str;
  1039. const char *fontCode = luaL_checklstring(L, 1,&len);
  1040. unsigned char size = luaL_checkinteger(L, 2);
  1041. int x = luaL_checkinteger(L, 3);
  1042. int y = luaL_checkinteger(L, 4);
  1043. lcd_str_fg_color = (luat_color_t)luaL_optinteger(L, 5,FORE_COLOR);
  1044. // lcd_str_bg_color = (luat_color_t)luaL_optinteger(L, 6,BACK_COLOR);
  1045. while ( i < len){
  1046. strhigh = *fontCode;
  1047. fontCode++;
  1048. strlow = *fontCode;
  1049. str = (strhigh<<8)|strlow;
  1050. fontCode++;
  1051. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  1052. if(font_size == 0){
  1053. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  1054. return 0;
  1055. }
  1056. gtfont_draw_w(buf , x ,y , font_size,size , size,luat_lcd_draw_point,default_conf,0);
  1057. x+=size;
  1058. i+=2;
  1059. }
  1060. lcd_auto_flush(default_conf);
  1061. return 0;
  1062. }
  1063. /*
  1064. 使用gtfont灰度显示gb2312字符串
  1065. @api lcd.drawGtfontGb2312Gray(str,size,gray,x,y)
  1066. @string str 显示字符串
  1067. @int size 字体大小 (支持16-192号大小字体)
  1068. @int gray 灰度[1阶/2阶/3阶/4阶]
  1069. @int x 横坐标
  1070. @int y 竖坐标
  1071. @usage
  1072. -- 注意, gtfont是额外的字体芯片硬件, 需要外挂在SPI总线才能调用本函数的
  1073. lcd.drawGtfontGb2312Gray("啊啊啊",32,4,0,40)
  1074. */
  1075. /*
  1076. 使用gtfont灰度显示gbk字符串
  1077. @api lcd.drawGtfontGbkGray(str,size,gray,x,y)
  1078. @string str 显示字符串
  1079. @int size 字体大小 (支持16-192号大小字体)
  1080. @int gray 灰度[1阶/2阶/3阶/4阶]
  1081. @int x 横坐标
  1082. @int y 竖坐标
  1083. @usage
  1084. -- 注意, gtfont是额外的字体芯片硬件, 需要外挂在SPI总线才能调用本函数的
  1085. lcd.drawGtfontGbkGray("啊啊啊",32,4,0,40)
  1086. */
  1087. static int l_lcd_draw_gtfont_gbk_gray(lua_State* L) {
  1088. unsigned char buf[2048];
  1089. size_t len;
  1090. int i = 0;
  1091. uint8_t strhigh,strlow ;
  1092. uint16_t str;
  1093. const char *fontCode = luaL_checklstring(L, 1,&len);
  1094. unsigned char size = luaL_checkinteger(L, 2);
  1095. unsigned char font_g = luaL_checkinteger(L, 3);
  1096. int x = luaL_checkinteger(L, 4);
  1097. int y = luaL_checkinteger(L, 5);
  1098. lcd_str_fg_color = (luat_color_t)luaL_optinteger(L, 6,FORE_COLOR);
  1099. // lcd_str_bg_color = (luat_color_t)luaL_optinteger(L, 7,BACK_COLOR);
  1100. while ( i < len){
  1101. strhigh = *fontCode;
  1102. fontCode++;
  1103. strlow = *fontCode;
  1104. str = (strhigh<<8)|strlow;
  1105. fontCode++;
  1106. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size*font_g, size*font_g, size*font_g);
  1107. if(font_size != size*font_g){
  1108. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  1109. return 0;
  1110. }
  1111. Gray_Process(buf,size,size,font_g);
  1112. gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,luat_lcd_draw_point,default_conf,0);
  1113. x+=size;
  1114. i+=2;
  1115. }
  1116. lcd_auto_flush(default_conf);
  1117. return 0;
  1118. }
  1119. #ifdef LUAT_USE_GTFONT_UTF8
  1120. extern unsigned short unicodetogb2312 ( unsigned short chr);
  1121. /*
  1122. 使用gtfont显示UTF8字符串
  1123. @api lcd.drawGtfontUtf8(str,size,x,y)
  1124. @string str 显示字符串
  1125. @int size 字体大小 (支持16-192号大小字体)
  1126. @int x 横坐标
  1127. @int y 竖坐标
  1128. @usage
  1129. lcd.drawGtfontUtf8("啊啊啊",32,0,0)
  1130. */
  1131. static int l_lcd_draw_gtfont_utf8(lua_State *L) {
  1132. unsigned char buf[128] = {0};
  1133. size_t len;
  1134. int i = 0;
  1135. uint8_t strhigh,strlow ;
  1136. uint16_t e,str;
  1137. const char *fontCode = luaL_checklstring(L, 1,&len);
  1138. unsigned char size = luaL_checkinteger(L, 2);
  1139. int x = luaL_checkinteger(L, 3);
  1140. int y = luaL_checkinteger(L, 4);
  1141. lcd_str_fg_color = (luat_color_t)luaL_optinteger(L, 5,FORE_COLOR);
  1142. // lcd_str_bg_color = (luat_color_t)luaL_optinteger(L, 6,BACK_COLOR);
  1143. for(;;){
  1144. e = utf8_next((uint8_t)*fontCode);
  1145. if ( e == 0x0ffff )
  1146. break;
  1147. fontCode++;
  1148. if ( e != 0x0fffe ){
  1149. uint16_t str = unicodetogb2312(e);
  1150. memset(buf,0,128);
  1151. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  1152. if(font_size == 0){
  1153. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  1154. return 0;
  1155. }
  1156. unsigned int dw = gtfont_draw_w(buf , x ,y , font_size,size , size,luat_lcd_draw_point,default_conf,0);
  1157. x+=str<0x80?dw:size;
  1158. }
  1159. }
  1160. lcd_auto_flush(default_conf);
  1161. return 0;
  1162. }
  1163. /*
  1164. 使用gtfont灰度显示UTF8字符串
  1165. @api lcd.drawGtfontUtf8Gray(str,size,gray,x,y)
  1166. @string str 显示字符串
  1167. @int size 字体大小 (支持16-192号大小字体)
  1168. @int gray 灰度[1阶/2阶/3阶/4阶]
  1169. @int x 横坐标
  1170. @int y 竖坐标
  1171. @usage
  1172. lcd.drawGtfontUtf8Gray("啊啊啊",32,4,0,40)
  1173. */
  1174. static int l_lcd_draw_gtfont_utf8_gray(lua_State* L) {
  1175. unsigned char buf[2048] = {0};
  1176. size_t len;
  1177. int i = 0;
  1178. uint8_t strhigh,strlow ;
  1179. uint16_t e,str;
  1180. const char *fontCode = luaL_checklstring(L, 1,&len);
  1181. unsigned char size = luaL_checkinteger(L, 2);
  1182. unsigned char font_g = luaL_checkinteger(L, 3);
  1183. int x = luaL_checkinteger(L, 4);
  1184. int y = luaL_checkinteger(L, 5);
  1185. lcd_str_fg_color = (luat_color_t)luaL_optinteger(L, 6,FORE_COLOR);
  1186. // lcd_str_bg_color = (luat_color_t)luaL_optinteger(L, 7,BACK_COLOR);
  1187. for(;;){
  1188. e = utf8_next((uint8_t)*fontCode);
  1189. if ( e == 0x0ffff )
  1190. break;
  1191. fontCode++;
  1192. if ( e != 0x0fffe ){
  1193. uint16_t str = unicodetogb2312(e);
  1194. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size*font_g, size*font_g, size*font_g);
  1195. if(font_size != size*font_g){
  1196. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  1197. return 0;
  1198. }
  1199. Gray_Process(buf,size,size,font_g);
  1200. gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,luat_lcd_draw_point,default_conf,0);
  1201. x+=size;
  1202. }
  1203. }
  1204. lcd_auto_flush(default_conf);
  1205. return 0;
  1206. }
  1207. #endif // LUAT_USE_GTFONT_UTF8
  1208. #endif // LUAT_USE_GTFONT
  1209. static int l_lcd_set_default(lua_State *L) {
  1210. if (lua_gettop(L) == 1) {
  1211. default_conf = lua_touserdata(L, 1);
  1212. lua_pushboolean(L, 1);
  1213. return 1;
  1214. }
  1215. return 1;
  1216. }
  1217. static int l_lcd_get_default(lua_State *L) {
  1218. if (default_conf == NULL)
  1219. return 0;
  1220. lua_pushlightuserdata(L, default_conf);
  1221. return 1;
  1222. }
  1223. /*
  1224. 获取屏幕尺寸
  1225. @api lcd.getSize()
  1226. @return int 宽, 如果未初始化会返回0
  1227. @return int 高, 如果未初始化会返回0
  1228. @usage
  1229. log.info("lcd", "size", lcd.getSize())
  1230. */
  1231. static int l_lcd_get_size(lua_State *L) {
  1232. if (lua_gettop(L) == 1) {
  1233. luat_lcd_conf_t * conf = lua_touserdata(L, 1);
  1234. if (conf) {
  1235. lua_pushinteger(L, conf->w);
  1236. lua_pushinteger(L, conf->h);
  1237. return 2;
  1238. }
  1239. }
  1240. if (default_conf == NULL) {
  1241. lua_pushinteger(L, 0);
  1242. lua_pushinteger(L, 0);
  1243. }
  1244. else {
  1245. lua_pushinteger(L, default_conf->w);
  1246. lua_pushinteger(L, default_conf->h);
  1247. }
  1248. return 2;
  1249. }
  1250. /*
  1251. 绘制位图
  1252. @api lcd.drawXbm(x, y, w, h, data)
  1253. @int X坐标
  1254. @int y坐标
  1255. @int 位图宽
  1256. @int 位图高
  1257. @int 位图数据,每一位代表一个像素
  1258. @usage
  1259. -- 取模使用PCtoLCD2002软件即可 阴码 逐行 逆向
  1260. -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
  1261. lcd.drawXbm(0, 0, 16,16, string.char(
  1262. 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
  1263. 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
  1264. ))
  1265. */
  1266. static int l_lcd_drawxbm(lua_State *L){
  1267. int x = luaL_checkinteger(L, 1);
  1268. int y = luaL_checkinteger(L, 2);
  1269. size_t w = luaL_checkinteger(L, 3);
  1270. size_t h = luaL_checkinteger(L, 4);
  1271. size_t len = 0;
  1272. const char* data = luaL_checklstring(L, 5, &len);
  1273. uint8_t mask = 1;
  1274. if (h < 1) return 0; // 行数必须大于0
  1275. if (len*8/h < w) return 0; // 起码要填满一行
  1276. int w1 = w/8;
  1277. if (w%8)w1++;
  1278. if (len != h*w1)return 0;
  1279. luat_color_t* color_w = luat_heap_malloc(sizeof(luat_color_t) * w);
  1280. for (size_t b = 0; b < h; b++){
  1281. size_t a = 0;
  1282. while (a < w){
  1283. for (size_t c = 0; c < 8; c++){
  1284. if (*data&(mask<<c)){
  1285. color_w[a]=FORE_COLOR;
  1286. }else{
  1287. color_w[a]=BACK_COLOR;
  1288. }
  1289. a++;
  1290. if (a == w)break;
  1291. }
  1292. data++;
  1293. }
  1294. luat_lcd_draw(default_conf, x, y+b, x+w-1, y+b, color_w);
  1295. }
  1296. luat_heap_free(color_w);
  1297. lcd_auto_flush(default_conf);
  1298. lua_pushboolean(L, 1);
  1299. return 1;
  1300. }
  1301. #ifdef LUAT_USE_TJPGD
  1302. #include "tjpgd.h"
  1303. #include "tjpgdcnf.h"
  1304. #define N_BPP (3 - JD_FORMAT)
  1305. /* Session identifier for input/output functions (name, members and usage are as user defined) */
  1306. typedef struct {
  1307. FILE *fp; /* Input stream */
  1308. int x;
  1309. int y;
  1310. // int width;
  1311. // int height;
  1312. uint16_t buff[16*16];
  1313. } IODEV;
  1314. static unsigned int file_in_func (JDEC* jd, uint8_t* buff, unsigned int nbyte){
  1315. IODEV *dev = (IODEV*)jd->device; /* Device identifier for the session (5th argument of jd_prepare function) */
  1316. if (buff) {
  1317. /* Read bytes from input stream */
  1318. return luat_fs_fread(buff, 1, nbyte, dev->fp);
  1319. } else {
  1320. /* Remove bytes from input stream */
  1321. return luat_fs_fseek(dev->fp, nbyte, SEEK_CUR) ? 0 : nbyte;
  1322. }
  1323. }
  1324. static int lcd_out_func (JDEC* jd, void* bitmap, JRECT* rect){
  1325. IODEV *dev = (IODEV*)jd->device;
  1326. uint16_t* tmp = (uint16_t*)bitmap;
  1327. // rgb高低位swap
  1328. uint16_t count = (rect->right - rect->left + 1) * (rect->bottom - rect->top + 1);
  1329. for (size_t i = 0; i < count; i++){
  1330. if (default_conf->port < LUAT_LCD_HW_ID_0 || default_conf->port == LUAT_LCD_SPI_DEVICE)
  1331. dev->buff[i] = ((tmp[i] >> 8) & 0xFF)+ ((tmp[i] << 8) & 0xFF00);
  1332. else
  1333. dev->buff[i] = tmp[i];
  1334. }
  1335. // LLOGD("jpeg seg %dx%d %dx%d", rect->left, rect->top, rect->right, rect->bottom);
  1336. // LLOGD("jpeg seg size %d %d %d", rect->right - rect->left + 1, rect->bottom - rect->top + 1, (rect->right - rect->left + 1) * (rect->bottom - rect->top + 1));
  1337. luat_lcd_draw(default_conf, dev->x + rect->left, dev->y + rect->top,
  1338. dev->x + rect->right, dev->y + rect->bottom,
  1339. dev->buff);
  1340. return 1; /* Continue to decompress */
  1341. }
  1342. static int lcd_draw_jpeg(const char* path, int xpos, int ypos) {
  1343. JRESULT res; /* Result code of TJpgDec API */
  1344. JDEC jdec; /* Decompression object */
  1345. void *work; /* Pointer to the decompressor work area */
  1346. #if JD_FASTDECODE == 2
  1347. size_t sz_work = 3500 * 3; /* Size of work area */
  1348. #else
  1349. size_t sz_work = 3500; /* Size of work area */
  1350. #endif
  1351. IODEV devid; /* User defined device identifier */
  1352. FILE* fd = luat_fs_fopen(path, "r");
  1353. if (fd == NULL) {
  1354. LLOGW("no such file %s", path);
  1355. return -1;
  1356. }
  1357. devid.fp = fd;
  1358. work = luat_heap_malloc(sz_work);
  1359. if (work == NULL) {
  1360. LLOGE("out of memory when malloc jpeg decode workbuff");
  1361. return -3;
  1362. }
  1363. res = jd_prepare(&jdec, file_in_func, work, sz_work, &devid);
  1364. if (res != JDR_OK) {
  1365. luat_heap_free(work);
  1366. luat_fs_fclose(fd);
  1367. LLOGW("jd_prepare file %s error %d", path, res);
  1368. return -2;
  1369. }
  1370. devid.x = xpos;
  1371. devid.y = ypos;
  1372. // devid.width = jdec.width;
  1373. // devid.height = jdec.height;
  1374. res = jd_decomp(&jdec, lcd_out_func, 0);
  1375. luat_heap_free(work);
  1376. luat_fs_fclose(fd);
  1377. if (res != JDR_OK) {
  1378. LLOGW("jd_decomp file %s error %d", path, res);
  1379. return -2;
  1380. }else {
  1381. lcd_auto_flush(default_conf);
  1382. return 0;
  1383. }
  1384. }
  1385. /*
  1386. 显示图片,当前只支持jpg,jpeg
  1387. @api lcd.showImage(x, y, file)
  1388. @int X坐标
  1389. @int y坐标
  1390. @string 文件路径
  1391. @usage
  1392. lcd.showImage(0,0,"/luadb/logo.jpg")
  1393. */
  1394. static int l_lcd_showimage(lua_State *L){
  1395. size_t size = 0;
  1396. int ret = 0;
  1397. int x = luaL_checkinteger(L, 1);
  1398. int y = luaL_checkinteger(L, 2);
  1399. const char* input_file = luaL_checklstring(L, 3, &size);
  1400. if (memcmp(input_file+size-4, ".jpg", 5) == 0 || memcmp(input_file+size-4, ".JPG", 5) == 0 || memcmp(input_file+size-5, ".jpeg", 6) == 0 || memcmp(input_file+size-5, ".JPEG", 6) == 0){
  1401. ret = lcd_draw_jpeg(input_file, x, y);
  1402. lua_pushboolean(L, ret == 0 ? 1 : 0);
  1403. } else{
  1404. LLOGE("input_file not support");
  1405. lua_pushboolean(L, 0);
  1406. }
  1407. return 1;
  1408. }
  1409. #endif
  1410. /*
  1411. 主动刷新数据到界面, 仅设置buff且禁用自动属性后使用
  1412. @api lcd.flush()
  1413. @return bool 成功返回true, 否则返回nil/false
  1414. @usage
  1415. -- 本API与 lcd.setupBuff lcd.autoFlush 配合使用
  1416. lcd.flush()
  1417. */
  1418. static int l_lcd_flush(lua_State* L) {
  1419. luat_lcd_conf_t * conf = NULL;
  1420. if (lua_gettop(L) == 1) {
  1421. conf = lua_touserdata(L, 1);
  1422. }else {
  1423. conf = default_conf;
  1424. }
  1425. if (conf == NULL) {
  1426. //LLOGW("lcd not init");
  1427. return 0;
  1428. }
  1429. if (conf->buff == NULL) {
  1430. //LLOGW("lcd without buff, not support flush");
  1431. return 0;
  1432. }
  1433. if (conf->auto_flush) {
  1434. //LLOGI("lcd auto flush is enable, no need for flush");
  1435. return 0;
  1436. }
  1437. luat_lcd_flush(conf);
  1438. lua_pushboolean(L, 1);
  1439. return 0;
  1440. }
  1441. /*
  1442. 设置显示缓冲区, 所需内存大小为 2×宽×高 字节. 请衡量内存需求与业务所需的刷新频次.
  1443. @api lcd.setupBuff(conf, onheap)
  1444. @userdata conf指针, 不需要传
  1445. @bool true使用heap内存, false使用vm内存, 默认使用vm内存, 不需要主动传
  1446. @return bool 是否成功
  1447. @usage
  1448. -- 初始化lcd的buff缓冲区, 可理解为FrameBuffer区域.
  1449. lcd.setupBuff()
  1450. */
  1451. static int l_lcd_setup_buff(lua_State* L) {
  1452. luat_lcd_conf_t * conf = NULL;
  1453. if (lua_gettop(L) == 1) {
  1454. conf = lua_touserdata(L, 1);
  1455. }
  1456. else {
  1457. conf = default_conf;
  1458. }
  1459. if (conf == NULL) {
  1460. LLOGW("lcd not init");
  1461. return 0;
  1462. }
  1463. if (conf->buff != NULL) {
  1464. LLOGW("lcd buff is ok");
  1465. return 0;
  1466. }
  1467. if (lua_isboolean(L, 2) && lua_toboolean(L, 2)) {
  1468. conf->buff = luat_heap_malloc(sizeof(luat_color_t) * conf->w * conf->h);
  1469. }
  1470. else {
  1471. conf->buff = lua_newuserdata(L, sizeof(luat_color_t) * conf->w * conf->h);
  1472. if (conf->buff) {
  1473. conf->buff_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1474. }
  1475. }
  1476. if (conf->buff == NULL) {
  1477. LLOGE("lcd buff malloc fail, out of memory?");
  1478. return 0;
  1479. }
  1480. // 先设置为不需要的区间
  1481. conf->flush_y_min = conf->h;
  1482. conf->flush_y_max = 0;
  1483. // luat_lcd_clear 会将区域扩展到整个屏幕
  1484. luat_lcd_clear(default_conf, BACK_COLOR);
  1485. lua_pushboolean(L, 1);
  1486. return 1;
  1487. }
  1488. /*
  1489. 设置自动刷新, 需配合lcd.setupBuff使用
  1490. @api lcd.autoFlush(enable)
  1491. @bool 是否自动刷新,默认为true
  1492. @usage
  1493. -- 设置buff 并禁用自动更新
  1494. lcd.setupBuff()
  1495. lcd.autoFlush(false)
  1496. -- 禁止自动更新后, 需要使用 lcd.flush() 主动刷新数据到屏幕
  1497. */
  1498. static int l_lcd_auto_flush(lua_State* L) {
  1499. luat_lcd_conf_t * conf = default_conf;
  1500. if (conf == NULL) {
  1501. LLOGW("lcd not init");
  1502. return 0;
  1503. }
  1504. conf->auto_flush = lua_toboolean(L, 1);
  1505. lua_pushboolean(L, conf->auto_flush);
  1506. return 1;
  1507. }
  1508. /*
  1509. RGB565颜色生成
  1510. @api lcd.rgb565(r, g, b, swap)
  1511. @int 红色, 0x00 ~ 0xFF
  1512. @int 绿色, 0x00 ~ 0xFF
  1513. @int 蓝色, 0x00 ~ 0xFF
  1514. @bool 是否翻转, true 翻转, false 不翻转. 默认翻转
  1515. @return int 颜色值
  1516. @usage
  1517. -- 本API支持多种模式, 参数数量分别是 1, 2, 3, 4
  1518. -- 1. 单参数形式, 24bit RGB值, swap = true, 推荐
  1519. local red = lcd.rgb565(0xFF0000)
  1520. local green = lcd.rgb565(0x00FF00)
  1521. local blue = lcd.rgb565(0x0000FF)
  1522. -- 2. 两参数形式, 24bit RGB值, 增加swap的设置
  1523. local red = lcd.rgb565(0xFF0000, true)
  1524. local green = lcd.rgb565(0x00FF00, true)
  1525. local blue = lcd.rgb565(0x0000FF, true)
  1526. -- 3. 三参数形式, 红/绿/蓝, 各8bit
  1527. local red = lcd.rgb565(0xFF, 0x00, 0x00)
  1528. local green = lcd.rgb565(0x00, 0xFF, 0x00)
  1529. local blue = lcd.rgb565(0x00, 0x00, 0xFF)
  1530. -- 4. 四参数形式, 红/绿/蓝, 各8bit, 增加swap的设置
  1531. local red = lcd.rgb565(0xFF, 0x00, 0x00, true)
  1532. local green = lcd.rgb565(0x00, 0xFF, 0x00, true)
  1533. local blue = lcd.rgb565(0x00, 0x00, 0xFF, true)
  1534. */
  1535. static int l_lcd_rgb565(lua_State* L) {
  1536. uint8_t r =0,g =0,b = 0;
  1537. uint8_t swap = 0;
  1538. uint16_t dst = 0;
  1539. int top = 0 ;
  1540. uint32_t rgb = 0;
  1541. top = lua_gettop(L);
  1542. if (top == 1 || top == 2) {
  1543. rgb = luaL_checkinteger(L, 1);
  1544. r = (uint8_t)((rgb >> 16 ) & 0xFF);
  1545. g = (uint8_t)((rgb >> 8 ) & 0xFF);
  1546. b = (uint8_t)((rgb >> 0 ) & 0xFF);
  1547. swap = (lua_isboolean(L, 2) && !lua_toboolean(L, 2)) ? 0U : 1U;
  1548. }
  1549. else if (top == 3 || top == 4) {
  1550. r = (uint8_t)luaL_checkinteger(L, 1);
  1551. g = (uint8_t)luaL_checkinteger(L, 2);
  1552. b = (uint8_t)luaL_checkinteger(L, 3);
  1553. swap = (lua_isboolean(L, 4) && !lua_toboolean(L, 4)) ? 0U : 1U;
  1554. }
  1555. else {
  1556. LLOGW("unkown args count %d", top);
  1557. dst = 0;
  1558. }
  1559. dst = (uint16_t)((r&0xF8)<<8) | (uint16_t)((g&0xFC)<<3) | (uint16_t)(b>>3);
  1560. if (swap) {
  1561. dst = ((dst >> 8) & 0xFF) + ((dst & 0xFF) << 8);
  1562. }
  1563. lua_pushinteger(L, dst);
  1564. return 1;
  1565. }
  1566. #ifdef LUAT_USE_UFONT
  1567. #include "luat_ufont.h"
  1568. static const int l_lcd_draw_utf8(lua_State *L) {
  1569. size_t sz = 0;
  1570. uint32_t letter = 0;
  1571. uint32_t str_offset;
  1572. int ret = 0;
  1573. uint16_t draw_offset = 0;
  1574. int draw_x = 0;
  1575. int draw_y = 0;
  1576. luat_font_char_desc_t desc = {0};
  1577. // 左上角坐标x,y
  1578. int x = luaL_checkinteger(L, 1);
  1579. int y = luaL_checkinteger(L, 2);
  1580. // 待绘制的字符串
  1581. const char* data = (const char*)luaL_checklstring(L, 3, &sz);
  1582. // 字体指针
  1583. lv_font_t* lfont = (lv_font_t*)lua_touserdata(L, 4);
  1584. if (lfont == NULL) {
  1585. LLOGW("draw without font");
  1586. return 0;
  1587. }
  1588. luat_font_header_t* font = (luat_font_header_t*)lfont->dsc;
  1589. // 是否填充背景
  1590. bool draw_bg = lua_isboolean(L, 5) ? lua_toboolean(L, 5) : true;
  1591. // 没内容, 不需要画了
  1592. if (sz == 0) {
  1593. // 直接返回原坐标
  1594. lua_pushinteger(L, x);
  1595. return 1;
  1596. }
  1597. // 没字体, 不需要画了
  1598. if (font == NULL) {
  1599. LLOGD("NULL font, skip draw");
  1600. // 直接返回原坐标
  1601. lua_pushinteger(L, x);
  1602. return 1;
  1603. }
  1604. // 超边界了没? 超了就没必要绘制了
  1605. if (default_conf->h < y || default_conf->w < x) {
  1606. //LLOGD("draw y %d h % font->line_height %d", y, default_conf->h, font->line_height);
  1607. // 直接返回原坐标
  1608. lua_pushinteger(L, x);
  1609. return 1;
  1610. }
  1611. luat_color_t* buff = NULL;
  1612. if (draw_bg)
  1613. buff = luat_heap_malloc(font->line_height * font->line_height * 2);
  1614. // if (buff == NULL)
  1615. // return 0;
  1616. int offset = 0;
  1617. uint8_t *data_ptr = data;
  1618. uint8_t utf8_state = 0;
  1619. uint16_t utf8_tmp = 0;
  1620. uint16_t utf8_out = 0;
  1621. luat_color_t color = FORE_COLOR;
  1622. for (size_t i = 0; i < sz; i++)
  1623. {
  1624. utf8_out = luat_utf8_next(data[i], &utf8_state, &utf8_tmp);
  1625. if (utf8_out == 0x0ffff)
  1626. break; // 结束了
  1627. if (utf8_out == 0x0fffe)
  1628. continue; // 没读完一个字符,继续下一个循环
  1629. letter = (uint32_t)utf8_out;
  1630. //LLOGD("draw letter %04X", letter);
  1631. int ret = luat_font_get_bitmap(font, &desc, letter);
  1632. if (ret != 0) {
  1633. LLOGD("not such char in font");
  1634. draw_offset += font->line_height / 2; // 找不到字符, 默认跳过半个字
  1635. continue;
  1636. }
  1637. offset = 0;
  1638. // desc.data = tmp;
  1639. memset(buff, 0, font->line_height * font->line_height * 2);
  1640. draw_x = x + draw_offset;
  1641. draw_offset += desc.char_w;
  1642. if (draw_x >= 0 && draw_x + desc.char_w <= default_conf->w) {
  1643. //if (default_conf->buff == NULL) {
  1644. for (size_t j = 0; j < font->line_height; j++)
  1645. {
  1646. //LLOGD("draw char pix line %d", i);
  1647. for (size_t k = 0; k < desc.char_w; k++)
  1648. {
  1649. if ((desc.data[offset / 8] >> (7 - (offset % 8))) & 0x01) {
  1650. color = FORE_COLOR;
  1651. if (buff)
  1652. buff[offset] = FORE_COLOR;
  1653. else
  1654. luat_lcd_draw_point(default_conf, draw_x + k, y + j, FORE_COLOR);
  1655. //LLOGD("draw char pix mark %d", offset);
  1656. }
  1657. else {
  1658. if (buff)
  1659. buff[offset] = BACK_COLOR;
  1660. //LLOGD("draw char pix offset %d color %04X", offset, FORE_COLOR);
  1661. }
  1662. offset ++;
  1663. }
  1664. }
  1665. //LLOGD("luat_lcd_draw %d %d %d %d", draw_x, y, draw_x + desc.char_w, y + font->line_height);
  1666. luat_lcd_draw(default_conf, draw_x, y, draw_x + desc.char_w - 1, y + font->line_height - 1, buff);
  1667. //}
  1668. //else {
  1669. //
  1670. //}
  1671. }
  1672. }
  1673. if (buff)
  1674. luat_heap_free(buff);
  1675. lcd_auto_flush(default_conf);
  1676. lua_pushinteger(L, draw_x + desc.char_w);
  1677. return 1;
  1678. }
  1679. #endif
  1680. #include "rotable2.h"
  1681. static const rotable_Reg_t reg_lcd[] =
  1682. {
  1683. { "init", ROREG_FUNC(l_lcd_init)},
  1684. { "clear", ROREG_FUNC(l_lcd_clear)},
  1685. { "fill", ROREG_FUNC(l_lcd_draw_fill)},
  1686. { "drawPoint", ROREG_FUNC(l_lcd_draw_point)},
  1687. { "drawLine", ROREG_FUNC(l_lcd_draw_line)},
  1688. { "drawRectangle", ROREG_FUNC(l_lcd_draw_rectangle)},
  1689. { "drawCircle", ROREG_FUNC(l_lcd_draw_circle)},
  1690. { "drawQrcode", ROREG_FUNC(l_lcd_drawQrcode)},
  1691. { "drawStr", ROREG_FUNC(l_lcd_draw_str)},
  1692. { "flush", ROREG_FUNC(l_lcd_flush)},
  1693. { "setupBuff", ROREG_FUNC(l_lcd_setup_buff)},
  1694. { "autoFlush", ROREG_FUNC(l_lcd_auto_flush)},
  1695. { "setFont", ROREG_FUNC(l_lcd_set_font)},
  1696. { "setDefault", ROREG_FUNC(l_lcd_set_default)},
  1697. { "getDefault", ROREG_FUNC(l_lcd_get_default)},
  1698. { "getSize", ROREG_FUNC(l_lcd_get_size)},
  1699. { "drawXbm", ROREG_FUNC(l_lcd_drawxbm)},
  1700. { "close", ROREG_FUNC(l_lcd_close)},
  1701. { "on", ROREG_FUNC(l_lcd_display_on)},
  1702. { "off", ROREG_FUNC(l_lcd_display_off)},
  1703. { "sleep", ROREG_FUNC(l_lcd_sleep)},
  1704. { "wakeup", ROREG_FUNC(l_lcd_wakeup)},
  1705. { "invon", ROREG_FUNC(l_lcd_inv_on)},
  1706. { "invoff", ROREG_FUNC(l_lcd_inv_off)},
  1707. { "cmd", ROREG_FUNC(l_lcd_write_cmd)},
  1708. { "data", ROREG_FUNC(l_lcd_write_data)},
  1709. { "setColor", ROREG_FUNC(l_lcd_set_color)},
  1710. { "draw", ROREG_FUNC(l_lcd_draw)},
  1711. { "rgb565", ROREG_FUNC(l_lcd_rgb565)},
  1712. #ifdef LUAT_USE_UFONT
  1713. { "drawUTF8", ROREG_FUNC(l_lcd_draw_utf8)},
  1714. #endif
  1715. #ifdef LUAT_USE_TJPGD
  1716. { "showImage", ROREG_FUNC(l_lcd_showimage)},
  1717. #endif
  1718. #ifdef LUAT_USE_GTFONT
  1719. { "drawGtfontGb2312", ROREG_FUNC(l_lcd_draw_gtfont_gbk)},
  1720. { "drawGtfontGb2312Gray", ROREG_FUNC(l_lcd_draw_gtfont_gbk_gray)},
  1721. { "drawGtfontGbk", ROREG_FUNC(l_lcd_draw_gtfont_gbk)},
  1722. { "drawGtfontGbkGray", ROREG_FUNC(l_lcd_draw_gtfont_gbk_gray)},
  1723. #ifdef LUAT_USE_GTFONT_UTF8
  1724. { "drawGtfontUtf8", ROREG_FUNC(l_lcd_draw_gtfont_utf8)},
  1725. { "drawGtfontUtf8Gray", ROREG_FUNC(l_lcd_draw_gtfont_utf8_gray)},
  1726. #endif // LUAT_USE_GTFONT_UTF8
  1727. #endif // LUAT_USE_GTFONT
  1728. // 默认只带英文12号字体
  1729. //@const font_opposansm12 font 12号字体
  1730. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  1731. #ifdef USE_U8G2_OPPOSANSM_ENGLISH
  1732. //@const font_unifont_t_symbols font 符号字体
  1733. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  1734. //@const font_open_iconic_weather_6x_t font 天气字体
  1735. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  1736. //@const font_opposansm16 font 16号字体
  1737. //@const font_opposansm18 font 18号字体
  1738. //@const font_opposansm20 font 20号字体
  1739. //@const font_opposansm22 font 22号字体
  1740. //@const font_opposansm24 font 24号字体
  1741. //@const font_opposansm32 font 32号字体
  1742. { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
  1743. { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
  1744. { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
  1745. { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
  1746. { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
  1747. { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
  1748. #endif
  1749. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  1750. //@const font_opposansm12_chinese font 12号中文字体
  1751. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  1752. #endif
  1753. #ifdef USE_U8G2_OPPOSANSM14_CHINESE
  1754. //@const font_opposansm14_chinese font 14号中文字体
  1755. { "font_opposansm14_chinese", ROREG_PTR((void*)u8g2_font_opposansm14_chinese)},
  1756. #endif
  1757. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  1758. //@const font_opposansm16_chinese font 16号中文字体
  1759. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  1760. #endif
  1761. #ifdef USE_U8G2_OPPOSANSM18_CHINESE
  1762. //@const font_opposansm18_chinese font 18号中文字体
  1763. { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
  1764. #endif
  1765. #ifdef USE_U8G2_OPPOSANSM20_CHINESE
  1766. //@const font_opposansm20_chinese font 20号中文字体
  1767. { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
  1768. #endif
  1769. #ifdef USE_U8G2_OPPOSANSM22_CHINESE
  1770. //@const font_opposansm22_chinese font 22号中文字体
  1771. { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
  1772. #endif
  1773. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  1774. //@const font_opposansm24_chinese font 24号中文字体
  1775. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  1776. #endif
  1777. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  1778. //@const font_opposansm32_chinese font 32号中文字体
  1779. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  1780. #endif
  1781. #ifdef USE_U8G2_SARASA_ENGLISH
  1782. { "font_sarasa_m12_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m12_ascii)},
  1783. { "font_sarasa_m14_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m14_ascii)},
  1784. { "font_sarasa_m16_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m16_ascii)},
  1785. { "font_sarasa_m18_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m18_ascii)},
  1786. { "font_sarasa_m20_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m20_ascii)},
  1787. { "font_sarasa_m22_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m22_ascii)},
  1788. //再大的很少用到先不加了
  1789. #endif
  1790. // #ifdef USE_U8G2_SARASA_M8_CHINESE
  1791. // { "font_sarasa_m8_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m8_chinese)},
  1792. // #endif
  1793. #ifdef USE_U8G2_SARASA_M10_CHINESE
  1794. { "font_sarasa_m10_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m10_chinese)},
  1795. #endif
  1796. #ifdef USE_U8G2_SARASA_M12_CHINESE
  1797. { "font_sarasa_m12_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m12_chinese)},
  1798. #endif
  1799. #ifdef USE_U8G2_SARASA_M14_CHINESE
  1800. { "font_sarasa_m14_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m14_chinese)},
  1801. #endif
  1802. #ifdef USE_U8G2_SARASA_M16_CHINESE
  1803. { "font_sarasa_m16_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m16_chinese)},
  1804. #endif
  1805. #ifdef USE_U8G2_SARASA_M18_CHINESE
  1806. { "font_sarasa_m18_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m18_chinese)},
  1807. #endif
  1808. #ifdef USE_U8G2_SARASA_M20_CHINESE
  1809. { "font_sarasa_m20_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m20_chinese)},
  1810. #endif
  1811. #ifdef USE_U8G2_SARASA_M22_CHINESE
  1812. { "font_sarasa_m22_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m22_chinese)},
  1813. #endif
  1814. #ifdef USE_U8G2_SARASA_M24_CHINESE
  1815. { "font_sarasa_m24_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m24_chinese)},
  1816. #endif
  1817. #ifdef USE_U8G2_SARASA_M26_CHINESE
  1818. { "font_sarasa_m26_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m26_chinese)},
  1819. #endif
  1820. #ifdef USE_U8G2_SARASA_M28_CHINESE
  1821. { "font_sarasa_m28_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m28_chinese)},
  1822. #endif
  1823. { "set_direction", ROREG_FUNC(l_lcd_set_direction)},
  1824. //@const direction_0 int 0°方向命令
  1825. { "direction_0", ROREG_INT(0)},
  1826. //@const direction_90 int 90°方向命令
  1827. { "direction_90", ROREG_INT(1)},
  1828. //@const direction_180 int 180°方向命令
  1829. { "direction_180", ROREG_INT(2)},
  1830. //@const direction_270 int 270°方向命令
  1831. { "direction_270", ROREG_INT(3)},
  1832. //@const SPI 硬件spi device lcd驱动
  1833. { "SPI", ROREG_INT(LUAT_LCD_SPI_DEVICE)},
  1834. //@const HWID_0 硬件lcd驱动id0 (根据芯片支持选择)
  1835. { "HWID_0", ROREG_INT(LUAT_LCD_HW_ID_0)},
  1836. //@const RGB 硬件RGB lcd驱动 (根据芯片支持选择)
  1837. { "RGB", ROREG_INT(LUAT_LCD_PORT_RGB)},
  1838. // //@const ARM2D 硬件ARM2D lcd驱动 (根据芯片支持选择)
  1839. // { "ARM2D", ROREG_INT(LUAT_LCD_PORT_ARM2D)},
  1840. // //@const DMA2D 硬件DMA2D lcd驱动 (根据芯片支持选择)
  1841. // { "DMA2D", ROREG_INT(LUAT_LCD_PORT_DMA2D)},
  1842. //@const WIRE_3_BIT_9_INTERFACE_I 三线spi 9bit 模式I
  1843. { "WIRE_3_BIT_9_INTERFACE_I", ROREG_INT(LUAT_LCD_IM_3_WIRE_9_BIT_INTERFACE_I)},
  1844. //@const WIRE_4_BIT_8_INTERFACE_I 四线spi 8bit 模式I
  1845. { "WIRE_4_BIT_8_INTERFACE_I", ROREG_INT(LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_I)},
  1846. //@const WIRE_3_BIT_9_INTERFACE_II 三线spi 9bit 模式II
  1847. { "WIRE_3_BIT_9_INTERFACE_II", ROREG_INT(LUAT_LCD_IM_3_WIRE_9_BIT_INTERFACE_II)},
  1848. //@const WIRE_4_BIT_8_INTERFACE_II 四线spi 8bit 模式II
  1849. { "WIRE_4_BIT_8_INTERFACE_II", ROREG_INT(LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_II)},
  1850. //@const DATA_2_LANE int 双通道模式
  1851. { "DATA_2_LANE", ROREG_INT(LUAT_LCD_IM_2_DATA_LANE)},
  1852. { "QSPI_MODE", ROREG_INT(LUAT_LCD_IM_QSPI_MODE)},
  1853. {NULL, ROREG_INT(0)}
  1854. };
  1855. LUAMOD_API int luaopen_lcd( lua_State *L ) {
  1856. luat_newlib2(L, reg_lcd);
  1857. return 1;
  1858. }