luat_lib_u8g2.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /*
  2. @module u8g2
  3. @summary u8g2图形处理库
  4. @author Dozingfiretruck
  5. @version 1.0
  6. @date 2021.01.25
  7. @demo u8g2
  8. @tag LUAT_USE_U8G2
  9. */
  10. #include "luat_base.h"
  11. #include "luat_malloc.h"
  12. #include "luat_u8g2.h"
  13. #include "luat_gpio.h"
  14. #include "luat_timer.h"
  15. #include "luat_i2c.h"
  16. #include "luat_spi.h"
  17. #include "qrcodegen.h"
  18. #include "u8g2.h"
  19. #include "u8g2_luat_fonts.h"
  20. #define LUAT_LOG_TAG "u8g2"
  21. #include "luat_log.h"
  22. static luat_u8g2_conf_t* conf = NULL;
  23. uint8_t pinType = 255; // I2C_SW = 1, I2C_HW = 2, SPI_SW_3PIN = 3, SPI_SW_4PIN = 4, SPI_HW_4PIN=5, P8080 = 6
  24. static uint8_t i2c_id;
  25. static uint8_t i2c_speed;
  26. static uint8_t i2c_scl;
  27. static uint8_t i2c_sda;
  28. // static uint8_t i2c_addr = 0x3C;
  29. static uint8_t spi_id;
  30. static uint8_t spi_res;
  31. static uint8_t spi_dc;
  32. static uint8_t spi_cs;
  33. static const char* mode_strs[] = {
  34. "i2c_sw",
  35. "i2c_hw",
  36. "spi_sw_3pin",
  37. "spi_sw_4pin",
  38. "spi_hw_4pin"
  39. };
  40. typedef void (*dev_setup_cb)(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb);
  41. typedef struct luat_u8g2_dev_reg
  42. {
  43. const char* name;
  44. dev_setup_cb devcb;
  45. uint16_t w; // 屏幕宽, 最大值
  46. uint16_t h; // 屏幕高, 最大值
  47. uint16_t spi_i2c; // 使用 I2C 0, 使用 SPI 1
  48. }luat_u8g2_dev_reg_t;
  49. static const luat_u8g2_dev_reg_t devregs[] = {
  50. // ssd1306是默认值
  51. {.name="ssd1306", .w=128, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x64_noname_f}, // ssd1306 128x64,I2C
  52. {.name="ssd1306", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_ssd1306_128x64_noname_f}, // ssd1306 128x64,SPI
  53. {.name="custom", .w=0, .h=0, .spi_i2c=0, .devcb=u8g2_Setup_custom_i2c_noname_f}, // custom,I2C
  54. {.name="custom", .w=0, .h=0, .spi_i2c=1, .devcb=u8g2_Setup_custom_noname_f}, // custom,SPI
  55. {.name="ssd1309", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_ssd1309_128x64_noname2_f}, // ssd1309 128x64,SPI
  56. {.name="ssd1322", .w=256, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_ssd1322_nhd_256x64_f}, // ssd1322 128x64
  57. {.name="sh1106", .w=128, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_sh1106_i2c_128x64_noname_f}, // sh1106 128x64,I2C
  58. {.name="sh1106", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_sh1106_128x64_noname_f}, // sh1106 128x64,SPI
  59. {.name="sh1107", .w=64, .h=128, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x64_noname_f}, // sh1107 64x128
  60. {.name="sh1108", .w=160, .h=160, .spi_i2c=0, .devcb=u8g2_Setup_sh1108_i2c_160x160_f}, // sh1108 160x160
  61. {.name="st7567", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_st7567_jlx12864_f}, // st7567 128x64
  62. {.name="uc1701", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_uc1701_mini12864_f}, // uc1701
  63. {.name="ssd1306_128x32", .w=128, .h=32, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x32_univision_f}, // ssd1306 128x32,I2C
  64. {.name="st7565", .w=132, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_st7565_ea_dogm132_f}, // st7565 128x32,SPI
  65. {.name="st7565_jlx12864g109pc", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_st7565_jlx12864g109pc_f}, // 2023年8月4日 晶联讯12864G-109-PC,12864G-139-P
  66. {.name=NULL} // 结尾用,必须加.
  67. };
  68. /*
  69. u8g2显示屏初始化
  70. @api u8g2.begin(conf)
  71. @table conf 配置信息 ic:支持 custom(自定义命令) ssd1306(默认) ssd1309 ssd1322 sh1106 sh1107 sh1108 st7567 uc1701 ssd1306_128x32, direction:方向,可选0 90 180 270 默认0 mode:模式,可选i2c_sw:软件i2c i2c_hw:硬件i2c spi_hw_4pin:硬件spi i2c_id:硬件i2c时有效 i2c_scl=1、i2c_sda:软件i2c时有效 spi_id、spi_res、spi_dc、spi_cs:硬件spi时生效
  72. @return int 正常初始化1,已经初始化过2,内存不够3,初始化失败返回4
  73. @usage
  74. -- 初始化硬件i2c的ssd1306
  75. u8g2.begin({ic = "ssd1306",direction = 0,mode="i2c_hw",i2c_id=0}) -- direction 可选0 90 180 270
  76. -- 初始化硬件spi的ssd1306
  77. u8g2.begin({ic = "ssd1306",direction = 0,mode="spi_hw_4pin",spi_id=0,spi_res=pin.PB03,spi_dc=pin.PB01,spi_cs=pin.PB04}) -- direction 可选0 90 180 270
  78. -- 初始化软件i2c的ssd1306
  79. u8g2.begin({ic = "ssd1306",direction = 0,mode="i2c_sw", i2c_scl=1, i2c_sda=4}) -- 通过PA1 SCL / PA4 SDA模拟
  80. */
  81. static int l_u8g2_begin(lua_State *L) {
  82. if (conf != NULL) {
  83. LLOGW("disp is aready inited");
  84. lua_pushinteger(L, 2);
  85. return 1;
  86. }
  87. conf = (luat_u8g2_conf_t*)lua_newuserdata(L, sizeof(luat_u8g2_conf_t));
  88. if (conf == NULL) {
  89. LLOGE("lua_newuserdata return NULL, out of memory ?!");
  90. lua_pushinteger(L, 3);
  91. return 1;
  92. }
  93. conf->u8g2.u8x8.user_ptr = conf;
  94. conf->direction = U8G2_R0;
  95. char mode[12] = {0};
  96. size_t mode_len = 0;
  97. if (lua_istable(L, 1)) {
  98. // 参数解析
  99. lua_pushliteral(L, "ic");
  100. lua_gettable(L, 1);
  101. if (lua_isstring(L, -1)) {
  102. conf->cname = (char*)luaL_checkstring(L, -1);
  103. //LLOGD("using ic: %s",conf.cname);
  104. }
  105. lua_pop(L, 1);
  106. lua_pushliteral(L, "direction");
  107. lua_gettable(L, 1);
  108. if (lua_isinteger(L, -1)) {
  109. int direction = luaL_checkinteger(L, -1);
  110. switch (direction)
  111. {
  112. case 0:
  113. conf->direction = U8G2_R0;
  114. break;
  115. case 90:
  116. conf->direction = U8G2_R1;
  117. break;
  118. case 180:
  119. conf->direction = U8G2_R2;
  120. break;
  121. case 270:
  122. conf->direction = U8G2_R3;
  123. break;
  124. default:
  125. conf->direction = U8G2_R0;
  126. break;
  127. }
  128. }
  129. lua_pop(L, 1);
  130. lua_pushliteral(L, "mode");
  131. lua_gettable(L, 1);
  132. if (!lua_isstring(L, -1)) {
  133. LLOGE("need mode!!!");
  134. return 0;
  135. }
  136. const char* tmp = luaL_checklstring(L, -1, &mode_len);
  137. if (mode_len < 1 || mode_len > 16) {
  138. LLOGE("mode string too short or too long!!");
  139. return 0;
  140. }
  141. memcpy(mode, tmp, strlen(tmp));
  142. lua_pop(L, 1);
  143. for (size_t i = 0; i < sizeof(mode_strs) / sizeof(const char*); i++)
  144. {
  145. if (strcmp(mode_strs[i], tmp) == 0) {
  146. pinType = i + 1;
  147. break;
  148. }
  149. }
  150. if (pinType == 255) {
  151. LLOGE("no such mode [%s]", tmp);
  152. return 0;
  153. }
  154. lua_pushliteral(L, "i2c_scl");
  155. lua_gettable(L, 1);
  156. if (lua_isinteger(L, -1)) {
  157. i2c_scl = luaL_checkinteger(L, -1);
  158. }
  159. lua_pop(L, 1);
  160. lua_pushliteral(L, "i2c_sda");
  161. lua_gettable(L, 1);
  162. if (lua_isinteger(L, -1)) {
  163. i2c_sda = luaL_checkinteger(L, -1);
  164. }
  165. lua_pop(L, 1);
  166. lua_pushliteral(L, "i2c_id");
  167. lua_gettable(L, 1);
  168. if (lua_isinteger(L, -1)) {
  169. i2c_id = luaL_checkinteger(L, -1);
  170. }
  171. lua_pop(L, 1);
  172. lua_pushliteral(L, "i2c_speed");
  173. lua_gettable(L, 1);
  174. if (lua_isinteger(L, -1)) {
  175. i2c_speed = luaL_checkinteger(L, -1);
  176. }
  177. lua_pop(L, 1);
  178. lua_pushliteral(L, "spi_id");
  179. lua_gettable(L, 1);
  180. if (lua_isinteger(L, -1)) {
  181. spi_id = luaL_checkinteger(L, -1);
  182. }
  183. lua_pop(L, 1);
  184. lua_pushliteral(L, "spi_res");
  185. lua_gettable(L, 1);
  186. if (lua_isinteger(L, -1)) {
  187. spi_res = luaL_checkinteger(L, -1);
  188. }
  189. lua_pop(L, 1);
  190. lua_pushliteral(L, "spi_dc");
  191. lua_gettable(L, 1);
  192. if (lua_isinteger(L, -1)) {
  193. spi_dc = luaL_checkinteger(L, -1);
  194. }
  195. lua_pop(L, 1);
  196. lua_pushliteral(L, "spi_cs");
  197. lua_gettable(L, 1);
  198. if (lua_isinteger(L, -1)) {
  199. spi_cs = luaL_checkinteger(L, -1);
  200. }
  201. lua_pop(L, 1);
  202. }
  203. if (lua_istable(L, 2) && strcmp("custom", conf->cname) == 0){
  204. lua_pushliteral(L, "width");
  205. lua_gettable(L, 2);
  206. if (lua_isinteger(L, -1)) {
  207. conf->w = luaL_checkinteger(L, -1);
  208. }
  209. lua_pop(L, 1);
  210. lua_pushliteral(L, "height");
  211. lua_gettable(L, 2);
  212. if (lua_isinteger(L, -1)) {
  213. conf->h = luaL_checkinteger(L, -1);
  214. }
  215. lua_pop(L, 1);
  216. lua_pushliteral(L, "sleepcmd");
  217. lua_gettable(L, 2);
  218. if (lua_isinteger(L, -1)) {
  219. conf->sleepcmd = luaL_checkinteger(L, -1);
  220. }
  221. lua_pop(L, 1);
  222. lua_pushliteral(L, "wakecmd");
  223. lua_gettable(L, 2);
  224. if (lua_isinteger(L, -1)) {
  225. conf->wakecmd = luaL_checkinteger(L, -1);
  226. }
  227. lua_pop(L, 1);
  228. luat_u8g2_custom_t *cst = luat_heap_malloc(sizeof(luat_u8g2_custom_t));
  229. lua_pushstring(L, "initcmd");
  230. lua_gettable(L, 2);
  231. if (lua_istable(L, -1)) {
  232. cst->init_cmd_count = lua_rawlen(L, -1);
  233. cst->initcmd = luat_heap_malloc(cst->init_cmd_count * sizeof(uint32_t));
  234. for (size_t i = 1; i <= cst->init_cmd_count; i++){
  235. lua_geti(L, -1, i);
  236. cst->initcmd[i-1] = luaL_checkinteger(L, -1);
  237. lua_pop(L, 1);
  238. }
  239. }
  240. lua_pop(L, 1);
  241. conf->userdata = cst;
  242. }
  243. LLOGD("driver %s mode %s", conf->cname, mode);
  244. if (luat_u8g2_setup(conf)) {
  245. conf = NULL;
  246. LLOGW("disp init fail");
  247. lua_pushinteger(L, 4);
  248. return 1; // 初始化失败
  249. }
  250. LLOGD("setup done");
  251. conf->lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  252. u8g2_SetFont(&conf->u8g2, u8g2_font_ncenB08_tr); // 设置默认字体
  253. lua_pushinteger(L, 1);
  254. return 1;
  255. }
  256. /*
  257. 关闭显示屏
  258. @api u8g2.close()
  259. @usage
  260. -- 关闭disp,再次使用disp相关API的话,需要重新初始化
  261. u8g2.close()
  262. */
  263. static int l_u8g2_close(lua_State *L) {
  264. if (conf == NULL) return 0;
  265. if (conf->lua_ref != 0) {
  266. lua_geti(L, LUA_REGISTRYINDEX, conf->lua_ref);
  267. if (lua_isuserdata(L, -1)) {
  268. luaL_unref(L, LUA_REGISTRYINDEX, conf->lua_ref);
  269. }
  270. conf->lua_ref = 0;
  271. }
  272. // buff也得释放掉
  273. if (conf->buff_ptr != NULL) {
  274. luat_heap_free(conf->buff_ptr);
  275. conf->buff_ptr = NULL;
  276. }
  277. conf = NULL;
  278. lua_gc(L, LUA_GCCOLLECT, 0);
  279. lua_gc(L, LUA_GCCOLLECT, 0);
  280. return 0;
  281. }
  282. /*
  283. 清屏,清除内存帧缓冲区中的所有像素
  284. @api u8g2.ClearBuffer()
  285. @usage
  286. -- 清屏
  287. u8g2.ClearBuffer()
  288. */
  289. static int l_u8g2_ClearBuffer(lua_State *L) {
  290. if (conf == NULL) return 0;
  291. u8g2_ClearBuffer(&conf->u8g2);
  292. return 0;
  293. }
  294. /*
  295. 将数据更新到屏幕,将存储器帧缓冲区的内容发送到显示器
  296. @api u8g2.SendBuffer()
  297. @usage
  298. -- 把显示数据更新到屏幕
  299. u8g2.SendBuffer()
  300. */
  301. static int l_u8g2_SendBuffer(lua_State *L) {
  302. if (conf == NULL) return 0;
  303. u8g2_SendBuffer(&conf->u8g2);
  304. return 0;
  305. }
  306. /*
  307. 在显示屏上画一段文字,在显示屏上画一段文字,要调用u8g2.SendBuffer()才会更新到屏幕
  308. @api u8g2.DrawUTF8(str, x, y)
  309. @string 文件内容
  310. @int 横坐标
  311. @int 竖坐标
  312. @usage
  313. u8g2.DrawUTF8("wifi is ready", 10, 20)
  314. */
  315. static int l_u8g2_DrawUTF8(lua_State *L) {
  316. if (conf == NULL) {
  317. LLOGW("disp not init yet!!!");
  318. return 0;
  319. }
  320. size_t len;
  321. size_t x, y;
  322. const char* str = luaL_checklstring(L, 1, &len);
  323. x = luaL_checkinteger(L, 2);
  324. y = luaL_checkinteger(L, 3);
  325. u8g2_DrawUTF8(&conf->u8g2, x, y, str);
  326. return 0;
  327. }
  328. /*
  329. 在提供的文本周围画一个框。这与 DrawUTF8 类似,但为文本添加了一些装饰。,要调用u8g2.SendBuffer()才会更新到屏幕
  330. @api u8g2.DrawButtonUTF8(str, x, y, flags, w, h, v, str)
  331. @string 文件内容
  332. @int 横坐标
  333. @int 竖坐标
  334. @int 标志 多个标志可以与“或”运算符一起使用。
  335. @int 文本的最小宽度。如果为0(或低于文本宽度),则文本宽度将用于框架。
  336. @int 文本前后的额外空间。
  337. @int 文本上下的额外空间。
  338. @usage
  339. u8g2.DrawButtonUTF8("str", 10, 20,u8g2.BTN_BW2,0,2,2)
  340. */
  341. static int l_u8g2_DrawButtonUTF8(lua_State *L) {
  342. if (conf == NULL) {
  343. LLOGW("disp not init yet!!!");
  344. return 0;
  345. }
  346. size_t len;
  347. size_t x, y,flags,w,h,v;
  348. const char* str = luaL_checklstring(L, 1, &len);
  349. x = luaL_checkinteger(L, 2);
  350. y = luaL_checkinteger(L, 3);
  351. flags = luaL_checkinteger(L, 4);
  352. w = luaL_checkinteger(L, 5);
  353. h = luaL_checkinteger(L, 6);
  354. v = luaL_checkinteger(L, 7);
  355. u8g2_DrawButtonUTF8(&conf->u8g2, x, y, flags, w, h, v, str);
  356. return 0;
  357. }
  358. /*
  359. 设置字体模式
  360. @api u8g2.SetFontMode(mode)
  361. @int mode字体模式,启用(1)或禁用(0)透明模式
  362. @usage
  363. u8g2.SetFontMode(1)
  364. */
  365. static int l_u8g2_SetFontMode(lua_State *L){
  366. if (conf == NULL) return 0;
  367. int font_mode = luaL_checkinteger(L, 1);
  368. if (font_mode < 0) {
  369. lua_pushboolean(L, 0);
  370. }
  371. u8g2_SetFontMode(&conf->u8g2, font_mode);
  372. u8g2_SetFontDirection(&conf->u8g2, 0);
  373. lua_pushboolean(L, 1);
  374. return 1;
  375. }
  376. /*
  377. 设置字体
  378. @api u8g2.SetFont(font)
  379. @userdata font, u8g2.font_opposansm8 为纯英文8号字体,还有font_opposansm10 font_opposansm12 font_opposansm16 font_opposansm18 font_opposansm20 font_opposansm22 font_opposansm24 font_opposansm32 可选 u8g2.font_opposansm12_chinese 为12x12全中文,还有 font_opposansm16_chinese font_opposansm24_chinese font_opposansm32_chinese 可选, u8g2.font_unifont_t_symbols 为符号.
  380. @usage
  381. -- 设置为中文字体,对之后的drawStr有效
  382. u8g2.SetFont(u8g2.font_opposansm12)
  383. */
  384. static int l_u8g2_SetFont(lua_State *L) {
  385. if (conf == NULL) {
  386. LLOGI("u8g2 not init yet!!!");
  387. lua_pushboolean(L, 0);
  388. return 1;
  389. }
  390. if (!lua_islightuserdata(L, 1)) {
  391. LLOGE("no such font");
  392. return 0;
  393. }
  394. const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
  395. if (ptr == NULL) {
  396. LLOGE("only font pointer is allow");
  397. return 0;
  398. }
  399. u8g2_SetFont(&conf->u8g2, ptr);
  400. lua_pushboolean(L, 1);
  401. return 1;
  402. }
  403. /*
  404. 获取显示屏高度
  405. @api u8g2.GetDisplayHeight()
  406. @return int 显示屏高度
  407. @usage
  408. u8g2.GetDisplayHeight()
  409. */
  410. static int l_u8g2_GetDisplayHeight(lua_State *L){
  411. if (conf == NULL) return 0;
  412. lua_pushinteger(L, u8g2_GetDisplayHeight(&conf->u8g2));
  413. return 1;
  414. }
  415. /*
  416. 获取显示屏宽度
  417. @api u8g2.GetDisplayWidth()
  418. @return int 显示屏宽度
  419. @usage
  420. u8g2.GetDisplayWidth()
  421. */
  422. static int l_u8g2_GetDisplayWidth(lua_State *L){
  423. if (conf == NULL) return 0;
  424. lua_pushinteger(L, u8g2_GetDisplayWidth(&conf->u8g2));
  425. return 1;
  426. }
  427. /*
  428. 为所有绘图功能分配绘图颜色。
  429. @api u8g2.SetDrawColor(c)
  430. @int c为颜色值 0没有色 1有色 2与底色xor
  431. @usage
  432. u8g2.SetDrawColor(0)
  433. */
  434. static int l_u8g2_SetDrawColor(lua_State *L){
  435. if (conf == NULL) return 0;
  436. u8g2_SetDrawColor(&conf->u8g2,luaL_checkinteger(L, 1));
  437. return 0;
  438. }
  439. /*
  440. 画一个点.
  441. @api u8g2.DrawPixel(x,y)
  442. @int X位置.
  443. @int Y位置.
  444. @usage
  445. u8g2.DrawPixel(20, 5)
  446. */
  447. static int l_u8g2_DrawPixel(lua_State *L){
  448. if (conf == NULL) return 0;
  449. u8g2_DrawPixel(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2));
  450. return 0;
  451. }
  452. /*
  453. 在两点之间画一条线.
  454. @api u8g2.DrawLine(x0,y0,x1,y1)
  455. @int 第一个点的X位置.
  456. @int 第一个点的Y位置.
  457. @int 第二个点的X位置.
  458. @int 第二个点的Y位置.
  459. @usage
  460. u8g2.DrawLine(20, 5, 5, 32)
  461. */
  462. static int l_u8g2_DrawLine(lua_State *L){
  463. if (conf == NULL) return 0;
  464. u8g2_DrawLine(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  465. return 0;
  466. }
  467. /*
  468. 在x,y位置画一个半径为rad的空心圆.
  469. @api u8g2.DrawCircle(x0,y0,rad,opt)
  470. @int 圆心位置
  471. @int 圆心位置
  472. @int 圆半径.
  473. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  474. @usage
  475. u8g2.DrawCircle(60,30,8,u8g2.DRAW_ALL)
  476. */
  477. static int l_u8g2_DrawCircle(lua_State *L){
  478. if (conf == NULL) return 0;
  479. u8g2_DrawCircle(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_optinteger(L, 4,U8G2_DRAW_ALL));
  480. return 0;
  481. }
  482. /*
  483. 在x,y位置画一个半径为rad的实心圆.
  484. @api u8g2.DrawDisc(x0,y0,rad,opt)
  485. @int 圆心位置
  486. @int 圆心位置
  487. @int 圆半径.
  488. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  489. @usage
  490. u8g2.DrawDisc(60,30,8,u8g2.DRAW_ALL)
  491. */
  492. static int l_u8g2_DrawDisc(lua_State *L){
  493. if (conf == NULL) return 0;
  494. u8g2_DrawDisc(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_optinteger(L, 4,U8G2_DRAW_ALL));
  495. return 0;
  496. }
  497. /*
  498. 在x,y位置画一个半径为rad的空心椭圆.
  499. @api u8g2.DrawEllipse(x0,y0,rx,ry,opt)
  500. @int 圆心位置
  501. @int 圆心位置
  502. @int 椭圆大小
  503. @int 椭圆大小
  504. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  505. @usage
  506. u8g2.DrawEllipse(60,30,8,u8g2.DRAW_ALL)
  507. */
  508. static int l_u8g2_DrawEllipse(lua_State *L){
  509. if (conf == NULL) return 0;
  510. u8g2_DrawEllipse(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_optinteger(L, 5,U8G2_DRAW_ALL));
  511. return 0;
  512. }
  513. /*
  514. 在x,y位置画一个半径为rad的实心椭圆.
  515. @api u8g2.DrawFilledEllipse(x0,y0,rx,ry,opt)
  516. @int 圆心位置
  517. @int 圆心位置
  518. @int 椭圆大小
  519. @int 椭圆大小
  520. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  521. @usage
  522. u8g2.DrawFilledEllipse(60,30,8,15)
  523. */
  524. static int l_u8g2_DrawFilledEllipse(lua_State *L){
  525. if (conf == NULL) return 0;
  526. u8g2_DrawFilledEllipse(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_optinteger(L, 5,U8G2_DRAW_ALL));
  527. return 0;
  528. }
  529. /*
  530. 从x / y位置(左上边缘)开始绘制一个框(填充的框).
  531. @api u8g2.DrawBox(x,y,w,h)
  532. @int 左上边缘的X位置
  533. @int 左上边缘的Y位置
  534. @int 盒子的宽度
  535. @int 盒子的高度
  536. @usage
  537. u8g2.DrawBox(3,7,25,15)
  538. */
  539. static int l_u8g2_DrawBox(lua_State *L){
  540. if (conf == NULL) return 0;
  541. u8g2_DrawBox(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  542. return 0;
  543. }
  544. /*
  545. 从x / y位置(左上边缘)开始绘制一个框(空框).
  546. @api u8g2.DrawFrame(x,y,w,h)
  547. @int 左上边缘的X位置
  548. @int 左上边缘的Y位置
  549. @int 盒子的宽度
  550. @int 盒子的高度
  551. @usage
  552. u8g2.DrawFrame(3,7,25,15)
  553. */
  554. static int l_u8g2_DrawFrame(lua_State *L){
  555. if (conf == NULL) return 0;
  556. u8g2_DrawFrame(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  557. return 0;
  558. }
  559. /*
  560. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的填充框/框架.
  561. @api u8g2.DrawRBox(x,y,w,h,r)
  562. @int 左上边缘的X位置
  563. @int 左上边缘的Y位置
  564. @int 盒子的宽度
  565. @int 盒子的高度
  566. @int 四个边缘的半径
  567. @usage
  568. u8g2.DrawRBox(3,7,25,15)
  569. */
  570. static int l_u8g2_DrawRBox(lua_State *L){
  571. if (conf == NULL) return 0;
  572. u8g2_DrawRBox(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  573. return 0;
  574. }
  575. /*
  576. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的空框/框架.
  577. @api u8g2.DrawRFrame(x,y,w,h,r)
  578. @int 左上边缘的X位置
  579. @int 左上边缘的Y位置
  580. @int 盒子的宽度
  581. @int 盒子的高度
  582. @int 四个边缘的半径
  583. @usage
  584. u8g2.DrawRFrame(3,7,25,15)
  585. */
  586. static int l_u8g2_DrawRFrame(lua_State *L){
  587. if (conf == NULL) return 0;
  588. u8g2_DrawRFrame(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  589. return 0;
  590. }
  591. /*
  592. 绘制一个图形字符。字符放置在指定的像素位置x和y.
  593. @api u8g2.DrawGlyph(x,y,encoding)
  594. @int 字符在显示屏上的位置
  595. @int 字符在显示屏上的位置
  596. @int 字符的Unicode值
  597. @usage
  598. u8g2.SetFont(u8g2_font_unifont_t_symbols)
  599. u8g2.DrawGlyph(5, 20, 0x2603) -- dec 9731/hex 2603 Snowman
  600. */
  601. static int l_u8g2_DrawGlyph(lua_State *L){
  602. if (conf == NULL) return 0;
  603. u8g2_DrawGlyph(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3));
  604. return 0;
  605. }
  606. /*
  607. 绘制一个三角形(实心多边形).
  608. @api u8g2.DrawTriangle(x0,y0,x1,y1,x2,y2)
  609. @int 点0X位置
  610. @int 点0Y位置
  611. @int 点1X位置
  612. @int 点1Y位置
  613. @int 点2X位置
  614. @int 点2Y位置
  615. @usage
  616. u8g2.DrawTriangle(20,5, 27,50, 5,32)
  617. */
  618. static int l_u8g2_DrawTriangle(lua_State *L){
  619. if (conf == NULL) return 0;
  620. u8g2_DrawTriangle(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5),luaL_checkinteger(L, 6));
  621. return 0;
  622. }
  623. /*
  624. 定义位图函数是否将写入背景色
  625. @api u8g2.SetBitmapMode(mode)
  626. @int mode字体模式,启用(1)或禁用(0)透明模式
  627. @usage
  628. u8g2.SetBitmapMode(1)
  629. */
  630. static int l_u8g2_SetBitmapMode(lua_State *L){
  631. if (conf == NULL) return 0;
  632. u8g2_SetBitmapMode(&conf->u8g2,luaL_checkinteger(L, 1));
  633. return 0;
  634. }
  635. /*
  636. 绘制位图
  637. @api u8g2.DrawXBM(x, y, w, h, data)
  638. @int X坐标
  639. @int y坐标
  640. @int 位图宽
  641. @int 位图高
  642. @int 位图数据,每一位代表一个像素
  643. @usage
  644. -- 取模使用PCtoLCD2002软件即可
  645. -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
  646. u8g2.DrawXBM(0, 0, 16,16, string.char(
  647. 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
  648. 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
  649. ))
  650. */
  651. static int l_u8g2_DrawXBM(lua_State *L){
  652. if (conf == NULL) return 0;
  653. int x = luaL_checkinteger(L, 1);
  654. int y = luaL_checkinteger(L, 2);
  655. int w = luaL_checkinteger(L, 3);
  656. int h = luaL_checkinteger(L, 4);
  657. size_t len = 0;
  658. const char* data = luaL_checklstring(L, 5, &len);
  659. if (h < 1) return 0; // 行数必须大于0
  660. if (len*8/h < w) return 0; // 起码要填满一行
  661. if (len != h*w/8)return 0;
  662. u8g2_DrawXBM(&conf->u8g2, x, y, w, h, (const uint8_t*)data);
  663. lua_pushboolean(L, 1);
  664. return 1;
  665. }
  666. /**
  667. 缓冲区绘制QRCode
  668. @api u8g2.DrawDrcode(x, y, str, size)
  669. @int x坐标
  670. @int y坐标
  671. @string 二维码的内容
  672. @int 显示大小 (注意:二维码生成大小与要显示内容和纠错等级有关,生成版本为1-40(对应 21x21 - 177x177)的不定大小,如果和设置大小不同会自动在指定的区域中间显示二维码,如二维码未显示请查看日志提示)
  673. @return nil 无返回值
  674. */
  675. static int l_u8g2_DrawDrcode(lua_State *L)
  676. {
  677. size_t len;
  678. int x = luaL_checkinteger(L, 1);
  679. int y = luaL_checkinteger(L, 2);
  680. const char* text = luaL_checklstring(L, 3, &len);
  681. int size = luaL_checkinteger(L, 4);
  682. uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  683. uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  684. if (qrcode == NULL || tempBuffer == NULL) {
  685. if (qrcode)
  686. luat_heap_free(qrcode);
  687. if (tempBuffer)
  688. luat_heap_free(tempBuffer);
  689. LLOGE("qrcode out of memory");
  690. return 0;
  691. }
  692. bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_LOW,
  693. qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
  694. if (ok){
  695. int qr_size = qrcodegen_getSize(qrcode);
  696. if (size < qr_size){
  697. LLOGE("size must be greater than qr_size %d",qr_size);
  698. goto end;
  699. }
  700. int scale = size / qr_size ;
  701. if (!scale)scale = 1;
  702. int margin = (size - qr_size * scale) / 2;
  703. x+=margin;
  704. y+=margin;
  705. for (int j = 0; j < qr_size; j++) {
  706. for (int i = 0; i < qr_size; i++) {
  707. if (qrcodegen_getModule(qrcode, i, j))
  708. u8g2_DrawBox(&conf->u8g2,x+i*scale,y+j*scale,scale,scale);
  709. }
  710. }
  711. }else{
  712. LLOGE("qrcodegen_encodeText false");
  713. }
  714. end:
  715. if (qrcode)
  716. luat_heap_free(qrcode);
  717. if (tempBuffer)
  718. luat_heap_free(tempBuffer);
  719. return 0;
  720. }
  721. /**
  722. 调整屏幕对比度
  723. @api u8g2.SetContrast(contrast)
  724. @int 对比度(取值范围与IC有关,如st7567为0-63)
  725. @return nil 无返回值
  726. */
  727. static int l_u8g2_SetContrast(lua_State *L)
  728. {
  729. if (conf == NULL) return 0;
  730. u8g2_SetContrast(&conf->u8g2,luaL_checkinteger(L, 1));
  731. return 0;
  732. }
  733. #ifdef LUAT_USE_GTFONT
  734. #include "GT5SLCD2E_1A.h"
  735. extern unsigned int gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int size,unsigned int widt,unsigned int high,int(*point)(void*),void* userdata,int mode);
  736. 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);
  737. static int gtfont_u8g2_DrawPixel(u8g2_t *u8g2, uint16_t x, uint16_t y,uint32_t color){
  738. u8g2_DrawPixel(&conf->u8g2,x, y);
  739. return 1;
  740. }
  741. /*
  742. 使用gtfont显示gb2312字符串
  743. @api u8g2.drawGtfontGb2312(str,size,x,y)
  744. @string str 显示字符串
  745. @int size 字体大小 (支持16-192号大小字体)
  746. @int x 横坐标
  747. @int y 竖坐标
  748. @usage
  749. u8g2.drawGtfontGb2312("啊啊啊",32,0,0)
  750. */
  751. static int l_u8g2_draw_gtfont_gb2312(lua_State *L) {
  752. unsigned char buf[128];
  753. int len;
  754. int i = 0;
  755. uint8_t strhigh,strlow ;
  756. uint16_t str;
  757. const char *fontCode = luaL_checklstring(L, 1,&len);
  758. unsigned char size = luaL_checkinteger(L, 2);
  759. int x = luaL_checkinteger(L, 3);
  760. int y = luaL_checkinteger(L, 4);
  761. while ( i < len){
  762. strhigh = *fontCode;
  763. fontCode++;
  764. strlow = *fontCode;
  765. str = (strhigh<<8)|strlow;
  766. fontCode++;
  767. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  768. if(font_size == 0){
  769. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  770. return 0;
  771. }
  772. gtfont_draw_w(buf , x ,y , font_size,size , size,gtfont_u8g2_DrawPixel,&conf->u8g2,2);
  773. x+=size;
  774. i+=2;
  775. }
  776. return 0;
  777. }
  778. #ifdef LUAT_USE_GTFONT_UTF8
  779. extern unsigned short unicodetogb2312 ( unsigned short chr);
  780. static uint8_t utf8_state;
  781. static uint16_t encoding;
  782. static uint16_t utf8_next(uint8_t b)
  783. {
  784. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  785. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  786. if ( utf8_state == 0 )
  787. {
  788. if ( b >= 0xfc ) /* 6 byte sequence */
  789. {
  790. utf8_state = 5;
  791. b &= 1;
  792. }
  793. else if ( b >= 0xf8 )
  794. {
  795. utf8_state = 4;
  796. b &= 3;
  797. }
  798. else if ( b >= 0xf0 )
  799. {
  800. utf8_state = 3;
  801. b &= 7;
  802. }
  803. else if ( b >= 0xe0 )
  804. {
  805. utf8_state = 2;
  806. b &= 15;
  807. }
  808. else if ( b >= 0xc0 )
  809. {
  810. utf8_state = 1;
  811. b &= 0x01f;
  812. }
  813. else
  814. {
  815. /* do nothing, just use the value as encoding */
  816. return b;
  817. }
  818. encoding = b;
  819. return 0x0fffe;
  820. }
  821. else
  822. {
  823. utf8_state--;
  824. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  825. encoding<<=6;
  826. b &= 0x03f;
  827. encoding |= b;
  828. if ( utf8_state != 0 )
  829. return 0x0fffe; /* nothing to do yet */
  830. }
  831. return encoding;
  832. }
  833. /*
  834. 使用gtfont显示UTF8字符串
  835. @api u8g2.drawGtfontUtf8(str,size,x,y)
  836. @string str 显示字符串
  837. @int size 字体大小 (支持16-192号大小字体)
  838. @int x 横坐标
  839. @int y 竖坐标
  840. @usage
  841. u8g2.drawGtfontUtf8("啊啊啊",32,0,0)
  842. */
  843. static int l_u8g2_draw_gtfont_utf8(lua_State *L) {
  844. unsigned char buf[128];
  845. int len;
  846. int i = 0;
  847. uint8_t strhigh,strlow ;
  848. uint16_t e,str;
  849. const char *fontCode = luaL_checklstring(L, 1,&len);
  850. unsigned char size = luaL_checkinteger(L, 2);
  851. int x = luaL_checkinteger(L, 3);
  852. int y = luaL_checkinteger(L, 4);
  853. for(;;){
  854. e = utf8_next((uint8_t)*fontCode);
  855. if ( e == 0x0ffff )
  856. break;
  857. fontCode++;
  858. if ( e != 0x0fffe ){
  859. uint16_t str = unicodetogb2312(e);
  860. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  861. if(font_size == 0){
  862. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  863. return 0;
  864. }
  865. gtfont_draw_w(buf , x ,y , font_size,size , size,gtfont_u8g2_DrawPixel,&conf->u8g2,2);
  866. x+=size;
  867. }
  868. }
  869. return 0;
  870. }
  871. #endif // LUAT_USE_GTFONT_UTF8
  872. #endif // LUAT_USE_GTFONT
  873. /*
  874. 获取底层图像缓冲区
  875. @api u8g2.CopyBuffer(buff)
  876. @userdata zbuff实例,空间要大于等于底层buff的大小
  877. @return int 成功返回buff大小,否则返回nil
  878. @usage
  879. -- 本函数在u8g2初始化之后才能调用
  880. -- 获取大小
  881. local sz = u8g2.CopyBuffer()
  882. -- 拷贝底层buff
  883. local buff = zbuff.create(sz)
  884. u8g2.CopyBuffer(buff)
  885. */
  886. #include "luat_zbuff.h"
  887. static int l_u8g2_CopyBuffer(lua_State *L) {
  888. if (conf == NULL) return 0;
  889. if (conf->buff_ptr == NULL) return 0;
  890. size_t len = u8g2_GetBufferSize(&conf->u8g2);
  891. if (lua_isnil(L, 1)) {
  892. lua_pushinteger(L, len);
  893. return 1;
  894. }
  895. luat_zbuff_t* buff = tozbuff(L);
  896. if (buff->len <= len) {
  897. return 0;
  898. }
  899. memcpy(buff->addr, conf->buff_ptr, len);
  900. lua_pushinteger(L, len);
  901. return 1;
  902. }
  903. /*
  904. 设置省电模式
  905. @api u8g2.SetPowerSave(is_enable)
  906. @boolean 启用与否. true 启用, false禁用
  907. @return nil 无返回值
  908. @usage
  909. -- 本API于2023.11.02新增
  910. -- 开启省电
  911. u8g2.SetPowerSave(true)
  912. -- 关闭省电
  913. u8g2.SetPowerSave(false)
  914. */
  915. static int l_u8g2_SetPowerSave(lua_State *L) {
  916. if (conf == NULL) return 0;
  917. int is_enable = 0;
  918. if (lua_isinteger(L, 1)) {
  919. is_enable = lua_tointeger(L, 1);
  920. }
  921. else if (lua_isboolean(L, 1)) {
  922. is_enable = lua_toboolean(L, 1);
  923. }
  924. u8g2_SetPowerSave(&conf->u8g2, is_enable);
  925. return 0;
  926. }
  927. #include "rotable2.h"
  928. static const rotable_Reg_t reg_u8g2[] =
  929. {
  930. { "begin", ROREG_FUNC(l_u8g2_begin)},
  931. { "init", ROREG_FUNC(l_u8g2_begin)}, // 兼容disp.init函数
  932. { "close", ROREG_FUNC(l_u8g2_close)},
  933. { "ClearBuffer", ROREG_FUNC(l_u8g2_ClearBuffer)},
  934. { "SendBuffer", ROREG_FUNC(l_u8g2_SendBuffer)},
  935. { "DrawUTF8", ROREG_FUNC(l_u8g2_DrawUTF8)},
  936. { "DrawButtonUTF8", ROREG_FUNC(l_u8g2_DrawButtonUTF8)},
  937. { "SetFontMode", ROREG_FUNC(l_u8g2_SetFontMode)},
  938. { "SetFont", ROREG_FUNC(l_u8g2_SetFont)},
  939. { "GetDisplayHeight", ROREG_FUNC(l_u8g2_GetDisplayHeight)},
  940. { "GetDisplayWidth", ROREG_FUNC(l_u8g2_GetDisplayWidth)},
  941. { "SetDrawColor", ROREG_FUNC(l_u8g2_SetDrawColor)},
  942. { "DrawPixel", ROREG_FUNC(l_u8g2_DrawPixel)},
  943. { "DrawLine", ROREG_FUNC(l_u8g2_DrawLine)},
  944. { "DrawCircle", ROREG_FUNC(l_u8g2_DrawCircle)},
  945. { "DrawDisc", ROREG_FUNC(l_u8g2_DrawDisc)},
  946. { "DrawEllipse", ROREG_FUNC(l_u8g2_DrawEllipse)},
  947. { "DrawFilledEllipse", ROREG_FUNC(l_u8g2_DrawFilledEllipse)},
  948. { "DrawBox", ROREG_FUNC(l_u8g2_DrawBox)},
  949. { "DrawFrame", ROREG_FUNC(l_u8g2_DrawFrame)},
  950. { "DrawRBox", ROREG_FUNC(l_u8g2_DrawRBox)},
  951. { "DrawRFrame", ROREG_FUNC(l_u8g2_DrawRFrame)},
  952. { "DrawGlyph", ROREG_FUNC(l_u8g2_DrawGlyph)},
  953. { "DrawTriangle", ROREG_FUNC(l_u8g2_DrawTriangle)},
  954. { "SetBitmapMode",ROREG_FUNC(l_u8g2_SetBitmapMode)},
  955. { "DrawXBM", ROREG_FUNC(l_u8g2_DrawXBM)},
  956. { "DrawDrcode", ROREG_FUNC(l_u8g2_DrawDrcode)},
  957. { "SetContrast", ROREG_FUNC(l_u8g2_SetContrast)},
  958. { "CopyBuffer", ROREG_FUNC(l_u8g2_CopyBuffer)},
  959. { "SetPowerSave", ROREG_FUNC(l_u8g2_SetPowerSave)},
  960. #ifdef LUAT_USE_GTFONT
  961. { "drawGtfontGb2312", ROREG_FUNC(l_u8g2_draw_gtfont_gb2312)},
  962. #ifdef LUAT_USE_GTFONT_UTF8
  963. { "drawGtfontUtf8", ROREG_FUNC(l_u8g2_draw_gtfont_utf8)},
  964. #endif // LUAT_USE_GTFONT_UTF8
  965. #endif // LUAT_USE_GTFONT
  966. // 默认只带8号字体
  967. { "font_opposansm8", ROREG_PTR((void*)u8g2_font_opposansm8)},
  968. #ifdef USE_U8G2_OPPOSANSM_ENGLISH
  969. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  970. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  971. { "font_opposansm10", ROREG_PTR((void*)u8g2_font_opposansm10)},
  972. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  973. { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
  974. { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
  975. { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
  976. { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
  977. { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
  978. { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
  979. #endif
  980. #ifdef USE_U8G2_OPPOSANSM8_CHINESE
  981. { "font_opposansm8_chinese", ROREG_PTR((void*)u8g2_font_opposansm8_chinese)},
  982. #endif
  983. #ifdef USE_U8G2_OPPOSANSM10_CHINESE
  984. { "font_opposansm10_chinese", ROREG_PTR((void*)u8g2_font_opposansm10_chinese)},
  985. #endif
  986. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  987. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  988. #endif
  989. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  990. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  991. #endif
  992. #ifdef USE_U8G2_OPPOSANSM18_CHINESE
  993. { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
  994. #endif
  995. #ifdef USE_U8G2_OPPOSANSM20_CHINESE
  996. { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
  997. #endif
  998. #ifdef USE_U8G2_OPPOSANSM22_CHINESE
  999. { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
  1000. #endif
  1001. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  1002. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  1003. #endif
  1004. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  1005. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  1006. #endif
  1007. #ifdef USE_U8G2_SARASA_ENGLISH
  1008. { "font_sarasa_m8_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m8_ascii)},
  1009. { "font_sarasa_m10_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m10_ascii)},
  1010. { "font_sarasa_m12_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m12_ascii)},
  1011. { "font_sarasa_m14_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m14_ascii)},
  1012. { "font_sarasa_m16_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m16_ascii)},
  1013. { "font_sarasa_m18_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m18_ascii)},
  1014. { "font_sarasa_m20_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m20_ascii)},
  1015. { "font_sarasa_m22_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m22_ascii)},
  1016. //再大的很少用到先不加了
  1017. #endif
  1018. #ifdef USE_U8G2_SARASA_M8_CHINESE
  1019. { "font_sarasa_m8_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m8_chinese)},
  1020. #endif
  1021. #ifdef USE_U8G2_SARASA_M10_CHINESE
  1022. { "font_sarasa_m10_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m10_chinese)},
  1023. #endif
  1024. #ifdef USE_U8G2_SARASA_M12_CHINESE
  1025. { "font_sarasa_m12_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m12_chinese)},
  1026. #endif
  1027. #ifdef USE_U8G2_SARASA_M14_CHINESE
  1028. { "font_sarasa_m14_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m14_chinese)},
  1029. #endif
  1030. #ifdef USE_U8G2_SARASA_M16_CHINESE
  1031. { "font_sarasa_m16_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m16_chinese)},
  1032. #endif
  1033. #ifdef USE_U8G2_SARASA_M18_CHINESE
  1034. { "font_sarasa_m18_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m18_chinese)},
  1035. #endif
  1036. #ifdef USE_U8G2_SARASA_M20_CHINESE
  1037. { "font_sarasa_m20_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m20_chinese)},
  1038. #endif
  1039. #ifdef USE_U8G2_SARASA_M22_CHINESE
  1040. { "font_sarasa_m22_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m22_chinese)},
  1041. #endif
  1042. #ifdef USE_U8G2_SARASA_M24_CHINESE
  1043. { "font_sarasa_m24_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m24_chinese)},
  1044. #endif
  1045. #ifdef USE_U8G2_SARASA_M26_CHINESE
  1046. { "font_sarasa_m26_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m26_chinese)},
  1047. #endif
  1048. #ifdef USE_U8G2_SARASA_M28_CHINESE
  1049. { "font_sarasa_m28_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m28_chinese)},
  1050. #endif
  1051. //@const DRAW_UPPER_RIGHT number 上右
  1052. { "DRAW_UPPER_RIGHT", ROREG_INT(U8G2_DRAW_UPPER_RIGHT)},
  1053. //@const DRAW_UPPER_LEFT number 上左
  1054. { "DRAW_UPPER_LEFT", ROREG_INT(U8G2_DRAW_UPPER_LEFT)},
  1055. //@const DRAW_LOWER_LEFT number 下左
  1056. { "DRAW_LOWER_LEFT", ROREG_INT(U8G2_DRAW_LOWER_LEFT)},
  1057. //@const DRAW_LOWER_RIGHT number 下右
  1058. { "DRAW_LOWER_RIGHT", ROREG_INT(U8G2_DRAW_LOWER_RIGHT)},
  1059. //@const DRAW_ALL number 全部
  1060. { "DRAW_ALL", ROREG_INT(U8G2_DRAW_ALL)},
  1061. //@const BTN_BW0 number 文本周围没有边框
  1062. { "BTN_BW0", ROREG_INT(U8G2_BTN_BW0)},
  1063. //@const BTN_BW1 number 文本周围的边框,1像素边框宽度
  1064. { "BTN_BW1", ROREG_INT(U8G2_BTN_BW1)},
  1065. //@const BTN_BW2 number 文本周围的边框,2像素边框宽度
  1066. { "BTN_BW2", ROREG_INT(U8G2_BTN_BW2)},
  1067. //@const BTN_BW3 number 文本周围的边框,3像素边框宽度
  1068. { "BTN_BW3", ROREG_INT(U8G2_BTN_BW3)},
  1069. //@const BTN_SHADOW0 number 启用阴影,与框架无间隙
  1070. { "BTN_SHADOW0", ROREG_INT(U8G2_BTN_SHADOW0)},
  1071. //@const BTN_SHADOW1 number 启用阴影,到帧的1像素间隙
  1072. { "BTN_SHADOW1", ROREG_INT(U8G2_BTN_SHADOW1)},
  1073. //@const BTN_SHADOW2 number 启用阴影,到帧的2像素间隙
  1074. { "BTN_SHADOW2", ROREG_INT(U8G2_BTN_SHADOW2)},
  1075. //@const BTN_INV number 反转文本
  1076. { "BTN_INV", ROREG_INT(U8G2_BTN_INV)},
  1077. //@const BTN_HCENTER number 将文本置于框架内的中心,并将参考位置更改为文本的中心
  1078. { "BTN_HCENTER", ROREG_INT(U8G2_BTN_HCENTER)},
  1079. //@const BTN_XFRAME number 在按钮周围绘制第二个1像素框
  1080. { "BTN_XFRAME", ROREG_INT(U8G2_BTN_XFRAME)},
  1081. { NULL, ROREG_INT(0)}
  1082. };
  1083. LUAMOD_API int luaopen_u8g2( lua_State *L ) {
  1084. lua_getglobal(L, "disp"); // disp库已经加载过u8g2库, 那就直接重用
  1085. if (lua_isuserdata(L, -1))
  1086. return 1;
  1087. luat_newlib2(L, reg_u8g2);
  1088. return 1;
  1089. }
  1090. //-------------------------------------------------------------------------------------------------
  1091. // 往下是一些U8G2方法的默认实现
  1092. uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1093. uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1094. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1095. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1096. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1097. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1098. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf);
  1099. static const luat_u8g2_dev_reg_t* search_dev_reg(luat_u8g2_conf_t *conf, uint16_t spi_i2c) {
  1100. size_t dev_reg_index = 0;
  1101. while (devregs[dev_reg_index].name != NULL){
  1102. if (devregs[dev_reg_index].spi_i2c == spi_i2c && strcmp(devregs[dev_reg_index].name, conf->cname) == 0) {
  1103. return &devregs[dev_reg_index];
  1104. }
  1105. dev_reg_index ++;
  1106. }
  1107. return &devregs[0];
  1108. }
  1109. #ifndef LUAT_COMPILER_NOWEAK
  1110. LUAT_WEAK int luat_u8g2_setup(luat_u8g2_conf_t *conf) {
  1111. return luat_u8g2_setup_default(conf);
  1112. }
  1113. LUAT_WEAK uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1114. return u8x8_luat_gpio_and_delay_default(u8x8, msg, arg_int, arg_ptr);
  1115. }
  1116. LUAT_WEAK uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1117. return u8x8_luat_byte_hw_i2c_default(u8x8, msg, arg_int, arg_ptr);
  1118. }
  1119. LUAT_WEAK uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1120. return u8x8_luat_byte_4wire_hw_spi_default(u8x8, msg, arg_int, arg_ptr);
  1121. }
  1122. #endif
  1123. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf) {
  1124. u8g2_t* u8g2 = &conf->u8g2;
  1125. const luat_u8g2_dev_reg_t* devreg = NULL;
  1126. // LLOGD("pinType %d", pinType);
  1127. devreg = search_dev_reg(conf, pinType == 5?1:0);
  1128. if (devreg == NULL) {
  1129. LLOGD("unkown dev %s", conf->cname);
  1130. return -1;
  1131. }
  1132. // LLOGD("devreg name:%s spi_i2c:%d", devreg->name,devreg->spi_i2c);
  1133. if (pinType == 1) {
  1134. devreg->devcb(u8g2, conf->direction, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay_default);
  1135. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_I2C_CLOCK, i2c_scl);
  1136. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_I2C_DATA, i2c_sda);
  1137. }else if (pinType == 2) {
  1138. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_hw_i2c_default, u8x8_luat_gpio_and_delay_default);
  1139. //LLOGD("setup disp i2c.hw");
  1140. }else if (pinType == 5) {
  1141. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_4wire_hw_spi_default, u8x8_luat_gpio_and_delay_default);
  1142. LLOGD("setup disp spi.hw spi_id=%d spi_dc=%d spi_cs=%d spi_res=%d",spi_id,spi_dc,spi_cs,spi_res);
  1143. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, spi_cs);
  1144. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, spi_dc);
  1145. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, spi_res);
  1146. } else {
  1147. LLOGI("no such u8g2 mode!!");
  1148. return -1;
  1149. }
  1150. #ifdef U8G2_USE_DYNAMIC_ALLOC
  1151. conf->buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  1152. u8g2_SetBufferPtr(u8g2, conf->buff_ptr);
  1153. #endif
  1154. u8g2_InitDisplay(u8g2);
  1155. u8g2_SetPowerSave(u8g2, 0);
  1156. return 0;
  1157. }
  1158. LUAT_WEAK int luat_u8g2_close(luat_u8g2_conf_t *conf) {
  1159. return 0;
  1160. }
  1161. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1162. static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes */
  1163. static uint8_t buf_idx;
  1164. uint8_t *data;
  1165. switch(msg)
  1166. {
  1167. case U8X8_MSG_BYTE_SEND:
  1168. data = (uint8_t *)arg_ptr;
  1169. while( arg_int > 0 )
  1170. {
  1171. buffer[buf_idx++] = *data;
  1172. data++;
  1173. arg_int--;
  1174. }
  1175. break;
  1176. case U8X8_MSG_BYTE_INIT:
  1177. //i2c_init(u8x8); /* init i2c communication */
  1178. luat_i2c_setup(i2c_id,i2c_speed);
  1179. break;
  1180. case U8X8_MSG_BYTE_SET_DC:
  1181. /* ignored for i2c */
  1182. break;
  1183. case U8X8_MSG_BYTE_START_TRANSFER:
  1184. buf_idx = 0;
  1185. break;
  1186. case U8X8_MSG_BYTE_END_TRANSFER:
  1187. luat_i2c_send(i2c_id, u8x8_GetI2CAddress(u8x8) >> 1, buffer, buf_idx,1);
  1188. break;
  1189. default:
  1190. return 0;
  1191. }
  1192. return 1;
  1193. }
  1194. int hw_spi_begin(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin )
  1195. {
  1196. luat_spi_t u8g2_spi = {0};
  1197. u8g2_spi.id = spi_id;
  1198. switch(spi_mode)
  1199. {
  1200. case 0: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 0; break;
  1201. case 1: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 0; break;
  1202. case 2: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 1; break;
  1203. case 3: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 1; break;
  1204. }
  1205. u8g2_spi.dataw = 8;
  1206. u8g2_spi.bit_dict = 1;
  1207. u8g2_spi.master = 1;
  1208. u8g2_spi.mode = 0;
  1209. u8g2_spi.bandrate = max_hz;
  1210. u8g2_spi.cs = -1;
  1211. LLOGI("spi_mode:%d bandrate:%d cs_pin:%d",spi_mode,max_hz,cs_pin);
  1212. luat_spi_setup(&u8g2_spi);
  1213. luat_gpio_mode(spi_res,Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1214. luat_gpio_mode(spi_dc,Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1215. return 0;
  1216. }
  1217. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  1218. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1219. // LLOGI("u8x8_luat_byte_4wire_hw_spi_default msg:%d arg_int:%d",msg,arg_int);
  1220. switch(msg)
  1221. {
  1222. case U8X8_MSG_BYTE_SEND:
  1223. luat_spi_send(spi_id, (const char*)arg_ptr, arg_int);
  1224. break;
  1225. case U8X8_MSG_BYTE_INIT:
  1226. /* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  1227. /* 0: clock active high, data out on falling edge, clock default value is zero, takover on rising edge */
  1228. /* 1: clock active high, data out on rising edge, clock default value is zero, takover on falling edge */
  1229. /* 2: clock active low, data out on rising edge */
  1230. /* 3: clock active low, data out on falling edge */
  1231. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1232. hw_spi_begin(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]);
  1233. break;
  1234. case U8X8_MSG_BYTE_SET_DC:
  1235. u8x8_gpio_SetDC(u8x8, arg_int);
  1236. break;
  1237. case U8X8_MSG_BYTE_START_TRANSFER:
  1238. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  1239. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  1240. break;
  1241. case U8X8_MSG_BYTE_END_TRANSFER:
  1242. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  1243. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1244. break;
  1245. default:
  1246. return 0;
  1247. }
  1248. return 1;
  1249. }
  1250. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1251. {
  1252. uint8_t i;
  1253. switch(msg)
  1254. {
  1255. case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
  1256. luat_timer_us_delay(1);
  1257. break;
  1258. case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  1259. luat_timer_us_delay(1);
  1260. break;
  1261. case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  1262. luat_timer_us_delay(1);
  1263. break;
  1264. case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  1265. luat_timer_mdelay(arg_int);
  1266. break;
  1267. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  1268. // Function which implements a delay, arg_int contains the amount of ms
  1269. // set spi pin mode
  1270. if (pinType == 1){
  1271. // set i2c pin mode
  1272. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1273. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1274. }else if (pinType == 3){
  1275. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);//d0 a5 15 d1 a7 17 res b0 18 dc b1 19 cs a4 14
  1276. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1277. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1278. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1279. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1280. }else if (pinType == 4){
  1281. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);//d0 a5 15 d1 a7 17 res b0 18 dc b1 19 cs a4 14
  1282. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1283. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1284. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1285. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1286. }else if (pinType == 5){
  1287. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1288. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1289. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1290. }else if (pinType == 6){
  1291. // set 8080 pin mode
  1292. luat_gpio_mode(u8x8->pins[U8X8_PIN_D0],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1293. luat_gpio_mode(u8x8->pins[U8X8_PIN_D1],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1294. luat_gpio_mode(u8x8->pins[U8X8_PIN_D2],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1295. luat_gpio_mode(u8x8->pins[U8X8_PIN_D3],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1296. luat_gpio_mode(u8x8->pins[U8X8_PIN_D4],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1297. luat_gpio_mode(u8x8->pins[U8X8_PIN_D5],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1298. luat_gpio_mode(u8x8->pins[U8X8_PIN_D6],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1299. luat_gpio_mode(u8x8->pins[U8X8_PIN_D7],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1300. luat_gpio_mode(u8x8->pins[U8X8_PIN_E],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1301. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1302. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1303. }
  1304. // // set menu pin mode
  1305. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_HOME],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1306. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_SELECT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1307. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_PREV],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1308. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_NEXT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1309. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_UP],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1310. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_DOWN],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1311. // // set value
  1312. // luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],Luat_GPIO_HIGH);
  1313. // luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_HIGH);
  1314. // luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_HIGH);
  1315. // luat_gpio_set(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_HIGH);
  1316. // luat_gpio_set(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_HIGH);
  1317. break;
  1318. case U8X8_MSG_DELAY_I2C:
  1319. // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  1320. // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  1321. if (arg_int<=2) {
  1322. luat_timer_us_delay(5);
  1323. }
  1324. else {
  1325. luat_timer_us_delay(2);
  1326. }
  1327. break;
  1328. //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
  1329. //case U8X8_MSG_GPIO_SPI_CLOCK:
  1330. //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
  1331. //case U8X8_MSG_GPIO_SPI_DATA:
  1332. case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
  1333. luat_gpio_set(u8x8->pins[U8X8_PIN_D2],arg_int);
  1334. break;
  1335. case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
  1336. luat_gpio_set(u8x8->pins[U8X8_PIN_D3],arg_int);
  1337. break;
  1338. case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
  1339. luat_gpio_set(u8x8->pins[U8X8_PIN_D4],arg_int);
  1340. break;
  1341. case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
  1342. luat_gpio_set(u8x8->pins[U8X8_PIN_D5],arg_int);
  1343. break;
  1344. case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
  1345. luat_gpio_set(u8x8->pins[U8X8_PIN_D6],arg_int);
  1346. break;
  1347. case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
  1348. luat_gpio_set(u8x8->pins[U8X8_PIN_D7],arg_int);
  1349. break;
  1350. case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
  1351. luat_gpio_set(u8x8->pins[U8X8_PIN_E],arg_int);
  1352. break;
  1353. case U8X8_MSG_GPIO_I2C_CLOCK:
  1354. // arg_int=0: Output low at I2C clock pin
  1355. // arg_int=1: Input dir with pullup high for I2C clock pin
  1356. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],arg_int);
  1357. break;
  1358. case U8X8_MSG_GPIO_I2C_DATA:
  1359. // arg_int=0: Output low at I2C data pin
  1360. // arg_int=1: Input dir with pullup high for I2C data pin
  1361. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],arg_int);
  1362. break;
  1363. case U8X8_MSG_GPIO_SPI_CLOCK:
  1364. //Function to define the logic level of the clockline
  1365. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],arg_int);
  1366. break;
  1367. case U8X8_MSG_GPIO_SPI_DATA:
  1368. //Function to define the logic level of the data line to the display
  1369. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],arg_int);
  1370. break;
  1371. case U8X8_MSG_GPIO_CS:
  1372. // Function to define the logic level of the CS line
  1373. luat_gpio_set(u8x8->pins[U8X8_PIN_CS],arg_int);
  1374. break;
  1375. case U8X8_MSG_GPIO_DC:
  1376. //Function to define the logic level of the Data/ Command line
  1377. luat_gpio_set(u8x8->pins[U8X8_PIN_DC],arg_int);
  1378. break;
  1379. case U8X8_MSG_GPIO_RESET:
  1380. //Function to define the logic level of the RESET line
  1381. luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],arg_int);
  1382. break;
  1383. default:
  1384. //A message was received which is not implemented, return 0 to indicate an error
  1385. if ( msg >= U8X8_MSG_GPIO(0) )
  1386. {
  1387. i = u8x8_GetPinValue(u8x8, msg);
  1388. if ( i != U8X8_PIN_NONE )
  1389. {
  1390. if ( u8x8_GetPinIndex(u8x8, msg) < U8X8_PIN_OUTPUT_CNT )
  1391. {
  1392. luat_gpio_set(i, arg_int);
  1393. }
  1394. else
  1395. {
  1396. if ( u8x8_GetPinIndex(u8x8, msg) == U8X8_PIN_OUTPUT_CNT )
  1397. {
  1398. // call yield() for the first pin only, u8x8 will always request all the pins, so this should be ok
  1399. // yield();
  1400. }
  1401. u8x8_SetGPIOResult(u8x8, luat_gpio_get(i) == 0 ? 0 : 1);
  1402. }
  1403. }
  1404. break;
  1405. }
  1406. return 0;
  1407. }
  1408. return 1;
  1409. }