luat_lib_lcd.c 54 KB

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