luat_lib_lcd.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  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->acc_hw = 0xFF;
  129. conf->bpp = 16;
  130. conf->lcd_cs_pin = LUAT_GPIO_NONE;
  131. conf->pin_dc = LUAT_GPIO_NONE;
  132. conf->pin_pwr = LUAT_GPIO_NONE;
  133. conf->interface_mode = LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_I;
  134. if (lua_type(L, 3) == LUA_TUSERDATA){
  135. // 如果是SPI Device模式, 就可能出现变量为local, 从而在某个时间点被GC掉的可能性
  136. conf->lcd_spi_device = (luat_spi_device_t*)lua_touserdata(L, 3);
  137. lua_pushvalue(L, 3);
  138. // 所以, 直接引用之外, 再加上强制引用, 避免被GC
  139. // 鉴于LCD不太可能重复初始化, 引用也没什么问题
  140. conf->lcd_spi_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  141. spi_device = 1;
  142. }
  143. const char* tp = luaL_checklstring(L, 1, &len);
  144. int16_t s_index = -1;//第几个屏幕,-1表示没匹配到
  145. for(int i = 0; i < 100; i++){
  146. if (strlen(lcd_regs[i].name) == 0)
  147. break;
  148. if(strcmp(lcd_regs[i].name,tp) == 0){
  149. s_index = i;
  150. break;
  151. }
  152. }
  153. if (s_index != -1) {
  154. LLOGD("ic support: %s",tp);
  155. if (lua_gettop(L) > 1) {
  156. conf->opts = (struct luat_lcd_opts *)lcd_regs[s_index].lcd_opts;
  157. lua_settop(L, 2); // 丢弃多余的参数
  158. lua_pushstring(L, "port");
  159. int port = lua_gettable(L, 2);
  160. if (LUA_TNUMBER == port) {
  161. conf->port = luaL_checkinteger(L, -1);
  162. }else if(LUA_TSTRING == port){
  163. size_t len;
  164. const char *lcd_port = luaL_checklstring(L, -1,&len);
  165. if(memcmp("device", lcd_port, len) == 0){
  166. conf->port = LUAT_LCD_SPI_DEVICE;
  167. }else if(memcmp("rgb", lcd_port, len) == 0){
  168. conf->port = LUAT_LCD_PORT_RGB;
  169. }else{
  170. LLOGE("port %s is not support ",lcd_port);
  171. goto end;
  172. }
  173. }
  174. if (spi_device == 1 && conf->port != LUAT_LCD_SPI_DEVICE) {
  175. LLOGE("port is not device but find luat_spi_device_t");
  176. goto end;
  177. }else if (spi_device == 0 && conf->port == LUAT_LCD_SPI_DEVICE){
  178. LLOGE("port is device but not find luat_spi_device_t");
  179. goto end;
  180. }
  181. if (conf->port < LUAT_LCD_HW_ID_0 || conf->port == LUAT_LCD_SPI_DEVICE){
  182. conf->endianness_swap = 1;
  183. }
  184. lua_pop(L, 1);
  185. lua_pushstring(L, "endianness_swap");
  186. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  187. conf->endianness_swap = lua_toboolean(L, -1);
  188. }
  189. lua_pop(L, 1);
  190. lua_pushstring(L, "rb_swap");
  191. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  192. conf->opts->rb_swap = lua_toboolean(L, -1);
  193. }
  194. lua_pop(L, 1);
  195. lua_pushstring(L, "pin_dc");
  196. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  197. conf->pin_dc = luaL_checkinteger(L, -1);
  198. }
  199. lua_pop(L, 1);
  200. lua_pushstring(L, "pin_pwr");
  201. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  202. conf->pin_pwr = luaL_checkinteger(L, -1);
  203. }
  204. lua_pop(L, 1);
  205. lua_pushstring(L, "pin_rst");
  206. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  207. conf->pin_rst = luaL_checkinteger(L, -1);
  208. }
  209. lua_pop(L, 1);
  210. lua_pushstring(L, "direction");
  211. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  212. conf->direction = luaL_checkinteger(L, -1);
  213. }
  214. lua_pop(L, 1);
  215. lua_pushstring(L, "direction0");
  216. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  217. conf->opts->direction0 = luaL_checkinteger(L, -1);
  218. }
  219. lua_pop(L, 1);
  220. lua_pushstring(L, "direction90");
  221. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  222. conf->opts->direction90 = luaL_checkinteger(L, -1);
  223. }
  224. lua_pop(L, 1);
  225. lua_pushstring(L, "direction180");
  226. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  227. conf->opts->direction180 = luaL_checkinteger(L, -1);
  228. }
  229. lua_pop(L, 1);
  230. lua_pushstring(L, "direction270");
  231. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  232. conf->opts->direction270 = luaL_checkinteger(L, -1);
  233. }
  234. lua_pop(L, 1);
  235. lua_pushstring(L, "w");
  236. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  237. conf->w = luaL_checkinteger(L, -1);
  238. }
  239. lua_pop(L, 1);
  240. lua_pushstring(L, "h");
  241. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  242. conf->h = luaL_checkinteger(L, -1);
  243. }
  244. lua_pop(L, 1);
  245. conf->buffer_size = (conf->w * conf->h) * sizeof(luat_color_t);
  246. lua_pushstring(L, "xoffset");
  247. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  248. conf->xoffset = luaL_checkinteger(L, -1);
  249. }
  250. lua_pop(L, 1);
  251. lua_pushstring(L, "yoffset");
  252. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  253. conf->yoffset = luaL_checkinteger(L, -1);
  254. }
  255. lua_pop(L, 1);
  256. lua_pushstring(L, "sleepcmd");
  257. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  258. conf->opts->sleep_cmd = luaL_checkinteger(L, -1);
  259. }
  260. lua_pop(L, 1);
  261. lua_pushstring(L, "wakecmd");
  262. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  263. conf->opts->wakeup_cmd = luaL_checkinteger(L, -1);
  264. }
  265. lua_pop(L, 1);
  266. lua_pushstring(L, "interface_mode");
  267. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  268. conf->interface_mode = luaL_checkinteger(L, -1);
  269. }
  270. lua_pop(L, 1);
  271. lua_pushstring(L, "bus_speed");
  272. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  273. conf->bus_speed = luaL_checkinteger(L, -1);
  274. }
  275. lua_pop(L, 1);
  276. lua_pushstring(L, "flush_rate");
  277. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  278. conf->flush_rate = luaL_checkinteger(L, -1);
  279. }
  280. lua_pop(L, 1);
  281. lua_pushstring(L, "hbp");
  282. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  283. conf->hbp = luaL_checkinteger(L, -1);
  284. }
  285. lua_pop(L, 1);
  286. lua_pushstring(L, "hspw");
  287. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  288. conf->hspw = luaL_checkinteger(L, -1);
  289. }
  290. lua_pop(L, 1);
  291. lua_pushstring(L, "hfp");
  292. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  293. conf->hfp = luaL_checkinteger(L, -1);
  294. }
  295. lua_pop(L, 1);
  296. lua_pushstring(L, "vbp");
  297. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  298. conf->vbp = luaL_checkinteger(L, -1);
  299. }
  300. lua_pop(L, 1);
  301. lua_pushstring(L, "vspw");
  302. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  303. conf->vspw = luaL_checkinteger(L, -1);
  304. }
  305. lua_pop(L, 1);
  306. lua_pushstring(L, "vfp");
  307. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  308. conf->vfp = luaL_checkinteger(L, -1);
  309. }
  310. lua_pop(L, 1);
  311. lua_pushstring(L, "pin_cs");
  312. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  313. conf->lcd_cs_pin = luaL_checkinteger(L, -1);
  314. }
  315. lua_pop(L, 1);
  316. }
  317. if (s_index == 0){
  318. unsigned int cmd = 0;
  319. lua_pushstring(L, "initcmd");
  320. lua_gettable(L, 2);
  321. if (lua_istable(L, -1)) {
  322. conf->opts->init_cmds_len = lua_rawlen(L, -1);
  323. conf->opts->init_cmds = luat_heap_malloc( conf->opts->init_cmds_len * sizeof(uint16_t));
  324. for (size_t i = 1; i <= conf->opts->init_cmds_len; i++){
  325. lua_geti(L, -1, i);
  326. cmd = luaL_checkinteger(L, -1);
  327. conf->opts->init_cmds[i-1] = ((cmd >> 8) & 0xFF00) | (cmd & 0xFF);
  328. lua_pop(L, 1);
  329. }
  330. }else if(lua_isstring(L, -1)){
  331. size_t len;
  332. const char *fail_name = luaL_checklstring(L, -1, &len);
  333. FILE* fd = (FILE *)luat_fs_fopen(fail_name, "rb");
  334. conf->opts->init_cmds_len = 0;
  335. if (fd){
  336. #define INITCMD_BUFF_SIZE 128
  337. char init_cmd_buff[INITCMD_BUFF_SIZE] ;
  338. conf->opts->init_cmds = luat_heap_malloc(sizeof(uint16_t));
  339. while (1) {
  340. memset(init_cmd_buff, 0, INITCMD_BUFF_SIZE);
  341. int readline_len = luat_fs_readline(init_cmd_buff, INITCMD_BUFF_SIZE-1, fd);
  342. if (readline_len < 1)
  343. break;
  344. if (memcmp(init_cmd_buff, "#", 1)==0){
  345. continue;
  346. }
  347. char *token = strtok(init_cmd_buff, ",");
  348. if (sscanf(token,"%x",&cmd) < 1){
  349. continue;
  350. }
  351. conf->opts->init_cmds_len = conf->opts->init_cmds_len + 1;
  352. conf->opts->init_cmds = luat_heap_realloc(conf->opts->init_cmds,conf->opts->init_cmds_len * sizeof(uint16_t));
  353. conf->opts->init_cmds[conf->opts->init_cmds_len-1]=((cmd >> 8) & 0xFF00) | (cmd & 0xFF);;
  354. while( token != NULL ) {
  355. token = strtok(NULL, ",");
  356. if (sscanf(token,"%x",&cmd) < 1){
  357. break;
  358. }
  359. conf->opts->init_cmds_len = conf->opts->init_cmds_len + 1;
  360. conf->opts->init_cmds = luat_heap_realloc(conf->opts->init_cmds,conf->opts->init_cmds_len * sizeof(uint16_t));
  361. conf->opts->init_cmds[conf->opts->init_cmds_len-1]=((cmd >> 8) & 0xFF00) | (cmd & 0xFF);;
  362. }
  363. }
  364. conf->opts->init_cmds[conf->opts->init_cmds_len]= 0;
  365. luat_fs_fclose(fd);
  366. }else{
  367. LLOGE("init_cmd fail open error");
  368. }
  369. }
  370. lua_pop(L, 1);
  371. }
  372. // 默认自动flush,即使没有buff
  373. conf->auto_flush = 1;
  374. #ifdef LUAT_USE_LCD_SDL2
  375. extern const luat_lcd_opts_t lcd_opts_sdl2;
  376. conf->opts = &lcd_opts_sdl2;
  377. #endif
  378. if (conf->port == LUAT_LCD_HW_ID_0) luat_lcd_IF_init(conf);
  379. #if defined LUAT_USE_LCD_SERVICE
  380. if (init_in_service) {
  381. ret = luat_lcd_init_in_service(conf);
  382. } else
  383. #endif
  384. {
  385. ret = luat_lcd_init(conf);
  386. }
  387. if (ret != 0) {
  388. LLOGE("lcd init fail %d", ret);
  389. luat_heap_free(conf);
  390. lua_pushboolean(L, 0);
  391. return 0;
  392. }
  393. // 初始化OK, 配置额外的参数
  394. lcd_dft_conf = conf;
  395. u8g2_SetFont(&(conf->luat_lcd_u8g2), u8g2_font_opposansm12);
  396. u8g2_SetFontMode(&(conf->luat_lcd_u8g2), 0);
  397. u8g2_SetFontDirection(&(conf->luat_lcd_u8g2), 0);
  398. lua_pushboolean(L, 1);
  399. return 1;
  400. }
  401. LLOGE("ic not support: %s",tp);
  402. end:
  403. lua_pushboolean(L, 0);
  404. luat_heap_free(conf);
  405. return 1;
  406. }
  407. /*
  408. 关闭lcd显示屏
  409. @api lcd.close()
  410. @usage
  411. -- 关闭lcd
  412. lcd.close()
  413. */
  414. static int l_lcd_close(lua_State* L) {
  415. int ret = luat_lcd_close(lcd_dft_conf);
  416. lua_pushboolean(L, ret == 0 ? 1 : 0);
  417. return 1;
  418. }
  419. /*
  420. 开启lcd显示屏背光
  421. @api lcd.on()
  422. @usage
  423. -- 开启lcd显示屏背光
  424. lcd.on()
  425. */
  426. static int l_lcd_display_on(lua_State* L) {
  427. int ret = luat_lcd_display_on(lcd_dft_conf);
  428. lua_pushboolean(L, ret == 0 ? 1 : 0);
  429. return 1;
  430. }
  431. /*
  432. 关闭lcd显示屏背光
  433. @api lcd.off()
  434. @usage
  435. -- 关闭lcd显示屏背光
  436. lcd.off()
  437. */
  438. static int l_lcd_display_off(lua_State* L) {
  439. int ret = luat_lcd_display_off(lcd_dft_conf);
  440. lua_pushboolean(L, ret == 0 ? 1 : 0);
  441. return 1;
  442. }
  443. /*
  444. lcd睡眠
  445. @api lcd.sleep()
  446. @usage
  447. -- lcd睡眠
  448. lcd.sleep()
  449. */
  450. static int l_lcd_sleep(lua_State* L) {
  451. int ret = luat_lcd_sleep(lcd_dft_conf);
  452. lua_pushboolean(L, ret == 0 ? 1 : 0);
  453. return 1;
  454. }
  455. /*
  456. lcd唤醒
  457. @api lcd.wakeup()
  458. @usage
  459. -- lcd唤醒
  460. lcd.wakeup()
  461. */
  462. static int l_lcd_wakeup(lua_State* L) {
  463. int ret = luat_lcd_wakeup(lcd_dft_conf);
  464. lua_pushboolean(L, ret == 0 ? 1 : 0);
  465. return 1;
  466. }
  467. /*
  468. lcd反显
  469. @api lcd.invon()
  470. @usage
  471. -- lcd反显
  472. lcd.invon()
  473. */
  474. static int l_lcd_inv_on(lua_State* L) {
  475. int ret = luat_lcd_inv_on(lcd_dft_conf);
  476. lua_pushboolean(L, ret == 0 ? 1 : 0);
  477. return 1;
  478. }
  479. /*
  480. lcd反显关闭
  481. @api lcd.invoff()
  482. @usage
  483. -- lcd反显关闭
  484. lcd.invoff()
  485. */
  486. static int l_lcd_inv_off(lua_State* L) {
  487. int ret = luat_lcd_inv_off(lcd_dft_conf);
  488. lua_pushboolean(L, ret == 0 ? 1 : 0);
  489. return 1;
  490. }
  491. /*
  492. lcd命令
  493. @api lcd.cmd(cmd, param, param_len)
  494. @int lcd命令模式下的命令值
  495. @int/zbuff lcd命令模式下的参数值,如果只有1个参数,可以用int,如果有多个,使用zbuff传入
  496. @int 参数长度,如果上一个参数是int,则忽略长度
  497. @return boolean
  498. @usage
  499. -- lcd命令
  500. lcd.cmd(0x21)
  501. lcd.cmd(0x21)
  502. */
  503. static int l_lcd_write_cmd(lua_State* L) {
  504. uint8_t param;
  505. uint32_t param_len = 0;
  506. const uint8_t *data = NULL;
  507. if (lua_isinteger(L, 2))
  508. {
  509. param = lua_tointeger(L, 2);
  510. param_len = 1;
  511. data = &param;
  512. }
  513. else if (lua_isuserdata(L, 2)) {
  514. // zbuff
  515. luat_zbuff_t* buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  516. data = (const uint8_t*)buff->addr;
  517. param_len = luaL_optinteger(L, 3, buff->used);
  518. }else if(lua_isstring(L, 2)){
  519. data = (const uint8_t*)luaL_checklstring(L, 2, &param_len);
  520. }
  521. int ret = lcd_write_cmd_data(lcd_dft_conf,(uint8_t)luaL_checkinteger(L, 1), data, param_len);
  522. lua_pushboolean(L, ret == 0 ? 1 : 0);
  523. return 1;
  524. }
  525. /*
  526. lcd数据
  527. @api lcd.data(data)
  528. @int data
  529. @usage
  530. -- lcd数据
  531. lcd.data(0x21)
  532. */
  533. static int l_lcd_write_data(lua_State* L) {
  534. int ret = lcd_write_data(lcd_dft_conf,(const uint8_t)luaL_checkinteger(L, 1));
  535. lua_pushboolean(L, ret == 0 ? 1 : 0);
  536. return 1;
  537. }
  538. /*
  539. lcd颜色设置
  540. @api lcd.setColor(back,fore)
  541. @int 背景色
  542. @int 前景色
  543. @usage
  544. -- lcd颜色设置
  545. lcd.setColor(0xFFFF,0x0000)
  546. */
  547. static int l_lcd_set_color(lua_State* L) {
  548. luat_color_t back,fore;
  549. back = (luat_color_t)luaL_checkinteger(L, 1);
  550. fore = (luat_color_t)luaL_checkinteger(L, 2);
  551. int ret = luat_lcd_set_color(back, fore);
  552. lua_pushboolean(L, ret == 0 ? 1 : 0);
  553. return 1;
  554. }
  555. static int l_lcd_set_direction(lua_State* L) {
  556. int ret = luat_lcd_set_direction(lcd_dft_conf, (uint8_t)luaL_checkinteger(L, 1));
  557. lua_pushboolean(L, ret == 0 ? 1 : 0);
  558. return 1;
  559. }
  560. /*
  561. lcd颜色填充
  562. @api lcd.draw(x1, y1, x2, y2,color)
  563. @int 左上边缘的X位置.
  564. @int 左上边缘的Y位置.
  565. @int 右下边缘的X位置(color传入framebuff的zbuff时可选,默认为x+图片宽度).
  566. @int 右下边缘的Y位置(color传入framebuff的zbuff时可选,默认为y+图片高度).
  567. @string 字符串或zbuff对象
  568. @usage
  569. -- lcd颜色填充
  570. local buff = zbuff.create({201,1,16},0x001F)
  571. lcd.draw(20,30,220,30,buff)
  572. */
  573. static int l_lcd_draw(lua_State* L) {
  574. int16_t x1, y1, x2, y2;
  575. int ret;
  576. luat_color_t *color = NULL;
  577. x1 = luaL_checkinteger(L, 1);
  578. y1 = luaL_checkinteger(L, 2);
  579. if (lcd_dft_conf == NULL) {
  580. LLOGE("lcd not init");
  581. return 0;
  582. }
  583. if (lua_isuserdata(L, 5)) {
  584. luat_zbuff_t *zbuff = luaL_checkudata(L, 5, LUAT_ZBUFF_TYPE);
  585. if (zbuff->width && zbuff->height){
  586. x2 = x1+zbuff->width-1;
  587. y2 = y1+zbuff->height-1;
  588. }else{
  589. x2 = luaL_checkinteger(L, 3);
  590. y2 = luaL_checkinteger(L, 4);
  591. if (x2-x1<zbuff->width-1 || y2-y1<zbuff->height-1) {
  592. LLOGE("The display area is too small");
  593. return 0;
  594. }
  595. }
  596. color = (luat_color_t *)zbuff->addr;
  597. }
  598. else if(lua_isstring(L, 5)) {
  599. x2 = luaL_checkinteger(L, 3);
  600. y2 = luaL_checkinteger(L, 4);
  601. color = (luat_color_t *)luaL_checkstring(L, 5);
  602. }
  603. else {
  604. return 0;
  605. }
  606. ret = luat_lcd_draw(lcd_dft_conf, x1, y1, x2, y2, color);
  607. lcd_auto_flush(lcd_dft_conf);
  608. lua_pushboolean(L, ret == 0 ? 1 : 0);
  609. return 1;
  610. }
  611. /*
  612. lcd清屏
  613. @api lcd.clear(color)
  614. @int 屏幕颜色 可选参数,默认背景色
  615. @usage
  616. -- lcd清屏
  617. lcd.clear()
  618. */
  619. static int l_lcd_clear(lua_State* L) {
  620. //size_t len = 0;
  621. luat_color_t color = BACK_COLOR;
  622. if (lua_gettop(L) > 0)
  623. color = (luat_color_t)luaL_checkinteger(L, 1);
  624. int ret = luat_lcd_clear(lcd_dft_conf, color);
  625. lcd_auto_flush(lcd_dft_conf);
  626. lua_pushboolean(L, ret == 0 ? 1 : 0);
  627. return 1;
  628. }
  629. /*
  630. lcd颜色填充
  631. @api lcd.fill(x1, y1, x2, y2,color)
  632. @int 左上边缘的X位置.
  633. @int 左上边缘的Y位置.
  634. @int 右下边缘的X位置,不含
  635. @int 右下边缘的Y位置,不含
  636. @int 绘画颜色 可选参数,默认背景色
  637. @usage
  638. -- lcd颜色填充
  639. lcd.fill(20,30,220,30,0x0000)
  640. */
  641. static int l_lcd_draw_fill(lua_State* L) {
  642. int16_t x1, y1, x2, y2;
  643. luat_color_t color = BACK_COLOR;
  644. x1 = luaL_checkinteger(L, 1);
  645. y1 = luaL_checkinteger(L, 2);
  646. x2 = luaL_checkinteger(L, 3);
  647. y2 = luaL_checkinteger(L, 4);
  648. if (lua_gettop(L) > 4)
  649. color = (luat_color_t)luaL_checkinteger(L, 5);
  650. int ret = luat_lcd_draw_fill(lcd_dft_conf, x1, y1, x2, y2, color);
  651. lcd_auto_flush(lcd_dft_conf);
  652. lua_pushboolean(L, ret == 0 ? 1 : 0);
  653. return 1;
  654. }
  655. /*
  656. 画一个点.
  657. @api lcd.drawPoint(x0,y0,color)
  658. @int 点的X位置.
  659. @int 点的Y位置.
  660. @int 绘画颜色 可选参数,默认前景色
  661. @usage
  662. lcd.drawPoint(20,30,0x001F)
  663. */
  664. static int l_lcd_draw_point(lua_State* L) {
  665. int16_t x, y;
  666. luat_color_t color = FORE_COLOR;
  667. x = luaL_checkinteger(L, 1);
  668. y = luaL_checkinteger(L, 2);
  669. if (lua_gettop(L) > 2)
  670. color = (luat_color_t)luaL_checkinteger(L, 3);
  671. int ret = luat_lcd_draw_point(lcd_dft_conf, x, y, color);
  672. lcd_auto_flush(lcd_dft_conf);
  673. lua_pushboolean(L, ret == 0 ? 1 : 0);
  674. return 1;
  675. }
  676. /*
  677. 在两点之间画一条线.
  678. @api lcd.drawLine(x0,y0,x1,y1,color)
  679. @int 第一个点的X位置.
  680. @int 第一个点的Y位置.
  681. @int 第二个点的X位置.
  682. @int 第二个点的Y位置.
  683. @int 绘画颜色 可选参数,默认前景色
  684. @usage
  685. lcd.drawLine(20,30,220,30,0x001F)
  686. */
  687. static int l_lcd_draw_line(lua_State* L) {
  688. int16_t x1, y1, x2, y2;
  689. luat_color_t color = FORE_COLOR;
  690. x1 = luaL_checkinteger(L, 1);
  691. y1 = luaL_checkinteger(L, 2);
  692. x2 = luaL_checkinteger(L, 3);
  693. y2 = luaL_checkinteger(L, 4);
  694. if (lua_gettop(L) > 4)
  695. color = (luat_color_t)luaL_checkinteger(L, 5);
  696. int ret = luat_lcd_draw_line(lcd_dft_conf, x1, y1, x2, y2, color);
  697. lcd_auto_flush(lcd_dft_conf);
  698. lua_pushboolean(L, ret == 0 ? 1 : 0);
  699. return 1;
  700. }
  701. /*
  702. 从x / y位置(左上边缘)开始绘制一个框
  703. @api lcd.drawRectangle(x0,y0,x1,y1,color)
  704. @int 左上边缘的X位置.
  705. @int 左上边缘的Y位置.
  706. @int 右下边缘的X位置.
  707. @int 右下边缘的Y位置.
  708. @int 绘画颜色 可选参数,默认前景色
  709. @usage
  710. lcd.drawRectangle(20,40,220,80,0x001F)
  711. */
  712. static int l_lcd_draw_rectangle(lua_State* L) {
  713. int16_t x1, y1, x2, y2;
  714. luat_color_t color = FORE_COLOR;
  715. x1 = luaL_checkinteger(L, 1);
  716. y1 = luaL_checkinteger(L, 2);
  717. x2 = luaL_checkinteger(L, 3);
  718. y2 = luaL_checkinteger(L, 4);
  719. if (lua_gettop(L) > 4)
  720. color = (luat_color_t)luaL_checkinteger(L, 5);
  721. int ret = luat_lcd_draw_rectangle(lcd_dft_conf, x1, y1, x2, y2, color);
  722. lcd_auto_flush(lcd_dft_conf);
  723. lua_pushboolean(L, ret == 0 ? 1 : 0);
  724. return 1;
  725. }
  726. /*
  727. 从x / y位置(圆心)开始绘制一个圆
  728. @api lcd.drawCircle(x0,y0,r,color)
  729. @int 圆心的X位置.
  730. @int 圆心的Y位置.
  731. @int 半径.
  732. @int 绘画颜色 可选参数,默认前景色
  733. @usage
  734. lcd.drawCircle(120,120,20,0x001F)
  735. */
  736. static int l_lcd_draw_circle(lua_State* L) {
  737. int16_t x0, y0, r;
  738. luat_color_t color = FORE_COLOR;
  739. x0 = luaL_checkinteger(L, 1);
  740. y0 = luaL_checkinteger(L, 2);
  741. r = luaL_checkinteger(L, 3);
  742. if (lua_gettop(L) > 3)
  743. color = (luat_color_t)luaL_checkinteger(L, 4);
  744. int ret = luat_lcd_draw_circle(lcd_dft_conf, x0, y0, r, color);
  745. lcd_auto_flush(lcd_dft_conf);
  746. lua_pushboolean(L, ret == 0 ? 1 : 0);
  747. return 1;
  748. }
  749. /*
  750. 缓冲区绘制QRCode
  751. @api lcd.drawQrcode(x, y, str, size)
  752. @int x坐标
  753. @int y坐标
  754. @string 二维码的内容
  755. @int 显示大小 (注意:二维码生成大小与要显示内容和纠错等级有关,生成版本为1-40(对应 21x21 - 177x177)的不定大小,如果和设置大小不同会自动在指定的区域中间显示二维码,如二维码未显示请查看日志提示)
  756. @return nil 无返回值
  757. */
  758. static int l_lcd_drawQrcode(lua_State *L)
  759. {
  760. size_t len;
  761. int x = luaL_checkinteger(L, 1);
  762. int y = luaL_checkinteger(L, 2);
  763. const char* text = luaL_checklstring(L, 3, &len);
  764. int size = luaL_checkinteger(L, 4);
  765. uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  766. uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  767. if (qrcode == NULL || tempBuffer == NULL) {
  768. if (qrcode)
  769. luat_heap_free(qrcode);
  770. if (tempBuffer)
  771. luat_heap_free(tempBuffer);
  772. LLOGE("qrcode out of memory");
  773. return 0;
  774. }
  775. bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_LOW,
  776. qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
  777. if (ok){
  778. int qr_size = qrcodegen_getSize(qrcode);
  779. if (size < qr_size){
  780. LLOGE("size must be greater than qr_size %d",qr_size);
  781. goto end;
  782. }
  783. int scale = size / qr_size ;
  784. if (!scale)scale = 1;
  785. int margin = (size - qr_size * scale) / 2;
  786. luat_lcd_draw_fill(lcd_dft_conf,x,y,x+size,y+size,BACK_COLOR);
  787. x+=margin;
  788. y+=margin;
  789. for (int j = 0; j < qr_size; j++) {
  790. for (int i = 0; i < qr_size; i++) {
  791. if (qrcodegen_getModule(qrcode, i, j))
  792. luat_lcd_draw_fill(lcd_dft_conf,x+i*scale,y+j*scale,x+(i+1)*scale,y+(j+1)*scale,FORE_COLOR);
  793. }
  794. }
  795. }else{
  796. LLOGE("qrcodegen_encodeText false");
  797. }
  798. end:
  799. if (qrcode)
  800. luat_heap_free(qrcode);
  801. if (tempBuffer)
  802. luat_heap_free(tempBuffer);
  803. lcd_auto_flush(lcd_dft_conf);
  804. return 0;
  805. }
  806. static uint8_t utf8_state;
  807. static uint16_t encoding;
  808. static uint16_t utf8_next(uint8_t b)
  809. {
  810. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  811. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  812. if ( utf8_state == 0 ){
  813. if ( b >= 0xfc ) /* 6 byte sequence */
  814. {
  815. utf8_state = 5;
  816. b &= 1;
  817. }
  818. else if ( b >= 0xf8 )
  819. {
  820. utf8_state = 4;
  821. b &= 3;
  822. }
  823. else if ( b >= 0xf0 )
  824. {
  825. utf8_state = 3;
  826. b &= 7;
  827. }
  828. else if ( b >= 0xe0 )
  829. {
  830. utf8_state = 2;
  831. b &= 15;
  832. }
  833. else if ( b >= 0xc0 )
  834. {
  835. utf8_state = 1;
  836. b &= 0x01f;
  837. }
  838. else
  839. {
  840. /* do nothing, just use the value as encoding */
  841. return b;
  842. }
  843. encoding = b;
  844. return 0x0fffe;
  845. } else {
  846. utf8_state--;
  847. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  848. encoding<<=6;
  849. b &= 0x03f;
  850. encoding |= b;
  851. if ( utf8_state != 0 )
  852. return 0x0fffe; /* nothing to do yet */
  853. }
  854. return encoding;
  855. }
  856. static void u8g2_draw_hv_line(u8g2_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color){
  857. switch(dir){
  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. /*
  1434. 解码图片,当前只支持jpg,jpeg
  1435. @api lcd.image2raw(file)
  1436. @string 文件路径
  1437. @return userdata 颜色zbuff
  1438. @usage
  1439. lcd_zbuff = lcd.image2raw("/luadb/logo.jpg")
  1440. lcd.draw(0, 0, 80, 80, lcd_zbuff)
  1441. */
  1442. int l_lcd_image2raw(lua_State *L) {
  1443. if (lcd_dft_conf == NULL) {
  1444. LLOGE("lcd not init");
  1445. return 0;
  1446. }
  1447. size_t size = 0;
  1448. int ret = 0;
  1449. luat_lcd_buff_info_t buff_info = {0};
  1450. const char* input_file = luaL_checklstring(L, 1, &size);
  1451. 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){
  1452. ret = lcd_jpeg_decode(lcd_dft_conf, input_file, &buff_info);
  1453. if(ret){
  1454. goto error;
  1455. }
  1456. } else{
  1457. LLOGE("input_file not support");
  1458. lua_pushboolean(L, 0);
  1459. return 1;
  1460. }
  1461. luat_zbuff_t *zbuff = (luat_zbuff_t *)lua_newuserdata(L, sizeof(luat_zbuff_t));
  1462. memset(zbuff,0,sizeof(luat_zbuff_t));
  1463. zbuff->addr = buff_info.buff;
  1464. zbuff->len = buff_info.len;
  1465. zbuff->width = buff_info.width;
  1466. zbuff->height = buff_info.height;
  1467. zbuff->bit = LUAT_LCD_COLOR_DEPTH;
  1468. // LLOGD("addr:%p len:%d width:%d height:%d bit:%d ",zbuff->addr,zbuff->len,zbuff->width,zbuff->height,zbuff->bit);
  1469. luaL_setmetatable(L, LUAT_ZBUFF_TYPE);
  1470. return 1;
  1471. error:
  1472. return 0;
  1473. }
  1474. #endif
  1475. /*
  1476. 主动刷新数据到界面, 仅设置buff且禁用自动属性后使用
  1477. @api lcd.flush()
  1478. @return bool 成功返回true, 否则返回nil/false
  1479. @usage
  1480. -- 本API与 lcd.setupBuff lcd.autoFlush 配合使用
  1481. lcd.flush()
  1482. */
  1483. static int l_lcd_flush(lua_State* L) {
  1484. luat_lcd_conf_t * conf = NULL;
  1485. if (lua_gettop(L) == 1) {
  1486. conf = lua_touserdata(L, 1);
  1487. }else {
  1488. conf = lcd_dft_conf;
  1489. }
  1490. if (conf == NULL) {
  1491. //LLOGW("lcd not init");
  1492. return 0;
  1493. }
  1494. if (conf->buff == NULL) {
  1495. //LLOGW("lcd without buff, not support flush");
  1496. return 0;
  1497. }
  1498. if (conf->auto_flush) {
  1499. //LLOGI("lcd auto flush is enable, no need for flush");
  1500. return 0;
  1501. }
  1502. luat_lcd_flush(conf);
  1503. lua_pushboolean(L, 1);
  1504. return 1;
  1505. }
  1506. /*
  1507. 设置显示缓冲区, 所需内存大小为 2×宽×高 字节. 请衡量内存需求与业务所需的刷新频次.
  1508. @api lcd.setupBuff(conf, onheap)
  1509. @userdata conf指针, 不需要传
  1510. @bool true使用heap内存, false使用vm内存, 默认使用vm内存, 不需要主动传
  1511. @return bool 是否成功
  1512. @usage
  1513. -- 初始化lcd的buff缓冲区, 可理解为FrameBuffer区域.
  1514. lcd.setupBuff()
  1515. */
  1516. static int l_lcd_setup_buff(lua_State* L) {
  1517. luat_lcd_conf_t * conf = NULL;
  1518. if (lua_gettop(L) == 1) {
  1519. conf = lua_touserdata(L, 1);
  1520. }
  1521. else {
  1522. conf = lcd_dft_conf;
  1523. }
  1524. if (conf == NULL) {
  1525. LLOGW("lcd not init");
  1526. return 0;
  1527. }
  1528. if (conf->buff != NULL && conf->buff_ex != NULL) {
  1529. LLOGI("lcd buff is aready exist");
  1530. return 0;
  1531. }
  1532. if (lua_isboolean(L, 2) && lua_toboolean(L, 2)) {
  1533. luat_lcd_setup_buff(conf);
  1534. }
  1535. else {
  1536. conf->buff = lua_newuserdata(L, sizeof(luat_color_t) * conf->w * conf->h);
  1537. if (conf->buff) {
  1538. conf->buff_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1539. }
  1540. }
  1541. if (conf->buff == NULL) {
  1542. LLOGE("lcd buff malloc fail, out of memory? size %d", sizeof(luat_color_t) * conf->w * conf->h);
  1543. return 0;
  1544. }
  1545. // 先设置为不需要的区间
  1546. conf->flush_y_min = conf->h;
  1547. conf->flush_y_max = 0;
  1548. // luat_lcd_clear 会将区域扩展到整个屏幕
  1549. luat_lcd_clear(lcd_dft_conf, BACK_COLOR);
  1550. lua_pushboolean(L, 1);
  1551. return 1;
  1552. }
  1553. /*
  1554. 设置自动刷新, 需配合lcd.setupBuff使用
  1555. @api lcd.autoFlush(enable)
  1556. @bool 是否自动刷新,默认为true
  1557. @usage
  1558. -- 设置buff 并禁用自动更新
  1559. lcd.setupBuff()
  1560. lcd.autoFlush(false)
  1561. -- 禁止自动更新后, 需要使用 lcd.flush() 主动刷新数据到屏幕
  1562. */
  1563. static int l_lcd_auto_flush(lua_State* L) {
  1564. luat_lcd_conf_t * conf = lcd_dft_conf;
  1565. if (conf == NULL) {
  1566. LLOGW("lcd not init");
  1567. return 0;
  1568. }
  1569. conf->auto_flush = lua_toboolean(L, 1);
  1570. lua_pushboolean(L, conf->auto_flush);
  1571. return 1;
  1572. }
  1573. /*
  1574. RGB565颜色生成
  1575. @api lcd.rgb565(r, g, b, swap)
  1576. @int 红色, 0x00 ~ 0xFF
  1577. @int 绿色, 0x00 ~ 0xFF
  1578. @int 蓝色, 0x00 ~ 0xFF
  1579. @bool 是否翻转, true 翻转, false 不翻转. 默认翻转
  1580. @return int 颜色值
  1581. @usage
  1582. -- 本API支持多种模式, 参数数量分别是 1, 2, 3, 4
  1583. -- 1. 单参数形式, 24bit RGB值, swap = true, 推荐
  1584. local red = lcd.rgb565(0xFF0000)
  1585. local green = lcd.rgb565(0x00FF00)
  1586. local blue = lcd.rgb565(0x0000FF)
  1587. -- 2. 两参数形式, 24bit RGB值, 增加swap的设置
  1588. local red = lcd.rgb565(0xFF0000, true)
  1589. local green = lcd.rgb565(0x00FF00, true)
  1590. local blue = lcd.rgb565(0x0000FF, true)
  1591. -- 3. 三参数形式, 红/绿/蓝, 各8bit
  1592. local red = lcd.rgb565(0xFF, 0x00, 0x00)
  1593. local green = lcd.rgb565(0x00, 0xFF, 0x00)
  1594. local blue = lcd.rgb565(0x00, 0x00, 0xFF)
  1595. -- 4. 四参数形式, 红/绿/蓝, 各8bit, 增加swap的设置
  1596. local red = lcd.rgb565(0xFF, 0x00, 0x00, true)
  1597. local green = lcd.rgb565(0x00, 0xFF, 0x00, true)
  1598. local blue = lcd.rgb565(0x00, 0x00, 0xFF, true)
  1599. */
  1600. static int l_lcd_rgb565(lua_State* L) {
  1601. uint8_t r =0,g =0,b = 0;
  1602. uint8_t swap = 0;
  1603. uint16_t dst = 0;
  1604. int top = 0 ;
  1605. uint32_t rgb = 0;
  1606. top = lua_gettop(L);
  1607. if (top == 1 || top == 2) {
  1608. rgb = luaL_checkinteger(L, 1);
  1609. r = (uint8_t)((rgb >> 16 ) & 0xFF);
  1610. g = (uint8_t)((rgb >> 8 ) & 0xFF);
  1611. b = (uint8_t)((rgb >> 0 ) & 0xFF);
  1612. swap = (lua_isboolean(L, 2) && !lua_toboolean(L, 2)) ? 0U : 1U;
  1613. }
  1614. else if (top == 3 || top == 4) {
  1615. r = (uint8_t)luaL_checkinteger(L, 1);
  1616. g = (uint8_t)luaL_checkinteger(L, 2);
  1617. b = (uint8_t)luaL_checkinteger(L, 3);
  1618. swap = (lua_isboolean(L, 4) && !lua_toboolean(L, 4)) ? 0U : 1U;
  1619. }
  1620. else {
  1621. LLOGW("unkown args count %d", top);
  1622. dst = 0;
  1623. }
  1624. dst = (uint16_t)((r&0xF8)<<8) | (uint16_t)((g&0xFC)<<3) | (uint16_t)(b>>3);
  1625. if (swap) {
  1626. dst = ((dst >> 8) & 0xFF) + ((dst & 0xFF) << 8);
  1627. }
  1628. lua_pushinteger(L, dst);
  1629. return 1;
  1630. }
  1631. #ifdef LUAT_USE_UFONT
  1632. #include "luat_ufont.h"
  1633. static const int l_lcd_draw_utf8(lua_State *L) {
  1634. size_t sz = 0;
  1635. uint32_t letter = 0;
  1636. uint32_t str_offset;
  1637. int ret = 0;
  1638. uint16_t draw_offset = 0;
  1639. int draw_x = 0;
  1640. int draw_y = 0;
  1641. luat_font_char_desc_t desc = {0};
  1642. // 左上角坐标x,y
  1643. int x = luaL_checkinteger(L, 1);
  1644. int y = luaL_checkinteger(L, 2);
  1645. // 待绘制的字符串
  1646. const char* data = (const char*)luaL_checklstring(L, 3, &sz);
  1647. // 字体指针
  1648. lv_font_t* lfont = (lv_font_t*)lua_touserdata(L, 4);
  1649. if (lfont == NULL) {
  1650. LLOGW("draw without font");
  1651. return 0;
  1652. }
  1653. luat_font_header_t* font = (luat_font_header_t*)lfont->dsc;
  1654. // 是否填充背景
  1655. bool draw_bg = lua_isboolean(L, 5) ? lua_toboolean(L, 5) : true;
  1656. // 没内容, 不需要画了
  1657. if (sz == 0) {
  1658. // 直接返回原坐标
  1659. lua_pushinteger(L, x);
  1660. return 1;
  1661. }
  1662. // 没字体, 不需要画了
  1663. if (font == NULL) {
  1664. LLOGD("NULL font, skip draw");
  1665. // 直接返回原坐标
  1666. lua_pushinteger(L, x);
  1667. return 1;
  1668. }
  1669. // 超边界了没? 超了就没必要绘制了
  1670. if (lcd_dft_conf->h < y || lcd_dft_conf->w < x) {
  1671. //LLOGD("draw y %d h % font->line_height %d", y, lcd_dft_conf->h, font->line_height);
  1672. // 直接返回原坐标
  1673. lua_pushinteger(L, x);
  1674. return 1;
  1675. }
  1676. luat_color_t* buff = NULL;
  1677. if (draw_bg)
  1678. buff = luat_heap_malloc(font->line_height * font->line_height * 2);
  1679. // if (buff == NULL)
  1680. // return 0;
  1681. int offset = 0;
  1682. uint8_t *data_ptr = data;
  1683. uint8_t utf8_state = 0;
  1684. uint16_t utf8_tmp = 0;
  1685. uint16_t utf8_out = 0;
  1686. luat_color_t color = FORE_COLOR;
  1687. for (size_t i = 0; i < sz; i++)
  1688. {
  1689. utf8_out = luat_utf8_next(data[i], &utf8_state, &utf8_tmp);
  1690. if (utf8_out == 0x0ffff)
  1691. break; // 结束了
  1692. if (utf8_out == 0x0fffe)
  1693. continue; // 没读完一个字符,继续下一个循环
  1694. letter = (uint32_t)utf8_out;
  1695. //LLOGD("draw letter %04X", letter);
  1696. int ret = luat_font_get_bitmap(font, &desc, letter);
  1697. if (ret != 0) {
  1698. LLOGD("not such char in font");
  1699. draw_offset += font->line_height / 2; // 找不到字符, 默认跳过半个字
  1700. continue;
  1701. }
  1702. offset = 0;
  1703. // desc.data = tmp;
  1704. memset(buff, 0, font->line_height * font->line_height * 2);
  1705. draw_x = x + draw_offset;
  1706. draw_offset += desc.char_w;
  1707. if (draw_x >= 0 && draw_x + desc.char_w <= lcd_dft_conf->w) {
  1708. //if (lcd_dft_conf->buff == NULL) {
  1709. for (size_t j = 0; j < font->line_height; j++)
  1710. {
  1711. //LLOGD("draw char pix line %d", i);
  1712. for (size_t k = 0; k < desc.char_w; k++)
  1713. {
  1714. if ((desc.data[offset / 8] >> (7 - (offset % 8))) & 0x01) {
  1715. color = FORE_COLOR;
  1716. if (buff)
  1717. buff[offset] = FORE_COLOR;
  1718. else
  1719. luat_lcd_draw_point(lcd_dft_conf, draw_x + k, y + j, FORE_COLOR);
  1720. //LLOGD("draw char pix mark %d", offset);
  1721. }else {
  1722. if (buff)
  1723. buff[offset] = BACK_COLOR;
  1724. //LLOGD("draw char pix offset %d color %04X", offset, FORE_COLOR);
  1725. }
  1726. offset ++;
  1727. }
  1728. }
  1729. //LLOGD("luat_lcd_draw %d %d %d %d", draw_x, y, draw_x + desc.char_w, y + font->line_height);
  1730. luat_lcd_draw(lcd_dft_conf, draw_x, y, draw_x + desc.char_w - 1, y + font->line_height - 1, buff);
  1731. //}
  1732. //else {
  1733. //
  1734. //}
  1735. }
  1736. }
  1737. if (buff)
  1738. luat_heap_free(buff);
  1739. lcd_auto_flush(lcd_dft_conf);
  1740. lua_pushinteger(L, draw_x + desc.char_w);
  1741. return 1;
  1742. }
  1743. #endif
  1744. /*
  1745. 硬件lcd qspi接口配置,需要在lcd.init前配置好
  1746. @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)
  1747. @int lcd命令模式下的qspi指令
  1748. @int lcd数据模式下,1线地址,4线数据的qspi指令,
  1749. @int lcd数据模式下,4线地址,4线数据的qspi指令,可以留空,如果存在,发送数据时优先使用这个模式
  1750. @int 帧同步时的地址值,只有无ram的屏幕需要,如果能用0x2c发送数据则不需要这个参数
  1751. @int 行同步时的指令,一般情况和命令模式下的指令一致,只有无ram的屏幕需要,如果能用0x2c发送数据则不需要这个参数
  1752. @int 行同步时的地址值,只有无ram的屏幕需要,如果能用0x2c发送数据则不需要这个参数
  1753. @return nil
  1754. @usage
  1755. -- sh8601z驱动ic所需的qspi配置
  1756. lcd.qspi(0x02, 0x32, 0x12)
  1757. -- jd9261t驱动ic所需的qspi配置
  1758. lcd.qspi(0xde, 0xde, nil, 0x61, 0xde, 0x60)
  1759. -- CO5300驱动ic所需的qspi配置
  1760. lcd.qspi(0x02, 0x32, 0x12)
  1761. */
  1762. static int l_lcd_qspi_config(lua_State* L){
  1763. luat_lcd_qspi_conf_t qspi_config = {0};
  1764. qspi_config.write_1line_cmd = luaL_optinteger(L, 1, 0x02);
  1765. qspi_config.write_4line_cmd = luaL_optinteger(L, 2, 0x32);
  1766. qspi_config.write_4line_data = luaL_optinteger(L, 3, 0);
  1767. qspi_config.vsync_reg = luaL_optinteger(L, 4, 0);
  1768. qspi_config.hsync_cmd = luaL_optinteger(L, 5, 0);
  1769. qspi_config.hsync_reg = luaL_optinteger(L, 6, 0);
  1770. luat_lcd_qspi_config(NULL, &qspi_config);
  1771. return 0;
  1772. }
  1773. /*
  1774. 用户使用脚本初始化LCD完成后,必须调用本API
  1775. @api lcd.user_done()
  1776. @return nil
  1777. */
  1778. static int l_lcd_user_ctrl_done(lua_State* L){
  1779. lcd_dft_conf->is_init_done = 1;
  1780. if (LUAT_LCD_IM_QSPI_MODE == lcd_dft_conf->interface_mode) {
  1781. if (luat_lcd_qspi_is_no_ram(lcd_dft_conf)) {
  1782. luat_lcd_qspi_auto_flush_on_off(lcd_dft_conf, 1);
  1783. }
  1784. }
  1785. return 0;
  1786. }
  1787. /*
  1788. 开启/关闭硬件加速(lcd.init后默认开启所有支持的硬件加速,当使用存在问题时可使用此接口进行开关指定的硬件加速)
  1789. @api lcd.setAcchw(type,enable)
  1790. @number type 支持的类型, 可选,默认全部类型 目前支持 lcd.ACC_HW_JPEG lcd.ACC_HW_ALL
  1791. @bool enable 开关, 可选 默认关闭 ture开启 false关闭
  1792. @return nil
  1793. @usage
  1794. lcd.setAcchw(lcd.ACC_HW_JPEG,false) -- 关闭硬件加速的jpeg解码功能
  1795. lcd.setAcchw(lcd.ACC_HW_ALL,false) -- 关闭所有硬件加速
  1796. */
  1797. static int l_lcd_set_acc_hw(lua_State* L){
  1798. luat_lcd_conf_t * conf = lcd_dft_conf;
  1799. uint8_t type = luaL_optinteger(L, 1, LUAT_LCD_ACC_HW_ALL);
  1800. uint8_t enable = 0;
  1801. if (lua_isboolean(L, 2)) {
  1802. enable = lua_toboolean(L, 2);
  1803. }
  1804. if (type == LUAT_LCD_ACC_HW_ALL){
  1805. if (enable){
  1806. conf->acc_hw = LUAT_LCD_ACC_HW_ALL;
  1807. }else{
  1808. conf->acc_hw = 0;
  1809. }
  1810. }else if(type == LUAT_LCD_ACC_HW_JPEG){
  1811. if (enable){
  1812. conf->acc_hw = 1;
  1813. }else{
  1814. conf->acc_hw = 0;
  1815. }
  1816. }
  1817. return 0;
  1818. }
  1819. #include "rotable2.h"
  1820. static const rotable_Reg_t reg_lcd[] =
  1821. {
  1822. { "init", ROREG_FUNC(l_lcd_init)},
  1823. { "clear", ROREG_FUNC(l_lcd_clear)},
  1824. { "fill", ROREG_FUNC(l_lcd_draw_fill)},
  1825. { "drawPoint", ROREG_FUNC(l_lcd_draw_point)},
  1826. { "drawLine", ROREG_FUNC(l_lcd_draw_line)},
  1827. { "drawRectangle", ROREG_FUNC(l_lcd_draw_rectangle)},
  1828. { "drawCircle", ROREG_FUNC(l_lcd_draw_circle)},
  1829. { "drawQrcode", ROREG_FUNC(l_lcd_drawQrcode)},
  1830. { "drawStr", ROREG_FUNC(l_lcd_draw_str)},
  1831. { "flush", ROREG_FUNC(l_lcd_flush)},
  1832. { "setupBuff", ROREG_FUNC(l_lcd_setup_buff)},
  1833. { "autoFlush", ROREG_FUNC(l_lcd_auto_flush)},
  1834. { "setFont", ROREG_FUNC(l_lcd_set_font)},
  1835. { "setDefault", ROREG_FUNC(l_lcd_set_default)},
  1836. { "getDefault", ROREG_FUNC(l_lcd_get_default)},
  1837. { "getSize", ROREG_FUNC(l_lcd_get_size)},
  1838. { "drawXbm", ROREG_FUNC(l_lcd_drawxbm)},
  1839. { "close", ROREG_FUNC(l_lcd_close)},
  1840. { "on", ROREG_FUNC(l_lcd_display_on)},
  1841. { "off", ROREG_FUNC(l_lcd_display_off)},
  1842. { "sleep", ROREG_FUNC(l_lcd_sleep)},
  1843. { "wakeup", ROREG_FUNC(l_lcd_wakeup)},
  1844. { "invon", ROREG_FUNC(l_lcd_inv_on)},
  1845. { "invoff", ROREG_FUNC(l_lcd_inv_off)},
  1846. { "cmd", ROREG_FUNC(l_lcd_write_cmd)},
  1847. { "data", ROREG_FUNC(l_lcd_write_data)},
  1848. { "setColor", ROREG_FUNC(l_lcd_set_color)},
  1849. { "draw", ROREG_FUNC(l_lcd_draw)},
  1850. { "rgb565", ROREG_FUNC(l_lcd_rgb565)},
  1851. { "qspi", ROREG_FUNC(l_lcd_qspi_config)},
  1852. { "user_done", ROREG_FUNC(l_lcd_user_ctrl_done)},
  1853. { "setAcchw", ROREG_FUNC(l_lcd_set_acc_hw)},
  1854. #ifdef LUAT_USE_UFONT
  1855. { "drawUTF8", ROREG_FUNC(l_lcd_draw_utf8)},
  1856. #endif
  1857. #ifdef LUAT_USE_TJPGD
  1858. { "image2raw", ROREG_FUNC(l_lcd_image2raw)},
  1859. { "showImage", ROREG_FUNC(l_lcd_showimage)},
  1860. #endif
  1861. #ifdef LUAT_USE_GTFONT
  1862. { "drawGtfontGb2312", ROREG_FUNC(l_lcd_draw_gtfont_gbk)},
  1863. { "drawGtfontGb2312Gray", ROREG_FUNC(l_lcd_draw_gtfont_gbk_gray)},
  1864. { "drawGtfontGbk", ROREG_FUNC(l_lcd_draw_gtfont_gbk)},
  1865. { "drawGtfontGbkGray", ROREG_FUNC(l_lcd_draw_gtfont_gbk_gray)},
  1866. #ifdef LUAT_USE_GTFONT_UTF8
  1867. { "drawGtfontUtf8", ROREG_FUNC(l_lcd_draw_gtfont_utf8)},
  1868. { "drawGtfontUtf8Gray", ROREG_FUNC(l_lcd_draw_gtfont_utf8_gray)},
  1869. #endif // LUAT_USE_GTFONT_UTF8
  1870. #endif // LUAT_USE_GTFONT
  1871. // 默认只带英文12号字体
  1872. //@const font_opposansm12 font 12号字体
  1873. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  1874. #ifdef USE_U8G2_OPPOSANSM_ENGLISH
  1875. //@const font_unifont_t_symbols font 符号字体
  1876. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  1877. //@const font_open_iconic_weather_6x_t font 天气字体
  1878. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  1879. //@const font_opposansm16 font 16号字体
  1880. //@const font_opposansm18 font 18号字体
  1881. //@const font_opposansm20 font 20号字体
  1882. //@const font_opposansm22 font 22号字体
  1883. //@const font_opposansm24 font 24号字体
  1884. //@const font_opposansm32 font 32号字体
  1885. { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
  1886. { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
  1887. { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
  1888. { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
  1889. { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
  1890. { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
  1891. #endif
  1892. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  1893. //@const font_opposansm12_chinese font 12号中文字体
  1894. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  1895. #endif
  1896. #ifdef USE_U8G2_OPPOSANSM14_CHINESE
  1897. //@const font_opposansm14_chinese font 14号中文字体
  1898. { "font_opposansm14_chinese", ROREG_PTR((void*)u8g2_font_opposansm14_chinese)},
  1899. #endif
  1900. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  1901. //@const font_opposansm16_chinese font 16号中文字体
  1902. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  1903. #endif
  1904. #ifdef USE_U8G2_OPPOSANSM18_CHINESE
  1905. //@const font_opposansm18_chinese font 18号中文字体
  1906. { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
  1907. #endif
  1908. #ifdef USE_U8G2_OPPOSANSM20_CHINESE
  1909. //@const font_opposansm20_chinese font 20号中文字体
  1910. { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
  1911. #endif
  1912. #ifdef USE_U8G2_OPPOSANSM22_CHINESE
  1913. //@const font_opposansm22_chinese font 22号中文字体
  1914. { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
  1915. #endif
  1916. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  1917. //@const font_opposansm24_chinese font 24号中文字体
  1918. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  1919. #endif
  1920. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  1921. //@const font_opposansm32_chinese font 32号中文字体
  1922. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  1923. #endif
  1924. #ifdef USE_U8G2_SARASA_ENGLISH
  1925. { "font_sarasa_m12_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m12_ascii)},
  1926. { "font_sarasa_m14_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m14_ascii)},
  1927. { "font_sarasa_m16_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m16_ascii)},
  1928. { "font_sarasa_m18_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m18_ascii)},
  1929. { "font_sarasa_m20_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m20_ascii)},
  1930. { "font_sarasa_m22_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m22_ascii)},
  1931. //再大的很少用到先不加了
  1932. #endif
  1933. // #ifdef USE_U8G2_SARASA_M8_CHINESE
  1934. // { "font_sarasa_m8_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m8_chinese)},
  1935. // #endif
  1936. #ifdef USE_U8G2_SARASA_M10_CHINESE
  1937. { "font_sarasa_m10_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m10_chinese)},
  1938. #endif
  1939. #ifdef USE_U8G2_SARASA_M12_CHINESE
  1940. { "font_sarasa_m12_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m12_chinese)},
  1941. #endif
  1942. #ifdef USE_U8G2_SARASA_M14_CHINESE
  1943. { "font_sarasa_m14_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m14_chinese)},
  1944. #endif
  1945. #ifdef USE_U8G2_SARASA_M16_CHINESE
  1946. { "font_sarasa_m16_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m16_chinese)},
  1947. #endif
  1948. #ifdef USE_U8G2_SARASA_M18_CHINESE
  1949. { "font_sarasa_m18_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m18_chinese)},
  1950. #endif
  1951. #ifdef USE_U8G2_SARASA_M20_CHINESE
  1952. { "font_sarasa_m20_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m20_chinese)},
  1953. #endif
  1954. #ifdef USE_U8G2_SARASA_M22_CHINESE
  1955. { "font_sarasa_m22_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m22_chinese)},
  1956. #endif
  1957. #ifdef USE_U8G2_SARASA_M24_CHINESE
  1958. { "font_sarasa_m24_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m24_chinese)},
  1959. #endif
  1960. #ifdef USE_U8G2_SARASA_M26_CHINESE
  1961. { "font_sarasa_m26_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m26_chinese)},
  1962. #endif
  1963. #ifdef USE_U8G2_SARASA_M28_CHINESE
  1964. { "font_sarasa_m28_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m28_chinese)},
  1965. #endif
  1966. { "set_direction", ROREG_FUNC(l_lcd_set_direction)},
  1967. //@const direction_0 int 0°方向命令
  1968. { "direction_0", ROREG_INT(LUAT_LCD_ROTATE_0)},
  1969. //@const direction_90 int 90°方向命令
  1970. { "direction_90", ROREG_INT(LUAT_LCD_ROTATE_90)},
  1971. //@const direction_180 int 180°方向命令
  1972. { "direction_180", ROREG_INT(LUAT_LCD_ROTATE_180)},
  1973. //@const direction_270 int 270°方向命令
  1974. { "direction_270", ROREG_INT(LUAT_LCD_ROTATE_270)},
  1975. //@const SPI 硬件spi device lcd驱动
  1976. { "SPI", ROREG_INT(LUAT_LCD_SPI_DEVICE)},
  1977. //@const HWID_0 硬件lcd驱动id0 (根据芯片支持选择)
  1978. { "HWID_0", ROREG_INT(LUAT_LCD_HW_ID_0)},
  1979. //@const RGB 硬件RGB lcd驱动 (根据芯片支持选择)
  1980. { "RGB", ROREG_INT(LUAT_LCD_PORT_RGB)},
  1981. // //@const ARM2D 硬件ARM2D lcd驱动 (根据芯片支持选择)
  1982. // { "ARM2D", ROREG_INT(LUAT_LCD_PORT_ARM2D)},
  1983. // //@const DMA2D 硬件DMA2D lcd驱动 (根据芯片支持选择)
  1984. // { "DMA2D", ROREG_INT(LUAT_LCD_PORT_DMA2D)},
  1985. //@const WIRE_3_BIT_9_INTERFACE_I 三线spi 9bit 模式I
  1986. { "WIRE_3_BIT_9_INTERFACE_I", ROREG_INT(LUAT_LCD_IM_3_WIRE_9_BIT_INTERFACE_I)},
  1987. //@const WIRE_4_BIT_8_INTERFACE_I 四线spi 8bit 模式I
  1988. { "WIRE_4_BIT_8_INTERFACE_I", ROREG_INT(LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_I)},
  1989. //@const WIRE_3_BIT_9_INTERFACE_II 三线spi 9bit 模式II
  1990. { "WIRE_3_BIT_9_INTERFACE_II", ROREG_INT(LUAT_LCD_IM_3_WIRE_9_BIT_INTERFACE_II)},
  1991. //@const WIRE_4_BIT_8_INTERFACE_II 四线spi 8bit 模式II
  1992. { "WIRE_4_BIT_8_INTERFACE_II", ROREG_INT(LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_II)},
  1993. //@const DATA_2_LANE spi双通道模式
  1994. { "DATA_2_LANE", ROREG_INT(LUAT_LCD_IM_2_DATA_LANE)},
  1995. //@const QSPI_MODE QSPI模式
  1996. { "QSPI_MODE", ROREG_INT(LUAT_LCD_IM_QSPI_MODE)},
  1997. //@const 8080_MODE 8080模式
  1998. { "8080_MODE", ROREG_INT(LUAT_LCD_IM_8080_MODE)},
  1999. //@const ACC_HW_JPEG JPEG硬件加速
  2000. {"ACC_HW_JPEG", ROREG_INT(LUAT_LCD_ACC_HW_JPEG)},
  2001. //@const ACC_HW_ALL 所有硬件加速
  2002. {"ACC_HW_ALL", ROREG_INT(LUAT_LCD_ACC_HW_ALL)},
  2003. {NULL, ROREG_INT(0)}
  2004. };
  2005. LUAMOD_API int luaopen_lcd( lua_State *L ) {
  2006. luat_newlib2(L, reg_lcd);
  2007. return 1;
  2008. }