luat_lib_lcd.c 58 KB

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