luat_lib_lcd.c 37 KB

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