luat_lib_lcd.c 54 KB

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