luat_lib_u8g2.c 42 KB

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