luat_lib_u8g2.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. /*
  2. @module u8g2
  3. @summary u8g2图形处理库
  4. @author Dozingfiretruck
  5. @version 1.0
  6. @date 2021.01.25
  7. @demo u8g2
  8. @tag LUAT_USE_U8G2
  9. */
  10. #include "luat_base.h"
  11. #include "luat_malloc.h"
  12. #include "luat_u8g2.h"
  13. #include "luat_gpio.h"
  14. #include "luat_timer.h"
  15. #include "luat_i2c.h"
  16. #include "luat_spi.h"
  17. #include "qrcodegen.h"
  18. #include "u8g2.h"
  19. #include "u8g2_luat_fonts.h"
  20. #define LUAT_LOG_TAG "u8g2"
  21. #include "luat_log.h"
  22. static u8g2_t* u8g2 = NULL;
  23. static int u8g2_lua_ref = 0;
  24. uint8_t pinType = 255; // I2C_SW = 1, I2C_HW = 2, SPI_SW_3PIN = 3, SPI_SW_4PIN = 4, SPI_HW_4PIN=5, P8080 = 6
  25. static uint8_t i2c_id;
  26. static uint8_t i2c_speed;
  27. static uint8_t i2c_scl;
  28. static uint8_t i2c_sda;
  29. // static uint8_t i2c_addr = 0x3C;
  30. static uint8_t spi_id;
  31. static uint8_t spi_res;
  32. static uint8_t spi_dc;
  33. static uint8_t spi_cs;
  34. static uint8_t * buff_ptr = NULL;
  35. static const char* mode_strs[] = {
  36. "i2c_sw",
  37. "i2c_hw",
  38. "spi_sw_3pin",
  39. "spi_sw_4pin",
  40. "spi_hw_4pin"
  41. };
  42. /*
  43. u8g2显示屏初始化
  44. @api u8g2.begin(conf)
  45. @table conf 配置信息 ic:支持 ssd1306 ssd1309 ssd1322 sh1106 sh1107 sh1108 st7567 uc1701 ssd1306_128x32, direction:方向,可选0 90 180 270 默认0 mode:模式,可选i2c_sw:软件i2c i2c_hw:硬件i2c spi_hw_4pin:硬件spi i2c_id:硬件i2c时有效 i2c_scl=1、i2c_sda:软件i2c时有效 spi_id、spi_res、spi_dc、spi_cs:硬件spi时生效
  46. @return int 正常初始化1,已经初始化过2,内存不够3,初始化失败返回4
  47. @usage
  48. -- 初始化硬件i2c的ssd1306
  49. u8g2.begin({ic = "ssd1306",direction = 0,mode="i2c_hw",i2c_id=0}) -- direction 可选0 90 180 270
  50. -- 初始化硬件spi的ssd1306
  51. u8g2.begin({ic = "ssd1306",direction = 0,mode="spi_hw_4pin",spi_id=0,spi_res=pin.PB03,spi_dc=pin.PB01,spi_cs=pin.PB04}) -- direction 可选0 90 180 270
  52. -- 初始化软件i2c的ssd1306
  53. u8g2.begin({ic = "ssd1306",direction = 0,mode="i2c_sw", i2c_scl=1, i2c_sda=4}) -- 通过PA1 SCL / PA4 SDA模拟
  54. */
  55. static int l_u8g2_begin(lua_State *L) {
  56. if (u8g2 != NULL) {
  57. LLOGW("disp is aready inited");
  58. lua_pushinteger(L, 2);
  59. return 1;
  60. }
  61. u8g2 = (u8g2_t*)lua_newuserdata(L, sizeof(u8g2_t));
  62. if (u8g2 == NULL) {
  63. LLOGE("lua_newuserdata return NULL, out of memory ?!");
  64. lua_pushinteger(L, 3);
  65. return 1;
  66. }
  67. luat_u8g2_conf_t conf = {0};
  68. conf.ptr = u8g2;
  69. conf.direction = U8G2_R0;
  70. char mode[12] = {0};
  71. size_t mode_len = 0;
  72. if (lua_istable(L, 1)) {
  73. // 参数解析
  74. lua_pushliteral(L, "ic");
  75. lua_gettable(L, 1);
  76. if (lua_isstring(L, -1)) {
  77. conf.cname = (char*)luaL_checkstring(L, -1);
  78. //LLOGD("using ic: %s",conf.cname);
  79. }
  80. lua_pop(L, 1);
  81. lua_pushliteral(L, "direction");
  82. lua_gettable(L, 1);
  83. if (lua_isinteger(L, -1)) {
  84. int direction = luaL_checkinteger(L, -1);
  85. switch (direction)
  86. {
  87. case 0:
  88. conf.direction = U8G2_R0;
  89. break;
  90. case 90:
  91. conf.direction = U8G2_R1;
  92. break;
  93. case 180:
  94. conf.direction = U8G2_R2;
  95. break;
  96. case 270:
  97. conf.direction = U8G2_R3;
  98. break;
  99. default:
  100. conf.direction = U8G2_R0;
  101. break;
  102. }
  103. }
  104. lua_pop(L, 1);
  105. lua_pushliteral(L, "mode");
  106. lua_gettable(L, 1);
  107. if (!lua_isstring(L, -1)) {
  108. LLOGE("need mode!!!");
  109. return 0;
  110. }
  111. const char* tmp = luaL_checklstring(L, -1, &mode_len);
  112. if (mode_len < 1 || mode_len > 16) {
  113. LLOGE("mode string too short or too long!!");
  114. return 0;
  115. }
  116. memcpy(mode, tmp, strlen(tmp));
  117. lua_pop(L, 1);
  118. for (size_t i = 0; i < sizeof(mode_strs) / sizeof(const char*); i++)
  119. {
  120. if (strcmp(mode_strs[i], tmp) == 0) {
  121. pinType = i + 1;
  122. break;
  123. }
  124. }
  125. if (pinType == 255) {
  126. LLOGE("no such mode [%s]", tmp);
  127. return 0;
  128. }
  129. lua_pushliteral(L, "i2c_scl");
  130. lua_gettable(L, 1);
  131. if (lua_isinteger(L, -1)) {
  132. i2c_scl = luaL_checkinteger(L, -1);
  133. }
  134. lua_pop(L, 1);
  135. lua_pushliteral(L, "i2c_sda");
  136. lua_gettable(L, 1);
  137. if (lua_isinteger(L, -1)) {
  138. i2c_sda = luaL_checkinteger(L, -1);
  139. }
  140. lua_pop(L, 1);
  141. lua_pushliteral(L, "i2c_id");
  142. lua_gettable(L, 1);
  143. if (lua_isinteger(L, -1)) {
  144. i2c_id = luaL_checkinteger(L, -1);
  145. }
  146. lua_pop(L, 1);
  147. lua_pushliteral(L, "i2c_speed");
  148. lua_gettable(L, 1);
  149. if (lua_isinteger(L, -1)) {
  150. i2c_speed = luaL_checkinteger(L, -1);
  151. }
  152. lua_pop(L, 1);
  153. lua_pushliteral(L, "spi_id");
  154. lua_gettable(L, 1);
  155. if (lua_isinteger(L, -1)) {
  156. spi_id = luaL_checkinteger(L, -1);
  157. }
  158. lua_pop(L, 1);
  159. lua_pushliteral(L, "spi_res");
  160. lua_gettable(L, 1);
  161. if (lua_isinteger(L, -1)) {
  162. spi_res = luaL_checkinteger(L, -1);
  163. }
  164. lua_pop(L, 1);
  165. lua_pushliteral(L, "spi_dc");
  166. lua_gettable(L, 1);
  167. if (lua_isinteger(L, -1)) {
  168. spi_dc = luaL_checkinteger(L, -1);
  169. }
  170. lua_pop(L, 1);
  171. lua_pushliteral(L, "spi_cs");
  172. lua_gettable(L, 1);
  173. if (lua_isinteger(L, -1)) {
  174. spi_cs = luaL_checkinteger(L, -1);
  175. }
  176. lua_pop(L, 1);
  177. }
  178. LLOGD("driver %s mode %s", conf.cname, mode);
  179. if (luat_u8g2_setup(&conf)) {
  180. u8g2 = NULL;
  181. LLOGW("disp init fail");
  182. lua_pushinteger(L, 4);
  183. return 1; // 初始化失败
  184. }
  185. LLOGD("setup done");
  186. u8g2_lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  187. u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr); // 设置默认字体
  188. lua_pushinteger(L, 1);
  189. return 1;
  190. }
  191. /*
  192. 关闭显示屏
  193. @api u8g2.close()
  194. @usage
  195. -- 关闭disp,再次使用disp相关API的话,需要重新初始化
  196. u8g2.close()
  197. */
  198. static int l_u8g2_close(lua_State *L) {
  199. if (u8g2_lua_ref != 0) {
  200. lua_geti(L, LUA_REGISTRYINDEX, u8g2_lua_ref);
  201. if (lua_isuserdata(L, -1)) {
  202. luaL_unref(L, LUA_REGISTRYINDEX, u8g2_lua_ref);
  203. }
  204. u8g2_lua_ref = 0;
  205. }
  206. // buff也得释放掉
  207. if (buff_ptr != NULL) {
  208. luat_heap_free(buff_ptr);
  209. buff_ptr = NULL;
  210. }
  211. lua_gc(L, LUA_GCCOLLECT, 0);
  212. lua_gc(L, LUA_GCCOLLECT, 0);
  213. u8g2 = NULL;
  214. return 0;
  215. }
  216. /*
  217. 清屏,清除内存帧缓冲区中的所有像素
  218. @api u8g2.ClearBuffer()
  219. @usage
  220. -- 清屏
  221. u8g2.ClearBuffer()
  222. */
  223. static int l_u8g2_ClearBuffer(lua_State *L) {
  224. if (u8g2 == NULL) return 0;
  225. u8g2_ClearBuffer(u8g2);
  226. return 0;
  227. }
  228. /*
  229. 将数据更新到屏幕,将存储器帧缓冲区的内容发送到显示器
  230. @api u8g2.SendBuffer()
  231. @usage
  232. -- 把显示数据更新到屏幕
  233. u8g2.SendBuffer()
  234. */
  235. static int l_u8g2_SendBuffer(lua_State *L) {
  236. if (u8g2 == NULL) return 0;
  237. u8g2_SendBuffer(u8g2);
  238. return 0;
  239. }
  240. /*
  241. 在显示屏上画一段文字,在显示屏上画一段文字,要调用u8g2.SendBuffer()才会更新到屏幕
  242. @api u8g2.DrawUTF8(str, x, y)
  243. @string 文件内容
  244. @int 横坐标
  245. @int 竖坐标
  246. @usage
  247. u8g2.DrawUTF8("wifi is ready", 10, 20)
  248. */
  249. static int l_u8g2_DrawUTF8(lua_State *L) {
  250. if (u8g2 == NULL) {
  251. LLOGW("disp not init yet!!!");
  252. return 0;
  253. }
  254. size_t len;
  255. size_t x, y;
  256. const char* str = luaL_checklstring(L, 1, &len);
  257. x = luaL_checkinteger(L, 2);
  258. y = luaL_checkinteger(L, 3);
  259. u8g2_DrawUTF8(u8g2, x, y, str);
  260. return 0;
  261. }
  262. /*
  263. 在提供的文本周围画一个框。这与 DrawUTF8 类似,但为文本添加了一些装饰。,要调用u8g2.SendBuffer()才会更新到屏幕
  264. @api u8g2.DrawButtonUTF8(str, x, y, flags, w, h, v, str)
  265. @string 文件内容
  266. @int 横坐标
  267. @int 竖坐标
  268. @int 标志 多个标志可以与“或”运算符一起使用。
  269. @int 文本的最小宽度。如果为0(或低于文本宽度),则文本宽度将用于框架。
  270. @int 文本前后的额外空间。
  271. @int 文本上下的额外空间。
  272. @usage
  273. u8g2.DrawButtonUTF8("str", 10, 20,u8g2.BTN_BW2,0,2,2)
  274. */
  275. static int l_u8g2_DrawButtonUTF8(lua_State *L) {
  276. if (u8g2 == NULL) {
  277. LLOGW("disp not init yet!!!");
  278. return 0;
  279. }
  280. size_t len;
  281. size_t x, y,flags,w,h,v;
  282. const char* str = luaL_checklstring(L, 1, &len);
  283. x = luaL_checkinteger(L, 2);
  284. y = luaL_checkinteger(L, 3);
  285. flags = luaL_checkinteger(L, 4);
  286. w = luaL_checkinteger(L, 5);
  287. h = luaL_checkinteger(L, 6);
  288. v = luaL_checkinteger(L, 7);
  289. u8g2_DrawButtonUTF8(u8g2, x, y, flags, w, h, v, str);
  290. return 0;
  291. }
  292. /*
  293. 设置字体模式
  294. @api u8g2.SetFontMode(mode)
  295. @int mode字体模式,启用(1)或禁用(0)透明模式
  296. @usage
  297. u8g2.SetFontMode(1)
  298. */
  299. static int l_u8g2_SetFontMode(lua_State *L){
  300. if (u8g2 == NULL) return 0;
  301. int font_mode = luaL_checkinteger(L, 1);
  302. if (font_mode < 0) {
  303. lua_pushboolean(L, 0);
  304. }
  305. u8g2_SetFontMode(u8g2, font_mode);
  306. u8g2_SetFontDirection(u8g2, 0);
  307. lua_pushboolean(L, 1);
  308. return 1;
  309. }
  310. /*
  311. 设置字体
  312. @api u8g2.SetFont(font)
  313. @userdata font, u8g2.font_opposansm8 为纯英文8号字体,还有font_opposansm10 font_opposansm12 font_opposansm16 font_opposansm18 font_opposansm20 font_opposansm22 font_opposansm24 font_opposansm32 可选 u8g2.font_opposansm12_chinese 为12x12全中文,还有 font_opposansm16_chinese font_opposansm24_chinese font_opposansm32_chinese 可选, u8g2.font_unifont_t_symbols 为符号.
  314. @usage
  315. -- 设置为中文字体,对之后的drawStr有效
  316. u8g2.SetFont(u8g2.font_opposansm12)
  317. */
  318. static int l_u8g2_SetFont(lua_State *L) {
  319. if (u8g2 == NULL) {
  320. LLOGI("u8g2 not init yet!!!");
  321. lua_pushboolean(L, 0);
  322. return 1;
  323. }
  324. if (!lua_islightuserdata(L, 1)) {
  325. LLOGE("no such font");
  326. return 0;
  327. }
  328. const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
  329. if (ptr == NULL) {
  330. LLOGE("only font pointer is allow");
  331. return 0;
  332. }
  333. u8g2_SetFont(u8g2, ptr);
  334. lua_pushboolean(L, 1);
  335. return 1;
  336. }
  337. /*
  338. 获取显示屏高度
  339. @api u8g2.GetDisplayHeight()
  340. @return int 显示屏高度
  341. @usage
  342. u8g2.GetDisplayHeight()
  343. */
  344. static int l_u8g2_GetDisplayHeight(lua_State *L){
  345. if (u8g2 == NULL) return 0;
  346. lua_pushinteger(L, u8g2_GetDisplayHeight(u8g2));
  347. return 1;
  348. }
  349. /*
  350. 获取显示屏宽度
  351. @api u8g2.GetDisplayWidth()
  352. @return int 显示屏宽度
  353. @usage
  354. u8g2.GetDisplayWidth()
  355. */
  356. static int l_u8g2_GetDisplayWidth(lua_State *L){
  357. if (u8g2 == NULL) return 0;
  358. lua_pushinteger(L, u8g2_GetDisplayWidth(u8g2));
  359. return 1;
  360. }
  361. /*
  362. 为所有绘图功能分配绘图颜色。
  363. @api u8g2.SetDrawColor(c)
  364. @int c为颜色值 0没有色 1有色 2与底色xor
  365. @usage
  366. u8g2.SetDrawColor(0)
  367. */
  368. static int l_u8g2_SetDrawColor(lua_State *L){
  369. if (u8g2 == NULL) return 0;
  370. u8g2_SetDrawColor(u8g2,luaL_checkinteger(L, 1));
  371. return 0;
  372. }
  373. /*
  374. 画一个点.
  375. @api u8g2.DrawPixel(x,y)
  376. @int X位置.
  377. @int Y位置.
  378. @usage
  379. u8g2.DrawPixel(20, 5)
  380. */
  381. static int l_u8g2_DrawPixel(lua_State *L){
  382. if (u8g2 == NULL) return 0;
  383. u8g2_DrawPixel(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2));
  384. return 0;
  385. }
  386. /*
  387. 在两点之间画一条线.
  388. @api u8g2.DrawLine(x0,y0,x1,y1)
  389. @int 第一个点的X位置.
  390. @int 第一个点的Y位置.
  391. @int 第二个点的X位置.
  392. @int 第二个点的Y位置.
  393. @usage
  394. u8g2.DrawLine(20, 5, 5, 32)
  395. */
  396. static int l_u8g2_DrawLine(lua_State *L){
  397. if (u8g2 == NULL) return 0;
  398. u8g2_DrawLine(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  399. return 0;
  400. }
  401. /*
  402. 在x,y位置画一个半径为rad的空心圆.
  403. @api u8g2.DrawCircle(x0,y0,rad,opt)
  404. @int 圆心位置
  405. @int 圆心位置
  406. @int 圆半径.
  407. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  408. @usage
  409. u8g2.DrawCircle(60,30,8,u8g2.DRAW_ALL)
  410. */
  411. static int l_u8g2_DrawCircle(lua_State *L){
  412. if (u8g2 == NULL) return 0;
  413. u8g2_DrawCircle(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_optinteger(L, 4,U8G2_DRAW_ALL));
  414. return 0;
  415. }
  416. /*
  417. 在x,y位置画一个半径为rad的实心圆.
  418. @api u8g2.DrawDisc(x0,y0,rad,opt)
  419. @int 圆心位置
  420. @int 圆心位置
  421. @int 圆半径.
  422. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  423. @usage
  424. u8g2.DrawDisc(60,30,8,u8g2.DRAW_ALL)
  425. */
  426. static int l_u8g2_DrawDisc(lua_State *L){
  427. if (u8g2 == NULL) return 0;
  428. u8g2_DrawDisc(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_optinteger(L, 4,U8G2_DRAW_ALL));
  429. return 0;
  430. }
  431. /*
  432. 在x,y位置画一个半径为rad的空心椭圆.
  433. @api u8g2.DrawEllipse(x0,y0,rx,ry,opt)
  434. @int 圆心位置
  435. @int 圆心位置
  436. @int 椭圆大小
  437. @int 椭圆大小
  438. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  439. @usage
  440. u8g2.DrawEllipse(60,30,8,u8g2.DRAW_ALL)
  441. */
  442. static int l_u8g2_DrawEllipse(lua_State *L){
  443. if (u8g2 == NULL) return 0;
  444. u8g2_DrawEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_optinteger(L, 5,U8G2_DRAW_ALL));
  445. return 0;
  446. }
  447. /*
  448. 在x,y位置画一个半径为rad的实心椭圆.
  449. @api u8g2.DrawFilledEllipse(x0,y0,rx,ry,opt)
  450. @int 圆心位置
  451. @int 圆心位置
  452. @int 椭圆大小
  453. @int 椭圆大小
  454. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  455. @usage
  456. u8g2.DrawFilledEllipse(60,30,8,15)
  457. */
  458. static int l_u8g2_DrawFilledEllipse(lua_State *L){
  459. if (u8g2 == NULL) return 0;
  460. u8g2_DrawFilledEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_optinteger(L, 5,U8G2_DRAW_ALL));
  461. return 0;
  462. }
  463. /*
  464. 从x / y位置(左上边缘)开始绘制一个框(填充的框).
  465. @api u8g2.DrawBox(x,y,w,h)
  466. @int 左上边缘的X位置
  467. @int 左上边缘的Y位置
  468. @int 盒子的宽度
  469. @int 盒子的高度
  470. @usage
  471. u8g2.DrawBox(3,7,25,15)
  472. */
  473. static int l_u8g2_DrawBox(lua_State *L){
  474. if (u8g2 == NULL) return 0;
  475. u8g2_DrawBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  476. return 0;
  477. }
  478. /*
  479. 从x / y位置(左上边缘)开始绘制一个框(空框).
  480. @api u8g2.DrawFrame(x,y,w,h)
  481. @int 左上边缘的X位置
  482. @int 左上边缘的Y位置
  483. @int 盒子的宽度
  484. @int 盒子的高度
  485. @usage
  486. u8g2.DrawFrame(3,7,25,15)
  487. */
  488. static int l_u8g2_DrawFrame(lua_State *L){
  489. if (u8g2 == NULL) return 0;
  490. u8g2_DrawFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  491. return 0;
  492. }
  493. /*
  494. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的填充框/框架.
  495. @api u8g2.DrawRBox(x,y,w,h,r)
  496. @int 左上边缘的X位置
  497. @int 左上边缘的Y位置
  498. @int 盒子的宽度
  499. @int 盒子的高度
  500. @int 四个边缘的半径
  501. @usage
  502. u8g2.DrawRBox(3,7,25,15)
  503. */
  504. static int l_u8g2_DrawRBox(lua_State *L){
  505. if (u8g2 == NULL) return 0;
  506. u8g2_DrawRBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  507. return 0;
  508. }
  509. /*
  510. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的空框/框架.
  511. @api u8g2.DrawRFrame(x,y,w,h,r)
  512. @int 左上边缘的X位置
  513. @int 左上边缘的Y位置
  514. @int 盒子的宽度
  515. @int 盒子的高度
  516. @int 四个边缘的半径
  517. @usage
  518. u8g2.DrawRFrame(3,7,25,15)
  519. */
  520. static int l_u8g2_DrawRFrame(lua_State *L){
  521. if (u8g2 == NULL) return 0;
  522. u8g2_DrawRFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  523. return 0;
  524. }
  525. /*
  526. 绘制一个图形字符。字符放置在指定的像素位置x和y.
  527. @api u8g2.DrawGlyph(x,y,encoding)
  528. @int 字符在显示屏上的位置
  529. @int 字符在显示屏上的位置
  530. @int 字符的Unicode值
  531. @usage
  532. u8g2.SetFont(u8g2_font_unifont_t_symbols)
  533. u8g2.DrawGlyph(5, 20, 0x2603) -- dec 9731/hex 2603 Snowman
  534. */
  535. static int l_u8g2_DrawGlyph(lua_State *L){
  536. if (u8g2 == NULL) return 0;
  537. u8g2_DrawGlyph(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3));
  538. return 0;
  539. }
  540. /*
  541. 绘制一个三角形(实心多边形).
  542. @api u8g2.DrawTriangle(x0,y0,x1,y1,x2,y2)
  543. @int 点0X位置
  544. @int 点0Y位置
  545. @int 点1X位置
  546. @int 点1Y位置
  547. @int 点2X位置
  548. @int 点2Y位置
  549. @usage
  550. u8g2.DrawTriangle(20,5, 27,50, 5,32)
  551. */
  552. static int l_u8g2_DrawTriangle(lua_State *L){
  553. if (u8g2 == NULL) return 0;
  554. 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));
  555. return 0;
  556. }
  557. /*
  558. 定义位图函数是否将写入背景色
  559. @api u8g2.SetBitmapMode(mode)
  560. @int mode字体模式,启用(1)或禁用(0)透明模式
  561. @usage
  562. u8g2.SetBitmapMode(1)
  563. */
  564. static int l_u8g2_SetBitmapMode(lua_State *L){
  565. if (u8g2 == NULL) return 0;
  566. u8g2_SetBitmapMode(u8g2,luaL_checkinteger(L, 1));
  567. return 0;
  568. }
  569. /*
  570. 绘制位图
  571. @api u8g2.DrawXBM(x, y, w, h, data)
  572. @int X坐标
  573. @int y坐标
  574. @int 位图宽
  575. @int 位图高
  576. @int 位图数据,每一位代表一个像素
  577. @usage
  578. -- 取模使用PCtoLCD2002软件即可
  579. -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
  580. u8g2.DrawXBM(0, 0, 16,16, string.char(
  581. 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
  582. 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
  583. ))
  584. */
  585. static int l_u8g2_DrawXBM(lua_State *L){
  586. if (u8g2 == NULL) return 0;
  587. int x = luaL_checkinteger(L, 1);
  588. int y = luaL_checkinteger(L, 2);
  589. int w = luaL_checkinteger(L, 3);
  590. int h = luaL_checkinteger(L, 4);
  591. size_t len = 0;
  592. const char* data = luaL_checklstring(L, 5, &len);
  593. if (h < 1) return 0; // 行数必须大于0
  594. if (len*8/h < w) return 0; // 起码要填满一行
  595. if (len != h*w/8)return 0;
  596. u8g2_DrawXBM(u8g2, x, y, w, h, (const uint8_t*)data);
  597. lua_pushboolean(L, 1);
  598. return 1;
  599. }
  600. /**
  601. 缓冲区绘制QRCode
  602. @api u8g2.DrawDrcode(x, y, str, size)
  603. @int x坐标
  604. @int y坐标
  605. @string 二维码的内容
  606. @int 显示大小 (注意:二维码生成大小与要显示内容和纠错等级有关,生成版本为1-40(对应 21x21 - 177x177)的不定大小,如果和设置大小不同会自动在指定的区域中间显示二维码,如二维码未显示请查看日志提示)
  607. @return nil 无返回值
  608. */
  609. static int l_u8g2_DrawDrcode(lua_State *L)
  610. {
  611. size_t len;
  612. int x = luaL_checkinteger(L, 1);
  613. int y = luaL_checkinteger(L, 2);
  614. const char* text = luaL_checklstring(L, 3, &len);
  615. int size = luaL_checkinteger(L, 4);
  616. uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  617. uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  618. if (qrcode == NULL || tempBuffer == NULL) {
  619. if (qrcode)
  620. luat_heap_free(qrcode);
  621. if (tempBuffer)
  622. luat_heap_free(tempBuffer);
  623. LLOGE("qrcode out of memory");
  624. return 0;
  625. }
  626. bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_LOW,
  627. qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
  628. if (ok){
  629. int qr_size = qrcodegen_getSize(qrcode);
  630. if (size < qr_size){
  631. LLOGE("size must be greater than qr_size %d",qr_size);
  632. goto end;
  633. }
  634. int scale = size / qr_size ;
  635. if (!scale)scale = 1;
  636. int margin = (size - qr_size * scale) / 2;
  637. x+=margin;
  638. y+=margin;
  639. for (int j = 0; j < qr_size; j++) {
  640. for (int i = 0; i < qr_size; i++) {
  641. if (qrcodegen_getModule(qrcode, i, j))
  642. u8g2_DrawBox(u8g2,x+i*scale,y+j*scale,scale,scale);
  643. }
  644. }
  645. }else{
  646. LLOGE("qrcodegen_encodeText false");
  647. }
  648. end:
  649. if (qrcode)
  650. luat_heap_free(qrcode);
  651. if (tempBuffer)
  652. luat_heap_free(tempBuffer);
  653. return 0;
  654. }
  655. /**
  656. 调整屏幕对比度
  657. @api u8g2.SetContrast(contrast)
  658. @int 对比度(取值范围与IC有关,如st7567为0-63)
  659. @return nil 无返回值
  660. */
  661. static int l_u8g2_SetContrast(lua_State *L)
  662. {
  663. if (u8g2 == NULL) return 0;
  664. u8g2_SetContrast(u8g2,luaL_checkinteger(L, 1));
  665. return 0;
  666. }
  667. #ifdef LUAT_USE_GTFONT
  668. #include "GT5SLCD2E_1A.h"
  669. extern unsigned int gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int size,unsigned int widt,unsigned int high,int(*point)(void*),void* userdata,int mode);
  670. 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);
  671. static int gtfont_u8g2_DrawPixel(u8g2_t *u8g2, uint16_t x, uint16_t y,uint32_t color){
  672. u8g2_DrawPixel(u8g2,x, y);
  673. return 1;
  674. }
  675. /*
  676. 使用gtfont显示gb2312字符串
  677. @api u8g2.drawGtfontGb2312(str,size,x,y)
  678. @string str 显示字符串
  679. @int size 字体大小 (支持16-192号大小字体)
  680. @int x 横坐标
  681. @int y 竖坐标
  682. @usage
  683. u8g2.drawGtfontGb2312("啊啊啊",32,0,0)
  684. */
  685. static int l_u8g2_draw_gtfont_gb2312(lua_State *L) {
  686. unsigned char buf[128];
  687. int len;
  688. int i = 0;
  689. uint8_t strhigh,strlow ;
  690. uint16_t str;
  691. const char *fontCode = luaL_checklstring(L, 1,&len);
  692. unsigned char size = luaL_checkinteger(L, 2);
  693. int x = luaL_checkinteger(L, 3);
  694. int y = luaL_checkinteger(L, 4);
  695. while ( i < len){
  696. strhigh = *fontCode;
  697. fontCode++;
  698. strlow = *fontCode;
  699. str = (strhigh<<8)|strlow;
  700. fontCode++;
  701. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  702. if(font_size == 0){
  703. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  704. return 0;
  705. }
  706. gtfont_draw_w(buf , x ,y , font_size,size , size,gtfont_u8g2_DrawPixel,u8g2,2);
  707. x+=size;
  708. i+=2;
  709. }
  710. return 0;
  711. }
  712. #ifdef LUAT_USE_GTFONT_UTF8
  713. extern unsigned short unicodetogb2312 ( unsigned short chr);
  714. static uint8_t utf8_state;
  715. static uint16_t encoding;
  716. static uint16_t utf8_next(uint8_t b)
  717. {
  718. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  719. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  720. if ( utf8_state == 0 )
  721. {
  722. if ( b >= 0xfc ) /* 6 byte sequence */
  723. {
  724. utf8_state = 5;
  725. b &= 1;
  726. }
  727. else if ( b >= 0xf8 )
  728. {
  729. utf8_state = 4;
  730. b &= 3;
  731. }
  732. else if ( b >= 0xf0 )
  733. {
  734. utf8_state = 3;
  735. b &= 7;
  736. }
  737. else if ( b >= 0xe0 )
  738. {
  739. utf8_state = 2;
  740. b &= 15;
  741. }
  742. else if ( b >= 0xc0 )
  743. {
  744. utf8_state = 1;
  745. b &= 0x01f;
  746. }
  747. else
  748. {
  749. /* do nothing, just use the value as encoding */
  750. return b;
  751. }
  752. encoding = b;
  753. return 0x0fffe;
  754. }
  755. else
  756. {
  757. utf8_state--;
  758. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  759. encoding<<=6;
  760. b &= 0x03f;
  761. encoding |= b;
  762. if ( utf8_state != 0 )
  763. return 0x0fffe; /* nothing to do yet */
  764. }
  765. return encoding;
  766. }
  767. /*
  768. 使用gtfont显示UTF8字符串
  769. @api u8g2.drawGtfontUtf8(str,size,x,y)
  770. @string str 显示字符串
  771. @int size 字体大小 (支持16-192号大小字体)
  772. @int x 横坐标
  773. @int y 竖坐标
  774. @usage
  775. u8g2.drawGtfontUtf8("啊啊啊",32,0,0)
  776. */
  777. static int l_u8g2_draw_gtfont_utf8(lua_State *L) {
  778. unsigned char buf[128];
  779. int len;
  780. int i = 0;
  781. uint8_t strhigh,strlow ;
  782. uint16_t e,str;
  783. const char *fontCode = luaL_checklstring(L, 1,&len);
  784. unsigned char size = luaL_checkinteger(L, 2);
  785. int x = luaL_checkinteger(L, 3);
  786. int y = luaL_checkinteger(L, 4);
  787. for(;;){
  788. e = utf8_next((uint8_t)*fontCode);
  789. if ( e == 0x0ffff )
  790. break;
  791. fontCode++;
  792. if ( e != 0x0fffe ){
  793. uint16_t str = unicodetogb2312(e);
  794. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  795. if(font_size == 0){
  796. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  797. return 0;
  798. }
  799. gtfont_draw_w(buf , x ,y , font_size,size , size,gtfont_u8g2_DrawPixel,u8g2,2);
  800. x+=size;
  801. }
  802. }
  803. return 0;
  804. }
  805. #endif // LUAT_USE_GTFONT_UTF8
  806. #endif // LUAT_USE_GTFONT
  807. #include "rotable2.h"
  808. static const rotable_Reg_t reg_u8g2[] =
  809. {
  810. { "begin", ROREG_FUNC(l_u8g2_begin)},
  811. { "init", ROREG_FUNC(l_u8g2_begin)}, // 兼容disp.init函数
  812. { "close", ROREG_FUNC(l_u8g2_close)},
  813. { "ClearBuffer", ROREG_FUNC(l_u8g2_ClearBuffer)},
  814. { "SendBuffer", ROREG_FUNC(l_u8g2_SendBuffer)},
  815. { "DrawUTF8", ROREG_FUNC(l_u8g2_DrawUTF8)},
  816. { "DrawButtonUTF8", ROREG_FUNC(l_u8g2_DrawButtonUTF8)},
  817. { "SetFontMode", ROREG_FUNC(l_u8g2_SetFontMode)},
  818. { "SetFont", ROREG_FUNC(l_u8g2_SetFont)},
  819. { "GetDisplayHeight", ROREG_FUNC(l_u8g2_GetDisplayHeight)},
  820. { "GetDisplayWidth", ROREG_FUNC(l_u8g2_GetDisplayWidth)},
  821. { "SetDrawColor", ROREG_FUNC(l_u8g2_SetDrawColor)},
  822. { "DrawPixel", ROREG_FUNC(l_u8g2_DrawPixel)},
  823. { "DrawLine", ROREG_FUNC(l_u8g2_DrawLine)},
  824. { "DrawCircle", ROREG_FUNC(l_u8g2_DrawCircle)},
  825. { "DrawDisc", ROREG_FUNC(l_u8g2_DrawDisc)},
  826. { "DrawEllipse", ROREG_FUNC(l_u8g2_DrawEllipse)},
  827. { "DrawFilledEllipse", ROREG_FUNC(l_u8g2_DrawFilledEllipse)},
  828. { "DrawBox", ROREG_FUNC(l_u8g2_DrawBox)},
  829. { "DrawFrame", ROREG_FUNC(l_u8g2_DrawFrame)},
  830. { "DrawRBox", ROREG_FUNC(l_u8g2_DrawRBox)},
  831. { "DrawRFrame", ROREG_FUNC(l_u8g2_DrawRFrame)},
  832. { "DrawGlyph", ROREG_FUNC(l_u8g2_DrawGlyph)},
  833. { "DrawTriangle", ROREG_FUNC(l_u8g2_DrawTriangle)},
  834. { "SetBitmapMode",ROREG_FUNC(l_u8g2_SetBitmapMode)},
  835. { "DrawXBM", ROREG_FUNC(l_u8g2_DrawXBM)},
  836. { "DrawDrcode", ROREG_FUNC(l_u8g2_DrawDrcode)},
  837. { "SetContrast", ROREG_FUNC(l_u8g2_SetContrast)},
  838. #ifdef LUAT_USE_GTFONT
  839. { "drawGtfontGb2312", ROREG_FUNC(l_u8g2_draw_gtfont_gb2312)},
  840. #ifdef LUAT_USE_GTFONT_UTF8
  841. { "drawGtfontUtf8", ROREG_FUNC(l_u8g2_draw_gtfont_utf8)},
  842. #endif // LUAT_USE_GTFONT_UTF8
  843. #endif // LUAT_USE_GTFONT
  844. // 默认只带8号字体
  845. { "font_opposansm8", ROREG_PTR((void*)u8g2_font_opposansm8)},
  846. #ifdef USE_U8G2_OPPOSANSM_ENGLISH
  847. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  848. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  849. { "font_opposansm10", ROREG_PTR((void*)u8g2_font_opposansm10)},
  850. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  851. { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
  852. { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
  853. { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
  854. { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
  855. { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
  856. { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
  857. #endif
  858. #ifdef USE_U8G2_OPPOSANSM8_CHINESE
  859. { "font_opposansm8_chinese", ROREG_PTR((void*)u8g2_font_opposansm8_chinese)},
  860. #endif
  861. #ifdef USE_U8G2_OPPOSANSM10_CHINESE
  862. { "font_opposansm10_chinese", ROREG_PTR((void*)u8g2_font_opposansm10_chinese)},
  863. #endif
  864. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  865. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  866. #endif
  867. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  868. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  869. #endif
  870. #ifdef USE_U8G2_OPPOSANSM18_CHINESE
  871. { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
  872. #endif
  873. #ifdef USE_U8G2_OPPOSANSM20_CHINESE
  874. { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
  875. #endif
  876. #ifdef USE_U8G2_OPPOSANSM22_CHINESE
  877. { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
  878. #endif
  879. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  880. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  881. #endif
  882. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  883. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  884. #endif
  885. #ifdef USE_U8G2_SARASA_ENGLISH
  886. { "font_sarasa_m8_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m8_ascii)},
  887. { "font_sarasa_m10_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m10_ascii)},
  888. { "font_sarasa_m12_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m12_ascii)},
  889. { "font_sarasa_m14_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m14_ascii)},
  890. { "font_sarasa_m16_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m16_ascii)},
  891. { "font_sarasa_m18_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m18_ascii)},
  892. { "font_sarasa_m20_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m20_ascii)},
  893. { "font_sarasa_m22_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m22_ascii)},
  894. //再大的很少用到先不加了
  895. #endif
  896. #ifdef USE_U8G2_SARASA_M8_CHINESE
  897. { "font_sarasa_m8_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m8_chinese)},
  898. #endif
  899. #ifdef USE_U8G2_SARASA_M10_CHINESE
  900. { "font_sarasa_m10_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m10_chinese)},
  901. #endif
  902. #ifdef USE_U8G2_SARASA_M12_CHINESE
  903. { "font_sarasa_m12_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m12_chinese)},
  904. #endif
  905. #ifdef USE_U8G2_SARASA_M14_CHINESE
  906. { "font_sarasa_m14_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m14_chinese)},
  907. #endif
  908. #ifdef USE_U8G2_SARASA_M16_CHINESE
  909. { "font_sarasa_m16_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m16_chinese)},
  910. #endif
  911. #ifdef USE_U8G2_SARASA_M18_CHINESE
  912. { "font_sarasa_m18_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m18_chinese)},
  913. #endif
  914. #ifdef USE_U8G2_SARASA_M20_CHINESE
  915. { "font_sarasa_m20_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m20_chinese)},
  916. #endif
  917. #ifdef USE_U8G2_SARASA_M22_CHINESE
  918. { "font_sarasa_m22_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m22_chinese)},
  919. #endif
  920. #ifdef USE_U8G2_SARASA_M24_CHINESE
  921. { "font_sarasa_m24_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m24_chinese)},
  922. #endif
  923. #ifdef USE_U8G2_SARASA_M26_CHINESE
  924. { "font_sarasa_m26_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m26_chinese)},
  925. #endif
  926. #ifdef USE_U8G2_SARASA_M28_CHINESE
  927. { "font_sarasa_m28_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m28_chinese)},
  928. #endif
  929. //@const DRAW_UPPER_RIGHT number 上右
  930. { "DRAW_UPPER_RIGHT", ROREG_INT(U8G2_DRAW_UPPER_RIGHT)},
  931. //@const DRAW_UPPER_LEFT number 上左
  932. { "DRAW_UPPER_LEFT", ROREG_INT(U8G2_DRAW_UPPER_LEFT)},
  933. //@const DRAW_LOWER_LEFT number 下左
  934. { "DRAW_LOWER_LEFT", ROREG_INT(U8G2_DRAW_LOWER_LEFT)},
  935. //@const DRAW_LOWER_RIGHT number 下右
  936. { "DRAW_LOWER_RIGHT", ROREG_INT(U8G2_DRAW_LOWER_RIGHT)},
  937. //@const DRAW_ALL number 全部
  938. { "DRAW_ALL", ROREG_INT(U8G2_DRAW_ALL)},
  939. //@const BTN_BW0 number 文本周围没有边框
  940. { "BTN_BW0", ROREG_INT(U8G2_BTN_BW0)},
  941. //@const BTN_BW1 number 文本周围的边框,1像素边框宽度
  942. { "BTN_BW1", ROREG_INT(U8G2_BTN_BW1)},
  943. //@const BTN_BW2 number 文本周围的边框,2像素边框宽度
  944. { "BTN_BW2", ROREG_INT(U8G2_BTN_BW2)},
  945. //@const BTN_BW3 number 文本周围的边框,3像素边框宽度
  946. { "BTN_BW3", ROREG_INT(U8G2_BTN_BW3)},
  947. //@const BTN_SHADOW0 number 启用阴影,与框架无间隙
  948. { "BTN_SHADOW0", ROREG_INT(U8G2_BTN_SHADOW0)},
  949. //@const BTN_SHADOW1 number 启用阴影,到帧的1像素间隙
  950. { "BTN_SHADOW1", ROREG_INT(U8G2_BTN_SHADOW1)},
  951. //@const BTN_SHADOW2 number 启用阴影,到帧的2像素间隙
  952. { "BTN_SHADOW2", ROREG_INT(U8G2_BTN_SHADOW2)},
  953. //@const BTN_INV number 反转文本
  954. { "BTN_INV", ROREG_INT(U8G2_BTN_INV)},
  955. //@const BTN_HCENTER number 将文本置于框架内的中心,并将参考位置更改为文本的中心
  956. { "BTN_HCENTER", ROREG_INT(U8G2_BTN_HCENTER)},
  957. //@const BTN_XFRAME number 在按钮周围绘制第二个1像素框
  958. { "BTN_XFRAME", ROREG_INT(U8G2_BTN_XFRAME)},
  959. { NULL, ROREG_INT(0)}
  960. };
  961. LUAMOD_API int luaopen_u8g2( lua_State *L ) {
  962. lua_getglobal(L, "disp"); // disp库已经加载过u8g2库, 那就直接重用
  963. if (lua_isuserdata(L, -1))
  964. return 1;
  965. luat_newlib2(L, reg_u8g2);
  966. return 1;
  967. }
  968. //-------------------------------------------------------------------------------------------------
  969. // 往下是一些U8G2方法的默认实现
  970. uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  971. uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  972. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  973. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  974. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  975. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  976. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf);
  977. typedef void (*dev_setup_cb)(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb);
  978. typedef struct luat_u8g2_dev_reg
  979. {
  980. const char* name;
  981. dev_setup_cb devcb;
  982. uint16_t w; // 屏幕宽, 最大值
  983. uint16_t h; // 屏幕高, 最大值
  984. uint16_t spi_i2c; // 使用 I2C 0, 使用 SPI 1
  985. }luat_u8g2_dev_reg_t;
  986. static const luat_u8g2_dev_reg_t devregs[] = {
  987. // ssd1306是默认值
  988. {.name="ssd1306", .w=128, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x64_noname_f}, // ssd1306 128x64,I2C
  989. {.name="ssd1306", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_ssd1306_128x64_noname_f}, // ssd1306 128x64,SPI
  990. {.name="ssd1309", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_ssd1309_128x64_noname2_f}, // ssd1309 128x64,SPI
  991. {.name="ssd1322", .w=256, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_ssd1322_nhd_256x64_f}, // ssd1322 128x64
  992. {.name="sh1106", .w=128, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_sh1106_i2c_128x64_noname_f}, // sh1106 128x64,I2C
  993. {.name="sh1106", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_sh1106_128x64_noname_f}, // sh1106 128x64,SPI
  994. {.name="sh1107", .w=64, .h=128, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x64_noname_f}, // sh1107 64x128
  995. {.name="sh1108", .w=160, .h=160, .spi_i2c=0, .devcb=u8g2_Setup_sh1108_i2c_160x160_f}, // sh1108 160x160
  996. {.name="st7567", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_st7567_jlx12864_f}, // st7567 128x64
  997. {.name="uc1701", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_uc1701_mini12864_f}, // uc1701
  998. {.name="ssd1306_128x32", .w=128, .h=32, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x32_univision_f}, // ssd1306 128x32,I2C
  999. {.name="st7565", .w=132, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_st7565_ea_dogm132_f}, // st7565 128x32,SPI
  1000. {.name="st7565_jlx12864g109pc", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_st7565_jlx12864g109pc_f}, // 2023年8月4日 晶联讯12864G-109-PC,12864G-139-P
  1001. {.name=NULL} // 结尾用,必须加.
  1002. };
  1003. static const luat_u8g2_dev_reg_t* search_dev_reg(luat_u8g2_conf_t *conf, uint16_t spi_i2c) {
  1004. size_t dev_reg_index = 0;
  1005. while (devregs[dev_reg_index].name != NULL){
  1006. if (devregs[dev_reg_index].spi_i2c == spi_i2c && strcmp(devregs[dev_reg_index].name, conf->cname) == 0) {
  1007. return &devregs[dev_reg_index];
  1008. }
  1009. dev_reg_index ++;
  1010. }
  1011. return &devregs[0];
  1012. }
  1013. #ifndef LUAT_COMPILER_NOWEAK
  1014. LUAT_WEAK int luat_u8g2_setup(luat_u8g2_conf_t *conf) {
  1015. return luat_u8g2_setup_default(conf);
  1016. }
  1017. LUAT_WEAK uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1018. return u8x8_luat_gpio_and_delay_default(u8x8, msg, arg_int, arg_ptr);
  1019. }
  1020. LUAT_WEAK uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1021. return u8x8_luat_byte_hw_i2c_default(u8x8, msg, arg_int, arg_ptr);
  1022. }
  1023. LUAT_WEAK uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1024. return u8x8_luat_byte_4wire_hw_spi_default(u8x8, msg, arg_int, arg_ptr);
  1025. }
  1026. #endif
  1027. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf) {
  1028. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  1029. const luat_u8g2_dev_reg_t* devreg = NULL;
  1030. // LLOGD("conf->pinType %d", conf->pinType);
  1031. if (pinType == 1) {
  1032. devreg = search_dev_reg(conf, 0);
  1033. if (devreg == NULL) {
  1034. LLOGD("unkown dev %s", conf->cname);
  1035. return -1;
  1036. }
  1037. devreg->devcb(u8g2, conf->direction, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay_default);
  1038. #ifdef U8G2_USE_DYNAMIC_ALLOC
  1039. buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  1040. u8g2_SetBufferPtr(u8g2, buff_ptr);
  1041. #endif
  1042. u8g2->u8x8.pins[U8X8_PIN_I2C_CLOCK] = i2c_scl;
  1043. u8g2->u8x8.pins[U8X8_PIN_I2C_DATA] = i2c_sda;
  1044. u8g2_InitDisplay(u8g2);
  1045. u8g2_SetPowerSave(u8g2, 0);
  1046. return 0;
  1047. }else if (pinType == 2) {
  1048. devreg = search_dev_reg(conf, 0);
  1049. if (devreg == NULL) {
  1050. LLOGD("unkown dev %s", conf->cname);
  1051. return -1;
  1052. }
  1053. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_hw_i2c_default, u8x8_luat_gpio_and_delay_default);
  1054. #ifdef U8G2_USE_DYNAMIC_ALLOC
  1055. buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  1056. u8g2_SetBufferPtr(u8g2, buff_ptr);
  1057. #endif
  1058. //LLOGD("setup disp i2c.hw");
  1059. u8g2_InitDisplay(u8g2);
  1060. u8g2_SetPowerSave(u8g2, 0);
  1061. return 0;
  1062. }else if (pinType == 5) {
  1063. devreg = search_dev_reg(conf, 1);
  1064. if (devreg == NULL) {
  1065. LLOGD("unkown dev %s", conf->cname);
  1066. return -1;
  1067. }
  1068. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_4wire_hw_spi_default, u8x8_luat_gpio_and_delay_default);
  1069. #ifdef U8G2_USE_DYNAMIC_ALLOC
  1070. buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  1071. u8g2_SetBufferPtr(u8g2, buff_ptr);
  1072. #endif
  1073. LLOGD("setup disp spi.hw spi_id=%d spi_dc=%d spi_cs=%d spi_res=%d",spi_id,spi_dc,spi_cs,spi_res);
  1074. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, spi_cs);
  1075. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, spi_dc);
  1076. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, spi_res);
  1077. u8g2_InitDisplay(u8g2);
  1078. u8g2_SetPowerSave(u8g2, 0);
  1079. return 0;
  1080. }
  1081. else {
  1082. LLOGI("no such u8g2 mode!!");
  1083. }
  1084. return -1;
  1085. }
  1086. LUAT_WEAK int luat_u8g2_close(luat_u8g2_conf_t *conf) {
  1087. return 0;
  1088. }
  1089. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1090. static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes */
  1091. static uint8_t buf_idx;
  1092. uint8_t *data;
  1093. switch(msg)
  1094. {
  1095. case U8X8_MSG_BYTE_SEND:
  1096. data = (uint8_t *)arg_ptr;
  1097. while( arg_int > 0 )
  1098. {
  1099. buffer[buf_idx++] = *data;
  1100. data++;
  1101. arg_int--;
  1102. }
  1103. break;
  1104. case U8X8_MSG_BYTE_INIT:
  1105. //i2c_init(u8x8); /* init i2c communication */
  1106. luat_i2c_setup(i2c_id,i2c_speed);
  1107. break;
  1108. case U8X8_MSG_BYTE_SET_DC:
  1109. /* ignored for i2c */
  1110. break;
  1111. case U8X8_MSG_BYTE_START_TRANSFER:
  1112. buf_idx = 0;
  1113. break;
  1114. case U8X8_MSG_BYTE_END_TRANSFER:
  1115. luat_i2c_send(i2c_id, u8x8_GetI2CAddress(u8x8) >> 1, buffer, buf_idx,1);
  1116. break;
  1117. default:
  1118. return 0;
  1119. }
  1120. return 1;
  1121. }
  1122. int hw_spi_begin(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin )
  1123. {
  1124. luat_spi_t u8g2_spi = {0};
  1125. u8g2_spi.id = spi_id;
  1126. switch(spi_mode)
  1127. {
  1128. case 0: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 0; break;
  1129. case 1: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 0; break;
  1130. case 2: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 1; break;
  1131. case 3: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 1; break;
  1132. }
  1133. u8g2_spi.dataw = 8;
  1134. u8g2_spi.bit_dict = 1;
  1135. u8g2_spi.master = 1;
  1136. u8g2_spi.mode = 0;
  1137. u8g2_spi.bandrate = max_hz;
  1138. u8g2_spi.cs = -1;
  1139. LLOGI("spi_mode:%d bandrate:%d cs_pin:%d",spi_mode,max_hz,cs_pin);
  1140. luat_spi_setup(&u8g2_spi);
  1141. luat_gpio_mode(spi_res,Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1142. luat_gpio_mode(spi_dc,Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1143. return 0;
  1144. }
  1145. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  1146. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1147. switch(msg)
  1148. {
  1149. case U8X8_MSG_BYTE_SEND:
  1150. luat_spi_send(spi_id, (const char*)arg_ptr, arg_int);
  1151. break;
  1152. case U8X8_MSG_BYTE_INIT:
  1153. /* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  1154. /* 0: clock active high, data out on falling edge, clock default value is zero, takover on rising edge */
  1155. /* 1: clock active high, data out on rising edge, clock default value is zero, takover on falling edge */
  1156. /* 2: clock active low, data out on rising edge */
  1157. /* 3: clock active low, data out on falling edge */
  1158. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1159. hw_spi_begin(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]);
  1160. break;
  1161. case U8X8_MSG_BYTE_SET_DC:
  1162. u8x8_gpio_SetDC(u8x8, arg_int);
  1163. break;
  1164. case U8X8_MSG_BYTE_START_TRANSFER:
  1165. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  1166. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  1167. break;
  1168. case U8X8_MSG_BYTE_END_TRANSFER:
  1169. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  1170. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1171. break;
  1172. default:
  1173. return 0;
  1174. }
  1175. return 1;
  1176. }
  1177. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1178. {
  1179. uint8_t i;
  1180. switch(msg)
  1181. {
  1182. case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
  1183. __asm__ volatile("nop");
  1184. break;
  1185. case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  1186. __asm__ volatile("nop");
  1187. break;
  1188. case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  1189. for (uint16_t n = 0; n < 320; n++)
  1190. {
  1191. __asm__ volatile("nop");
  1192. }
  1193. break;
  1194. case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  1195. luat_timer_mdelay(arg_int);
  1196. break;
  1197. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  1198. // Function which implements a delay, arg_int contains the amount of ms
  1199. // set spi pin mode
  1200. if (pinType == 1){
  1201. // set i2c pin mode
  1202. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1203. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1204. }else if (pinType == 3){
  1205. 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
  1206. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1207. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1208. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1209. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1210. }else if (pinType == 4){
  1211. 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
  1212. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1213. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1214. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1215. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1216. }else if (pinType == 5){
  1217. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1218. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1219. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1220. }else if (pinType == 6){
  1221. // set 8080 pin mode
  1222. luat_gpio_mode(u8x8->pins[U8X8_PIN_D0],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1223. luat_gpio_mode(u8x8->pins[U8X8_PIN_D1],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1224. luat_gpio_mode(u8x8->pins[U8X8_PIN_D2],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1225. luat_gpio_mode(u8x8->pins[U8X8_PIN_D3],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1226. luat_gpio_mode(u8x8->pins[U8X8_PIN_D4],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1227. luat_gpio_mode(u8x8->pins[U8X8_PIN_D5],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1228. luat_gpio_mode(u8x8->pins[U8X8_PIN_D6],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1229. luat_gpio_mode(u8x8->pins[U8X8_PIN_D7],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1230. luat_gpio_mode(u8x8->pins[U8X8_PIN_E],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1231. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1232. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1233. }
  1234. // // set menu pin mode
  1235. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_HOME],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1236. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_SELECT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1237. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_PREV],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1238. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_NEXT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1239. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_UP],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1240. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_DOWN],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1241. // // set value
  1242. // luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],Luat_GPIO_HIGH);
  1243. // luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_HIGH);
  1244. // luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_HIGH);
  1245. // luat_gpio_set(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_HIGH);
  1246. // luat_gpio_set(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_HIGH);
  1247. break;
  1248. case U8X8_MSG_DELAY_I2C:
  1249. // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  1250. // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  1251. for (uint16_t n = 0; n < (arg_int<=2?160:40); n++)
  1252. {
  1253. __asm__ volatile("nop");
  1254. }
  1255. break;
  1256. //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
  1257. //case U8X8_MSG_GPIO_SPI_CLOCK:
  1258. //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
  1259. //case U8X8_MSG_GPIO_SPI_DATA:
  1260. case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
  1261. luat_gpio_set(u8x8->pins[U8X8_PIN_D2],arg_int);
  1262. break;
  1263. case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
  1264. luat_gpio_set(u8x8->pins[U8X8_PIN_D3],arg_int);
  1265. break;
  1266. case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
  1267. luat_gpio_set(u8x8->pins[U8X8_PIN_D4],arg_int);
  1268. break;
  1269. case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
  1270. luat_gpio_set(u8x8->pins[U8X8_PIN_D5],arg_int);
  1271. break;
  1272. case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
  1273. luat_gpio_set(u8x8->pins[U8X8_PIN_D6],arg_int);
  1274. break;
  1275. case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
  1276. luat_gpio_set(u8x8->pins[U8X8_PIN_D7],arg_int);
  1277. break;
  1278. case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
  1279. luat_gpio_set(u8x8->pins[U8X8_PIN_E],arg_int);
  1280. break;
  1281. case U8X8_MSG_GPIO_I2C_CLOCK:
  1282. // arg_int=0: Output low at I2C clock pin
  1283. // arg_int=1: Input dir with pullup high for I2C clock pin
  1284. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],arg_int);
  1285. break;
  1286. case U8X8_MSG_GPIO_I2C_DATA:
  1287. // arg_int=0: Output low at I2C data pin
  1288. // arg_int=1: Input dir with pullup high for I2C data pin
  1289. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],arg_int);
  1290. break;
  1291. case U8X8_MSG_GPIO_SPI_CLOCK:
  1292. //Function to define the logic level of the clockline
  1293. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],arg_int);
  1294. break;
  1295. case U8X8_MSG_GPIO_SPI_DATA:
  1296. //Function to define the logic level of the data line to the display
  1297. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],arg_int);
  1298. break;
  1299. case U8X8_MSG_GPIO_CS:
  1300. // Function to define the logic level of the CS line
  1301. luat_gpio_set(u8x8->pins[U8X8_PIN_CS],arg_int);
  1302. break;
  1303. case U8X8_MSG_GPIO_DC:
  1304. //Function to define the logic level of the Data/ Command line
  1305. luat_gpio_set(u8x8->pins[U8X8_PIN_DC],arg_int);
  1306. break;
  1307. case U8X8_MSG_GPIO_RESET:
  1308. //Function to define the logic level of the RESET line
  1309. luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],arg_int);
  1310. break;
  1311. default:
  1312. //A message was received which is not implemented, return 0 to indicate an error
  1313. if ( msg >= U8X8_MSG_GPIO(0) )
  1314. {
  1315. i = u8x8_GetPinValue(u8x8, msg);
  1316. if ( i != U8X8_PIN_NONE )
  1317. {
  1318. if ( u8x8_GetPinIndex(u8x8, msg) < U8X8_PIN_OUTPUT_CNT )
  1319. {
  1320. luat_gpio_set(i, arg_int);
  1321. }
  1322. else
  1323. {
  1324. if ( u8x8_GetPinIndex(u8x8, msg) == U8X8_PIN_OUTPUT_CNT )
  1325. {
  1326. // call yield() for the first pin only, u8x8 will always request all the pins, so this should be ok
  1327. // yield();
  1328. }
  1329. u8x8_SetGPIOResult(u8x8, luat_gpio_get(i) == 0 ? 0 : 1);
  1330. }
  1331. }
  1332. break;
  1333. }
  1334. return 0;
  1335. }
  1336. return 1;
  1337. }