luat_lib_u8g2.c 33 KB

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