luat_lib_u8g2.c 33 KB

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