luat_lib_lcd.c 64 KB

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