luat_lib_u8g2.c 38 KB

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