luat_lib_lcd.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*
  2. @module lcd
  3. @summary lcd驱动模块
  4. @version 1.0
  5. @date 2021.06.16
  6. */
  7. #include "luat_base.h"
  8. #include "luat_lcd.h"
  9. #include "luat_malloc.h"
  10. #include "luat_zbuff.h"
  11. #include "luat_fs.h"
  12. #define LUAT_LOG_TAG "lcd"
  13. #include "luat_log.h"
  14. #include "u8g2.h"
  15. #include "u8g2_luat_fonts.h"
  16. #include "../qrcode/qrcode.h"
  17. int8_t u8g2_font_decode_get_signed_bits(u8g2_font_decode_t *f, uint8_t cnt);
  18. uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt);
  19. extern luat_color_t BACK_COLOR , FORE_COLOR ;
  20. extern const luat_lcd_opts_t lcd_opts_st7735;
  21. extern const luat_lcd_opts_t lcd_opts_st7735v;
  22. extern const luat_lcd_opts_t lcd_opts_st7735s;
  23. extern const luat_lcd_opts_t lcd_opts_st7789;
  24. extern const luat_lcd_opts_t lcd_opts_gc9a01;
  25. extern const luat_lcd_opts_t lcd_opts_gc9106l;
  26. extern const luat_lcd_opts_t lcd_opts_gc9306x;
  27. extern const luat_lcd_opts_t lcd_opts_ili9341;
  28. extern const luat_lcd_opts_t lcd_opts_ili9488;
  29. extern const luat_lcd_opts_t lcd_opts_custom;
  30. typedef struct lcd_reg {
  31. const char *name;
  32. const luat_lcd_opts_t *lcd_opts;
  33. }lcd_reg_t;
  34. static const lcd_reg_t lcd_regs[] = {
  35. {"custom", &lcd_opts_custom}, //0 固定为第零个
  36. {"st7735", &lcd_opts_st7735}, //1
  37. {"st7735v", &lcd_opts_st7735v}, //2
  38. {"st7735s", &lcd_opts_st7735s}, //3
  39. {"st7789", &lcd_opts_st7789}, //4
  40. {"gc9a01", &lcd_opts_gc9a01}, //5
  41. {"gc9106l", &lcd_opts_gc9106l}, //6
  42. {"gc9306x", &lcd_opts_gc9306x}, //7
  43. {"gc9306", &lcd_opts_gc9306x}, //gc9306是gc9306x的别名
  44. {"ili9341", &lcd_opts_ili9341}, //8
  45. {"ili9488", &lcd_opts_ili9488}, //9
  46. {"", NULL} // 最后一个必须是空字符串
  47. };
  48. static luat_lcd_conf_t *default_conf = NULL;
  49. static int dft_conf_lua_ref = 0;
  50. static luat_color_t lcd_str_fg_color,lcd_str_bg_color;
  51. /*
  52. lcd显示屏初始化
  53. @api lcd.init(tp, args)
  54. @string lcd类型, 当前支持st7789/st7735/st7735v/st7735s/gc9a01/gc9106l/gc9306x/ili9341/custom
  55. @table 附加参数,与具体设备有关,pin_pwr为可选项,可不设置port:spi端口,例如0,1,2...如果为device方式则为"device";pin_dc:lcd数据/命令选择引脚;pin_rst:lcd复位引脚;pin_pwr:lcd背光引脚 可选项,可不设置;direction:lcd屏幕方向 0:0° 1:180° 2:270° 3:90°;w:lcd 水平分辨率;h:lcd 竖直分辨率;xoffset:x偏移(不同屏幕ic 不同屏幕方向会有差异);yoffset:y偏移(不同屏幕ic 不同屏幕方向会有差异)
  56. @userdata spi设备,当port = "device"时有效
  57. @usage
  58. -- 初始化spi0的st7789 注意:lcd初始化之前需要先初始化spi
  59. spi_lcd = spi.deviceSetup(0,20,0,0,8,2000000,spi.MSB,1,1)
  60. log.info("lcd.init",
  61. 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))
  62. */
  63. static int l_lcd_init(lua_State* L) {
  64. size_t len = 0;
  65. luat_lcd_conf_t *conf = luat_heap_malloc(sizeof(luat_lcd_conf_t));
  66. if (conf == NULL) {
  67. LLOGE("out of system memory!!!");
  68. return 0;
  69. }
  70. memset(conf, 0, sizeof(luat_lcd_conf_t)); // 填充0,保证无脏数据
  71. conf->pin_pwr = 255;
  72. if (lua_type(L, 3) == LUA_TUSERDATA){
  73. conf->lcd_spi_device = (luat_spi_device_t*)lua_touserdata(L, 3);
  74. conf->port = LUAT_LCD_SPI_DEVICE;
  75. }
  76. const char* tp = luaL_checklstring(L, 1, &len);
  77. int16_t s_index = -1;//第几个屏幕,-1表示没匹配到
  78. for(int i = 0; i < 100; i++){
  79. if (strlen(lcd_regs[i].name) == 0)
  80. break;
  81. if(strcmp(lcd_regs[i].name,tp) == 0){
  82. s_index = i;
  83. break;
  84. }
  85. }
  86. if (s_index != -1) {
  87. LLOGD("ic support: %s",tp);
  88. if (lua_gettop(L) > 1) {
  89. conf->opts = (struct luat_lcd_opts *)lcd_regs[s_index].lcd_opts;
  90. lua_settop(L, 2); // 丢弃多余的参数
  91. lua_pushstring(L, "port");
  92. int port = lua_gettable(L, 2);
  93. if (conf->port == LUAT_LCD_SPI_DEVICE && port ==LUA_TNUMBER) {
  94. LLOGE("port is not device but find luat_spi_device_t");
  95. goto end;
  96. }else if (conf->port != LUAT_LCD_SPI_DEVICE && LUA_TSTRING == port){
  97. LLOGE("port is device but not find luat_spi_device_t");
  98. goto end;
  99. }else if (LUA_TNUMBER == port) {
  100. conf->port = luaL_checkinteger(L, -1);
  101. }else if (LUA_TSTRING == port){
  102. conf->port = LUAT_LCD_SPI_DEVICE;
  103. }
  104. lua_pop(L, 1);
  105. lua_pushstring(L, "pin_dc");
  106. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  107. conf->pin_dc = luaL_checkinteger(L, -1);
  108. }
  109. lua_pop(L, 1);
  110. lua_pushstring(L, "pin_pwr");
  111. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  112. conf->pin_pwr = luaL_checkinteger(L, -1);
  113. }
  114. lua_pop(L, 1);
  115. lua_pushstring(L, "pin_rst");
  116. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  117. conf->pin_rst = luaL_checkinteger(L, -1);
  118. }
  119. lua_pop(L, 1);
  120. lua_pushstring(L, "direction");
  121. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  122. conf->direction = luaL_checkinteger(L, -1);
  123. }
  124. lua_pop(L, 1);
  125. lua_pushstring(L, "w");
  126. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  127. conf->w = luaL_checkinteger(L, -1);
  128. }
  129. lua_pop(L, 1);
  130. lua_pushstring(L, "h");
  131. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  132. conf->h = luaL_checkinteger(L, -1);
  133. }
  134. lua_pop(L, 1);
  135. conf->buffer_size = (conf->w * conf->h) * 2;
  136. lua_pushstring(L, "xoffset");
  137. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  138. conf->xoffset = luaL_checkinteger(L, -1);
  139. }
  140. lua_pop(L, 1);
  141. lua_pushstring(L, "yoffset");
  142. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  143. conf->yoffset = luaL_checkinteger(L, -1);
  144. }
  145. lua_pop(L, 1);
  146. }
  147. if (s_index == 0){
  148. luat_lcd_custom_t *cst = luat_heap_malloc(sizeof(luat_lcd_custom_t));
  149. lua_pushstring(L, "initcmd");
  150. lua_gettable(L, 2);
  151. if (lua_istable(L, -1)) {
  152. cst->init_cmd_count = lua_rawlen(L, -1);
  153. cst->initcmd = luat_heap_malloc(cst->init_cmd_count * sizeof(uint32_t));
  154. for (size_t i = 1; i <= cst->init_cmd_count; i++){
  155. lua_geti(L, -1, i);
  156. cst->initcmd[i-1] = luaL_checkinteger(L, -1);
  157. lua_pop(L, 1);
  158. }
  159. }else if(lua_isstring(L, -1)){
  160. size_t len,cmd;
  161. const char *fail_name = luaL_checklstring(L, -1, &len);
  162. FILE* fd = (FILE *)luat_fs_fopen(fail_name, "rb");
  163. cst->init_cmd_count = 0;
  164. if (fd){
  165. #define INITCMD_BUFF_SIZE 128
  166. char init_cmd_buff[INITCMD_BUFF_SIZE] ;
  167. cst->initcmd = luat_heap_malloc(sizeof(uint32_t));
  168. while (1) {
  169. memset(init_cmd_buff, 0, INITCMD_BUFF_SIZE);
  170. int readline_len = luat_fs_readline(init_cmd_buff, INITCMD_BUFF_SIZE-1, fd);
  171. if (readline_len < 1)
  172. break;
  173. if (memcmp(init_cmd_buff, "#", 1)==0){
  174. continue;
  175. }
  176. char *token = strtok(init_cmd_buff, ",");
  177. if (sscanf(token,"%x",&cmd) < 1){
  178. continue;
  179. }
  180. cst->init_cmd_count = cst->init_cmd_count + 1;
  181. cst->initcmd = luat_heap_realloc(cst->initcmd,cst->init_cmd_count * sizeof(uint32_t));
  182. cst->initcmd[cst->init_cmd_count-1]=cmd;
  183. while( token != NULL ) {
  184. token = strtok(NULL, ",");
  185. if (sscanf(token,"%x",&cmd) < 1){
  186. break;
  187. }
  188. cst->init_cmd_count = cst->init_cmd_count + 1;
  189. cst->initcmd = luat_heap_realloc(cst->initcmd,cst->init_cmd_count * sizeof(uint32_t));
  190. cst->initcmd[cst->init_cmd_count-1]=cmd;
  191. }
  192. }
  193. cst->initcmd[cst->init_cmd_count]= 0;
  194. luat_fs_fclose(fd);
  195. }else{
  196. LLOGE("init_cmd fail open error");
  197. }
  198. }
  199. lua_pop(L, 1);
  200. conf->userdata = cst;
  201. }
  202. int ret = luat_lcd_init(conf);
  203. if (ret == 0) {
  204. if (dft_conf_lua_ref) {
  205. luaL_unref(L, LUA_REGISTRYINDEX, dft_conf_lua_ref);
  206. }
  207. default_conf = conf;
  208. dft_conf_lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  209. }
  210. u8g2_SetFontMode(&(conf->luat_lcd_u8g2), 0);
  211. u8g2_SetFontDirection(&(conf->luat_lcd_u8g2), 0);
  212. lua_pushboolean(L, ret == 0 ? 1 : 0);
  213. // lua_pushlightuserdata(L, conf);
  214. return 1;
  215. }
  216. LLOGE("ic not support: %s",tp);
  217. end:
  218. lua_pushboolean(L, 0);
  219. luat_heap_free(conf);
  220. return 1;
  221. }
  222. /*
  223. 关闭lcd显示屏
  224. @api lcd.close()
  225. @usage
  226. -- 关闭lcd
  227. lcd.close()
  228. */
  229. static int l_lcd_close(lua_State* L) {
  230. int ret = luat_lcd_close(default_conf);
  231. lua_pushboolean(L, ret == 0 ? 1 : 0);
  232. return 1;
  233. }
  234. /*
  235. 开启lcd显示屏背光
  236. @api lcd.on()
  237. @usage
  238. -- 开启lcd显示屏背光
  239. lcd.on()
  240. */
  241. static int l_lcd_display_on(lua_State* L) {
  242. int ret = luat_lcd_display_on(default_conf);
  243. lua_pushboolean(L, ret == 0 ? 1 : 0);
  244. return 1;
  245. }
  246. /*
  247. 关闭lcd显示屏背光
  248. @api lcd.off()
  249. @usage
  250. -- 关闭lcd显示屏背光
  251. lcd.off()
  252. */
  253. static int l_lcd_display_off(lua_State* L) {
  254. int ret = luat_lcd_display_off(default_conf);
  255. lua_pushboolean(L, ret == 0 ? 1 : 0);
  256. return 1;
  257. }
  258. /*
  259. lcd睡眠
  260. @api lcd.sleep()
  261. @usage
  262. -- lcd睡眠
  263. lcd.sleep()
  264. */
  265. static int l_lcd_sleep(lua_State* L) {
  266. int ret = luat_lcd_sleep(default_conf);
  267. lua_pushboolean(L, ret == 0 ? 1 : 0);
  268. return 1;
  269. }
  270. /*
  271. lcd唤醒
  272. @api lcd.wakeup()
  273. @usage
  274. -- lcd唤醒
  275. lcd.wakeup()
  276. */
  277. static int l_lcd_wakeup(lua_State* L) {
  278. int ret = luat_lcd_wakeup(default_conf);
  279. lua_pushboolean(L, ret == 0 ? 1 : 0);
  280. return 1;
  281. }
  282. /*
  283. lcd反显
  284. @api lcd.invon()
  285. @usage
  286. -- lcd反显
  287. lcd.invon()
  288. */
  289. static int l_lcd_inv_on(lua_State* L) {
  290. int ret = luat_lcd_inv_on(default_conf);
  291. lua_pushboolean(L, ret == 0 ? 1 : 0);
  292. return 1;
  293. }
  294. /*
  295. lcd反显关闭
  296. @api lcd.invoff()
  297. @usage
  298. -- lcd反显关闭
  299. lcd.invoff()
  300. */
  301. static int l_lcd_inv_off(lua_State* L) {
  302. int ret = luat_lcd_inv_off(default_conf);
  303. lua_pushboolean(L, ret == 0 ? 1 : 0);
  304. return 1;
  305. }
  306. /*
  307. lcd命令
  308. @api lcd.cmd(cmd)
  309. @int cmd
  310. @usage
  311. -- lcd命令
  312. lcd.cmd(0x21)
  313. */
  314. static int l_lcd_write_cmd(lua_State* L) {
  315. int ret = lcd_write_cmd(default_conf,(const uint8_t)luaL_checkinteger(L, 1));
  316. lua_pushboolean(L, ret == 0 ? 1 : 0);
  317. return 1;
  318. }
  319. /*
  320. lcd数据
  321. @api lcd.data(data)
  322. @int data
  323. @usage
  324. -- lcd数据
  325. lcd.data(0x21)
  326. */
  327. static int l_lcd_write_data(lua_State* L) {
  328. int ret = lcd_write_data(default_conf,(const uint8_t)luaL_checkinteger(L, 1));
  329. lua_pushboolean(L, ret == 0 ? 1 : 0);
  330. return 1;
  331. }
  332. /*
  333. lcd颜色设置
  334. @api lcd.setColor(back,fore)
  335. @int 背景色
  336. @int 前景色
  337. @usage
  338. -- lcd颜色设置
  339. lcd.setColor(0xFFFF,0x0000)
  340. */
  341. static int l_lcd_set_color(lua_State* L) {
  342. luat_color_t back,fore;
  343. back = (luat_color_t)luaL_checkinteger(L, 1);
  344. fore = (luat_color_t)luaL_checkinteger(L, 2);
  345. int ret = luat_lcd_set_color(back, fore);
  346. lua_pushboolean(L, ret == 0 ? 1 : 0);
  347. return 1;
  348. }
  349. /*
  350. lcd颜色填充
  351. @api lcd.draw(x1, y1, x2, y2,color)
  352. @int 左上边缘的X位置.
  353. @int 左上边缘的Y位置.
  354. @int 右上边缘的X位置.
  355. @int 右上边缘的Y位置.
  356. @string 字符串或zbuff对象
  357. @usage
  358. -- lcd颜色填充
  359. local buff = zbuff.create({201,1,16},0x001F)
  360. lcd.draw(20,30,220,30,buff)
  361. */
  362. static int l_lcd_draw(lua_State* L) {
  363. uint16_t x1, y1, x2, y2;
  364. int ret;
  365. // luat_color_t *color = NULL;
  366. luat_zbuff_t *buff;
  367. x1 = luaL_checkinteger(L, 1);
  368. y1 = luaL_checkinteger(L, 2);
  369. x2 = luaL_checkinteger(L, 3);
  370. y2 = luaL_checkinteger(L, 4);
  371. if (lua_isinteger(L, 5)) {
  372. // color = (luat_color_t *)luaL_checkstring(L, 5);
  373. luat_color_t color = (uint32_t)luaL_checkinteger(L, 1);
  374. ret = luat_lcd_draw(default_conf, x1, y1, x2, y2, &color);
  375. }
  376. else if (lua_isuserdata(L, 5)) {
  377. buff = luaL_checkudata(L, 5, LUAT_ZBUFF_TYPE);
  378. luat_color_t *color = (luat_color_t *)buff->addr;
  379. ret = luat_lcd_draw(default_conf, x1, y1, x2, y2, color);
  380. }
  381. else if(lua_isstring(L, 5)) {
  382. luat_color_t *color = (luat_color_t *)luaL_checkstring(L, 5);
  383. ret = luat_lcd_draw(default_conf, x1, y1, x2, y2, color);
  384. }
  385. else {
  386. return 0;
  387. }
  388. // int ret = luat_lcd_draw(default_conf, x1, y1, x2, y2, color);
  389. lua_pushboolean(L, ret == 0 ? 1 : 0);
  390. return 1;
  391. }
  392. /*
  393. lcd清屏
  394. @api lcd.clear(color)
  395. @int 屏幕颜色 可选参数,默认背景色
  396. @usage
  397. -- lcd清屏
  398. lcd.clear()
  399. */
  400. static int l_lcd_clear(lua_State* L) {
  401. //size_t len = 0;
  402. luat_color_t color = BACK_COLOR;
  403. if (lua_gettop(L) > 0)
  404. color = (uint32_t)luaL_checkinteger(L, 1);
  405. int ret = luat_lcd_clear(default_conf, color);
  406. lua_pushboolean(L, ret == 0 ? 1 : 0);
  407. return 1;
  408. }
  409. /*
  410. lcd颜色填充
  411. @api lcd.fill(x1, y1, x2, y2,color)
  412. @int 左上边缘的X位置.
  413. @int 左上边缘的Y位置.
  414. @int 右上边缘的X位置.
  415. @int 右上边缘的Y位置.
  416. @int 绘画颜色 可选参数,默认背景色
  417. @usage
  418. -- lcd颜色填充
  419. lcd.fill(20,30,220,30,0x0000)
  420. */
  421. static int l_lcd_draw_fill(lua_State* L) {
  422. uint16_t x1, y1, x2, y2;
  423. luat_color_t color = BACK_COLOR;
  424. x1 = luaL_checkinteger(L, 1);
  425. y1 = luaL_checkinteger(L, 2);
  426. x2 = luaL_checkinteger(L, 3);
  427. y2 = luaL_checkinteger(L, 4);
  428. if (lua_gettop(L) > 4)
  429. color = (luat_color_t)luaL_checkinteger(L, 5);
  430. int ret = luat_lcd_draw_fill(default_conf, x1, y1, x2, y2, color);
  431. lua_pushboolean(L, ret == 0 ? 1 : 0);
  432. return 1;
  433. }
  434. /*
  435. 画一个点.
  436. @api lcd.drawPoint(x0,y0,color)
  437. @int 点的X位置.
  438. @int 点的Y位置.
  439. @int 绘画颜色 可选参数,默认前景色
  440. @usage
  441. lcd.drawPoint(20,30,0x001F)
  442. */
  443. static int l_lcd_draw_point(lua_State* L) {
  444. uint16_t x, y;
  445. luat_color_t color = FORE_COLOR;
  446. x = luaL_checkinteger(L, 1);
  447. y = luaL_checkinteger(L, 2);
  448. if (lua_gettop(L) > 2)
  449. color = (luat_color_t)luaL_checkinteger(L, 3);
  450. int ret = luat_lcd_draw_point(default_conf, x, y, color);
  451. lua_pushboolean(L, ret == 0 ? 1 : 0);
  452. return 1;
  453. }
  454. /*
  455. 在两点之间画一条线.
  456. @api lcd.drawLine(x0,y0,x1,y1,color)
  457. @int 第一个点的X位置.
  458. @int 第一个点的Y位置.
  459. @int 第二个点的X位置.
  460. @int 第二个点的Y位置.
  461. @int 绘画颜色 可选参数,默认前景色
  462. @usage
  463. lcd.drawLine(20,30,220,30,0x001F)
  464. */
  465. static int l_lcd_draw_line(lua_State* L) {
  466. uint16_t x1, y1, x2, y2;
  467. luat_color_t color = FORE_COLOR;
  468. x1 = luaL_checkinteger(L, 1);
  469. y1 = luaL_checkinteger(L, 2);
  470. x2 = luaL_checkinteger(L, 3);
  471. y2 = luaL_checkinteger(L, 4);
  472. if (lua_gettop(L) > 4)
  473. color = (luat_color_t)luaL_checkinteger(L, 5);
  474. int ret = luat_lcd_draw_line(default_conf, x1, y1, x2, y2, color);
  475. lua_pushboolean(L, ret == 0 ? 1 : 0);
  476. return 1;
  477. }
  478. /*
  479. 从x / y位置(左上边缘)开始绘制一个框
  480. @api lcd.drawRectangle(x0,y0,x1,y1,color)
  481. @int 左上边缘的X位置.
  482. @int 左上边缘的Y位置.
  483. @int 右下边缘的X位置.
  484. @int 右下边缘的Y位置.
  485. @int 绘画颜色 可选参数,默认前景色
  486. @usage
  487. lcd.drawRectangle(20,40,220,80,0x001F)
  488. */
  489. static int l_lcd_draw_rectangle(lua_State* L) {
  490. uint16_t x1, y1, x2, y2;
  491. luat_color_t color = FORE_COLOR;
  492. x1 = luaL_checkinteger(L, 1);
  493. y1 = luaL_checkinteger(L, 2);
  494. x2 = luaL_checkinteger(L, 3);
  495. y2 = luaL_checkinteger(L, 4);
  496. if (lua_gettop(L) > 4)
  497. color = (luat_color_t)luaL_checkinteger(L, 5);
  498. int ret = luat_lcd_draw_rectangle(default_conf, x1, y1, x2, y2, color);
  499. lua_pushboolean(L, ret == 0 ? 1 : 0);
  500. return 1;
  501. }
  502. /*
  503. 从x / y位置(圆心)开始绘制一个圆
  504. @api lcd.drawCircle(x0,y0,r,color)
  505. @int 圆心的X位置.
  506. @int 圆心的Y位置.
  507. @int 半径.
  508. @int 绘画颜色 可选参数,默认前景色
  509. @usage
  510. lcd.drawCircle(120,120,20,0x001F)
  511. */
  512. static int l_lcd_draw_circle(lua_State* L) {
  513. uint16_t x0, y0, r;
  514. luat_color_t color = FORE_COLOR;
  515. x0 = luaL_checkinteger(L, 1);
  516. y0 = luaL_checkinteger(L, 2);
  517. r = luaL_checkinteger(L, 3);
  518. if (lua_gettop(L) > 3)
  519. color = (luat_color_t)luaL_checkinteger(L, 4);
  520. int ret = luat_lcd_draw_circle(default_conf, x0, y0, r, color);
  521. lua_pushboolean(L, ret == 0 ? 1 : 0);
  522. return 1;
  523. }
  524. /**
  525. 缓冲区绘制QRCode
  526. @api lcd.drawQrcode(x, y, str, version)
  527. @int x坐标
  528. @int y坐标
  529. @string 二维码的内容
  530. @int 二维码版本号 可选1_40 对应21*21到177*177
  531. @return nil 无返回值
  532. */
  533. static int l_lcd_drawQrcode(lua_State *L)
  534. {
  535. size_t len;
  536. int x = luaL_checkinteger(L, 1);
  537. int y = luaL_checkinteger(L, 2);
  538. const char* str = luaL_checklstring(L, 3, &len);
  539. int version = luaL_checkinteger(L, 4);
  540. // Create the QR code
  541. QRCode qrcode;
  542. uint8_t qrcodeData[qrcode_getBufferSize(version)];
  543. qrcode_initText(&qrcode, qrcodeData, version, ECC_LOW, str);
  544. for(int i = 0; i < qrcode.size; i++)
  545. {
  546. for (int j = 0; j < qrcode.size; j++)
  547. {
  548. qrcode_getModule(&qrcode, j, i) ? luat_lcd_draw_point(default_conf, x+j, y+i, FORE_COLOR) : luat_lcd_draw_point(default_conf, x+j, y+i, BACK_COLOR);
  549. }
  550. }
  551. return 0;
  552. }
  553. static uint8_t utf8_state;
  554. static uint16_t encoding;
  555. static uint16_t utf8_next(uint8_t b)
  556. {
  557. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  558. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  559. if ( utf8_state == 0 )
  560. {
  561. if ( b >= 0xfc ) /* 6 byte sequence */
  562. {
  563. utf8_state = 5;
  564. b &= 1;
  565. }
  566. else if ( b >= 0xf8 )
  567. {
  568. utf8_state = 4;
  569. b &= 3;
  570. }
  571. else if ( b >= 0xf0 )
  572. {
  573. utf8_state = 3;
  574. b &= 7;
  575. }
  576. else if ( b >= 0xe0 )
  577. {
  578. utf8_state = 2;
  579. b &= 15;
  580. }
  581. else if ( b >= 0xc0 )
  582. {
  583. utf8_state = 1;
  584. b &= 0x01f;
  585. }
  586. else
  587. {
  588. /* do nothing, just use the value as encoding */
  589. return b;
  590. }
  591. encoding = b;
  592. return 0x0fffe;
  593. }
  594. else
  595. {
  596. utf8_state--;
  597. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  598. encoding<<=6;
  599. b &= 0x03f;
  600. encoding |= b;
  601. if ( utf8_state != 0 )
  602. return 0x0fffe; /* nothing to do yet */
  603. }
  604. return encoding;
  605. }
  606. static void u8g2_draw_hv_line(u8g2_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color){
  607. switch(dir)
  608. {
  609. case 0:
  610. luat_lcd_draw_hline(default_conf,x,y,len,color);
  611. break;
  612. case 1:
  613. luat_lcd_draw_vline(default_conf,x,y,len,color);
  614. break;
  615. case 2:
  616. luat_lcd_draw_hline(default_conf,x-len+1,y,len,color);
  617. break;
  618. case 3:
  619. luat_lcd_draw_vline(default_conf,x,y-len+1,len,color);
  620. break;
  621. }
  622. }
  623. static void u8g2_font_decode_len(u8g2_t *u8g2, uint8_t len, uint8_t is_foreground){
  624. uint8_t cnt; /* total number of remaining pixels, which have to be drawn */
  625. uint8_t rem; /* remaining pixel to the right edge of the glyph */
  626. uint8_t current; /* number of pixels, which need to be drawn for the draw procedure */
  627. /* current is either equal to cnt or equal to rem */
  628. /* local coordinates of the glyph */
  629. uint8_t lx,ly;
  630. /* target position on the screen */
  631. int16_t x, y;
  632. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  633. cnt = len;
  634. /* get the local position */
  635. lx = decode->x;
  636. ly = decode->y;
  637. for(;;){
  638. /* calculate the number of pixel to the right edge of the glyph */
  639. rem = decode->glyph_width;
  640. rem -= lx;
  641. /* calculate how many pixel to draw. This is either to the right edge */
  642. /* or lesser, if not enough pixel are left */
  643. current = rem;
  644. if ( cnt < rem )
  645. current = cnt;
  646. /* now draw the line, but apply the rotation around the glyph target position */
  647. //u8g2_font_decode_draw_pixel(u8g2, lx,ly,current, is_foreground);
  648. // printf("lx:%d,ly:%d,current:%d, is_foreground:%d \r\n",lx,ly,current, is_foreground);
  649. /* get target position */
  650. x = decode->target_x;
  651. y = decode->target_y;
  652. /* apply rotation */
  653. x = u8g2_add_vector_x(x, lx, ly, decode->dir);
  654. y = u8g2_add_vector_y(y, lx, ly, decode->dir);
  655. /* draw foreground and background (if required) */
  656. if ( current > 0 ) /* avoid drawing zero length lines, issue #4 */
  657. {
  658. if ( is_foreground )
  659. {
  660. u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, lcd_str_fg_color);
  661. }
  662. // else if ( decode->is_transparent == 0 )
  663. // {
  664. // u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, lcd_str_bg_color);
  665. // }
  666. }
  667. /* check, whether the end of the run length code has been reached */
  668. if ( cnt < rem )
  669. break;
  670. cnt -= rem;
  671. lx = 0;
  672. ly++;
  673. }
  674. lx += cnt;
  675. decode->x = lx;
  676. decode->y = ly;
  677. }
  678. static void u8g2_font_setup_decode(u8g2_t *u8g2, const uint8_t *glyph_data)
  679. {
  680. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  681. decode->decode_ptr = glyph_data;
  682. decode->decode_bit_pos = 0;
  683. /* 8 Nov 2015, this is already done in the glyph data search procedure */
  684. /*
  685. decode->decode_ptr += 1;
  686. decode->decode_ptr += 1;
  687. */
  688. decode->glyph_width = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_char_width);
  689. decode->glyph_height = u8g2_font_decode_get_unsigned_bits(decode,u8g2->font_info.bits_per_char_height);
  690. }
  691. static int8_t u8g2_font_decode_glyph(u8g2_t *u8g2, const uint8_t *glyph_data){
  692. uint8_t a, b;
  693. int8_t x, y;
  694. int8_t d;
  695. int8_t h;
  696. u8g2_font_decode_t *decode = &(u8g2->font_decode);
  697. u8g2_font_setup_decode(u8g2, glyph_data);
  698. h = u8g2->font_decode.glyph_height;
  699. x = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_x);
  700. y = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_y);
  701. d = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_delta_x);
  702. if ( decode->glyph_width > 0 )
  703. {
  704. decode->target_x = u8g2_add_vector_x(decode->target_x, x, -(h+y), decode->dir);
  705. decode->target_y = u8g2_add_vector_y(decode->target_y, x, -(h+y), decode->dir);
  706. //u8g2_add_vector(&(decode->target_x), &(decode->target_y), x, -(h+y), decode->dir);
  707. /* reset local x/y position */
  708. decode->x = 0;
  709. decode->y = 0;
  710. /* decode glyph */
  711. for(;;){
  712. a = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_0);
  713. b = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_1);
  714. do{
  715. u8g2_font_decode_len(u8g2, a, 0);
  716. u8g2_font_decode_len(u8g2, b, 1);
  717. } while( u8g2_font_decode_get_unsigned_bits(decode, 1) != 0 );
  718. if ( decode->y >= h )
  719. break;
  720. }
  721. }
  722. return d;
  723. }
  724. const uint8_t *u8g2_font_get_glyph_data(u8g2_t *u8g2, uint16_t encoding);
  725. static int16_t u8g2_font_draw_glyph(u8g2_t *u8g2, int16_t x, int16_t y, uint16_t encoding){
  726. int16_t dx = 0;
  727. u8g2->font_decode.target_x = x;
  728. u8g2->font_decode.target_y = y;
  729. const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, encoding);
  730. if ( glyph_data != NULL ){
  731. dx = u8g2_font_decode_glyph(u8g2, glyph_data);
  732. }
  733. return dx;
  734. }
  735. /*
  736. 设置字体
  737. @api lcd.setFont(font)
  738. @int font lcd.font_opposansm8 lcd.font_opposansm10 lcd.font_opposansm16 lcd.font_opposansm18 lcd.font_opposansm20 lcd.font_opposansm22 lcd.font_opposansm24 lcd.font_opposansm32 lcd.font_opposansm12_chinese lcd.font_opposansm16_chinese lcd.font_opposansm24_chinese lcd.font_opposansm32_chinese
  739. @usage
  740. -- 设置为字体,对之后的drawStr有效,调用lcd.drawStr前一定要先设置
  741. -- 使用中文字体需在luat_conf_bsp.h中开启相对应的宏
  742. lcd.setFont(lcd.font_opposansm12)
  743. lcd.drawStr(40,10,"drawStr")
  744. sys.wait(2000)
  745. lcd.setFont(lcd.font_opposansm12_chinese)
  746. lcd.drawStr(40,40,"drawStr测试")
  747. */
  748. static int l_lcd_set_font(lua_State *L) {
  749. if (!lua_islightuserdata(L, 1)) {
  750. LLOGE("only font pointer is allow");
  751. return 0;
  752. }
  753. const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
  754. if (ptr == NULL) {
  755. LLOGE("only font pointer is allow");
  756. return 0;
  757. }
  758. u8g2_SetFont(&(default_conf->luat_lcd_u8g2), ptr);
  759. lua_pushboolean(L, 1);
  760. return 1;
  761. }
  762. /*
  763. 显示字符串
  764. @api lcd.drawStr(x,y,str,fg_color)
  765. @int x 横坐标
  766. @int y 竖坐标 注意:此(x,y)为左下起始坐标
  767. @string str 文件内容
  768. @int fg_color str颜色 注意:此参数可选,如不填写则使用之前设置的颜色,绘制只会绘制字体部分,背景需要自己清除
  769. @usage
  770. -- 显示之前先设置为中文字体,对之后的drawStr有效,使用中文字体需在luat_conf_bsp.h.h开启#define USE_U8G2_OPPOSANSMxx_CHINESE xx代表字号
  771. lcd.setFont(lcd.font_opposansm12)
  772. lcd.drawStr(40,10,"drawStr")
  773. sys.wait(2000)
  774. lcd.setFont(lcd.font_opposansm16_chinese)
  775. lcd.drawStr(40,40,"drawStr测试")
  776. */
  777. static int l_lcd_draw_str(lua_State* L) {
  778. int x, y;
  779. size_t sz;
  780. const uint8_t* data;
  781. x = luaL_checkinteger(L, 1);
  782. y = luaL_checkinteger(L, 2);
  783. data = (const uint8_t*)luaL_checklstring(L, 3, &sz);
  784. lcd_str_fg_color = (luat_color_t)luaL_optinteger(L, 4,FORE_COLOR);
  785. // lcd_str_bg_color = (uint32_t)luaL_optinteger(L, 5,BACK_COLOR);
  786. if (sz == 0)
  787. return 0;
  788. uint16_t e;
  789. int16_t delta;
  790. utf8_state = 0;
  791. for(;;){
  792. e = utf8_next((uint8_t)*data);
  793. if ( e == 0x0ffff )
  794. break;
  795. data++;
  796. if ( e != 0x0fffe ){
  797. delta = u8g2_font_draw_glyph(&(default_conf->luat_lcd_u8g2), x, y, e);
  798. switch(default_conf->luat_lcd_u8g2.font_decode.dir){
  799. case 0:
  800. x += delta;
  801. break;
  802. case 1:
  803. y += delta;
  804. break;
  805. case 2:
  806. x -= delta;
  807. break;
  808. case 3:
  809. y -= delta;
  810. break;
  811. }
  812. }
  813. }
  814. return 0;
  815. }
  816. #ifdef LUAT_USE_GTFONT
  817. #include "GT5SLCD2E_1A.h"
  818. extern void gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int widt,unsigned int high,int(*point)(void*),void* userdata,int mode);
  819. 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);
  820. /*
  821. 使用gtfont显示gb2312字符串
  822. @api lcd.drawGtfontGb2312(str,size,x,y)
  823. @string str 显示字符串
  824. @int size 字体大小 (支持16-192号大小字体)
  825. @int x 横坐标
  826. @int y 竖坐标
  827. @usage
  828. lcd.drawGtfontGb2312("啊啊啊",32,0,0)
  829. */
  830. static int l_lcd_draw_gtfont_gb2312(lua_State *L) {
  831. unsigned char buf[128];
  832. size_t len;
  833. int i = 0;
  834. uint8_t strhigh,strlow ;
  835. uint16_t str;
  836. const char *fontCode = luaL_checklstring(L, 1,&len);
  837. unsigned char size = luaL_checkinteger(L, 2);
  838. int x = luaL_checkinteger(L, 3);
  839. int y = luaL_checkinteger(L, 4);
  840. while ( i < len){
  841. strhigh = *fontCode;
  842. fontCode++;
  843. strlow = *fontCode;
  844. str = (strhigh<<8)|strlow;
  845. fontCode++;
  846. int font_size = get_font(buf, 1, str, size, size, size);
  847. if(font_size != size){
  848. LLOGW("get gtfont error");
  849. return 0;
  850. }
  851. gtfont_draw_w(buf , x ,y , size , size,luat_lcd_draw_point,default_conf,0);
  852. x+=size;
  853. i+=2;
  854. }
  855. return 0;
  856. }
  857. /*
  858. 使用gtfont灰度显示gb2312字符串
  859. @api lcd.drawGtfontGb2312Gray(str,size,gray,x,y)
  860. @string str 显示字符串
  861. @int size 字体大小 (支持16-192号大小字体)
  862. @int gray 灰度[1阶/2阶/3阶/4阶]
  863. @int x 横坐标
  864. @int y 竖坐标
  865. @usage
  866. lcd.drawGtfontGb2312Gray("啊啊啊",32,4,0,40)
  867. */
  868. static int l_lcd_draw_gtfont_gb2312_gray(lua_State* L) {
  869. unsigned char buf[2048];
  870. size_t len;
  871. int i = 0;
  872. uint8_t strhigh,strlow ;
  873. uint16_t str;
  874. const char *fontCode = luaL_checklstring(L, 1,&len);
  875. unsigned char size = luaL_checkinteger(L, 2);
  876. unsigned char font_g = luaL_checkinteger(L, 3);
  877. int x = luaL_checkinteger(L, 4);
  878. int y = luaL_checkinteger(L, 5);
  879. while ( i < len){
  880. strhigh = *fontCode;
  881. fontCode++;
  882. strlow = *fontCode;
  883. str = (strhigh<<8)|strlow;
  884. fontCode++;
  885. int font_size = get_font(buf, 1, str, size*font_g, size*font_g, size*font_g);
  886. if(font_size != size*font_g){
  887. LLOGW("get gtfont error");
  888. return 0;
  889. }
  890. Gray_Process(buf,size,size,font_g);
  891. gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,luat_lcd_draw_point,default_conf,0);
  892. x+=size;
  893. i+=2;
  894. }
  895. return 0;
  896. }
  897. #ifdef LUAT_USE_GTFONT_UTF8
  898. extern unsigned short unicodetogb2312 ( unsigned short chr);
  899. /*
  900. 使用gtfont显示UTF8字符串
  901. @api lcd.drawGtfontUtf8(str,size,x,y)
  902. @string str 显示字符串
  903. @int size 字体大小 (支持16-192号大小字体)
  904. @int x 横坐标
  905. @int y 竖坐标
  906. @usage
  907. lcd.drawGtfontUtf8("啊啊啊",32,0,0)
  908. */
  909. static int l_lcd_draw_gtfont_utf8(lua_State *L) {
  910. unsigned char buf[128];
  911. size_t len;
  912. int i = 0;
  913. uint8_t strhigh,strlow ;
  914. uint16_t e,str;
  915. const char *fontCode = luaL_checklstring(L, 1,&len);
  916. unsigned char size = luaL_checkinteger(L, 2);
  917. int x = luaL_checkinteger(L, 3);
  918. int y = luaL_checkinteger(L, 4);
  919. for(;;){
  920. e = utf8_next((uint8_t)*fontCode);
  921. if ( e == 0x0ffff )
  922. break;
  923. fontCode++;
  924. if ( e != 0x0fffe ){
  925. uint16_t str = unicodetogb2312(e);
  926. int font_size = get_font(buf, 1, str, size, size, size);
  927. if(font_size != size){
  928. LLOGW("get gtfont error");
  929. return 0;
  930. }
  931. gtfont_draw_w(buf , x ,y , size , size,luat_lcd_draw_point,default_conf,0);
  932. x+=size;
  933. }
  934. }
  935. return 0;
  936. }
  937. /*
  938. 使用gtfont灰度显示UTF8字符串
  939. @api lcd.drawGtfontUtf8Gray(str,size,gray,x,y)
  940. @string str 显示字符串
  941. @int size 字体大小 (支持16-192号大小字体)
  942. @int gray 灰度[1阶/2阶/3阶/4阶]
  943. @int x 横坐标
  944. @int y 竖坐标
  945. @usage
  946. lcd.drawGtfontUtf8Gray("啊啊啊",32,4,0,40)
  947. */
  948. static int l_lcd_draw_gtfont_utf8_gray(lua_State* L) {
  949. unsigned char buf[2048];
  950. size_t len;
  951. int i = 0;
  952. uint8_t strhigh,strlow ;
  953. uint16_t e,str;
  954. const char *fontCode = luaL_checklstring(L, 1,&len);
  955. unsigned char size = luaL_checkinteger(L, 2);
  956. unsigned char font_g = luaL_checkinteger(L, 3);
  957. int x = luaL_checkinteger(L, 4);
  958. int y = luaL_checkinteger(L, 5);
  959. for(;;){
  960. e = utf8_next((uint8_t)*fontCode);
  961. if ( e == 0x0ffff )
  962. break;
  963. fontCode++;
  964. if ( e != 0x0fffe ){
  965. uint16_t str = unicodetogb2312(e);
  966. int font_size = get_font(buf, 1, str, size*font_g, size*font_g, size*font_g);
  967. if(font_size != size*font_g){
  968. LLOGW("get gtfont error");
  969. return 0;
  970. }
  971. Gray_Process(buf,size,size,font_g);
  972. gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,luat_lcd_draw_point,default_conf,0);
  973. x+=size;
  974. }
  975. }
  976. return 0;
  977. }
  978. #endif // LUAT_USE_GTFONT_UTF8
  979. #endif // LUAT_USE_GTFONT
  980. static int l_lcd_set_default(lua_State *L) {
  981. if (lua_gettop(L) == 1) {
  982. default_conf = lua_touserdata(L, 1);
  983. lua_pushboolean(L, 1);
  984. return 1;
  985. }
  986. return 1;
  987. }
  988. static int l_lcd_get_default(lua_State *L) {
  989. if (default_conf == NULL)
  990. return 0;
  991. lua_pushlightuserdata(L, default_conf);
  992. return 1;
  993. }
  994. /*
  995. 获取屏幕尺寸
  996. @api lcd.getSize()
  997. @return int 宽, 如果未初始化会返回0
  998. @return int 高, 如果未初始化会返回0
  999. @usage
  1000. log.info("lcd", "size", lcd.getSize())
  1001. */
  1002. static int l_lcd_get_size(lua_State *L) {
  1003. if (lua_gettop(L) == 1) {
  1004. luat_lcd_conf_t * conf = lua_touserdata(L, 1);
  1005. if (conf) {
  1006. lua_pushinteger(L, conf->w);
  1007. lua_pushinteger(L, conf->h);
  1008. return 2;
  1009. }
  1010. }
  1011. if (default_conf == NULL) {
  1012. lua_pushinteger(L, 0);
  1013. lua_pushinteger(L, 0);
  1014. }
  1015. else {
  1016. lua_pushinteger(L, default_conf->w);
  1017. lua_pushinteger(L, default_conf->h);
  1018. }
  1019. return 2;
  1020. }
  1021. static void lcd_DrawHXBM(uint16_t x, uint16_t y, uint16_t len, const uint8_t *b){
  1022. uint8_t mask;
  1023. mask = 1;
  1024. while(len > 0) {
  1025. if ( *b & mask ) luat_lcd_draw_hline(default_conf, x, y, 1,FORE_COLOR);
  1026. else luat_lcd_draw_vline(default_conf, x, y, 1,BACK_COLOR);
  1027. x++;
  1028. mask <<= 1;
  1029. if ( mask == 0 ){
  1030. mask = 1;
  1031. b++;
  1032. }
  1033. len--;
  1034. }
  1035. }
  1036. /*
  1037. 绘制位图
  1038. @api lcd.drawXbm(x, y, w, h, data)
  1039. @int X坐标
  1040. @int y坐标
  1041. @int 位图宽
  1042. @int 位图高
  1043. @int 位图数据,每一位代表一个像素
  1044. @usage
  1045. -- 取模使用PCtoLCD2002软件即可
  1046. -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
  1047. lcd.drawXbm(0, 0, 16,16, string.char(
  1048. 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
  1049. 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
  1050. ))
  1051. */
  1052. static int l_lcd_drawxbm(lua_State *L){
  1053. int x = luaL_checkinteger(L, 1);
  1054. int y = luaL_checkinteger(L, 2);
  1055. int w = luaL_checkinteger(L, 3);
  1056. int h = luaL_checkinteger(L, 4);
  1057. size_t len = 0;
  1058. const char* data = luaL_checklstring(L, 5, &len);
  1059. if (h < 1) return 0; // 行数必须大于0
  1060. if (w < h) return 0; // 起码要填满一行
  1061. uint8_t blen;
  1062. blen = w;
  1063. blen += 7;
  1064. blen >>= 3;
  1065. while( h > 0 ){
  1066. lcd_DrawHXBM(x, y, w, (const uint8_t*)data);
  1067. data += blen;
  1068. y++;
  1069. h--;
  1070. }
  1071. lua_pushboolean(L, 1);
  1072. return 1;
  1073. }
  1074. #include "rotable2.h"
  1075. static const rotable_Reg_t reg_lcd[] =
  1076. {
  1077. { "init", ROREG_FUNC(l_lcd_init)},
  1078. { "close", ROREG_FUNC(l_lcd_close)},
  1079. { "on", ROREG_FUNC(l_lcd_display_on)},
  1080. { "off", ROREG_FUNC(l_lcd_display_off)},
  1081. { "sleep", ROREG_FUNC(l_lcd_sleep)},
  1082. { "wakeup", ROREG_FUNC(l_lcd_wakeup)},
  1083. { "invon", ROREG_FUNC(l_lcd_inv_on)},
  1084. { "invoff", ROREG_FUNC(l_lcd_inv_off)},
  1085. { "cmd", ROREG_FUNC(l_lcd_write_cmd)},
  1086. { "data", ROREG_FUNC(l_lcd_write_data)},
  1087. { "setColor", ROREG_FUNC(l_lcd_set_color)},
  1088. { "draw", ROREG_FUNC(l_lcd_draw)},
  1089. { "clear", ROREG_FUNC(l_lcd_clear)},
  1090. { "fill", ROREG_FUNC(l_lcd_draw_fill)},
  1091. { "drawPoint", ROREG_FUNC(l_lcd_draw_point)},
  1092. { "drawLine", ROREG_FUNC(l_lcd_draw_line)},
  1093. { "drawRectangle", ROREG_FUNC(l_lcd_draw_rectangle)},
  1094. { "drawCircle", ROREG_FUNC(l_lcd_draw_circle)},
  1095. { "drawQrcode", ROREG_FUNC(l_lcd_drawQrcode)},
  1096. { "drawStr", ROREG_FUNC(l_lcd_draw_str)},
  1097. { "setFont", ROREG_FUNC(l_lcd_set_font)},
  1098. { "setDefault", ROREG_FUNC(l_lcd_set_default)},
  1099. { "getDefault", ROREG_FUNC(l_lcd_get_default)},
  1100. { "getSize", ROREG_FUNC(l_lcd_get_size)},
  1101. { "drawXbm", ROREG_FUNC(l_lcd_drawxbm)},
  1102. #ifdef LUAT_USE_GTFONT
  1103. { "drawGtfontGb2312", ROREG_FUNC(l_lcd_draw_gtfont_gb2312)},
  1104. { "drawGtfontGb2312Gray", ROREG_FUNC(l_lcd_draw_gtfont_gb2312_gray)},
  1105. #ifdef LUAT_USE_GTFONT_UTF8
  1106. { "drawGtfontUtf8", ROREG_FUNC(l_lcd_draw_gtfont_utf8)},
  1107. { "drawGtfontUtf8Gray", ROREG_FUNC(l_lcd_draw_gtfont_utf8_gray)},
  1108. #endif // LUAT_USE_GTFONT_UTF8
  1109. #endif // LUAT_USE_GTFONT
  1110. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  1111. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  1112. { "font_opposansm8", ROREG_PTR((void*)u8g2_font_opposansm8)},
  1113. { "font_opposansm10", ROREG_PTR((void*)u8g2_font_opposansm10)},
  1114. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  1115. { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
  1116. { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
  1117. { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
  1118. { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
  1119. { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
  1120. { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
  1121. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  1122. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  1123. #endif
  1124. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  1125. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  1126. #endif
  1127. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  1128. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  1129. #endif
  1130. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  1131. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  1132. #endif
  1133. {NULL, ROREG_INT(0)}
  1134. };
  1135. LUAMOD_API int luaopen_lcd( lua_State *L ) {
  1136. luat_newlib2(L, reg_lcd);
  1137. return 1;
  1138. }