luat_lib_lcd.c 63 KB

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