luat_lib_lcd.c 38 KB

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