luat_lib_lcd.c 62 KB

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