luat_lib_lcd.c 62 KB

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