luat_lib_u8g2.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. #include "u8g2_luat_fonts.h"
  17. #define LUAT_LOG_TAG "luat.u8g2"
  18. #include "luat_log.h"
  19. enum
  20. {
  21. font_opposansm8,
  22. font_opposansm12_chinese,
  23. font_unifont_t_symbols,
  24. font_open_iconic_weather_6x_t,
  25. };
  26. static u8g2_t* u8g2;
  27. static int u8g2_lua_ref;
  28. static uint8_t i2c_id;
  29. static uint8_t i2c_speed;
  30. // static uint8_t i2c_addr = 0x3C;
  31. static uint8_t spi_id;
  32. static uint8_t spi_res;
  33. static uint8_t spi_dc;
  34. static uint8_t spi_cs;
  35. /*
  36. u8g2显示屏初始化
  37. @api u8g2.begin(conf)
  38. @table conf 配置信息
  39. @return int 正常初始化1,已经初始化过2,内存不够3,初始化失败返回4
  40. @usage
  41. -- 初始化i2c1的ssd1306
  42. u8g2.begin({ic = "SSD1306",mode="i2c_hw",i2c_id=0})
  43. */
  44. static int l_u8g2_begin(lua_State *L) {
  45. if (u8g2 != NULL) {
  46. LLOGW("disp is aready inited");
  47. lua_pushinteger(L, 2);
  48. return 1;
  49. }
  50. u8g2 = (u8g2_t*)lua_newuserdata(L, sizeof(u8g2_t));
  51. if (u8g2 == NULL) {
  52. LLOGE("lua_newuserdata return NULL, out of memory ?!");
  53. lua_pushinteger(L, 3);
  54. return 1;
  55. }
  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, "spi_res");
  146. lua_gettable(L, 1);
  147. if (lua_isinteger(L, -1)) {
  148. spi_res = luaL_checkinteger(L, -1);
  149. LLOGD("spi_res=%d", spi_res);
  150. }
  151. lua_pop(L, 1);
  152. lua_pushliteral(L, "spi_dc");
  153. lua_gettable(L, 1);
  154. if (lua_isinteger(L, -1)) {
  155. spi_dc = luaL_checkinteger(L, -1);
  156. LLOGD("spi_dc=%d", spi_dc);
  157. }
  158. lua_pop(L, 1);
  159. lua_pushliteral(L, "spi_cs");
  160. lua_gettable(L, 1);
  161. if (lua_isinteger(L, -1)) {
  162. spi_cs = luaL_checkinteger(L, -1);
  163. LLOGD("spi_cs=%d", spi_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. u8g2_SetFontDirection(u8g2, 0);
  266. lua_pushboolean(L, 1);
  267. return 1;
  268. }
  269. /*
  270. 设置字体
  271. @api u8g2.SetFont(font)
  272. @int font, u8g2.font_ncenB08_tr为纯英文8x8字节,u8g2.font_opposansm12_chinese 为12x12全中文,u8g2.font_unifont_t_symbols 为符号.
  273. @usage
  274. -- 设置为中文字体,对之后的drawStr有效,使用中文字体需在luat_base.h开启#define USE_U8G2_OPPOSANSM12_CHINESE
  275. u8g2.setFont(u8g2.font_wqy12_t_gb2312)
  276. */
  277. static int l_u8g2_SetFont(lua_State *L) {
  278. if (u8g2 == NULL) {
  279. LLOGI("disp not init yet!!!");
  280. lua_pushboolean(L, 0);
  281. return 1;
  282. }
  283. int font = luaL_checkinteger(L, 1);
  284. switch (font)
  285. {
  286. case font_opposansm8:
  287. LLOGI("font_opposansm8");
  288. u8g2_SetFont(u8g2, u8g2_font_opposansm8);
  289. lua_pushboolean(L, 1);
  290. break;
  291. #if defined USE_U8G2_OPPOSANSM12_CHINESE
  292. case font_opposansm12_chinese:
  293. LLOGI("font_opposansm12_chinese");
  294. u8g2_SetFont(u8g2, u8g2_font_opposansm12_chinese);
  295. lua_pushboolean(L, 1);
  296. break;
  297. #endif
  298. #if defined USE_U8G2_UNIFONT_SYMBOLS
  299. case font_unifont_t_symbols:
  300. LLOGI("font_unifont_t_symbols");
  301. u8g2_SetFont(u8g2, u8g2_font_unifont_t_symbols);
  302. lua_pushboolean(L, 1);
  303. break;
  304. #endif
  305. #if defined USE_U8G2_ICONIC_WEATHER_6X
  306. case font_open_iconic_weather_6x_t:
  307. LLOGI("font_open_iconic_weather_6x_t");
  308. u8g2_SetFont(u8g2, u8g2_font_open_iconic_weather_6x_t);
  309. lua_pushboolean(L, 1);
  310. break;
  311. #endif
  312. default:
  313. lua_pushboolean(L, 0);
  314. LLOGI("default");
  315. break;
  316. }
  317. return 0;
  318. }
  319. /*
  320. 获取显示屏高度
  321. @api u8g2.GetDisplayHeight()
  322. @return int 显示屏高度
  323. @usage
  324. u8g2.GetDisplayHeight()
  325. */
  326. static int l_u8g2_GetDisplayHeight(lua_State *L){
  327. if (u8g2 == NULL) return 0;
  328. lua_pushinteger(L, u8g2_GetDisplayHeight(u8g2));
  329. return 1;
  330. }
  331. /*
  332. 获取显示屏宽度
  333. @api u8g2.GetDisplayWidth()
  334. @return int 显示屏宽度
  335. @usage
  336. u8g2.GetDisplayWidth()
  337. */
  338. static int l_u8g2_GetDisplayWidth(lua_State *L){
  339. if (u8g2 == NULL) return 0;
  340. lua_pushinteger(L, u8g2_GetDisplayWidth(u8g2));
  341. return 1;
  342. }
  343. /*
  344. 画一个点.
  345. @api u8g2.DrawPixel(x,y)
  346. @int X位置.
  347. @int Y位置.
  348. @usage
  349. u8g2.DrawPixel(20, 5)
  350. */
  351. static int l_u8g2_DrawPixel(lua_State *L){
  352. if (u8g2 == NULL) return 0;
  353. u8g2_DrawPixel(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2));
  354. return 1;
  355. }
  356. /*
  357. 在两点之间画一条线.
  358. @api u8g2.DrawLine(x0,y0,x1,y1)
  359. @int 第一个点的X位置.
  360. @int 第一个点的Y位置.
  361. @int 第二个点的X位置.
  362. @int 第二个点的Y位置.
  363. @usage
  364. u8g2.DrawLine(20, 5, 5, 32)
  365. */
  366. static int l_u8g2_DrawLine(lua_State *L){
  367. if (u8g2 == NULL) return 0;
  368. u8g2_DrawLine(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  369. return 1;
  370. }
  371. /*
  372. 在x,y位置画一个半径为rad的空心圆.
  373. @api u8g2.DrawCircle(x0,y0,rad,opt)
  374. @int 圆心位置
  375. @int 圆心位置
  376. @int 圆半径.
  377. @int 选择圆的部分或全部.
  378. 右上: 0x01
  379. 左上: 0x02
  380. 左下: 0x04
  381. 右下: 0x08
  382. 完整圆: (0x01|0x02|0x04|0x08)
  383. @usage
  384. u8g2.DrawCircle(60,30,8,15)
  385. */
  386. static int l_u8g2_DrawCircle(lua_State *L){
  387. if (u8g2 == NULL) return 0;
  388. u8g2_DrawCircle(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  389. return 1;
  390. }
  391. /*
  392. 在x,y位置画一个半径为rad的实心圆.
  393. @api u8g2.DrawDisc(x0,y0,rad,opt)
  394. @int 圆心位置
  395. @int 圆心位置
  396. @int 圆半径.
  397. @int 选择圆的部分或全部.
  398. 右上: 0x01
  399. 左上: 0x02
  400. 左下: 0x04
  401. 右下: 0x08
  402. 完整圆: (0x01|0x02|0x04|0x08)
  403. @usage
  404. u8g2.DrawDisc(60,30,8,15)
  405. */
  406. static int l_u8g2_DrawDisc(lua_State *L){
  407. if (u8g2 == NULL) return 0;
  408. u8g2_DrawDisc(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  409. return 1;
  410. }
  411. /*
  412. 在x,y位置画一个半径为rad的空心椭圆.
  413. @api u8g2.DrawEllipse(x0,y0,rx,ry,opt)
  414. @int 圆心位置
  415. @int 圆心位置
  416. @int 椭圆大小
  417. @int 椭圆大小
  418. @int 选择圆的部分或全部.
  419. 右上: 0x01
  420. 左上: 0x02
  421. 左下: 0x04
  422. 右下: 0x08
  423. 完整圆: (0x01|0x02|0x04|0x08)
  424. @usage
  425. u8g2.DrawEllipse(60,30,8,15)
  426. */
  427. static int l_u8g2_DrawEllipse(lua_State *L){
  428. if (u8g2 == NULL) return 0;
  429. u8g2_DrawEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  430. return 0;
  431. }
  432. /*
  433. 在x,y位置画一个半径为rad的实心椭圆.
  434. @api u8g2.DrawFilledEllipse(x0,y0,rx,ry,opt)
  435. @int 圆心位置
  436. @int 圆心位置
  437. @int 椭圆大小
  438. @int 椭圆大小
  439. @int 选择圆的部分或全部.
  440. 右上: 0x01
  441. 左上: 0x02
  442. 左下: 0x04
  443. 右下: 0x08
  444. 完整圆: (0x01|0x02|0x04|0x08)
  445. @usage
  446. u8g2.DrawFilledEllipse(60,30,8,15)
  447. */
  448. static int l_u8g2_DrawFilledEllipse(lua_State *L){
  449. if (u8g2 == NULL) return 0;
  450. u8g2_DrawFilledEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  451. return 0;
  452. }
  453. /*
  454. 从x / y位置(左上边缘)开始绘制一个框(填充的框).
  455. @api u8g2.DrawBox(x,y,w,h)
  456. @int 左上边缘的X位置
  457. @int 左上边缘的Y位置
  458. @int 盒子的宽度
  459. @int 盒子的高度
  460. @usage
  461. u8g2.DrawBox(3,7,25,15)
  462. */
  463. static int l_u8g2_DrawBox(lua_State *L){
  464. if (u8g2 == NULL) return 0;
  465. u8g2_DrawBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  466. return 0;
  467. }
  468. /*
  469. 从x / y位置(左上边缘)开始绘制一个框(空框).
  470. @api u8g2.DrawFrame(x,y,w,h)
  471. @int 左上边缘的X位置
  472. @int 左上边缘的Y位置
  473. @int 盒子的宽度
  474. @int 盒子的高度
  475. @usage
  476. u8g2.DrawFrame(3,7,25,15)
  477. */
  478. static int l_u8g2_DrawFrame(lua_State *L){
  479. if (u8g2 == NULL) return 0;
  480. u8g2_DrawFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  481. return 0;
  482. }
  483. /*
  484. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的填充框/框架.
  485. @api u8g2.DrawRBox(x,y,w,h,r)
  486. @int 左上边缘的X位置
  487. @int 左上边缘的Y位置
  488. @int 盒子的宽度
  489. @int 盒子的高度
  490. @int 四个边缘的半径
  491. @usage
  492. u8g2.DrawRBox(3,7,25,15)
  493. */
  494. static int l_u8g2_DrawRBox(lua_State *L){
  495. if (u8g2 == NULL) return 0;
  496. u8g2_DrawRBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  497. return 0;
  498. }
  499. /*
  500. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的空框/框架.
  501. @api u8g2.DrawRFrame(x,y,w,h,r)
  502. @int 左上边缘的X位置
  503. @int 左上边缘的Y位置
  504. @int 盒子的宽度
  505. @int 盒子的高度
  506. @int 四个边缘的半径
  507. @usage
  508. u8g2.DrawRFrame(3,7,25,15)
  509. */
  510. static int l_u8g2_DrawRFrame(lua_State *L){
  511. if (u8g2 == NULL) return 0;
  512. u8g2_DrawRFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  513. return 0;
  514. }
  515. /*
  516. 绘制一个图形字符。字符放置在指定的像素位置x和y.
  517. @api u8g2.DrawGlyph(x,y,encoding)
  518. @int 字符在显示屏上的位置
  519. @int 字符在显示屏上的位置
  520. @int 字符的Unicode值
  521. @usage
  522. u8g2.SetFont(u8g2_font_unifont_t_symbols)
  523. u8g2.DrawGlyph(5, 20, 0x2603) -- dec 9731/hex 2603 Snowman
  524. */
  525. static int l_u8g2_DrawGlyph(lua_State *L){
  526. if (u8g2 == NULL) return 0;
  527. u8g2_DrawGlyph(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3));
  528. return 0;
  529. }
  530. /*
  531. 绘制一个三角形(实心多边形).
  532. @api u8g2.DrawTriangle(x0,y0,x1,y1,x2,y2)
  533. @int 点0X位置
  534. @int 点0Y位置
  535. @int 点1X位置
  536. @int 点1Y位置
  537. @int 点2X位置
  538. @int 点2Y位置
  539. @usage
  540. u8g2.DrawTriangle(20,5, 27,50, 5,32)
  541. */
  542. static int l_u8g2_DrawTriangle(lua_State *L){
  543. if (u8g2 == NULL) return 0;
  544. 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));
  545. return 0;
  546. }
  547. /*
  548. 定义位图函数是否将写入背景色
  549. @api u8g2.SetBitmapMode(mode)
  550. @int mode字体模式,启用(1)或禁用(0)透明模式
  551. @usage
  552. u8g2.SetBitmapMode(1)
  553. */
  554. static int l_u8g2_SetBitmapMode(lua_State *L){
  555. if (u8g2 == NULL) return 0;
  556. u8g2_SetBitmapMode(u8g2,luaL_checkinteger(L, 1));
  557. return 0;
  558. }
  559. /*
  560. 绘制位图
  561. @api u8g2.DrawBitmap(x, y, h, data)
  562. @int X坐标
  563. @int y坐标
  564. @int 行数
  565. @int 位图数据,每一位代表一个像素
  566. @usage
  567. -- 在(10,10)为左上角,绘制 10x4 的位图
  568. u8g2.DrawBitmapMode(10, 10, 10, string.char(0x20, 0xFF, 0xFF, 0xAF, 0xDE))
  569. */
  570. static int l_u8g2_DrawBitmap(lua_State *L){
  571. if (u8g2 == NULL) return 0;
  572. int x = luaL_checkinteger(L, 1);
  573. int y = luaL_checkinteger(L, 2);
  574. int h = luaL_checkinteger(L, 3);
  575. size_t len = 0;
  576. const char* data = luaL_checklstring(L, 4, &len);
  577. if (h < 1) return 0; // 行数必须大于0
  578. if (len < h) return 0; // 起码要填满一行
  579. //if (len % h != 0) return 0; // 必须是行数的整数倍
  580. u8g2_DrawBitmap(u8g2, x, y, len, h, (const uint8_t*)data);
  581. lua_pushboolean(L, 1);
  582. return 1;
  583. }
  584. #ifdef LUAT_USE_GTFONT
  585. #include "GT5SLCD2E_1A.h"
  586. extern void gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int widt,unsigned int high,int(*point)(void*),void* userdata,int mode);
  587. extern void gtfont_draw_gray_hz(unsigned char *data,unsigned short x,unsigned short y,unsigned short w ,unsigned short h,unsigned char grade, unsigned char HB_par,int(*point)(void*,uint16_t, uint16_t, uint32_t),void* userdata,int mode);
  588. static int gtfont_u8g2_DrawPixel(u8g2_t *u8g2, uint16_t x, uint16_t y,uint32_t color){
  589. u8g2_DrawPixel(u8g2,x, y);
  590. return 1;
  591. }
  592. /*
  593. 使用gtfont显示gb2312字符串
  594. @api u8g2.drawGtfontGb2312(str,size,x,y)
  595. @string str 显示字符串
  596. @int size 字体大小 (支持16-192号大小字体)
  597. @int x 横坐标
  598. @int y 竖坐标
  599. @usage
  600. u8g2.drawGtfontGb2312("啊啊啊",32,0,0)
  601. */
  602. static int l_u8g2_draw_gtfont_gb2312(lua_State *L) {
  603. unsigned char buf[128];
  604. int len;
  605. int i = 0;
  606. uint8_t strhigh,strlow ;
  607. uint16_t str;
  608. const char *fontCode = luaL_checklstring(L, 1,&len);
  609. unsigned char size = luaL_checkinteger(L, 2);
  610. int x = luaL_checkinteger(L, 3);
  611. int y = luaL_checkinteger(L, 4);
  612. while ( i < len){
  613. strhigh = *fontCode;
  614. fontCode++;
  615. strlow = *fontCode;
  616. str = (strhigh<<8)|strlow;
  617. fontCode++;
  618. get_font(buf, 1, str, size, size, size);
  619. gtfont_draw_w(buf , x ,y , size , size,gtfont_u8g2_DrawPixel,u8g2,2);
  620. x+=size;
  621. i+=2;
  622. }
  623. return 0;
  624. }
  625. #ifdef LUAT_USE_GTFONT_UTF8
  626. extern unsigned short unicodetogb2312 ( unsigned short chr);
  627. static uint8_t utf8_state;
  628. static uint16_t encoding;
  629. static uint16_t utf8_next(uint8_t b)
  630. {
  631. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  632. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  633. if ( utf8_state == 0 )
  634. {
  635. if ( b >= 0xfc ) /* 6 byte sequence */
  636. {
  637. utf8_state = 5;
  638. b &= 1;
  639. }
  640. else if ( b >= 0xf8 )
  641. {
  642. utf8_state = 4;
  643. b &= 3;
  644. }
  645. else if ( b >= 0xf0 )
  646. {
  647. utf8_state = 3;
  648. b &= 7;
  649. }
  650. else if ( b >= 0xe0 )
  651. {
  652. utf8_state = 2;
  653. b &= 15;
  654. }
  655. else if ( b >= 0xc0 )
  656. {
  657. utf8_state = 1;
  658. b &= 0x01f;
  659. }
  660. else
  661. {
  662. /* do nothing, just use the value as encoding */
  663. return b;
  664. }
  665. encoding = b;
  666. return 0x0fffe;
  667. }
  668. else
  669. {
  670. utf8_state--;
  671. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  672. encoding<<=6;
  673. b &= 0x03f;
  674. encoding |= b;
  675. if ( utf8_state != 0 )
  676. return 0x0fffe; /* nothing to do yet */
  677. }
  678. return encoding;
  679. }
  680. /*
  681. 使用gtfont显示UTF8字符串
  682. @api u8g2.drawGtfontUtf8(str,size,x,y)
  683. @string str 显示字符串
  684. @int size 字体大小 (支持16-192号大小字体)
  685. @int x 横坐标
  686. @int y 竖坐标
  687. @usage
  688. u8g2.drawGtfontUtf8("啊啊啊",32,0,0)
  689. */
  690. static int l_u8g2_draw_gtfont_utf8(lua_State *L) {
  691. unsigned char buf[128];
  692. int len;
  693. int i = 0;
  694. uint8_t strhigh,strlow ;
  695. uint16_t e,str;
  696. const char *fontCode = luaL_checklstring(L, 1,&len);
  697. unsigned char size = luaL_checkinteger(L, 2);
  698. int x = luaL_checkinteger(L, 3);
  699. int y = luaL_checkinteger(L, 4);
  700. for(;;){
  701. e = utf8_next((uint8_t)*fontCode);
  702. if ( e == 0x0ffff )
  703. break;
  704. fontCode++;
  705. if ( e != 0x0fffe ){
  706. uint16_t str = unicodetogb2312(e);
  707. get_font(buf, 1, str, size, size, size);
  708. gtfont_draw_w(buf , x ,y , size , size,gtfont_u8g2_DrawPixel,u8g2,2);
  709. x+=size;
  710. }
  711. }
  712. return 0;
  713. }
  714. #endif // LUAT_USE_GTFONT_UTF8
  715. #endif // LUAT_USE_GTFONT
  716. #include "rotable.h"
  717. static const rotable_Reg reg_u8g2[] =
  718. {
  719. { "begin", l_u8g2_begin, 0},
  720. { "close", l_u8g2_close, 0},
  721. { "ClearBuffer", l_u8g2_ClearBuffer, 0},
  722. { "SendBuffer", l_u8g2_SendBuffer, 0},
  723. { "DrawUTF8", l_u8g2_DrawUTF8, 0},
  724. { "SetFontMode", l_u8g2_SetFontMode, 0},
  725. { "SetFont", l_u8g2_SetFont, 0},
  726. { "GetDisplayHeight", l_u8g2_GetDisplayHeight, 0},
  727. { "GetDisplayWidth", l_u8g2_GetDisplayWidth, 0},
  728. { "DrawPixel", l_u8g2_DrawPixel, 0},
  729. { "DrawLine", l_u8g2_DrawLine, 0},
  730. { "DrawCircle", l_u8g2_DrawCircle, 0},
  731. { "DrawDisc", l_u8g2_DrawDisc, 0},
  732. { "DrawEllipse", l_u8g2_DrawEllipse, 0},
  733. { "DrawFilledEllipse", l_u8g2_DrawFilledEllipse, 0},
  734. { "DrawBox", l_u8g2_DrawBox, 0},
  735. { "DrawFrame", l_u8g2_DrawFrame, 0},
  736. { "DrawRBox", l_u8g2_DrawRBox, 0},
  737. { "DrawRFrame", l_u8g2_DrawRFrame, 0},
  738. { "DrawGlyph", l_u8g2_DrawGlyph, 0},
  739. { "DrawTriangle", l_u8g2_DrawTriangle, 0},
  740. { "SetBitmapMode", l_u8g2_SetBitmapMode, 0},
  741. { "DrawBitmap", l_u8g2_DrawBitmap, 0},
  742. #ifdef LUAT_USE_GTFONT
  743. { "drawGtfontGb2312", l_u8g2_draw_gtfont_gb2312, 0},
  744. #ifdef LUAT_USE_GTFONT_UTF8
  745. { "drawGtfontUtf8", l_u8g2_draw_gtfont_utf8, 0},
  746. #endif // LUAT_USE_GTFONT_UTF8
  747. #endif // LUAT_USE_GTFONT
  748. { "font_opposansm8", NULL, font_opposansm8},
  749. { "font_opposansm12_chinese", NULL, font_opposansm12_chinese},
  750. { "font_unifont_t_symbols", NULL, font_unifont_t_symbols},
  751. { "font_open_iconic_weather_6x_t", NULL, font_open_iconic_weather_6x_t},
  752. { NULL, NULL, 0}
  753. };
  754. LUAMOD_API int luaopen_u8g2( lua_State *L ) {
  755. u8g2_lua_ref = 0;
  756. u8g2 = NULL;
  757. luat_newlib(L, reg_u8g2);
  758. return 1;
  759. }
  760. //-----------------------------
  761. // 往下是一些U8G2方法的默认实现
  762. uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  763. uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  764. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  765. LUAT_WEAK int luat_u8g2_setup(luat_u8g2_conf_t *conf) {
  766. if (conf->pinType == 1) {
  767. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  768. if (strncmp("ssd1306", conf->cname, 7) == 0 || strncmp("SSD1306", conf->cname, 7) == 0){
  769. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay);
  770. #ifdef U8G2_USE_SH1106
  771. }else if (strncmp("sh1106", conf->cname, 6) == 0 || strncmp("SH1106", conf->cname, 6) == 0){
  772. u8g2_Setup_sh1106_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay);
  773. #endif
  774. }else{
  775. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay);
  776. }
  777. u8g2->u8x8.pins[U8X8_PIN_I2C_CLOCK] = conf->pin0;
  778. u8g2->u8x8.pins[U8X8_PIN_I2C_DATA] = conf->pin1;
  779. LLOGD("setup disp i2c.sw SCL=%ld SDA=%ld", conf->pin0, conf->pin1);
  780. u8g2_InitDisplay(u8g2);
  781. u8g2_SetPowerSave(u8g2, 0);
  782. return 0;
  783. }
  784. else if (conf->pinType == 2) {
  785. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  786. if (strncmp("ssd1306", conf->cname, 7) == 0 || strncmp("SSD1306", conf->cname, 7) == 0){
  787. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_hw_i2c, u8x8_luat_gpio_and_delay);
  788. #ifdef U8G2_USE_SH1106
  789. }else if (strncmp("sh1106", conf->cname, 6) == 0 || strncmp("SH1106", conf->cname, 6) == 0){
  790. u8g2_Setup_sh1106_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_hw_i2c, u8x8_luat_gpio_and_delay);
  791. #endif
  792. }else{
  793. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_hw_i2c, u8x8_luat_gpio_and_delay);
  794. }
  795. LLOGD("setup disp i2c.hw");
  796. u8g2_InitDisplay(u8g2);
  797. u8g2_SetPowerSave(u8g2, 0);
  798. return 0;
  799. }
  800. else if (conf->pinType == 5) {
  801. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  802. if (strncmp("ssd1306", conf->cname, 7) == 0 || strncmp("SSD1306", conf->cname, 7) == 0){
  803. u8g2_Setup_ssd1306_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_4wire_hw_spi, u8x8_luat_gpio_and_delay);
  804. #ifdef U8G2_USE_SH1106
  805. }else if (strncmp("sh1106", conf->cname, 6) == 0 || strncmp("SH1106", conf->cname, 6) == 0){
  806. u8g2_Setup_sh1106_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_4wire_hw_spi, u8x8_luat_gpio_and_delay);
  807. #endif
  808. #ifdef U8G2_USE_ST7567
  809. }else if (strncmp("st7567", conf->cname, 6) == 0 || strncmp("ST7567", conf->cname, 6) == 0){
  810. u8g2_Setup_st7567_jlx12864_f( u8g2, U8G2_R0, u8x8_luat_byte_4wire_hw_spi, u8x8_luat_gpio_and_delay);
  811. #endif
  812. }else{
  813. u8g2_Setup_ssd1306_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_4wire_hw_spi, u8x8_luat_gpio_and_delay);
  814. }
  815. LLOGD("setup disp spi.hw");
  816. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, spi_cs);
  817. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, spi_dc);
  818. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, spi_res);
  819. u8g2_InitDisplay(u8g2);
  820. u8g2_SetPowerSave(u8g2, 0);
  821. return 0;
  822. }
  823. LLOGI("only i2c sw mode is support, by default impl");
  824. return -1;
  825. }
  826. LUAT_WEAK int luat_u8g2_close(luat_u8g2_conf_t *conf) {
  827. return 0;
  828. }
  829. LUAT_WEAK uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  830. static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes */
  831. static uint8_t buf_idx;
  832. uint8_t *data;
  833. switch(msg)
  834. {
  835. case U8X8_MSG_BYTE_SEND:
  836. data = (uint8_t *)arg_ptr;
  837. while( arg_int > 0 )
  838. {
  839. buffer[buf_idx++] = *data;
  840. data++;
  841. arg_int--;
  842. }
  843. break;
  844. case U8X8_MSG_BYTE_INIT:
  845. //i2c_init(u8x8); /* init i2c communication */
  846. luat_i2c_setup(i2c_id,i2c_speed,NULL);
  847. break;
  848. case U8X8_MSG_BYTE_SET_DC:
  849. /* ignored for i2c */
  850. break;
  851. case U8X8_MSG_BYTE_START_TRANSFER:
  852. buf_idx = 0;
  853. break;
  854. case U8X8_MSG_BYTE_END_TRANSFER:
  855. luat_i2c_send(i2c_id, u8x8_GetI2CAddress(u8x8) >> 1, buffer, buf_idx);
  856. break;
  857. default:
  858. return 0;
  859. }
  860. return 1;
  861. }
  862. int hw_spi_begin(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin )
  863. {
  864. luat_spi_t u8g2_spi = {0};
  865. u8g2_spi.id = spi_id;
  866. switch(spi_mode)
  867. {
  868. case 0: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 0; break;
  869. case 1: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 0; break;
  870. case 2: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 1; break;
  871. case 3: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 1; break;
  872. }
  873. u8g2_spi.dataw = 8;
  874. u8g2_spi.bit_dict = 1;
  875. u8g2_spi.master = 1;
  876. u8g2_spi.mode = 1;
  877. u8g2_spi.bandrate = max_hz;
  878. u8g2_spi.cs = cs_pin;
  879. LLOGI("cs_pin=%d",cs_pin);
  880. luat_spi_setup(&u8g2_spi);
  881. return 1;
  882. }
  883. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  884. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  885. uint8_t i;
  886. uint8_t *data;
  887. uint8_t tx[256];
  888. uint8_t rx[256];
  889. static uint8_t buf_idx;
  890. static uint8_t buffer_tx[256];
  891. switch(msg)
  892. {
  893. case U8X8_MSG_BYTE_SEND:
  894. data = (uint8_t *)arg_ptr;
  895. while( arg_int > 0)
  896. {
  897. buffer_tx[buf_idx++] = (uint8_t)*data;
  898. luat_spi_send(spi_id, (const char*)data, 1);
  899. data++;
  900. arg_int--;
  901. }
  902. //luat_spi_send(spi_id, (uint8_t*)data, arg_int);
  903. break;
  904. case U8X8_MSG_BYTE_INIT:
  905. /* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  906. /* 0: clock active high, data out on falling edge, clock default value is zero, takover on rising edge */
  907. /* 1: clock active high, data out on rising edge, clock default value is zero, takover on falling edge */
  908. /* 2: clock active low, data out on rising edge */
  909. /* 3: clock active low, data out on falling edge */
  910. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  911. hw_spi_begin(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]);
  912. break;
  913. case U8X8_MSG_BYTE_SET_DC:
  914. u8x8_gpio_SetDC(u8x8, arg_int);
  915. break;
  916. case U8X8_MSG_BYTE_START_TRANSFER:
  917. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  918. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  919. break;
  920. case U8X8_MSG_BYTE_END_TRANSFER:
  921. memset( tx, 0, ARRAY_SIZE(tx)*sizeof(uint8_t) );
  922. memset( rx, 0, ARRAY_SIZE(rx)*sizeof(uint8_t) );
  923. for (i = 0; i < buf_idx; ++i)
  924. {
  925. tx[i] = buffer_tx[i];
  926. }
  927. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  928. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  929. buf_idx = 0;
  930. break;
  931. default:
  932. return 0;
  933. }
  934. return 1;
  935. }
  936. LUAT_WEAK uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  937. {
  938. switch(msg)
  939. {
  940. case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
  941. __asm__ volatile("nop");
  942. break;
  943. case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  944. __asm__ volatile("nop");
  945. break;
  946. case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  947. for (uint16_t n = 0; n < 320; n++)
  948. {
  949. __asm__ volatile("nop");
  950. }
  951. break;
  952. case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  953. luat_timer_mdelay(arg_int);
  954. break;
  955. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  956. // Function which implements a delay, arg_int contains the amount of ms
  957. // set spi pin mode
  958. if (u8x8->pins[U8X8_PIN_SPI_CLOCK] != 255) {
  959. 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
  960. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  961. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  962. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  963. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  964. }
  965. // set i2c pin mode
  966. if (u8x8->pins[U8X8_PIN_I2C_DATA] != 255) {
  967. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  968. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  969. }
  970. // set 8080 pin mode
  971. if (u8x8->pins[U8X8_PIN_D0] != 255) {
  972. luat_gpio_mode(u8x8->pins[U8X8_PIN_D0],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  973. luat_gpio_mode(u8x8->pins[U8X8_PIN_D1],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  974. luat_gpio_mode(u8x8->pins[U8X8_PIN_D2],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  975. luat_gpio_mode(u8x8->pins[U8X8_PIN_D3],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  976. luat_gpio_mode(u8x8->pins[U8X8_PIN_D4],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  977. luat_gpio_mode(u8x8->pins[U8X8_PIN_D5],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  978. luat_gpio_mode(u8x8->pins[U8X8_PIN_D6],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  979. luat_gpio_mode(u8x8->pins[U8X8_PIN_D7],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  980. luat_gpio_mode(u8x8->pins[U8X8_PIN_E],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  981. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  982. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  983. }
  984. // set value
  985. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],1);
  986. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],1);
  987. luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],1);
  988. luat_gpio_set(u8x8->pins[U8X8_PIN_DC],1);
  989. luat_gpio_set(u8x8->pins[U8X8_PIN_CS],1);
  990. break;
  991. case U8X8_MSG_DELAY_I2C:
  992. // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  993. // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  994. for (uint16_t n = 0; n < (arg_int<=2?160:40); n++)
  995. {
  996. __asm__ volatile("nop");
  997. }
  998. break;
  999. //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
  1000. //case U8X8_MSG_GPIO_SPI_CLOCK:
  1001. //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
  1002. //case U8X8_MSG_GPIO_SPI_DATA:
  1003. case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
  1004. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D2],1);
  1005. else luat_gpio_set(u8x8->pins[U8X8_PIN_D2],0);
  1006. break;
  1007. case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
  1008. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D3],1);
  1009. else luat_gpio_set(u8x8->pins[U8X8_PIN_D3],0);
  1010. break;
  1011. case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
  1012. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D4],1);
  1013. else luat_gpio_set(u8x8->pins[U8X8_PIN_D4],0);
  1014. break;
  1015. case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
  1016. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D5],1);
  1017. else luat_gpio_set(u8x8->pins[U8X8_PIN_D5],0);
  1018. break;
  1019. case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
  1020. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D6],1);
  1021. else luat_gpio_set(u8x8->pins[U8X8_PIN_D6],0);
  1022. break;
  1023. case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
  1024. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D7],1);
  1025. else luat_gpio_set(u8x8->pins[U8X8_PIN_D7],0);
  1026. break;
  1027. case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
  1028. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_E],1);
  1029. else luat_gpio_set(u8x8->pins[U8X8_PIN_E],0);
  1030. break;
  1031. case U8X8_MSG_GPIO_I2C_CLOCK:
  1032. // arg_int=0: Output low at I2C clock pin
  1033. // arg_int=1: Input dir with pullup high for I2C clock pin
  1034. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],1);
  1035. else luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],0);
  1036. break;
  1037. case U8X8_MSG_GPIO_I2C_DATA:
  1038. // arg_int=0: Output low at I2C data pin
  1039. // arg_int=1: Input dir with pullup high for I2C data pin
  1040. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],1);
  1041. else luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],0);
  1042. break;
  1043. case U8X8_MSG_GPIO_SPI_CLOCK:
  1044. //Function to define the logic level of the clockline
  1045. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],1);
  1046. else luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],0);
  1047. break;
  1048. case U8X8_MSG_GPIO_SPI_DATA:
  1049. //Function to define the logic level of the data line to the display
  1050. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],1);
  1051. else luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],0);
  1052. break;
  1053. case U8X8_MSG_GPIO_CS:
  1054. // Function to define the logic level of the CS line
  1055. if(arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_CS],1);
  1056. else luat_gpio_set(u8x8->pins[U8X8_PIN_CS],0);
  1057. break;
  1058. case U8X8_MSG_GPIO_DC:
  1059. //Function to define the logic level of the Data/ Command line
  1060. if(arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_DC],1);
  1061. else luat_gpio_set(u8x8->pins[U8X8_PIN_DC],0);
  1062. break;
  1063. case U8X8_MSG_GPIO_RESET:
  1064. //Function to define the logic level of the RESET line
  1065. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],1);
  1066. else luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],0);
  1067. break;
  1068. default:
  1069. //A message was received which is not implemented, return 0 to indicate an error
  1070. return 0;
  1071. }
  1072. return 1;
  1073. }