luat_lib_lcd.c 64 KB

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