luat_lib_lcd.c 80 KB

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