luat_lib_u8g2.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. @module u8g2
  3. @summary u8g2图形处理库
  4. @author Dozingfiretruck
  5. @version 1.0
  6. @date 2021.01.25
  7. */
  8. #include "luat_base.h"
  9. #include "luat_malloc.h"
  10. #include "luat_u8g2.h"
  11. #include "luat_gpio.h"
  12. #include "luat_timer.h"
  13. #include "luat_i2c.h"
  14. #include "luat_spi.h"
  15. #include "u8g2.h"
  16. #define LUAT_LOG_TAG "luat.u8g2"
  17. #include "luat_log.h"
  18. enum
  19. {
  20. font_opposansm8,
  21. font_opposansm12_chinese,
  22. font_unifont_t_symbols,
  23. font_open_iconic_weather_6x_t,
  24. };
  25. static u8g2_t* u8g2;
  26. static int u8g2_lua_ref;
  27. static uint8_t i2c_id;
  28. static uint8_t i2c_speed;
  29. // static uint8_t i2c_addr = 0x3C;
  30. static uint8_t spi_id;
  31. static uint8_t spi_res;
  32. static uint8_t spi_dc;
  33. static uint8_t spi_cs;
  34. /*
  35. u8g2显示屏初始化
  36. @api u8g2.begin(conf)
  37. @table conf 配置信息
  38. @return int 正常初始化1,已经初始化过2,内存不够3,初始化失败返回4
  39. @usage
  40. -- 初始化i2c1的ssd1306
  41. u8g2.begin({ic = "SSD1306",mode="i2c_hw",i2c_id=0})
  42. */
  43. static int l_u8g2_begin(lua_State *L) {
  44. if (u8g2 != NULL) {
  45. LLOGW("disp is aready inited");
  46. lua_pushinteger(L, 2);
  47. return 1;
  48. }
  49. u8g2 = (u8g2_t*)lua_newuserdata(L, sizeof(u8g2_t));
  50. if (u8g2 == NULL) {
  51. LLOGE("lua_newuserdata return NULL, out of memory ?!");
  52. lua_pushinteger(L, 3);
  53. return 1;
  54. }
  55. luat_u8g2_conf_t conf = {0};
  56. conf.pinType = 2; // I2C 硬件(或者是个假硬件)
  57. conf.ptr = u8g2;
  58. if (lua_istable(L, 1)) {
  59. // 参数解析
  60. lua_pushliteral(L, "ic");
  61. lua_gettable(L, 1);
  62. if (lua_isstring(L, -1)) {
  63. conf.cname = (char*)luaL_checkstring(L, -1);
  64. LLOGD("using ic: %s",conf.cname);
  65. }
  66. lua_pop(L, 1);
  67. lua_pushliteral(L, "mode");
  68. lua_gettable(L, 1);
  69. if (lua_isstring(L, -1)) {
  70. const char* mode = luaL_checkstring(L, -1);
  71. LLOGD("mode = [%s]", mode);
  72. if (strcmp("i2c_sw", mode) == 0) {
  73. LLOGD("using i2c_sw");
  74. conf.pinType = 1;
  75. }
  76. else if (strcmp("i2c_hw", mode) == 0) {
  77. LLOGD("using i2c_hw");
  78. conf.pinType = 2;
  79. }
  80. else if (strcmp("spi_sw_3pin", mode) == 0) {
  81. LLOGD("using spi_sw_3pin");
  82. conf.pinType = 3;
  83. }
  84. else if (strcmp("spi_sw_4pin", mode) == 0) {
  85. LLOGD("using spi_sw_4pin");
  86. conf.pinType = 4;
  87. }
  88. else if (strcmp("spi_hw_4pin", mode) == 0) {
  89. LLOGD("using spi_hw_4pin");
  90. conf.pinType = 5;
  91. }
  92. }
  93. lua_pop(L, 1);
  94. // 解析pin0 ~ pin7
  95. lua_pushliteral(L, "pin0");
  96. lua_gettable(L, 1);
  97. if (lua_isinteger(L, -1)) {
  98. conf.pin0 = luaL_checkinteger(L, -1);
  99. }
  100. lua_pop(L, 1);
  101. lua_pushliteral(L, "pin1");
  102. lua_gettable(L, 1);
  103. if (lua_isinteger(L, -1)) {
  104. conf.pin1 = luaL_checkinteger(L, -1);
  105. }
  106. lua_pop(L, 1);
  107. lua_pushliteral(L, "pin2");
  108. lua_gettable(L, 1);
  109. if (lua_isinteger(L, -1)) {
  110. conf.pin2 = luaL_checkinteger(L, -1);
  111. }
  112. lua_pop(L, 1);
  113. lua_pushliteral(L, "pin3");
  114. lua_gettable(L, 1);
  115. if (lua_isinteger(L, -1)) {
  116. conf.pin3 = luaL_checkinteger(L, -1);
  117. }
  118. lua_pop(L, 1);
  119. lua_pushliteral(L, "i2c_id");
  120. lua_gettable(L, 1);
  121. if (lua_isinteger(L, -1)) {
  122. i2c_id = luaL_checkinteger(L, -1);
  123. }
  124. lua_pop(L, 1);
  125. lua_pushliteral(L, "i2c_speed");
  126. lua_gettable(L, 1);
  127. if (lua_isinteger(L, -1)) {
  128. i2c_speed = luaL_checkinteger(L, -1);
  129. }
  130. lua_pop(L, 1);
  131. // lua_pushliteral(L, "i2c_addr");
  132. // lua_gettable(L, 1);
  133. // if (lua_isinteger(L, -1)) {
  134. // i2c_addr = luaL_checkinteger(L, -1);
  135. // }
  136. // lua_pop(L, 1);
  137. lua_pushliteral(L, "spi_id");
  138. lua_gettable(L, 1);
  139. if (lua_isinteger(L, -1)) {
  140. spi_id = luaL_checkinteger(L, -1);
  141. LLOGD("spi_id=%d", spi_id);
  142. }
  143. lua_pop(L, 1);
  144. lua_pushliteral(L, "spi_res");
  145. lua_gettable(L, 1);
  146. if (lua_isinteger(L, -1)) {
  147. spi_res = luaL_checkinteger(L, -1);
  148. LLOGD("spi_res=%d", spi_res);
  149. }
  150. lua_pop(L, 1);
  151. lua_pushliteral(L, "spi_dc");
  152. lua_gettable(L, 1);
  153. if (lua_isinteger(L, -1)) {
  154. spi_dc = luaL_checkinteger(L, -1);
  155. LLOGD("spi_dc=%d", spi_dc);
  156. }
  157. lua_pop(L, 1);
  158. lua_pushliteral(L, "spi_cs");
  159. lua_gettable(L, 1);
  160. if (lua_isinteger(L, -1)) {
  161. spi_cs = luaL_checkinteger(L, -1);
  162. LLOGD("spi_cs=%d", spi_cs);
  163. }
  164. lua_pop(L, 1);
  165. // lua_pushliteral(L, "spi_id");
  166. // lua_gettable(L, 1);
  167. // if (lua_isinteger(L, -1)) {
  168. // spi_id = luaL_checkinteger(L, -1);
  169. // }
  170. // lua_pop(L, 1);
  171. // pin4 ~ pin7暂时用不到,先不设置了
  172. }
  173. LLOGD("pinType=%d", conf.pinType);
  174. if (luat_u8g2_setup(&conf)) {
  175. u8g2 = NULL;
  176. LLOGW("disp init fail");
  177. lua_pushinteger(L, 4);
  178. return 1; // 初始化失败
  179. }
  180. u8g2_lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  181. u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr); // 设置默认字体
  182. lua_pushinteger(L, 1);
  183. return 1;
  184. }
  185. /*
  186. 关闭显示屏
  187. @api u8g2.close()
  188. @usage
  189. -- 关闭disp,再次使用disp相关API的话,需要重新初始化
  190. u8g2.close()
  191. */
  192. static int l_u8g2_close(lua_State *L) {
  193. if (u8g2_lua_ref != 0) {
  194. lua_geti(L, LUA_REGISTRYINDEX, u8g2_lua_ref);
  195. if (lua_isuserdata(L, -1)) {
  196. luaL_unref(L, LUA_REGISTRYINDEX, u8g2_lua_ref);
  197. }
  198. u8g2_lua_ref = 0;
  199. }
  200. lua_gc(L, LUA_GCCOLLECT, 0);
  201. u8g2 = NULL;
  202. return 0;
  203. }
  204. /*
  205. 清屏,清除内存帧缓冲区中的所有像素
  206. @api u8g2.ClearBuffer()
  207. @usage
  208. -- 清屏
  209. u8g2.ClearBuffer()
  210. */
  211. static int l_u8g2_ClearBuffer(lua_State *L) {
  212. if (u8g2 == NULL) return 0;
  213. u8g2_ClearBuffer(u8g2);
  214. return 0;
  215. }
  216. /*
  217. 将数据更新到屏幕,将存储器帧缓冲区的内容发送到显示器
  218. @api u8g2.SendBuffer()
  219. @usage
  220. -- 把显示数据更新到屏幕
  221. u8g2.SendBuffer()
  222. */
  223. static int l_u8g2_SendBuffer(lua_State *L) {
  224. if (u8g2 == NULL) return 0;
  225. u8g2_SendBuffer(u8g2);
  226. return 0;
  227. }
  228. /*
  229. 在显示屏上画一段文字,在显示屏上画一段文字,要调用u8g2.SendBuffer()才会更新到屏幕
  230. @api u8g2.DrawUTF8(str, x, y)
  231. @string 文件内容
  232. @int 横坐标
  233. @int 竖坐标
  234. @usage
  235. u8g2.DrawUTF8("wifi is ready", 10, 20)
  236. */
  237. static int l_u8g2_DrawUTF8(lua_State *L) {
  238. if (u8g2 == NULL) {
  239. LLOGW("disp not init yet!!!");
  240. return 0;
  241. }
  242. size_t len;
  243. size_t x, y;
  244. const char* str = luaL_checklstring(L, 1, &len);
  245. x = luaL_checkinteger(L, 2);
  246. y = luaL_checkinteger(L, 3);
  247. u8g2_DrawUTF8(u8g2, x, y, str);
  248. return 0;
  249. }
  250. /*
  251. 设置字体模式
  252. @api u8g2.SetFontMode(mode)
  253. @int mode字体模式,启用(1)或禁用(0)透明模式
  254. @usage
  255. u8g2.SetFontMode(1)
  256. */
  257. static int l_u8g2_SetFontMode(lua_State *L){
  258. if (u8g2 == NULL) return 0;
  259. int font_mode = luaL_checkinteger(L, 1);
  260. if (font_mode < 0) {
  261. lua_pushboolean(L, 0);
  262. }
  263. u8g2_SetFontMode(u8g2, font_mode);
  264. lua_pushboolean(L, 1);
  265. return 1;
  266. }
  267. /*
  268. 设置字体
  269. @api u8g2.SetFont(font)
  270. @int font, u8g2.font_ncenB08_tr为纯英文8x8字节,u8g2.font_opposansm12_chinese 为12x12全中文,u8g2.font_unifont_t_symbols 为符号.
  271. @usage
  272. -- 设置为中文字体,对之后的drawStr有效,使用中文字体需在luat_base.h开启#define USE_U8G2_OPPOSANSM12_CHINESE
  273. u8g2.setFont(u8g2.font_wqy12_t_gb2312)
  274. */
  275. static int l_u8g2_SetFont(lua_State *L) {
  276. if (u8g2 == NULL) {
  277. LLOGI("disp not init yet!!!");
  278. lua_pushboolean(L, 0);
  279. return 1;
  280. }
  281. int font = luaL_checkinteger(L, 1);
  282. switch (font)
  283. {
  284. case font_opposansm8:
  285. LLOGI("font_opposansm8");
  286. u8g2_SetFont(u8g2, u8g2_font_opposansm8);
  287. lua_pushboolean(L, 1);
  288. break;
  289. #if defined USE_U8G2_OPPOSANSM12_CHINESE
  290. case font_opposansm12_chinese:
  291. LLOGI("font_opposansm12_chinese");
  292. u8g2_SetFont(u8g2, u8g2_font_opposansm12_chinese);
  293. lua_pushboolean(L, 1);
  294. break;
  295. #endif
  296. #if defined USE_U8G2_UNIFONT_SYMBOLS
  297. case font_unifont_t_symbols:
  298. LLOGI("font_unifont_t_symbols");
  299. u8g2_SetFont(u8g2, u8g2_font_unifont_t_symbols);
  300. lua_pushboolean(L, 1);
  301. break;
  302. #endif
  303. #if defined USE_U8G2_ICONIC_WEATHER_6X
  304. case font_open_iconic_weather_6x_t:
  305. LLOGI("font_open_iconic_weather_6x_t");
  306. u8g2_SetFont(u8g2, u8g2_font_open_iconic_weather_6x_t);
  307. lua_pushboolean(L, 1);
  308. break;
  309. #endif
  310. default:
  311. lua_pushboolean(L, 0);
  312. LLOGI("default");
  313. break;
  314. }
  315. return 0;
  316. }
  317. /*
  318. 获取显示屏高度
  319. @api u8g2.GetDisplayHeight()
  320. @return int 显示屏高度
  321. @usage
  322. u8g2.GetDisplayHeight()
  323. */
  324. static int l_u8g2_GetDisplayHeight(lua_State *L){
  325. if (u8g2 == NULL) return 0;
  326. lua_pushinteger(L, u8g2_GetDisplayHeight(u8g2));
  327. return 1;
  328. }
  329. /*
  330. 获取显示屏宽度
  331. @api u8g2.GetDisplayWidth()
  332. @return int 显示屏宽度
  333. @usage
  334. u8g2.GetDisplayWidth()
  335. */
  336. static int l_u8g2_GetDisplayWidth(lua_State *L){
  337. if (u8g2 == NULL) return 0;
  338. lua_pushinteger(L, u8g2_GetDisplayWidth(u8g2));
  339. return 1;
  340. }
  341. /*
  342. 画一个点.
  343. @api u8g2.DrawPixel(x,y)
  344. @int X位置.
  345. @int Y位置.
  346. @usage
  347. u8g2.DrawPixel(20, 5)
  348. */
  349. static int l_u8g2_DrawPixel(lua_State *L){
  350. if (u8g2 == NULL) return 0;
  351. u8g2_DrawPixel(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2));
  352. return 1;
  353. }
  354. /*
  355. 在两点之间画一条线.
  356. @api u8g2.DrawLine(x0,y0,x1,y1)
  357. @int 第一个点的X位置.
  358. @int 第一个点的Y位置.
  359. @int 第二个点的X位置.
  360. @int 第二个点的Y位置.
  361. @usage
  362. u8g2.DrawLine(20, 5, 5, 32)
  363. */
  364. static int l_u8g2_DrawLine(lua_State *L){
  365. if (u8g2 == NULL) return 0;
  366. u8g2_DrawLine(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  367. return 1;
  368. }
  369. /*
  370. 在x,y位置画一个半径为rad的空心圆.
  371. @api u8g2.DrawCircle(x0,y0,rad,opt)
  372. @int 圆心位置
  373. @int 圆心位置
  374. @int 圆半径.
  375. @int 选择圆的部分或全部.
  376. 右上: 0x01
  377. 左上: 0x02
  378. 左下: 0x04
  379. 右下: 0x08
  380. 完整圆: (0x01|0x02|0x04|0x08)
  381. @usage
  382. u8g2.DrawCircle(60,30,8,15)
  383. */
  384. static int l_u8g2_DrawCircle(lua_State *L){
  385. if (u8g2 == NULL) return 0;
  386. u8g2_DrawCircle(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  387. return 1;
  388. }
  389. /*
  390. 在x,y位置画一个半径为rad的实心圆.
  391. @api u8g2.DrawDisc(x0,y0,rad,opt)
  392. @int 圆心位置
  393. @int 圆心位置
  394. @int 圆半径.
  395. @int 选择圆的部分或全部.
  396. 右上: 0x01
  397. 左上: 0x02
  398. 左下: 0x04
  399. 右下: 0x08
  400. 完整圆: (0x01|0x02|0x04|0x08)
  401. @usage
  402. u8g2.DrawDisc(60,30,8,15)
  403. */
  404. static int l_u8g2_DrawDisc(lua_State *L){
  405. if (u8g2 == NULL) return 0;
  406. u8g2_DrawDisc(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  407. return 1;
  408. }
  409. /*
  410. 在x,y位置画一个半径为rad的空心椭圆.
  411. @api u8g2.DrawEllipse(x0,y0,rx,ry,opt)
  412. @int 圆心位置
  413. @int 圆心位置
  414. @int 椭圆大小
  415. @int 椭圆大小
  416. @int 选择圆的部分或全部.
  417. 右上: 0x01
  418. 左上: 0x02
  419. 左下: 0x04
  420. 右下: 0x08
  421. 完整圆: (0x01|0x02|0x04|0x08)
  422. @usage
  423. u8g2.DrawEllipse(60,30,8,15)
  424. */
  425. static int l_u8g2_DrawEllipse(lua_State *L){
  426. if (u8g2 == NULL) return 0;
  427. u8g2_DrawEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  428. return 0;
  429. }
  430. /*
  431. 在x,y位置画一个半径为rad的实心椭圆.
  432. @api u8g2.DrawFilledEllipse(x0,y0,rx,ry,opt)
  433. @int 圆心位置
  434. @int 圆心位置
  435. @int 椭圆大小
  436. @int 椭圆大小
  437. @int 选择圆的部分或全部.
  438. 右上: 0x01
  439. 左上: 0x02
  440. 左下: 0x04
  441. 右下: 0x08
  442. 完整圆: (0x01|0x02|0x04|0x08)
  443. @usage
  444. u8g2.DrawFilledEllipse(60,30,8,15)
  445. */
  446. static int l_u8g2_DrawFilledEllipse(lua_State *L){
  447. if (u8g2 == NULL) return 0;
  448. u8g2_DrawFilledEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  449. return 0;
  450. }
  451. /*
  452. 从x / y位置(左上边缘)开始绘制一个框(填充的框).
  453. @api u8g2.DrawBox(x,y,w,h)
  454. @int 左上边缘的X位置
  455. @int 左上边缘的Y位置
  456. @int 盒子的宽度
  457. @int 盒子的高度
  458. @usage
  459. u8g2.DrawBox(3,7,25,15)
  460. */
  461. static int l_u8g2_DrawBox(lua_State *L){
  462. if (u8g2 == NULL) return 0;
  463. u8g2_DrawBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  464. return 0;
  465. }
  466. /*
  467. 从x / y位置(左上边缘)开始绘制一个框(空框).
  468. @api u8g2.DrawFrame(x,y,w,h)
  469. @int 左上边缘的X位置
  470. @int 左上边缘的Y位置
  471. @int 盒子的宽度
  472. @int 盒子的高度
  473. @usage
  474. u8g2.DrawFrame(3,7,25,15)
  475. */
  476. static int l_u8g2_DrawFrame(lua_State *L){
  477. if (u8g2 == NULL) return 0;
  478. u8g2_DrawFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  479. return 0;
  480. }
  481. /*
  482. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的填充框/框架.
  483. @api u8g2.DrawRBox(x,y,w,h,r)
  484. @int 左上边缘的X位置
  485. @int 左上边缘的Y位置
  486. @int 盒子的宽度
  487. @int 盒子的高度
  488. @int 四个边缘的半径
  489. @usage
  490. u8g2.DrawRBox(3,7,25,15)
  491. */
  492. static int l_u8g2_DrawRBox(lua_State *L){
  493. if (u8g2 == NULL) return 0;
  494. u8g2_DrawRBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  495. return 0;
  496. }
  497. /*
  498. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的空框/框架.
  499. @api u8g2.DrawRFrame(x,y,w,h,r)
  500. @int 左上边缘的X位置
  501. @int 左上边缘的Y位置
  502. @int 盒子的宽度
  503. @int 盒子的高度
  504. @int 四个边缘的半径
  505. @usage
  506. u8g2.DrawRFrame(3,7,25,15)
  507. */
  508. static int l_u8g2_DrawRFrame(lua_State *L){
  509. if (u8g2 == NULL) return 0;
  510. u8g2_DrawRFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  511. return 0;
  512. }
  513. /*
  514. 绘制一个图形字符。字符放置在指定的像素位置x和y.
  515. @api u8g2.DrawGlyph(x,y,encoding)
  516. @int 字符在显示屏上的位置
  517. @int 字符在显示屏上的位置
  518. @int 字符的Unicode值
  519. @usage
  520. u8g2.SetFont(u8g2_font_unifont_t_symbols)
  521. u8g2.DrawGlyph(5, 20, 0x2603) -- dec 9731/hex 2603 Snowman
  522. */
  523. static int l_u8g2_DrawGlyph(lua_State *L){
  524. if (u8g2 == NULL) return 0;
  525. u8g2_DrawGlyph(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3));
  526. return 0;
  527. }
  528. /*
  529. 绘制一个三角形(实心多边形).
  530. @api u8g2.DrawTriangle(x0,y0,x1,y1,x2,y2)
  531. @int 点0X位置
  532. @int 点0Y位置
  533. @int 点1X位置
  534. @int 点1Y位置
  535. @int 点2X位置
  536. @int 点2Y位置
  537. @usage
  538. u8g2.DrawTriangle(20,5, 27,50, 5,32)
  539. */
  540. static int l_u8g2_DrawTriangle(lua_State *L){
  541. if (u8g2 == NULL) return 0;
  542. u8g2_DrawTriangle(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));
  543. return 0;
  544. }
  545. /*
  546. 定义位图函数是否将写入背景色
  547. @api u8g2.SetBitmapMode(mode)
  548. @int mode字体模式,启用(1)或禁用(0)透明模式
  549. @usage
  550. u8g2.SetBitmapMode(1)
  551. */
  552. static int l_u8g2_SetBitmapMode(lua_State *L){
  553. if (u8g2 == NULL) return 0;
  554. u8g2_SetBitmapMode(u8g2,luaL_checkinteger(L, 1));
  555. return 0;
  556. }
  557. /*
  558. 绘制位图
  559. @api u8g2.DrawBitmap(x, y, h, data)
  560. @int X坐标
  561. @int y坐标
  562. @int 行数
  563. @int 位图数据,每一位代表一个字节
  564. @usage
  565. -- 在(10,10)为左上角,绘制 10x4 的位图
  566. u8g2.DrawBitmapMode(10, 10, 10, string.char(0x20, 0xFF, 0xFF, 0xAF, 0xDE))
  567. */
  568. static int l_u8g2_DrawBitmap(lua_State *L){
  569. if (u8g2 == NULL) return 0;
  570. int x = luaL_checkinteger(L, 1);
  571. int y = luaL_checkinteger(L, 2);
  572. int h = luaL_checkinteger(L, 3);
  573. size_t len = 0;
  574. const char* data = luaL_checklstring(L, 4, &len);
  575. if (h < 1) return 0; // 行数必须大于0
  576. if (len < h) return 0; // 起码要填满一行
  577. //if (len % h != 0) return 0; // 必须是行数的整数倍
  578. u8g2_DrawBitmap(u8g2, x, y, len, h, (const uint8_t*)data);
  579. lua_pushboolean(L, 1);
  580. return 1;
  581. }
  582. #include "rotable.h"
  583. static const rotable_Reg reg_u8g2[] =
  584. {
  585. { "begin", l_u8g2_begin, 0},
  586. { "close", l_u8g2_close, 0},
  587. { "ClearBuffer", l_u8g2_ClearBuffer, 0},
  588. { "SendBuffer", l_u8g2_SendBuffer, 0},
  589. { "DrawUTF8", l_u8g2_DrawUTF8, 0},
  590. { "SetFontMode", l_u8g2_SetFontMode, 0},
  591. { "SetFont", l_u8g2_SetFont, 0},
  592. { "GetDisplayHeight", l_u8g2_GetDisplayHeight, 0},
  593. { "GetDisplayWidth", l_u8g2_GetDisplayWidth, 0},
  594. { "DrawPixel", l_u8g2_DrawPixel, 0},
  595. { "DrawLine", l_u8g2_DrawLine, 0},
  596. { "DrawCircle", l_u8g2_DrawCircle, 0},
  597. { "DrawDisc", l_u8g2_DrawDisc, 0},
  598. { "DrawEllipse", l_u8g2_DrawEllipse, 0},
  599. { "DrawFilledEllipse", l_u8g2_DrawFilledEllipse, 0},
  600. { "DrawBox", l_u8g2_DrawBox, 0},
  601. { "DrawFrame", l_u8g2_DrawFrame, 0},
  602. { "DrawRBox", l_u8g2_DrawRBox, 0},
  603. { "DrawRFrame", l_u8g2_DrawRFrame, 0},
  604. { "DrawGlyph", l_u8g2_DrawGlyph, 0},
  605. { "DrawTriangle", l_u8g2_DrawTriangle, 0},
  606. { "SetBitmapMode", l_u8g2_SetBitmapMode, 0},
  607. { "DrawBitmap", l_u8g2_DrawBitmap, 0},
  608. { "font_opposansm8", NULL, font_opposansm8},
  609. { "font_opposansm12_chinese", NULL, font_opposansm12_chinese},
  610. { "font_unifont_t_symbols", NULL, font_unifont_t_symbols},
  611. { "font_open_iconic_weather_6x_t", NULL, font_open_iconic_weather_6x_t},
  612. { NULL, NULL, 0}
  613. };
  614. LUAMOD_API int luaopen_u8g2( lua_State *L ) {
  615. u8g2_lua_ref = 0;
  616. u8g2 = NULL;
  617. luat_newlib(L, reg_u8g2);
  618. return 1;
  619. }
  620. //-----------------------------
  621. // 往下是一些U8G2方法的默认实现
  622. uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  623. uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  624. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  625. LUAT_WEAK int luat_u8g2_setup(luat_u8g2_conf_t *conf) {
  626. if (conf->pinType == 1) {
  627. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  628. if (strncmp("ssd1306", conf->cname, 7) == 0 || strncmp("SSD1306", conf->cname, 7) == 0){
  629. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay);
  630. #ifdef U8G2_USE_SH1106
  631. }else if (strncmp("sh1106", conf->cname, 6) == 0 || strncmp("SH1106", conf->cname, 6) == 0){
  632. u8g2_Setup_sh1106_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay);
  633. #endif
  634. }else{
  635. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay);
  636. }
  637. u8g2->u8x8.pins[U8X8_PIN_I2C_CLOCK] = conf->pin0;
  638. u8g2->u8x8.pins[U8X8_PIN_I2C_DATA] = conf->pin1;
  639. LLOGD("setup disp i2c.sw SCL=%ld SDA=%ld", conf->pin0, conf->pin1);
  640. u8g2_InitDisplay(u8g2);
  641. u8g2_SetPowerSave(u8g2, 0);
  642. return 0;
  643. }
  644. else if (conf->pinType == 2) {
  645. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  646. if (strncmp("ssd1306", conf->cname, 7) == 0 || strncmp("SSD1306", conf->cname, 7) == 0){
  647. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_hw_i2c, u8x8_luat_gpio_and_delay);
  648. #ifdef U8G2_USE_SH1106
  649. }else if (strncmp("sh1106", conf->cname, 6) == 0 || strncmp("SH1106", conf->cname, 6) == 0){
  650. u8g2_Setup_sh1106_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_hw_i2c, u8x8_luat_gpio_and_delay);
  651. #endif
  652. }else{
  653. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_hw_i2c, u8x8_luat_gpio_and_delay);
  654. }
  655. LLOGD("setup disp i2c.hw");
  656. u8g2_InitDisplay(u8g2);
  657. u8g2_SetPowerSave(u8g2, 0);
  658. return 0;
  659. }
  660. else if (conf->pinType == 5) {
  661. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  662. if (strncmp("ssd1306", conf->cname, 7) == 0 || strncmp("SSD1306", conf->cname, 7) == 0){
  663. u8g2_Setup_ssd1306_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_4wire_hw_spi, u8x8_luat_gpio_and_delay);
  664. #ifdef U8G2_USE_SH1106
  665. }else if (strncmp("sh1106", conf->cname, 6) == 0 || strncmp("SH1106", conf->cname, 6) == 0){
  666. u8g2_Setup_sh1106_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_4wire_hw_spi, u8x8_luat_gpio_and_delay);
  667. #endif
  668. #ifdef U8G2_USE_ST7567
  669. }else if (strncmp("st7567", conf->cname, 6) == 0 || strncmp("ST7567", conf->cname, 6) == 0){
  670. u8g2_Setup_st7567_jlx12864_f( u8g2, U8G2_R0, u8x8_luat_byte_4wire_hw_spi, u8x8_luat_gpio_and_delay);
  671. #endif
  672. }else{
  673. u8g2_Setup_ssd1306_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_4wire_hw_spi, u8x8_luat_gpio_and_delay);
  674. }
  675. LLOGD("setup disp spi.hw");
  676. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, spi_cs);
  677. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, spi_dc);
  678. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, spi_res);
  679. u8g2_InitDisplay(u8g2);
  680. u8g2_SetPowerSave(u8g2, 0);
  681. return 0;
  682. }
  683. LLOGI("only i2c sw mode is support, by default impl");
  684. return -1;
  685. }
  686. LUAT_WEAK int luat_u8g2_close(luat_u8g2_conf_t *conf) {
  687. return 0;
  688. }
  689. LUAT_WEAK uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  690. static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes */
  691. static uint8_t buf_idx;
  692. uint8_t *data;
  693. switch(msg)
  694. {
  695. case U8X8_MSG_BYTE_SEND:
  696. data = (uint8_t *)arg_ptr;
  697. while( arg_int > 0 )
  698. {
  699. buffer[buf_idx++] = *data;
  700. data++;
  701. arg_int--;
  702. }
  703. break;
  704. case U8X8_MSG_BYTE_INIT:
  705. //i2c_init(u8x8); /* init i2c communication */
  706. luat_i2c_setup(i2c_id,i2c_speed,NULL);
  707. break;
  708. case U8X8_MSG_BYTE_SET_DC:
  709. /* ignored for i2c */
  710. break;
  711. case U8X8_MSG_BYTE_START_TRANSFER:
  712. buf_idx = 0;
  713. break;
  714. case U8X8_MSG_BYTE_END_TRANSFER:
  715. luat_i2c_send(i2c_id, u8x8_GetI2CAddress(u8x8) >> 1, buffer, buf_idx);
  716. break;
  717. default:
  718. return 0;
  719. }
  720. return 1;
  721. }
  722. int hw_spi_begin(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin )
  723. {
  724. luat_spi_t u8g2_spi = {0};
  725. u8g2_spi.id = spi_id;
  726. switch(spi_mode)
  727. {
  728. case 0: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 0; break;
  729. case 1: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 0; break;
  730. case 2: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 1; break;
  731. case 3: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 1; break;
  732. }
  733. u8g2_spi.dataw = 8;
  734. u8g2_spi.bit_dict = 1;
  735. u8g2_spi.master = 1;
  736. u8g2_spi.mode = 1;
  737. u8g2_spi.bandrate = max_hz;
  738. u8g2_spi.cs = cs_pin;
  739. LLOGI("cs_pin=%d",cs_pin);
  740. luat_spi_setup(&u8g2_spi);
  741. return 1;
  742. }
  743. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  744. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  745. uint8_t i;
  746. uint8_t *data;
  747. uint8_t tx[256];
  748. uint8_t rx[256];
  749. static uint8_t buf_idx;
  750. static uint8_t buffer_tx[256];
  751. switch(msg)
  752. {
  753. case U8X8_MSG_BYTE_SEND:
  754. data = (uint8_t *)arg_ptr;
  755. while( arg_int > 0)
  756. {
  757. buffer_tx[buf_idx++] = (uint8_t)*data;
  758. luat_spi_send(spi_id, (const char*)data, 1);
  759. data++;
  760. arg_int--;
  761. }
  762. //luat_spi_send(spi_id, (uint8_t*)data, arg_int);
  763. break;
  764. case U8X8_MSG_BYTE_INIT:
  765. /* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  766. /* 0: clock active high, data out on falling edge, clock default value is zero, takover on rising edge */
  767. /* 1: clock active high, data out on rising edge, clock default value is zero, takover on falling edge */
  768. /* 2: clock active low, data out on rising edge */
  769. /* 3: clock active low, data out on falling edge */
  770. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  771. hw_spi_begin(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]);
  772. break;
  773. case U8X8_MSG_BYTE_SET_DC:
  774. u8x8_gpio_SetDC(u8x8, arg_int);
  775. break;
  776. case U8X8_MSG_BYTE_START_TRANSFER:
  777. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  778. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  779. break;
  780. case U8X8_MSG_BYTE_END_TRANSFER:
  781. memset( tx, 0, ARRAY_SIZE(tx)*sizeof(uint8_t) );
  782. memset( rx, 0, ARRAY_SIZE(rx)*sizeof(uint8_t) );
  783. for (i = 0; i < buf_idx; ++i)
  784. {
  785. tx[i] = buffer_tx[i];
  786. }
  787. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  788. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  789. buf_idx = 0;
  790. break;
  791. default:
  792. return 0;
  793. }
  794. return 1;
  795. }
  796. LUAT_WEAK uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  797. {
  798. switch(msg)
  799. {
  800. case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
  801. __asm__ volatile("nop");
  802. break;
  803. case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  804. __asm__ volatile("nop");
  805. break;
  806. case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  807. for (uint16_t n = 0; n < 320; n++)
  808. {
  809. __asm__ volatile("nop");
  810. }
  811. break;
  812. case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  813. luat_timer_mdelay(arg_int);
  814. break;
  815. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  816. // Function which implements a delay, arg_int contains the amount of ms
  817. // set spi pin mode
  818. if (u8x8->pins[U8X8_PIN_SPI_CLOCK] != 255) {
  819. 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
  820. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  821. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  822. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  823. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  824. }
  825. // set i2c pin mode
  826. if (u8x8->pins[U8X8_PIN_I2C_DATA] != 255) {
  827. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  828. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  829. }
  830. // set 8080 pin mode
  831. if (u8x8->pins[U8X8_PIN_D0] != 255) {
  832. luat_gpio_mode(u8x8->pins[U8X8_PIN_D0],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  833. luat_gpio_mode(u8x8->pins[U8X8_PIN_D1],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  834. luat_gpio_mode(u8x8->pins[U8X8_PIN_D2],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  835. luat_gpio_mode(u8x8->pins[U8X8_PIN_D3],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  836. luat_gpio_mode(u8x8->pins[U8X8_PIN_D4],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  837. luat_gpio_mode(u8x8->pins[U8X8_PIN_D5],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  838. luat_gpio_mode(u8x8->pins[U8X8_PIN_D6],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  839. luat_gpio_mode(u8x8->pins[U8X8_PIN_D7],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  840. luat_gpio_mode(u8x8->pins[U8X8_PIN_E],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  841. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  842. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  843. }
  844. // set value
  845. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],1);
  846. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],1);
  847. luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],1);
  848. luat_gpio_set(u8x8->pins[U8X8_PIN_DC],1);
  849. luat_gpio_set(u8x8->pins[U8X8_PIN_CS],1);
  850. break;
  851. case U8X8_MSG_DELAY_I2C:
  852. // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  853. // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  854. for (uint16_t n = 0; n < (arg_int<=2?160:40); n++)
  855. {
  856. __asm__ volatile("nop");
  857. }
  858. break;
  859. //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
  860. //case U8X8_MSG_GPIO_SPI_CLOCK:
  861. //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
  862. //case U8X8_MSG_GPIO_SPI_DATA:
  863. case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
  864. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D2],1);
  865. else luat_gpio_set(u8x8->pins[U8X8_PIN_D2],0);
  866. break;
  867. case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
  868. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D3],1);
  869. else luat_gpio_set(u8x8->pins[U8X8_PIN_D3],0);
  870. break;
  871. case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
  872. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D4],1);
  873. else luat_gpio_set(u8x8->pins[U8X8_PIN_D4],0);
  874. break;
  875. case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
  876. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D5],1);
  877. else luat_gpio_set(u8x8->pins[U8X8_PIN_D5],0);
  878. break;
  879. case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
  880. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D6],1);
  881. else luat_gpio_set(u8x8->pins[U8X8_PIN_D6],0);
  882. break;
  883. case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
  884. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D7],1);
  885. else luat_gpio_set(u8x8->pins[U8X8_PIN_D7],0);
  886. break;
  887. case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
  888. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_E],1);
  889. else luat_gpio_set(u8x8->pins[U8X8_PIN_E],0);
  890. break;
  891. case U8X8_MSG_GPIO_I2C_CLOCK:
  892. // arg_int=0: Output low at I2C clock pin
  893. // arg_int=1: Input dir with pullup high for I2C clock pin
  894. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],1);
  895. else luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],0);
  896. break;
  897. case U8X8_MSG_GPIO_I2C_DATA:
  898. // arg_int=0: Output low at I2C data pin
  899. // arg_int=1: Input dir with pullup high for I2C data pin
  900. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],1);
  901. else luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],0);
  902. break;
  903. case U8X8_MSG_GPIO_SPI_CLOCK:
  904. //Function to define the logic level of the clockline
  905. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],1);
  906. else luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],0);
  907. break;
  908. case U8X8_MSG_GPIO_SPI_DATA:
  909. //Function to define the logic level of the data line to the display
  910. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],1);
  911. else luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],0);
  912. break;
  913. case U8X8_MSG_GPIO_CS:
  914. // Function to define the logic level of the CS line
  915. if(arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_CS],1);
  916. else luat_gpio_set(u8x8->pins[U8X8_PIN_CS],0);
  917. break;
  918. case U8X8_MSG_GPIO_DC:
  919. //Function to define the logic level of the Data/ Command line
  920. if(arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_DC],1);
  921. else luat_gpio_set(u8x8->pins[U8X8_PIN_DC],0);
  922. break;
  923. case U8X8_MSG_GPIO_RESET:
  924. //Function to define the logic level of the RESET line
  925. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],1);
  926. else luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],0);
  927. break;
  928. default:
  929. //A message was received which is not implemented, return 0 to indicate an error
  930. return 0;
  931. }
  932. return 1;
  933. }