luat_lib_u8g2.c 39 KB

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