luat_lib_lcd.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  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. }
  518. int ret = lcd_write_cmd_data(lcd_dft_conf,(uint8_t)luaL_checkinteger(L, 1), data, param_len);
  519. lua_pushboolean(L, ret == 0 ? 1 : 0);
  520. return 1;
  521. }
  522. /*
  523. lcd数据
  524. @api lcd.data(data)
  525. @int data
  526. @usage
  527. -- lcd数据
  528. lcd.data(0x21)
  529. */
  530. static int l_lcd_write_data(lua_State* L) {
  531. int ret = lcd_write_data(lcd_dft_conf,(const uint8_t)luaL_checkinteger(L, 1));
  532. lua_pushboolean(L, ret == 0 ? 1 : 0);
  533. return 1;
  534. }
  535. /*
  536. lcd颜色设置
  537. @api lcd.setColor(back,fore)
  538. @int 背景色
  539. @int 前景色
  540. @usage
  541. -- lcd颜色设置
  542. lcd.setColor(0xFFFF,0x0000)
  543. */
  544. static int l_lcd_set_color(lua_State* L) {
  545. luat_color_t back,fore;
  546. back = (luat_color_t)luaL_checkinteger(L, 1);
  547. fore = (luat_color_t)luaL_checkinteger(L, 2);
  548. int ret = luat_lcd_set_color(back, fore);
  549. lua_pushboolean(L, ret == 0 ? 1 : 0);
  550. return 1;
  551. }
  552. static int l_lcd_set_direction(lua_State* L) {
  553. int ret = luat_lcd_set_direction(lcd_dft_conf, (uint8_t)luaL_checkinteger(L, 1));
  554. lua_pushboolean(L, ret == 0 ? 1 : 0);
  555. return 1;
  556. }
  557. /*
  558. lcd颜色填充
  559. @api lcd.draw(x1, y1, x2, y2,color)
  560. @int 左上边缘的X位置.
  561. @int 左上边缘的Y位置.
  562. @int 右下边缘的X位置.
  563. @int 右下边缘的Y位置.
  564. @string 字符串或zbuff对象
  565. @usage
  566. -- lcd颜色填充
  567. local buff = zbuff.create({201,1,16},0x001F)
  568. lcd.draw(20,30,220,30,buff)
  569. */
  570. static int l_lcd_draw(lua_State* L) {
  571. int16_t x1, y1, x2, y2;
  572. int ret;
  573. // luat_color_t *color = NULL;
  574. luat_zbuff_t *buff;
  575. x1 = luaL_checkinteger(L, 1);
  576. y1 = luaL_checkinteger(L, 2);
  577. x2 = luaL_checkinteger(L, 3);
  578. y2 = luaL_checkinteger(L, 4);
  579. if (lcd_dft_conf == NULL) {
  580. LLOGE("lcd not init");
  581. return 0;
  582. }
  583. if (lua_isinteger(L, 5)) {
  584. // color = (luat_color_t *)luaL_checkstring(L, 5);
  585. luat_color_t color = (luat_color_t)luaL_checkinteger(L, 1);
  586. ret = luat_lcd_draw(lcd_dft_conf, x1, y1, x2, y2, &color);
  587. }
  588. else if (lua_isuserdata(L, 5)) {
  589. buff = luaL_checkudata(L, 5, LUAT_ZBUFF_TYPE);
  590. luat_color_t *color = (luat_color_t *)buff->addr;
  591. ret = luat_lcd_draw(lcd_dft_conf, x1, y1, x2, y2, color);
  592. }
  593. else if(lua_isstring(L, 5)) {
  594. luat_color_t *color = (luat_color_t *)luaL_checkstring(L, 5);
  595. ret = luat_lcd_draw(lcd_dft_conf, x1, y1, x2, y2, color);
  596. }
  597. else {
  598. return 0;
  599. }
  600. lcd_auto_flush(lcd_dft_conf);
  601. // int ret = luat_lcd_draw(lcd_dft_conf, x1, y1, x2, y2, color);
  602. lua_pushboolean(L, ret == 0 ? 1 : 0);
  603. return 1;
  604. }
  605. /*
  606. lcd清屏
  607. @api lcd.clear(color)
  608. @int 屏幕颜色 可选参数,默认背景色
  609. @usage
  610. -- lcd清屏
  611. lcd.clear()
  612. */
  613. static int l_lcd_clear(lua_State* L) {
  614. //size_t len = 0;
  615. luat_color_t color = BACK_COLOR;
  616. if (lua_gettop(L) > 0)
  617. color = (luat_color_t)luaL_checkinteger(L, 1);
  618. int ret = luat_lcd_clear(lcd_dft_conf, color);
  619. lcd_auto_flush(lcd_dft_conf);
  620. lua_pushboolean(L, ret == 0 ? 1 : 0);
  621. return 1;
  622. }
  623. /*
  624. lcd颜色填充
  625. @api lcd.fill(x1, y1, x2, y2,color)
  626. @int 左上边缘的X位置.
  627. @int 左上边缘的Y位置.
  628. @int 右下边缘的X位置,不含
  629. @int 右下边缘的Y位置,不含
  630. @int 绘画颜色 可选参数,默认背景色
  631. @usage
  632. -- lcd颜色填充
  633. lcd.fill(20,30,220,30,0x0000)
  634. */
  635. static int l_lcd_draw_fill(lua_State* L) {
  636. int16_t x1, y1, x2, y2;
  637. luat_color_t color = BACK_COLOR;
  638. x1 = luaL_checkinteger(L, 1);
  639. y1 = luaL_checkinteger(L, 2);
  640. x2 = luaL_checkinteger(L, 3);
  641. y2 = luaL_checkinteger(L, 4);
  642. if (lua_gettop(L) > 4)
  643. color = (luat_color_t)luaL_checkinteger(L, 5);
  644. int ret = luat_lcd_draw_fill(lcd_dft_conf, x1, y1, x2, y2, color);
  645. lcd_auto_flush(lcd_dft_conf);
  646. lua_pushboolean(L, ret == 0 ? 1 : 0);
  647. return 1;
  648. }
  649. /*
  650. 画一个点.
  651. @api lcd.drawPoint(x0,y0,color)
  652. @int 点的X位置.
  653. @int 点的Y位置.
  654. @int 绘画颜色 可选参数,默认前景色
  655. @usage
  656. lcd.drawPoint(20,30,0x001F)
  657. */
  658. static int l_lcd_draw_point(lua_State* L) {
  659. int16_t x, y;
  660. luat_color_t color = FORE_COLOR;
  661. x = luaL_checkinteger(L, 1);
  662. y = luaL_checkinteger(L, 2);
  663. if (lua_gettop(L) > 2)
  664. color = (luat_color_t)luaL_checkinteger(L, 3);
  665. int ret = luat_lcd_draw_point(lcd_dft_conf, x, y, color);
  666. lcd_auto_flush(lcd_dft_conf);
  667. lua_pushboolean(L, ret == 0 ? 1 : 0);
  668. return 1;
  669. }
  670. /*
  671. 在两点之间画一条线.
  672. @api lcd.drawLine(x0,y0,x1,y1,color)
  673. @int 第一个点的X位置.
  674. @int 第一个点的Y位置.
  675. @int 第二个点的X位置.
  676. @int 第二个点的Y位置.
  677. @int 绘画颜色 可选参数,默认前景色
  678. @usage
  679. lcd.drawLine(20,30,220,30,0x001F)
  680. */
  681. static int l_lcd_draw_line(lua_State* L) {
  682. int16_t x1, y1, x2, y2;
  683. luat_color_t color = FORE_COLOR;
  684. x1 = luaL_checkinteger(L, 1);
  685. y1 = luaL_checkinteger(L, 2);
  686. x2 = luaL_checkinteger(L, 3);
  687. y2 = luaL_checkinteger(L, 4);
  688. if (lua_gettop(L) > 4)
  689. color = (luat_color_t)luaL_checkinteger(L, 5);
  690. int ret = luat_lcd_draw_line(lcd_dft_conf, x1, y1, x2, y2, color);
  691. lcd_auto_flush(lcd_dft_conf);
  692. lua_pushboolean(L, ret == 0 ? 1 : 0);
  693. return 1;
  694. }
  695. /*
  696. 从x / y位置(左上边缘)开始绘制一个框
  697. @api lcd.drawRectangle(x0,y0,x1,y1,color)
  698. @int 左上边缘的X位置.
  699. @int 左上边缘的Y位置.
  700. @int 右下边缘的X位置.
  701. @int 右下边缘的Y位置.
  702. @int 绘画颜色 可选参数,默认前景色
  703. @usage
  704. lcd.drawRectangle(20,40,220,80,0x001F)
  705. */
  706. static int l_lcd_draw_rectangle(lua_State* L) {
  707. int16_t x1, y1, x2, y2;
  708. luat_color_t color = FORE_COLOR;
  709. x1 = luaL_checkinteger(L, 1);
  710. y1 = luaL_checkinteger(L, 2);
  711. x2 = luaL_checkinteger(L, 3);
  712. y2 = luaL_checkinteger(L, 4);
  713. if (lua_gettop(L) > 4)
  714. color = (luat_color_t)luaL_checkinteger(L, 5);
  715. int ret = luat_lcd_draw_rectangle(lcd_dft_conf, x1, y1, x2, y2, color);
  716. lcd_auto_flush(lcd_dft_conf);
  717. lua_pushboolean(L, ret == 0 ? 1 : 0);
  718. return 1;
  719. }
  720. /*
  721. 从x / y位置(圆心)开始绘制一个圆
  722. @api lcd.drawCircle(x0,y0,r,color)
  723. @int 圆心的X位置.
  724. @int 圆心的Y位置.
  725. @int 半径.
  726. @int 绘画颜色 可选参数,默认前景色
  727. @usage
  728. lcd.drawCircle(120,120,20,0x001F)
  729. */
  730. static int l_lcd_draw_circle(lua_State* L) {
  731. int16_t x0, y0, r;
  732. luat_color_t color = FORE_COLOR;
  733. x0 = luaL_checkinteger(L, 1);
  734. y0 = luaL_checkinteger(L, 2);
  735. r = luaL_checkinteger(L, 3);
  736. if (lua_gettop(L) > 3)
  737. color = (luat_color_t)luaL_checkinteger(L, 4);
  738. int ret = luat_lcd_draw_circle(lcd_dft_conf, x0, y0, r, color);
  739. lcd_auto_flush(lcd_dft_conf);
  740. lua_pushboolean(L, ret == 0 ? 1 : 0);
  741. return 1;
  742. }
  743. /*
  744. 缓冲区绘制QRCode
  745. @api lcd.drawQrcode(x, y, str, size)
  746. @int x坐标
  747. @int y坐标
  748. @string 二维码的内容
  749. @int 显示大小 (注意:二维码生成大小与要显示内容和纠错等级有关,生成版本为1-40(对应 21x21 - 177x177)的不定大小,如果和设置大小不同会自动在指定的区域中间显示二维码,如二维码未显示请查看日志提示)
  750. @return nil 无返回值
  751. */
  752. static int l_lcd_drawQrcode(lua_State *L)
  753. {
  754. size_t len;
  755. int x = luaL_checkinteger(L, 1);
  756. int y = luaL_checkinteger(L, 2);
  757. const char* text = luaL_checklstring(L, 3, &len);
  758. int size = luaL_checkinteger(L, 4);
  759. uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  760. uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  761. if (qrcode == NULL || tempBuffer == NULL) {
  762. if (qrcode)
  763. luat_heap_free(qrcode);
  764. if (tempBuffer)
  765. luat_heap_free(tempBuffer);
  766. LLOGE("qrcode out of memory");
  767. return 0;
  768. }
  769. bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_LOW,
  770. qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
  771. if (ok){
  772. int qr_size = qrcodegen_getSize(qrcode);
  773. if (size < qr_size){
  774. LLOGE("size must be greater than qr_size %d",qr_size);
  775. goto end;
  776. }
  777. int scale = size / qr_size ;
  778. if (!scale)scale = 1;
  779. int margin = (size - qr_size * scale) / 2;
  780. luat_lcd_draw_fill(lcd_dft_conf,x,y,x+size,y+size,BACK_COLOR);
  781. x+=margin;
  782. y+=margin;
  783. for (int j = 0; j < qr_size; j++) {
  784. for (int i = 0; i < qr_size; i++) {
  785. if (qrcodegen_getModule(qrcode, i, j))
  786. luat_lcd_draw_fill(lcd_dft_conf,x+i*scale,y+j*scale,x+(i+1)*scale,y+(j+1)*scale,FORE_COLOR);
  787. }
  788. }
  789. }else{
  790. LLOGE("qrcodegen_encodeText false");
  791. }
  792. end:
  793. if (qrcode)
  794. luat_heap_free(qrcode);
  795. if (tempBuffer)
  796. luat_heap_free(tempBuffer);
  797. lcd_auto_flush(lcd_dft_conf);
  798. return 0;
  799. }
  800. static uint8_t utf8_state;
  801. static uint16_t encoding;
  802. static uint16_t utf8_next(uint8_t b)
  803. {
  804. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  805. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  806. if ( utf8_state == 0 )
  807. {
  808. if ( b >= 0xfc ) /* 6 byte sequence */
  809. {
  810. utf8_state = 5;
  811. b &= 1;
  812. }
  813. else if ( b >= 0xf8 )
  814. {
  815. utf8_state = 4;
  816. b &= 3;
  817. }
  818. else if ( b >= 0xf0 )
  819. {
  820. utf8_state = 3;
  821. b &= 7;
  822. }
  823. else if ( b >= 0xe0 )
  824. {
  825. utf8_state = 2;
  826. b &= 15;
  827. }
  828. else if ( b >= 0xc0 )
  829. {
  830. utf8_state = 1;
  831. b &= 0x01f;
  832. }
  833. else
  834. {
  835. /* do nothing, just use the value as encoding */
  836. return b;
  837. }
  838. encoding = b;
  839. return 0x0fffe;
  840. }
  841. else
  842. {
  843. utf8_state--;
  844. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  845. encoding<<=6;
  846. b &= 0x03f;
  847. encoding |= b;
  848. if ( utf8_state != 0 )
  849. return 0x0fffe; /* nothing to do yet */
  850. }
  851. return encoding;
  852. }
  853. static void u8g2_draw_hv_line(u8g2_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color){
  854. switch(dir)
  855. {
  856. case 0:
  857. luat_lcd_draw_hline(lcd_dft_conf,x,y,len,color);
  858. break;
  859. case 1:
  860. luat_lcd_draw_vline(lcd_dft_conf,x,y,len,color);
  861. break;
  862. case 2:
  863. luat_lcd_draw_hline(lcd_dft_conf,x-len+1,y,len,color);
  864. break;
  865. case 3:
  866. luat_lcd_draw_vline(lcd_dft_conf,x,y-len+1,len,color);
  867. break;
  868. }
  869. }
  870. static void u8g2_font_decode_len(u8g2_t *u8g2, uint8_t len, uint8_t is_foreground){
  871. uint8_t cnt; /* total number of remaining pixels, which have to be drawn */
  872. uint8_t rem; /* remaining pixel to the right edge of the glyph */
  873. uint8_t current; /* number of pixels, which need to be drawn for the draw procedure */
  874. /* current is either equal to cnt or equal to rem */
  875. /* local coordinates of the glyph */
  876. uint8_t lx,ly;
  877. /* target position on the screen */
  878. int16_t x, y;
  879. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  880. cnt = len;
  881. /* get the local position */
  882. lx = decode->x;
  883. ly = decode->y;
  884. for(;;){
  885. /* calculate the number of pixel to the right edge of the glyph */
  886. rem = decode->glyph_width;
  887. rem -= lx;
  888. /* calculate how many pixel to draw. This is either to the right edge */
  889. /* or lesser, if not enough pixel are left */
  890. current = rem;
  891. if ( cnt < rem )
  892. current = cnt;
  893. /* now draw the line, but apply the rotation around the glyph target position */
  894. //u8g2_font_decode_draw_pixel(u8g2, lx,ly,current, is_foreground);
  895. // printf("lx:%d,ly:%d,current:%d, is_foreground:%d \r\n",lx,ly,current, is_foreground);
  896. /* get target position */
  897. x = decode->target_x;
  898. y = decode->target_y;
  899. /* apply rotation */
  900. x = u8g2_add_vector_x(x, lx, ly, decode->dir);
  901. y = u8g2_add_vector_y(y, lx, ly, decode->dir);
  902. /* draw foreground and background (if required) */
  903. if ( current > 0 ) /* avoid drawing zero length lines, issue #4 */
  904. {
  905. if ( is_foreground )
  906. {
  907. u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, FORE_COLOR);
  908. }
  909. // else if ( decode->is_transparent == 0 )
  910. // {
  911. // u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, BACK_COLOR);
  912. // }
  913. }
  914. /* check, whether the end of the run length code has been reached */
  915. if ( cnt < rem )
  916. break;
  917. cnt -= rem;
  918. lx = 0;
  919. ly++;
  920. }
  921. lx += cnt;
  922. decode->x = lx;
  923. decode->y = ly;
  924. }
  925. static void u8g2_font_setup_decode(u8g2_t *u8g2, const uint8_t *glyph_data)
  926. {
  927. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  928. decode->decode_ptr = glyph_data;
  929. decode->decode_bit_pos = 0;
  930. /* 8 Nov 2015, this is already done in the glyph data search procedure */
  931. /*
  932. decode->decode_ptr += 1;
  933. decode->decode_ptr += 1;
  934. */
  935. decode->glyph_width = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_char_width);
  936. decode->glyph_height = u8g2_font_decode_get_unsigned_bits(decode,u8g2->font_info.bits_per_char_height);
  937. }
  938. static int8_t u8g2_font_decode_glyph(u8g2_t *u8g2, const uint8_t *glyph_data){
  939. uint8_t a, b;
  940. int8_t x, y;
  941. int8_t d;
  942. int8_t h;
  943. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  944. u8g2_font_setup_decode(u8g2, glyph_data);
  945. h = u8g2->font_decode.glyph_height;
  946. x = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_x);
  947. y = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_y);
  948. d = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_delta_x);
  949. if ( decode->glyph_width > 0 )
  950. {
  951. decode->target_x = u8g2_add_vector_x(decode->target_x, x, -(h+y), decode->dir);
  952. decode->target_y = u8g2_add_vector_y(decode->target_y, x, -(h+y), decode->dir);
  953. //u8g2_add_vector(&(decode->target_x), &(decode->target_y), x, -(h+y), decode->dir);
  954. /* reset local x/y position */
  955. decode->x = 0;
  956. decode->y = 0;
  957. /* decode glyph */
  958. for(;;){
  959. a = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_0);
  960. b = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_1);
  961. do{
  962. u8g2_font_decode_len(u8g2, a, 0);
  963. u8g2_font_decode_len(u8g2, b, 1);
  964. } while( u8g2_font_decode_get_unsigned_bits(decode, 1) != 0 );
  965. if ( decode->y >= h )
  966. break;
  967. }
  968. }
  969. return d;
  970. }
  971. const uint8_t *u8g2_font_get_glyph_data(u8g2_t *u8g2, uint16_t encoding);
  972. static int16_t u8g2_font_draw_glyph(u8g2_t *u8g2, int16_t x, int16_t y, uint16_t encoding){
  973. int16_t dx = 0;
  974. u8g2->font_decode.target_x = x;
  975. u8g2->font_decode.target_y = y;
  976. const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, encoding);
  977. if ( glyph_data != NULL ){
  978. dx = u8g2_font_decode_glyph(u8g2, glyph_data);
  979. }
  980. return dx;
  981. }
  982. extern void luat_u8g2_set_ascii_indentation(uint8_t value);
  983. /*
  984. 设置字体
  985. @api lcd.setFont(font, indentation)
  986. @int font lcd.font_XXX 请查阅常量表
  987. @int indentation, 等宽字体ascii右侧缩进0~127个pixel,等宽字体的ascii字符可能在右侧有大片空白,用户可以选择删除部分。留空或者超过127则直接删除右半边, 非等宽字体无效
  988. @usage
  989. -- 设置为字体,对之后的drawStr有效,调用lcd.drawStr前一定要先设置
  990. -- 若提示 "only font pointer is allow" , 则代表当前固件不含对应字体, 可使用云编译服务免费定制
  991. -- 云编译文档: https://wiki.luatos.com/develop/compile/Cloud_compilation.html
  992. -- lcd库的默认字体均以 lcd.font_ 开头
  993. lcd.setFont(lcd.font_opposansm12)
  994. lcd.drawStr(40,10,"drawStr")
  995. sys.wait(2000)
  996. lcd.setFont(lcd.font_opposansm12_chinese) -- 具体取值可参考api文档的常量表
  997. lcd.drawStr(40,40,"drawStr测试")
  998. */
  999. static int l_lcd_set_font(lua_State *L) {
  1000. if (!lua_islightuserdata(L, 1)) {
  1001. LLOGE("only font pointer is allow");
  1002. return 0;
  1003. }
  1004. const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
  1005. if (ptr == NULL) {
  1006. LLOGE("only font pointer is allow");
  1007. return 0;
  1008. }
  1009. if (lcd_dft_conf == NULL) {
  1010. LLOGE("lcd not init");
  1011. return 0;
  1012. }
  1013. luat_u8g2_set_ascii_indentation(0xff);
  1014. u8g2_SetFont(&(lcd_dft_conf->luat_lcd_u8g2), ptr);
  1015. if (lua_isinteger(L, 2)) {
  1016. int indentation = luaL_checkinteger(L, 2);
  1017. luat_u8g2_set_ascii_indentation(indentation);
  1018. }
  1019. lua_pushboolean(L, 1);
  1020. return 1;
  1021. }
  1022. /*
  1023. 显示字符串
  1024. @api lcd.drawStr(x,y,str,fg_color)
  1025. @int x 横坐标
  1026. @int y 竖坐标 注意:此(x,y)为左下起始坐标
  1027. @string str 文件内容
  1028. @int fg_color str颜色 注意:此参数可选,如不填写则使用之前设置的颜色,绘制只会绘制字体部分,背景需要自己清除
  1029. @usage
  1030. -- 显示之前先设置为中文字体,对之后的drawStr有效
  1031. lcd.setFont(lcd.font_opposansm12)
  1032. lcd.drawStr(40,10,"drawStr")
  1033. sys.wait(2000)
  1034. lcd.setFont(lcd.font_opposansm16_chinese)
  1035. lcd.drawStr(40,40,"drawStr测试")
  1036. */
  1037. static int l_lcd_draw_str(lua_State* L) {
  1038. int x, y;
  1039. size_t sz;
  1040. const uint8_t* data;
  1041. x = luaL_checkinteger(L, 1);
  1042. y = luaL_checkinteger(L, 2);
  1043. data = (const uint8_t*)luaL_checklstring(L, 3, &sz);
  1044. FORE_COLOR = (luat_color_t)luaL_optinteger(L, 4,FORE_COLOR);
  1045. // BACK_COLOR = (uint32_t)luaL_optinteger(L, 5,BACK_COLOR);
  1046. if (sz == 0)
  1047. return 0;
  1048. if (lcd_dft_conf == NULL) {
  1049. LLOGE("lcd not init");
  1050. return 0;
  1051. }
  1052. uint16_t e;
  1053. int16_t delta;
  1054. utf8_state = 0;
  1055. for(;;){
  1056. e = utf8_next((uint8_t)*data);
  1057. if ( e == 0x0ffff )
  1058. break;
  1059. data++;
  1060. if ( e != 0x0fffe ){
  1061. delta = u8g2_font_draw_glyph(&(lcd_dft_conf->luat_lcd_u8g2), x, y, e);
  1062. if (e < 0x0080) delta = luat_u8g2_need_ascii_cut(delta);
  1063. switch(lcd_dft_conf->luat_lcd_u8g2.font_decode.dir){
  1064. case 0:
  1065. x += delta;
  1066. break;
  1067. case 1:
  1068. y += delta;
  1069. break;
  1070. case 2:
  1071. x -= delta;
  1072. break;
  1073. case 3:
  1074. y -= delta;
  1075. break;
  1076. }
  1077. }
  1078. }
  1079. lcd_auto_flush(lcd_dft_conf);
  1080. return 0;
  1081. }
  1082. #ifdef LUAT_USE_GTFONT
  1083. #include "luat_gtfont.h"
  1084. /*
  1085. 使用gtfont显示gb2312字符串
  1086. @api lcd.drawGtfontGb2312(str,size,x,y)
  1087. @string str 显示字符串
  1088. @int size 字体大小 (支持16-192号大小字体)
  1089. @int x 横坐标
  1090. @int y 竖坐标
  1091. @usage
  1092. -- 注意, gtfont是额外的字体芯片硬件, 需要外挂在SPI总线才能调用本函数的
  1093. lcd.drawGtfontGb2312("啊啊啊",32,0,0)
  1094. */
  1095. /*
  1096. 使用gtfont显示gbk字符串
  1097. @api lcd.drawGtfontGbk(str,size,x,y)
  1098. @string str 显示字符串
  1099. @int size 字体大小 (支持16-192号大小字体)
  1100. @int x 横坐标
  1101. @int y 竖坐标
  1102. @usage
  1103. -- 注意, gtfont是额外的字体芯片硬件, 需要外挂在SPI总线才能调用本函数的
  1104. lcd.drawGtfontGbk("啊啊啊",32,0,0)
  1105. */
  1106. static int l_lcd_draw_gtfont_gbk(lua_State *L) {
  1107. size_t len;
  1108. int i = 0;
  1109. uint8_t strhigh,strlow ;
  1110. uint16_t str;
  1111. const char *fontCode = luaL_checklstring(L, 1,&len);
  1112. unsigned char size = luaL_checkinteger(L, 2);
  1113. int x = luaL_checkinteger(L, 3);
  1114. int y = luaL_checkinteger(L, 4);
  1115. FORE_COLOR = (luat_color_t)luaL_optinteger(L, 5,FORE_COLOR);
  1116. // BACK_COLOR = (luat_color_t)luaL_optinteger(L, 6,BACK_COLOR);
  1117. if (lcd_dft_conf == NULL) {
  1118. LLOGE("lcd not init");
  1119. return 0;
  1120. }
  1121. int buff_size = size*size/8;
  1122. unsigned char* buf = luat_heap_malloc(buff_size);
  1123. if (buf == NULL){
  1124. LLOGE("malloc error");
  1125. return 0;
  1126. }
  1127. while ( i < len){
  1128. memset(buf,0x00,buff_size);
  1129. strhigh = *fontCode;
  1130. fontCode++;
  1131. strlow = *fontCode;
  1132. str = (strhigh<<8)|strlow;
  1133. fontCode++;
  1134. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  1135. if(font_size == 0){
  1136. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  1137. return 0;
  1138. }
  1139. gtfont_draw_w(buf , x ,y , font_size,size , size,luat_lcd_draw_point,lcd_dft_conf,0);
  1140. x+=size;
  1141. i+=2;
  1142. }
  1143. lcd_auto_flush(lcd_dft_conf);
  1144. luat_heap_free(buf);
  1145. return 0;
  1146. }
  1147. /*
  1148. 使用gtfont灰度显示gb2312字符串
  1149. @api lcd.drawGtfontGb2312Gray(str,size,gray,x,y)
  1150. @string str 显示字符串
  1151. @int size 字体大小 (支持16-192号大小字体)
  1152. @int gray 灰度[1阶/2阶/4阶]
  1153. @int x 横坐标
  1154. @int y 竖坐标
  1155. @usage
  1156. -- 注意, gtfont是额外的字体芯片硬件, 需要外挂在SPI总线才能调用本函数的
  1157. lcd.drawGtfontGb2312Gray("啊啊啊",32,4,0,40)
  1158. */
  1159. /*
  1160. 使用gtfont灰度显示gbk字符串
  1161. @api lcd.drawGtfontGbkGray(str,size,gray,x,y)
  1162. @string str 显示字符串
  1163. @int size 字体大小 (支持16-192号大小字体)
  1164. @int gray 灰度[1阶/2阶/4阶]
  1165. @int x 横坐标
  1166. @int y 竖坐标
  1167. @usage
  1168. -- 注意, gtfont是额外的字体芯片硬件, 需要外挂在SPI总线才能调用本函数的
  1169. lcd.drawGtfontGbkGray("啊啊啊",32,4,0,40)
  1170. */
  1171. static int l_lcd_draw_gtfont_gbk_gray(lua_State* L) {
  1172. size_t len;
  1173. int i = 0;
  1174. uint8_t strhigh,strlow ;
  1175. uint16_t str;
  1176. const char *fontCode = luaL_checklstring(L, 1,&len);
  1177. unsigned char size = luaL_checkinteger(L, 2);
  1178. unsigned char font_g = luaL_optinteger(L, 3, 4);
  1179. int x = luaL_checkinteger(L, 4);
  1180. int y = luaL_checkinteger(L, 5);
  1181. FORE_COLOR = (luat_color_t)luaL_optinteger(L, 6,FORE_COLOR);
  1182. // BACK_COLOR = (luat_color_t)luaL_optinteger(L, 7,BACK_COLOR);
  1183. int buff_size = size*4*size*4/8+512;
  1184. unsigned char* buf = luat_heap_malloc(buff_size);
  1185. if (buf == NULL){
  1186. LLOGE("malloc error");
  1187. return 0;
  1188. }
  1189. while ( i < len){
  1190. memset(buf,0x00,buff_size);
  1191. strhigh = *fontCode;
  1192. fontCode++;
  1193. strlow = *fontCode;
  1194. str = (strhigh<<8)|strlow;
  1195. fontCode++;
  1196. unsigned int* width = NULL;
  1197. width = get_Font_Gray(buf,str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY,str,size, size);
  1198. // LLOGW("get_Font_Gray width[0]:%d width[1]:%d",width[0], width[1]);
  1199. int dw = gtfont_draw_gray_hz(buf, x, y, width[0] , size, width[1], luat_lcd_draw_point,lcd_dft_conf,0);
  1200. x+=dw;
  1201. i+=2;
  1202. }
  1203. lcd_auto_flush(lcd_dft_conf);
  1204. luat_heap_free(buf);
  1205. return 0;
  1206. }
  1207. #ifdef LUAT_USE_GTFONT_UTF8
  1208. /*
  1209. 使用gtfont显示UTF8字符串
  1210. @api lcd.drawGtfontUtf8(str,size,x,y)
  1211. @string str 显示字符串
  1212. @int size 字体大小 (支持16-192号大小字体)
  1213. @int x 横坐标
  1214. @int y 竖坐标
  1215. @usage
  1216. lcd.drawGtfontUtf8("啊啊啊",32,0,0)
  1217. */
  1218. static int l_lcd_draw_gtfont_utf8(lua_State *L) {
  1219. size_t len;
  1220. int i = 0;
  1221. uint8_t strhigh,strlow ;
  1222. uint16_t e,str;
  1223. const char *fontCode = luaL_checklstring(L, 1,&len);
  1224. unsigned char size = luaL_checkinteger(L, 2);
  1225. int x = luaL_checkinteger(L, 3);
  1226. int y = luaL_checkinteger(L, 4);
  1227. FORE_COLOR = (luat_color_t)luaL_optinteger(L, 5,FORE_COLOR);
  1228. // BACK_COLOR = (luat_color_t)luaL_optinteger(L, 6,BACK_COLOR);
  1229. int buff_size = size*size/8+512;
  1230. unsigned char* buf = luat_heap_malloc(buff_size);
  1231. if (buf == NULL){
  1232. LLOGE("malloc error");
  1233. return 0;
  1234. }
  1235. for(;;){
  1236. memset(buf,0,buff_size);
  1237. e = utf8_next((uint8_t)*fontCode);
  1238. if ( e == 0x0ffff )
  1239. break;
  1240. fontCode++;
  1241. if ( e != 0x0fffe ){
  1242. uint16_t str = gt_unicode2gb18030(e);
  1243. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  1244. if(font_size == 0){
  1245. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  1246. return 0;
  1247. }
  1248. unsigned int dw = gtfont_draw_w(buf , x ,y , font_size,size , size,luat_lcd_draw_point,lcd_dft_conf,0);
  1249. if (str==0x20){
  1250. x+=size/2;
  1251. }else{
  1252. x+=(str<0x80)?dw:size;
  1253. }
  1254. }
  1255. }
  1256. lcd_auto_flush(lcd_dft_conf);
  1257. luat_heap_free(buf);
  1258. return 0;
  1259. }
  1260. /*
  1261. 使用gtfont灰度显示UTF8字符串
  1262. @api lcd.drawGtfontUtf8Gray(str,size,gray,x,y)
  1263. @string str 显示字符串
  1264. @int size 字体大小 (支持16-192号大小字体)
  1265. @int gray 灰度[1阶/2阶/4阶]
  1266. @int x 横坐标
  1267. @int y 竖坐标
  1268. @usage
  1269. lcd.drawGtfontUtf8Gray("啊啊啊",32,4,0,40)
  1270. */
  1271. static int l_lcd_draw_gtfont_utf8_gray(lua_State* L) {
  1272. size_t len;
  1273. int i = 0;
  1274. uint8_t strhigh,strlow;
  1275. uint16_t e,str;
  1276. const char *fontCode = luaL_checklstring(L, 1,&len);
  1277. unsigned char size = luaL_checkinteger(L, 2);
  1278. unsigned char font_g = luaL_optinteger(L, 3, 4);
  1279. int x = luaL_checkinteger(L, 4);
  1280. int y = luaL_checkinteger(L, 5);
  1281. FORE_COLOR = (luat_color_t)luaL_optinteger(L, 6,FORE_COLOR);
  1282. // BACK_COLOR = (luat_color_t)luaL_optinteger(L, 7,BACK_COLOR);
  1283. uint16_t buff_size = size*4*size*4/8+512;
  1284. unsigned char* buf = luat_heap_malloc(buff_size);
  1285. // LLOGD("buff_size:%d buf:%p", buff_size,buf);
  1286. if (buf == NULL){
  1287. LLOGE("malloc error");
  1288. return 0;
  1289. }
  1290. for(;;){
  1291. memset(buf,0x00,buff_size);
  1292. e = utf8_next((uint8_t)*fontCode);
  1293. if ( e == 0x0ffff )
  1294. break;
  1295. fontCode++;
  1296. if ( e != 0x0fffe ){
  1297. uint16_t str = gt_unicode2gb18030(e);
  1298. unsigned int* width = NULL;
  1299. width = get_Font_Gray(buf,str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY,str,size, size);
  1300. // LLOGW("get_Font_Gray size:%d width[0]:%d width[1]:%d", size, width[0], width[1]);
  1301. int dw = gtfont_draw_gray_hz(buf, x, y, width[0] , size, width[1], luat_lcd_draw_point,lcd_dft_conf,0);
  1302. // LLOGW("gtfont_draw_gray_hz dw:%d",dw);
  1303. x+=dw;
  1304. }
  1305. }
  1306. lcd_auto_flush(lcd_dft_conf);
  1307. luat_heap_free(buf);
  1308. return 0;
  1309. }
  1310. #endif // LUAT_USE_GTFONT_UTF8
  1311. #endif // LUAT_USE_GTFONT
  1312. static int l_lcd_set_default(lua_State *L) {
  1313. if (lua_gettop(L) == 1) {
  1314. lcd_dft_conf = lua_touserdata(L, 1);
  1315. lua_pushboolean(L, 1);
  1316. return 1;
  1317. }
  1318. return 1;
  1319. }
  1320. static int l_lcd_get_default(lua_State *L) {
  1321. if (lcd_dft_conf == NULL)
  1322. return 0;
  1323. lua_pushlightuserdata(L, lcd_dft_conf);
  1324. return 1;
  1325. }
  1326. /*
  1327. 获取屏幕尺寸
  1328. @api lcd.getSize()
  1329. @return int 宽, 如果未初始化会返回0
  1330. @return int 高, 如果未初始化会返回0
  1331. @usage
  1332. log.info("lcd", "size", lcd.getSize())
  1333. */
  1334. static int l_lcd_get_size(lua_State *L) {
  1335. if (lua_gettop(L) == 1) {
  1336. luat_lcd_conf_t * conf = lua_touserdata(L, 1);
  1337. if (conf) {
  1338. lua_pushinteger(L, conf->w);
  1339. lua_pushinteger(L, conf->h);
  1340. return 2;
  1341. }
  1342. }
  1343. if (lcd_dft_conf == NULL) {
  1344. lua_pushinteger(L, 0);
  1345. lua_pushinteger(L, 0);
  1346. }
  1347. else {
  1348. lua_pushinteger(L, lcd_dft_conf->w);
  1349. lua_pushinteger(L, lcd_dft_conf->h);
  1350. }
  1351. return 2;
  1352. }
  1353. /*
  1354. 绘制位图
  1355. @api lcd.drawXbm(x, y, w, h, data)
  1356. @int X坐标
  1357. @int y坐标
  1358. @int 位图宽
  1359. @int 位图高
  1360. @int 位图数据,每一位代表一个像素
  1361. @usage
  1362. -- 取模使用PCtoLCD2002软件即可 阴码 逐行 逆向
  1363. -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
  1364. lcd.drawXbm(0, 0, 16,16, string.char(
  1365. 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
  1366. 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
  1367. ))
  1368. */
  1369. static int l_lcd_drawxbm(lua_State *L){
  1370. int x = luaL_checkinteger(L, 1);
  1371. int y = luaL_checkinteger(L, 2);
  1372. size_t w = luaL_checkinteger(L, 3);
  1373. size_t h = luaL_checkinteger(L, 4);
  1374. size_t len = 0;
  1375. const char* data = luaL_checklstring(L, 5, &len);
  1376. uint8_t mask = 1;
  1377. if (h < 1) return 0; // 行数必须大于0
  1378. if (len*8/h < w) return 0; // 起码要填满一行
  1379. int w1 = w/8;
  1380. if (w%8)w1++;
  1381. if (len != h*w1)return 0;
  1382. if (lcd_dft_conf == NULL) {
  1383. LLOGE("lcd not init");
  1384. return 0;
  1385. }
  1386. luat_color_t* color_w = luat_heap_malloc(sizeof(luat_color_t) * w);
  1387. for (size_t b = 0; b < h; b++){
  1388. size_t a = 0;
  1389. while (a < w){
  1390. for (size_t c = 0; c < 8; c++){
  1391. if (*data&(mask<<c)){
  1392. color_w[a]=FORE_COLOR;
  1393. }else{
  1394. color_w[a]=BACK_COLOR;
  1395. }
  1396. a++;
  1397. if (a == w)break;
  1398. }
  1399. data++;
  1400. }
  1401. luat_lcd_draw(lcd_dft_conf, x, y+b, x+w-1, y+b, color_w);
  1402. }
  1403. luat_heap_free(color_w);
  1404. lcd_auto_flush(lcd_dft_conf);
  1405. lua_pushboolean(L, 1);
  1406. return 1;
  1407. }
  1408. #ifdef LUAT_USE_TJPGD
  1409. /*
  1410. 显示图片,当前只支持jpg,jpeg
  1411. @api lcd.showImage(x, y, file)
  1412. @int X坐标
  1413. @int y坐标
  1414. @string 文件路径
  1415. @usage
  1416. lcd.showImage(0,0,"/luadb/logo.jpg")
  1417. */
  1418. int l_lcd_showimage(lua_State *L) {
  1419. if (lcd_dft_conf == NULL) {
  1420. LLOGE("lcd not init");
  1421. return 0;
  1422. }
  1423. size_t size = 0;
  1424. int ret = 0;
  1425. int x = luaL_checkinteger(L, 1);
  1426. int y = luaL_checkinteger(L, 2);
  1427. const char* input_file = luaL_checklstring(L, 3, &size);
  1428. 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){
  1429. ret = lcd_draw_jpeg(lcd_dft_conf, input_file, x, y);
  1430. lua_pushboolean(L, ret == 0 ? 1 : 0);
  1431. } else{
  1432. LLOGE("input_file not support");
  1433. lua_pushboolean(L, 0);
  1434. }
  1435. return 1;
  1436. }
  1437. #endif
  1438. /*
  1439. 主动刷新数据到界面, 仅设置buff且禁用自动属性后使用
  1440. @api lcd.flush()
  1441. @return bool 成功返回true, 否则返回nil/false
  1442. @usage
  1443. -- 本API与 lcd.setupBuff lcd.autoFlush 配合使用
  1444. lcd.flush()
  1445. */
  1446. static int l_lcd_flush(lua_State* L) {
  1447. luat_lcd_conf_t * conf = NULL;
  1448. if (lua_gettop(L) == 1) {
  1449. conf = lua_touserdata(L, 1);
  1450. }else {
  1451. conf = lcd_dft_conf;
  1452. }
  1453. if (conf == NULL) {
  1454. //LLOGW("lcd not init");
  1455. return 0;
  1456. }
  1457. if (conf->buff == NULL) {
  1458. //LLOGW("lcd without buff, not support flush");
  1459. return 0;
  1460. }
  1461. if (conf->auto_flush) {
  1462. //LLOGI("lcd auto flush is enable, no need for flush");
  1463. return 0;
  1464. }
  1465. luat_lcd_flush(conf);
  1466. lua_pushboolean(L, 1);
  1467. return 1;
  1468. }
  1469. /*
  1470. 设置显示缓冲区, 所需内存大小为 2×宽×高 字节. 请衡量内存需求与业务所需的刷新频次.
  1471. @api lcd.setupBuff(conf, onheap)
  1472. @userdata conf指针, 不需要传
  1473. @bool true使用heap内存, false使用vm内存, 默认使用vm内存, 不需要主动传
  1474. @return bool 是否成功
  1475. @usage
  1476. -- 初始化lcd的buff缓冲区, 可理解为FrameBuffer区域.
  1477. lcd.setupBuff()
  1478. */
  1479. static int l_lcd_setup_buff(lua_State* L) {
  1480. luat_lcd_conf_t * conf = NULL;
  1481. if (lua_gettop(L) == 1) {
  1482. conf = lua_touserdata(L, 1);
  1483. }
  1484. else {
  1485. conf = lcd_dft_conf;
  1486. }
  1487. if (conf == NULL) {
  1488. LLOGW("lcd not init");
  1489. return 0;
  1490. }
  1491. if (conf->buff != NULL && conf->buff_ex != NULL) {
  1492. LLOGI("lcd buff is aready exist");
  1493. return 0;
  1494. }
  1495. if (lua_isboolean(L, 2) && lua_toboolean(L, 2)) {
  1496. luat_lcd_setup_buff(conf);
  1497. }
  1498. else {
  1499. conf->buff = lua_newuserdata(L, sizeof(luat_color_t) * conf->w * conf->h);
  1500. if (conf->buff) {
  1501. conf->buff_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1502. }
  1503. }
  1504. if (conf->buff == NULL) {
  1505. LLOGE("lcd buff malloc fail, out of memory? size %d", sizeof(luat_color_t) * conf->w * conf->h);
  1506. return 0;
  1507. }
  1508. // 先设置为不需要的区间
  1509. conf->flush_y_min = conf->h;
  1510. conf->flush_y_max = 0;
  1511. // luat_lcd_clear 会将区域扩展到整个屏幕
  1512. luat_lcd_clear(lcd_dft_conf, BACK_COLOR);
  1513. lua_pushboolean(L, 1);
  1514. return 1;
  1515. }
  1516. /*
  1517. 设置自动刷新, 需配合lcd.setupBuff使用
  1518. @api lcd.autoFlush(enable)
  1519. @bool 是否自动刷新,默认为true
  1520. @usage
  1521. -- 设置buff 并禁用自动更新
  1522. lcd.setupBuff()
  1523. lcd.autoFlush(false)
  1524. -- 禁止自动更新后, 需要使用 lcd.flush() 主动刷新数据到屏幕
  1525. */
  1526. static int l_lcd_auto_flush(lua_State* L) {
  1527. luat_lcd_conf_t * conf = lcd_dft_conf;
  1528. if (conf == NULL) {
  1529. LLOGW("lcd not init");
  1530. return 0;
  1531. }
  1532. conf->auto_flush = lua_toboolean(L, 1);
  1533. lua_pushboolean(L, conf->auto_flush);
  1534. return 1;
  1535. }
  1536. /*
  1537. RGB565颜色生成
  1538. @api lcd.rgb565(r, g, b, swap)
  1539. @int 红色, 0x00 ~ 0xFF
  1540. @int 绿色, 0x00 ~ 0xFF
  1541. @int 蓝色, 0x00 ~ 0xFF
  1542. @bool 是否翻转, true 翻转, false 不翻转. 默认翻转
  1543. @return int 颜色值
  1544. @usage
  1545. -- 本API支持多种模式, 参数数量分别是 1, 2, 3, 4
  1546. -- 1. 单参数形式, 24bit RGB值, swap = true, 推荐
  1547. local red = lcd.rgb565(0xFF0000)
  1548. local green = lcd.rgb565(0x00FF00)
  1549. local blue = lcd.rgb565(0x0000FF)
  1550. -- 2. 两参数形式, 24bit RGB值, 增加swap的设置
  1551. local red = lcd.rgb565(0xFF0000, true)
  1552. local green = lcd.rgb565(0x00FF00, true)
  1553. local blue = lcd.rgb565(0x0000FF, true)
  1554. -- 3. 三参数形式, 红/绿/蓝, 各8bit
  1555. local red = lcd.rgb565(0xFF, 0x00, 0x00)
  1556. local green = lcd.rgb565(0x00, 0xFF, 0x00)
  1557. local blue = lcd.rgb565(0x00, 0x00, 0xFF)
  1558. -- 4. 四参数形式, 红/绿/蓝, 各8bit, 增加swap的设置
  1559. local red = lcd.rgb565(0xFF, 0x00, 0x00, true)
  1560. local green = lcd.rgb565(0x00, 0xFF, 0x00, true)
  1561. local blue = lcd.rgb565(0x00, 0x00, 0xFF, true)
  1562. */
  1563. static int l_lcd_rgb565(lua_State* L) {
  1564. uint8_t r =0,g =0,b = 0;
  1565. uint8_t swap = 0;
  1566. uint16_t dst = 0;
  1567. int top = 0 ;
  1568. uint32_t rgb = 0;
  1569. top = lua_gettop(L);
  1570. if (top == 1 || top == 2) {
  1571. rgb = luaL_checkinteger(L, 1);
  1572. r = (uint8_t)((rgb >> 16 ) & 0xFF);
  1573. g = (uint8_t)((rgb >> 8 ) & 0xFF);
  1574. b = (uint8_t)((rgb >> 0 ) & 0xFF);
  1575. swap = (lua_isboolean(L, 2) && !lua_toboolean(L, 2)) ? 0U : 1U;
  1576. }
  1577. else if (top == 3 || top == 4) {
  1578. r = (uint8_t)luaL_checkinteger(L, 1);
  1579. g = (uint8_t)luaL_checkinteger(L, 2);
  1580. b = (uint8_t)luaL_checkinteger(L, 3);
  1581. swap = (lua_isboolean(L, 4) && !lua_toboolean(L, 4)) ? 0U : 1U;
  1582. }
  1583. else {
  1584. LLOGW("unkown args count %d", top);
  1585. dst = 0;
  1586. }
  1587. dst = (uint16_t)((r&0xF8)<<8) | (uint16_t)((g&0xFC)<<3) | (uint16_t)(b>>3);
  1588. if (swap) {
  1589. dst = ((dst >> 8) & 0xFF) + ((dst & 0xFF) << 8);
  1590. }
  1591. lua_pushinteger(L, dst);
  1592. return 1;
  1593. }
  1594. #ifdef LUAT_USE_UFONT
  1595. #include "luat_ufont.h"
  1596. static const int l_lcd_draw_utf8(lua_State *L) {
  1597. size_t sz = 0;
  1598. uint32_t letter = 0;
  1599. uint32_t str_offset;
  1600. int ret = 0;
  1601. uint16_t draw_offset = 0;
  1602. int draw_x = 0;
  1603. int draw_y = 0;
  1604. luat_font_char_desc_t desc = {0};
  1605. // 左上角坐标x,y
  1606. int x = luaL_checkinteger(L, 1);
  1607. int y = luaL_checkinteger(L, 2);
  1608. // 待绘制的字符串
  1609. const char* data = (const char*)luaL_checklstring(L, 3, &sz);
  1610. // 字体指针
  1611. lv_font_t* lfont = (lv_font_t*)lua_touserdata(L, 4);
  1612. if (lfont == NULL) {
  1613. LLOGW("draw without font");
  1614. return 0;
  1615. }
  1616. luat_font_header_t* font = (luat_font_header_t*)lfont->dsc;
  1617. // 是否填充背景
  1618. bool draw_bg = lua_isboolean(L, 5) ? lua_toboolean(L, 5) : true;
  1619. // 没内容, 不需要画了
  1620. if (sz == 0) {
  1621. // 直接返回原坐标
  1622. lua_pushinteger(L, x);
  1623. return 1;
  1624. }
  1625. // 没字体, 不需要画了
  1626. if (font == NULL) {
  1627. LLOGD("NULL font, skip draw");
  1628. // 直接返回原坐标
  1629. lua_pushinteger(L, x);
  1630. return 1;
  1631. }
  1632. // 超边界了没? 超了就没必要绘制了
  1633. if (lcd_dft_conf->h < y || lcd_dft_conf->w < x) {
  1634. //LLOGD("draw y %d h % font->line_height %d", y, lcd_dft_conf->h, font->line_height);
  1635. // 直接返回原坐标
  1636. lua_pushinteger(L, x);
  1637. return 1;
  1638. }
  1639. luat_color_t* buff = NULL;
  1640. if (draw_bg)
  1641. buff = luat_heap_malloc(font->line_height * font->line_height * 2);
  1642. // if (buff == NULL)
  1643. // return 0;
  1644. int offset = 0;
  1645. uint8_t *data_ptr = data;
  1646. uint8_t utf8_state = 0;
  1647. uint16_t utf8_tmp = 0;
  1648. uint16_t utf8_out = 0;
  1649. luat_color_t color = FORE_COLOR;
  1650. for (size_t i = 0; i < sz; i++)
  1651. {
  1652. utf8_out = luat_utf8_next(data[i], &utf8_state, &utf8_tmp);
  1653. if (utf8_out == 0x0ffff)
  1654. break; // 结束了
  1655. if (utf8_out == 0x0fffe)
  1656. continue; // 没读完一个字符,继续下一个循环
  1657. letter = (uint32_t)utf8_out;
  1658. //LLOGD("draw letter %04X", letter);
  1659. int ret = luat_font_get_bitmap(font, &desc, letter);
  1660. if (ret != 0) {
  1661. LLOGD("not such char in font");
  1662. draw_offset += font->line_height / 2; // 找不到字符, 默认跳过半个字
  1663. continue;
  1664. }
  1665. offset = 0;
  1666. // desc.data = tmp;
  1667. memset(buff, 0, font->line_height * font->line_height * 2);
  1668. draw_x = x + draw_offset;
  1669. draw_offset += desc.char_w;
  1670. if (draw_x >= 0 && draw_x + desc.char_w <= lcd_dft_conf->w) {
  1671. //if (lcd_dft_conf->buff == NULL) {
  1672. for (size_t j = 0; j < font->line_height; j++)
  1673. {
  1674. //LLOGD("draw char pix line %d", i);
  1675. for (size_t k = 0; k < desc.char_w; k++)
  1676. {
  1677. if ((desc.data[offset / 8] >> (7 - (offset % 8))) & 0x01) {
  1678. color = FORE_COLOR;
  1679. if (buff)
  1680. buff[offset] = FORE_COLOR;
  1681. else
  1682. luat_lcd_draw_point(lcd_dft_conf, draw_x + k, y + j, FORE_COLOR);
  1683. //LLOGD("draw char pix mark %d", offset);
  1684. }
  1685. else {
  1686. if (buff)
  1687. buff[offset] = BACK_COLOR;
  1688. //LLOGD("draw char pix offset %d color %04X", offset, FORE_COLOR);
  1689. }
  1690. offset ++;
  1691. }
  1692. }
  1693. //LLOGD("luat_lcd_draw %d %d %d %d", draw_x, y, draw_x + desc.char_w, y + font->line_height);
  1694. luat_lcd_draw(lcd_dft_conf, draw_x, y, draw_x + desc.char_w - 1, y + font->line_height - 1, buff);
  1695. //}
  1696. //else {
  1697. //
  1698. //}
  1699. }
  1700. }
  1701. if (buff)
  1702. luat_heap_free(buff);
  1703. lcd_auto_flush(lcd_dft_conf);
  1704. lua_pushinteger(L, draw_x + desc.char_w);
  1705. return 1;
  1706. }
  1707. #endif
  1708. /*
  1709. 硬件lcd qspi接口配置,需要在lcd.init前配置好
  1710. @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)
  1711. @int lcd命令模式下的qspi指令
  1712. @int lcd数据模式下,1线地址,4线数据的qspi指令,
  1713. @int lcd数据模式下,4线地址,4线数据的qspi指令,可以留空,如果存在,发送数据时优先使用这个模式
  1714. @int 帧同步时的地址值,只有无ram的屏幕需要,如果能用0x2c发送数据则不需要这个参数
  1715. @int 行同步时的指令,一般情况和命令模式下的指令一致,只有无ram的屏幕需要,如果能用0x2c发送数据则不需要这个参数
  1716. @int 行同步时的地址值,只有无ram的屏幕需要,如果能用0x2c发送数据则不需要这个参数
  1717. @return nil
  1718. @usage
  1719. -- sh8601z驱动ic所需的qspi配置
  1720. lcd.qspi(0x02, 0x32, 0x12)
  1721. -- jd9261t驱动ic所需的qspi配置
  1722. lcd.qspi(0xde, 0xde, nil, 0x61, 0xde, 0x60)
  1723. -- CO5300驱动ic所需的qspi配置
  1724. lcd.qspi(0x02, 0x32, 0x12)
  1725. */
  1726. static int l_lcd_qspi_config(lua_State* L)
  1727. {
  1728. luat_lcd_qspi_conf_t qspi_config = {0};
  1729. qspi_config.write_1line_cmd = luaL_optinteger(L, 1, 0x02);
  1730. qspi_config.write_4line_cmd = luaL_optinteger(L, 2, 0x32);
  1731. qspi_config.write_4line_data = luaL_optinteger(L, 3, 0);
  1732. qspi_config.vsync_reg = luaL_optinteger(L, 4, 0);
  1733. qspi_config.hsync_cmd = luaL_optinteger(L, 5, 0);
  1734. qspi_config.hsync_reg = luaL_optinteger(L, 6, 0);
  1735. luat_lcd_qspi_config(NULL, &qspi_config);
  1736. return 0;
  1737. }
  1738. /*
  1739. 用户使用脚本初始化LCD完成后,必须调用本API
  1740. @api lcd.user_done()
  1741. @return nil
  1742. */
  1743. static int l_lcd_user_ctrl_done(lua_State* L)
  1744. {
  1745. lcd_dft_conf->is_init_done = 1;
  1746. if (LUAT_LCD_IM_QSPI_MODE == lcd_dft_conf->interface_mode) {
  1747. if (luat_lcd_qspi_is_no_ram(lcd_dft_conf)) {
  1748. luat_lcd_qspi_auto_flush_on_off(lcd_dft_conf, 1);
  1749. }
  1750. }
  1751. return 0;
  1752. }
  1753. #include "rotable2.h"
  1754. static const rotable_Reg_t reg_lcd[] =
  1755. {
  1756. { "init", ROREG_FUNC(l_lcd_init)},
  1757. { "clear", ROREG_FUNC(l_lcd_clear)},
  1758. { "fill", ROREG_FUNC(l_lcd_draw_fill)},
  1759. { "drawPoint", ROREG_FUNC(l_lcd_draw_point)},
  1760. { "drawLine", ROREG_FUNC(l_lcd_draw_line)},
  1761. { "drawRectangle", ROREG_FUNC(l_lcd_draw_rectangle)},
  1762. { "drawCircle", ROREG_FUNC(l_lcd_draw_circle)},
  1763. { "drawQrcode", ROREG_FUNC(l_lcd_drawQrcode)},
  1764. { "drawStr", ROREG_FUNC(l_lcd_draw_str)},
  1765. { "flush", ROREG_FUNC(l_lcd_flush)},
  1766. { "setupBuff", ROREG_FUNC(l_lcd_setup_buff)},
  1767. { "autoFlush", ROREG_FUNC(l_lcd_auto_flush)},
  1768. { "setFont", ROREG_FUNC(l_lcd_set_font)},
  1769. { "setDefault", ROREG_FUNC(l_lcd_set_default)},
  1770. { "getDefault", ROREG_FUNC(l_lcd_get_default)},
  1771. { "getSize", ROREG_FUNC(l_lcd_get_size)},
  1772. { "drawXbm", ROREG_FUNC(l_lcd_drawxbm)},
  1773. { "close", ROREG_FUNC(l_lcd_close)},
  1774. { "on", ROREG_FUNC(l_lcd_display_on)},
  1775. { "off", ROREG_FUNC(l_lcd_display_off)},
  1776. { "sleep", ROREG_FUNC(l_lcd_sleep)},
  1777. { "wakeup", ROREG_FUNC(l_lcd_wakeup)},
  1778. { "invon", ROREG_FUNC(l_lcd_inv_on)},
  1779. { "invoff", ROREG_FUNC(l_lcd_inv_off)},
  1780. { "cmd", ROREG_FUNC(l_lcd_write_cmd)},
  1781. { "data", ROREG_FUNC(l_lcd_write_data)},
  1782. { "setColor", ROREG_FUNC(l_lcd_set_color)},
  1783. { "draw", ROREG_FUNC(l_lcd_draw)},
  1784. { "rgb565", ROREG_FUNC(l_lcd_rgb565)},
  1785. { "qspi", ROREG_FUNC(l_lcd_qspi_config)},
  1786. { "user_done", ROREG_FUNC(l_lcd_user_ctrl_done)},
  1787. #ifdef LUAT_USE_UFONT
  1788. { "drawUTF8", ROREG_FUNC(l_lcd_draw_utf8)},
  1789. #endif
  1790. #ifdef LUAT_USE_TJPGD
  1791. { "showImage", ROREG_FUNC(l_lcd_showimage)},
  1792. #endif
  1793. #ifdef LUAT_USE_GTFONT
  1794. { "drawGtfontGb2312", ROREG_FUNC(l_lcd_draw_gtfont_gbk)},
  1795. { "drawGtfontGb2312Gray", ROREG_FUNC(l_lcd_draw_gtfont_gbk_gray)},
  1796. { "drawGtfontGbk", ROREG_FUNC(l_lcd_draw_gtfont_gbk)},
  1797. { "drawGtfontGbkGray", ROREG_FUNC(l_lcd_draw_gtfont_gbk_gray)},
  1798. #ifdef LUAT_USE_GTFONT_UTF8
  1799. { "drawGtfontUtf8", ROREG_FUNC(l_lcd_draw_gtfont_utf8)},
  1800. { "drawGtfontUtf8Gray", ROREG_FUNC(l_lcd_draw_gtfont_utf8_gray)},
  1801. #endif // LUAT_USE_GTFONT_UTF8
  1802. #endif // LUAT_USE_GTFONT
  1803. // 默认只带英文12号字体
  1804. //@const font_opposansm12 font 12号字体
  1805. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  1806. #ifdef USE_U8G2_OPPOSANSM_ENGLISH
  1807. //@const font_unifont_t_symbols font 符号字体
  1808. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  1809. //@const font_open_iconic_weather_6x_t font 天气字体
  1810. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  1811. //@const font_opposansm16 font 16号字体
  1812. //@const font_opposansm18 font 18号字体
  1813. //@const font_opposansm20 font 20号字体
  1814. //@const font_opposansm22 font 22号字体
  1815. //@const font_opposansm24 font 24号字体
  1816. //@const font_opposansm32 font 32号字体
  1817. { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
  1818. { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
  1819. { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
  1820. { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
  1821. { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
  1822. { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
  1823. #endif
  1824. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  1825. //@const font_opposansm12_chinese font 12号中文字体
  1826. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  1827. #endif
  1828. #ifdef USE_U8G2_OPPOSANSM14_CHINESE
  1829. //@const font_opposansm14_chinese font 14号中文字体
  1830. { "font_opposansm14_chinese", ROREG_PTR((void*)u8g2_font_opposansm14_chinese)},
  1831. #endif
  1832. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  1833. //@const font_opposansm16_chinese font 16号中文字体
  1834. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  1835. #endif
  1836. #ifdef USE_U8G2_OPPOSANSM18_CHINESE
  1837. //@const font_opposansm18_chinese font 18号中文字体
  1838. { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
  1839. #endif
  1840. #ifdef USE_U8G2_OPPOSANSM20_CHINESE
  1841. //@const font_opposansm20_chinese font 20号中文字体
  1842. { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
  1843. #endif
  1844. #ifdef USE_U8G2_OPPOSANSM22_CHINESE
  1845. //@const font_opposansm22_chinese font 22号中文字体
  1846. { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
  1847. #endif
  1848. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  1849. //@const font_opposansm24_chinese font 24号中文字体
  1850. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  1851. #endif
  1852. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  1853. //@const font_opposansm32_chinese font 32号中文字体
  1854. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  1855. #endif
  1856. #ifdef USE_U8G2_SARASA_ENGLISH
  1857. { "font_sarasa_m12_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m12_ascii)},
  1858. { "font_sarasa_m14_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m14_ascii)},
  1859. { "font_sarasa_m16_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m16_ascii)},
  1860. { "font_sarasa_m18_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m18_ascii)},
  1861. { "font_sarasa_m20_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m20_ascii)},
  1862. { "font_sarasa_m22_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m22_ascii)},
  1863. //再大的很少用到先不加了
  1864. #endif
  1865. // #ifdef USE_U8G2_SARASA_M8_CHINESE
  1866. // { "font_sarasa_m8_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m8_chinese)},
  1867. // #endif
  1868. #ifdef USE_U8G2_SARASA_M10_CHINESE
  1869. { "font_sarasa_m10_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m10_chinese)},
  1870. #endif
  1871. #ifdef USE_U8G2_SARASA_M12_CHINESE
  1872. { "font_sarasa_m12_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m12_chinese)},
  1873. #endif
  1874. #ifdef USE_U8G2_SARASA_M14_CHINESE
  1875. { "font_sarasa_m14_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m14_chinese)},
  1876. #endif
  1877. #ifdef USE_U8G2_SARASA_M16_CHINESE
  1878. { "font_sarasa_m16_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m16_chinese)},
  1879. #endif
  1880. #ifdef USE_U8G2_SARASA_M18_CHINESE
  1881. { "font_sarasa_m18_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m18_chinese)},
  1882. #endif
  1883. #ifdef USE_U8G2_SARASA_M20_CHINESE
  1884. { "font_sarasa_m20_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m20_chinese)},
  1885. #endif
  1886. #ifdef USE_U8G2_SARASA_M22_CHINESE
  1887. { "font_sarasa_m22_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m22_chinese)},
  1888. #endif
  1889. #ifdef USE_U8G2_SARASA_M24_CHINESE
  1890. { "font_sarasa_m24_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m24_chinese)},
  1891. #endif
  1892. #ifdef USE_U8G2_SARASA_M26_CHINESE
  1893. { "font_sarasa_m26_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m26_chinese)},
  1894. #endif
  1895. #ifdef USE_U8G2_SARASA_M28_CHINESE
  1896. { "font_sarasa_m28_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m28_chinese)},
  1897. #endif
  1898. { "set_direction", ROREG_FUNC(l_lcd_set_direction)},
  1899. //@const direction_0 int 0°方向命令
  1900. { "direction_0", ROREG_INT(LUAT_LCD_ROTATE_0)},
  1901. //@const direction_90 int 90°方向命令
  1902. { "direction_90", ROREG_INT(LUAT_LCD_ROTATE_90)},
  1903. //@const direction_180 int 180°方向命令
  1904. { "direction_180", ROREG_INT(LUAT_LCD_ROTATE_180)},
  1905. //@const direction_270 int 270°方向命令
  1906. { "direction_270", ROREG_INT(LUAT_LCD_ROTATE_270)},
  1907. //@const SPI 硬件spi device lcd驱动
  1908. { "SPI", ROREG_INT(LUAT_LCD_SPI_DEVICE)},
  1909. //@const HWID_0 硬件lcd驱动id0 (根据芯片支持选择)
  1910. { "HWID_0", ROREG_INT(LUAT_LCD_HW_ID_0)},
  1911. //@const RGB 硬件RGB lcd驱动 (根据芯片支持选择)
  1912. { "RGB", ROREG_INT(LUAT_LCD_PORT_RGB)},
  1913. // //@const ARM2D 硬件ARM2D lcd驱动 (根据芯片支持选择)
  1914. // { "ARM2D", ROREG_INT(LUAT_LCD_PORT_ARM2D)},
  1915. // //@const DMA2D 硬件DMA2D lcd驱动 (根据芯片支持选择)
  1916. // { "DMA2D", ROREG_INT(LUAT_LCD_PORT_DMA2D)},
  1917. //@const WIRE_3_BIT_9_INTERFACE_I 三线spi 9bit 模式I
  1918. { "WIRE_3_BIT_9_INTERFACE_I", ROREG_INT(LUAT_LCD_IM_3_WIRE_9_BIT_INTERFACE_I)},
  1919. //@const WIRE_4_BIT_8_INTERFACE_I 四线spi 8bit 模式I
  1920. { "WIRE_4_BIT_8_INTERFACE_I", ROREG_INT(LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_I)},
  1921. //@const WIRE_3_BIT_9_INTERFACE_II 三线spi 9bit 模式II
  1922. { "WIRE_3_BIT_9_INTERFACE_II", ROREG_INT(LUAT_LCD_IM_3_WIRE_9_BIT_INTERFACE_II)},
  1923. //@const WIRE_4_BIT_8_INTERFACE_II 四线spi 8bit 模式II
  1924. { "WIRE_4_BIT_8_INTERFACE_II", ROREG_INT(LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_II)},
  1925. //@const DATA_2_LANE spi双通道模式
  1926. { "DATA_2_LANE", ROREG_INT(LUAT_LCD_IM_2_DATA_LANE)},
  1927. //@const QSPI_MODE QSPI模式
  1928. { "QSPI_MODE", ROREG_INT(LUAT_LCD_IM_QSPI_MODE)},
  1929. //@const 8080_MODE 8080模式
  1930. { "8080_MODE", ROREG_INT(LUAT_LCD_IM_8080_MODE)},
  1931. {NULL, ROREG_INT(0)}
  1932. };
  1933. LUAMOD_API int luaopen_lcd( lua_State *L ) {
  1934. luat_newlib2(L, reg_lcd);
  1935. return 1;
  1936. }