luat_lib_lcd.c 58 KB

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