luat_lib_lcd.c 68 KB

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