luat_lib_u8g2.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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 配置信息
  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. 设置字体模式
  264. @api u8g2.SetFontMode(mode)
  265. @int mode字体模式,启用(1)或禁用(0)透明模式
  266. @usage
  267. u8g2.SetFontMode(1)
  268. */
  269. static int l_u8g2_SetFontMode(lua_State *L){
  270. if (u8g2 == NULL) return 0;
  271. int font_mode = luaL_checkinteger(L, 1);
  272. if (font_mode < 0) {
  273. lua_pushboolean(L, 0);
  274. }
  275. u8g2_SetFontMode(u8g2, font_mode);
  276. u8g2_SetFontDirection(u8g2, 0);
  277. lua_pushboolean(L, 1);
  278. return 1;
  279. }
  280. /*
  281. 设置字体
  282. @api u8g2.SetFont(font)
  283. @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 为符号.
  284. @usage
  285. -- 设置为中文字体,对之后的drawStr有效
  286. u8g2.SetFont(u8g2.font_opposansm12)
  287. */
  288. static int l_u8g2_SetFont(lua_State *L) {
  289. if (u8g2 == NULL) {
  290. LLOGI("u8g2 not init yet!!!");
  291. lua_pushboolean(L, 0);
  292. return 1;
  293. }
  294. if (!lua_islightuserdata(L, 1)) {
  295. LLOGE("no such font");
  296. return 0;
  297. }
  298. const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
  299. if (ptr == NULL) {
  300. LLOGE("only font pointer is allow");
  301. return 0;
  302. }
  303. u8g2_SetFont(u8g2, ptr);
  304. lua_pushboolean(L, 1);
  305. return 1;
  306. }
  307. /*
  308. 获取显示屏高度
  309. @api u8g2.GetDisplayHeight()
  310. @return int 显示屏高度
  311. @usage
  312. u8g2.GetDisplayHeight()
  313. */
  314. static int l_u8g2_GetDisplayHeight(lua_State *L){
  315. if (u8g2 == NULL) return 0;
  316. lua_pushinteger(L, u8g2_GetDisplayHeight(u8g2));
  317. return 1;
  318. }
  319. /*
  320. 获取显示屏宽度
  321. @api u8g2.GetDisplayWidth()
  322. @return int 显示屏宽度
  323. @usage
  324. u8g2.GetDisplayWidth()
  325. */
  326. static int l_u8g2_GetDisplayWidth(lua_State *L){
  327. if (u8g2 == NULL) return 0;
  328. lua_pushinteger(L, u8g2_GetDisplayWidth(u8g2));
  329. return 1;
  330. }
  331. /*
  332. 为所有绘图功能分配绘图颜色。
  333. @api u8g2.SetDrawColor(c)
  334. @int c为颜色值 0没有色 1有色 2与底色xor
  335. @usage
  336. u8g2.SetDrawColor(0)
  337. */
  338. static int l_u8g2_SetDrawColor(lua_State *L){
  339. if (u8g2 == NULL) return 0;
  340. u8g2_SetDrawColor(u8g2,luaL_checkinteger(L, 1));
  341. return 0;
  342. }
  343. /*
  344. 画一个点.
  345. @api u8g2.DrawPixel(x,y)
  346. @int X位置.
  347. @int Y位置.
  348. @usage
  349. u8g2.DrawPixel(20, 5)
  350. */
  351. static int l_u8g2_DrawPixel(lua_State *L){
  352. if (u8g2 == NULL) return 0;
  353. u8g2_DrawPixel(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2));
  354. return 0;
  355. }
  356. /*
  357. 在两点之间画一条线.
  358. @api u8g2.DrawLine(x0,y0,x1,y1)
  359. @int 第一个点的X位置.
  360. @int 第一个点的Y位置.
  361. @int 第二个点的X位置.
  362. @int 第二个点的Y位置.
  363. @usage
  364. u8g2.DrawLine(20, 5, 5, 32)
  365. */
  366. static int l_u8g2_DrawLine(lua_State *L){
  367. if (u8g2 == NULL) return 0;
  368. u8g2_DrawLine(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  369. return 0;
  370. }
  371. /*
  372. 在x,y位置画一个半径为rad的空心圆.
  373. @api u8g2.DrawCircle(x0,y0,rad,opt)
  374. @int 圆心位置
  375. @int 圆心位置
  376. @int 圆半径.
  377. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  378. @usage
  379. u8g2.DrawCircle(60,30,8,u8g2.DRAW_ALL)
  380. */
  381. static int l_u8g2_DrawCircle(lua_State *L){
  382. if (u8g2 == NULL) return 0;
  383. u8g2_DrawCircle(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_optinteger(L, 4,U8G2_DRAW_ALL));
  384. return 0;
  385. }
  386. /*
  387. 在x,y位置画一个半径为rad的实心圆.
  388. @api u8g2.DrawDisc(x0,y0,rad,opt)
  389. @int 圆心位置
  390. @int 圆心位置
  391. @int 圆半径.
  392. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  393. @usage
  394. u8g2.DrawDisc(60,30,8,u8g2.DRAW_ALL)
  395. */
  396. static int l_u8g2_DrawDisc(lua_State *L){
  397. if (u8g2 == NULL) return 0;
  398. u8g2_DrawDisc(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_optinteger(L, 4,U8G2_DRAW_ALL));
  399. return 0;
  400. }
  401. /*
  402. 在x,y位置画一个半径为rad的空心椭圆.
  403. @api u8g2.DrawEllipse(x0,y0,rx,ry,opt)
  404. @int 圆心位置
  405. @int 圆心位置
  406. @int 椭圆大小
  407. @int 椭圆大小
  408. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  409. @usage
  410. u8g2.DrawEllipse(60,30,8,u8g2.DRAW_ALL)
  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_optinteger(L, 5,U8G2_DRAW_ALL));
  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 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  425. @usage
  426. u8g2.DrawFilledEllipse(60,30,8,15)
  427. */
  428. static int l_u8g2_DrawFilledEllipse(lua_State *L){
  429. if (u8g2 == NULL) return 0;
  430. 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));
  431. return 0;
  432. }
  433. /*
  434. 从x / y位置(左上边缘)开始绘制一个框(填充的框).
  435. @api u8g2.DrawBox(x,y,w,h)
  436. @int 左上边缘的X位置
  437. @int 左上边缘的Y位置
  438. @int 盒子的宽度
  439. @int 盒子的高度
  440. @usage
  441. u8g2.DrawBox(3,7,25,15)
  442. */
  443. static int l_u8g2_DrawBox(lua_State *L){
  444. if (u8g2 == NULL) return 0;
  445. u8g2_DrawBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  446. return 0;
  447. }
  448. /*
  449. 从x / y位置(左上边缘)开始绘制一个框(空框).
  450. @api u8g2.DrawFrame(x,y,w,h)
  451. @int 左上边缘的X位置
  452. @int 左上边缘的Y位置
  453. @int 盒子的宽度
  454. @int 盒子的高度
  455. @usage
  456. u8g2.DrawFrame(3,7,25,15)
  457. */
  458. static int l_u8g2_DrawFrame(lua_State *L){
  459. if (u8g2 == NULL) return 0;
  460. u8g2_DrawFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  461. return 0;
  462. }
  463. /*
  464. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的填充框/框架.
  465. @api u8g2.DrawRBox(x,y,w,h,r)
  466. @int 左上边缘的X位置
  467. @int 左上边缘的Y位置
  468. @int 盒子的宽度
  469. @int 盒子的高度
  470. @int 四个边缘的半径
  471. @usage
  472. u8g2.DrawRBox(3,7,25,15)
  473. */
  474. static int l_u8g2_DrawRBox(lua_State *L){
  475. if (u8g2 == NULL) return 0;
  476. u8g2_DrawRBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  477. return 0;
  478. }
  479. /*
  480. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的空框/框架.
  481. @api u8g2.DrawRFrame(x,y,w,h,r)
  482. @int 左上边缘的X位置
  483. @int 左上边缘的Y位置
  484. @int 盒子的宽度
  485. @int 盒子的高度
  486. @int 四个边缘的半径
  487. @usage
  488. u8g2.DrawRFrame(3,7,25,15)
  489. */
  490. static int l_u8g2_DrawRFrame(lua_State *L){
  491. if (u8g2 == NULL) return 0;
  492. u8g2_DrawRFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  493. return 0;
  494. }
  495. /*
  496. 绘制一个图形字符。字符放置在指定的像素位置x和y.
  497. @api u8g2.DrawGlyph(x,y,encoding)
  498. @int 字符在显示屏上的位置
  499. @int 字符在显示屏上的位置
  500. @int 字符的Unicode值
  501. @usage
  502. u8g2.SetFont(u8g2_font_unifont_t_symbols)
  503. u8g2.DrawGlyph(5, 20, 0x2603) -- dec 9731/hex 2603 Snowman
  504. */
  505. static int l_u8g2_DrawGlyph(lua_State *L){
  506. if (u8g2 == NULL) return 0;
  507. u8g2_DrawGlyph(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3));
  508. return 0;
  509. }
  510. /*
  511. 绘制一个三角形(实心多边形).
  512. @api u8g2.DrawTriangle(x0,y0,x1,y1,x2,y2)
  513. @int 点0X位置
  514. @int 点0Y位置
  515. @int 点1X位置
  516. @int 点1Y位置
  517. @int 点2X位置
  518. @int 点2Y位置
  519. @usage
  520. u8g2.DrawTriangle(20,5, 27,50, 5,32)
  521. */
  522. static int l_u8g2_DrawTriangle(lua_State *L){
  523. if (u8g2 == NULL) return 0;
  524. 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));
  525. return 0;
  526. }
  527. /*
  528. 定义位图函数是否将写入背景色
  529. @api u8g2.SetBitmapMode(mode)
  530. @int mode字体模式,启用(1)或禁用(0)透明模式
  531. @usage
  532. u8g2.SetBitmapMode(1)
  533. */
  534. static int l_u8g2_SetBitmapMode(lua_State *L){
  535. if (u8g2 == NULL) return 0;
  536. u8g2_SetBitmapMode(u8g2,luaL_checkinteger(L, 1));
  537. return 0;
  538. }
  539. /*
  540. 绘制位图
  541. @api u8g2.DrawXBM(x, y, w, h, data)
  542. @int X坐标
  543. @int y坐标
  544. @int 位图宽
  545. @int 位图高
  546. @int 位图数据,每一位代表一个像素
  547. @usage
  548. -- 取模使用PCtoLCD2002软件即可
  549. -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
  550. u8g2.DrawXBM(0, 0, 16,16, string.char(
  551. 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
  552. 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
  553. ))
  554. */
  555. static int l_u8g2_DrawXBM(lua_State *L){
  556. if (u8g2 == NULL) return 0;
  557. int x = luaL_checkinteger(L, 1);
  558. int y = luaL_checkinteger(L, 2);
  559. int w = luaL_checkinteger(L, 3);
  560. int h = luaL_checkinteger(L, 4);
  561. size_t len = 0;
  562. const char* data = luaL_checklstring(L, 5, &len);
  563. if (h < 1) return 0; // 行数必须大于0
  564. if (len*8/h < w) return 0; // 起码要填满一行
  565. if (len != h*w/8)return 0;
  566. u8g2_DrawXBM(u8g2, x, y, w, h, (const uint8_t*)data);
  567. lua_pushboolean(L, 1);
  568. return 1;
  569. }
  570. /**
  571. 缓冲区绘制QRCode
  572. @api u8g2.DrawDrcode(x, y, str, size)
  573. @int x坐标
  574. @int y坐标
  575. @string 二维码的内容
  576. @int 显示大小 (注意:二维码生成大小与要显示内容和纠错等级有关,生成版本为1-40(对应 21x21 - 177x177)的不定大小,如果和设置大小不同会自动在指定的区域中间显示二维码,如二维码未显示请查看日志提示)
  577. @return nil 无返回值
  578. */
  579. static int l_u8g2_DrawDrcode(lua_State *L)
  580. {
  581. size_t len;
  582. int x = luaL_checkinteger(L, 1);
  583. int y = luaL_checkinteger(L, 2);
  584. const char* text = luaL_checklstring(L, 3, &len);
  585. int size = luaL_checkinteger(L, 4);
  586. uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  587. uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  588. if (qrcode == NULL || tempBuffer == NULL) {
  589. if (qrcode)
  590. luat_heap_free(qrcode);
  591. if (tempBuffer)
  592. luat_heap_free(tempBuffer);
  593. LLOGE("qrcode out of memory");
  594. return 0;
  595. }
  596. bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_LOW,
  597. qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
  598. if (ok){
  599. int qr_size = qrcodegen_getSize(qrcode);
  600. if (size < qr_size){
  601. LLOGE("size must be greater than qr_size %d",qr_size);
  602. goto end;
  603. }
  604. int scale = size / qr_size ;
  605. if (!scale)scale = 1;
  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. }else{
  616. LLOGE("qrcodegen_encodeText false");
  617. }
  618. end:
  619. if (qrcode)
  620. luat_heap_free(qrcode);
  621. if (tempBuffer)
  622. luat_heap_free(tempBuffer);
  623. return 0;
  624. }
  625. #ifdef LUAT_USE_GTFONT
  626. #include "GT5SLCD2E_1A.h"
  627. 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);
  628. 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);
  629. static int gtfont_u8g2_DrawPixel(u8g2_t *u8g2, uint16_t x, uint16_t y,uint32_t color){
  630. u8g2_DrawPixel(u8g2,x, y);
  631. return 1;
  632. }
  633. /*
  634. 使用gtfont显示gb2312字符串
  635. @api u8g2.drawGtfontGb2312(str,size,x,y)
  636. @string str 显示字符串
  637. @int size 字体大小 (支持16-192号大小字体)
  638. @int x 横坐标
  639. @int y 竖坐标
  640. @usage
  641. u8g2.drawGtfontGb2312("啊啊啊",32,0,0)
  642. */
  643. static int l_u8g2_draw_gtfont_gb2312(lua_State *L) {
  644. unsigned char buf[128];
  645. int len;
  646. int i = 0;
  647. uint8_t strhigh,strlow ;
  648. uint16_t str;
  649. const char *fontCode = luaL_checklstring(L, 1,&len);
  650. unsigned char size = luaL_checkinteger(L, 2);
  651. int x = luaL_checkinteger(L, 3);
  652. int y = luaL_checkinteger(L, 4);
  653. while ( i < len){
  654. strhigh = *fontCode;
  655. fontCode++;
  656. strlow = *fontCode;
  657. str = (strhigh<<8)|strlow;
  658. fontCode++;
  659. get_font(buf, 1, str, size, size, size);
  660. gtfont_draw_w(buf , x ,y , size , size,gtfont_u8g2_DrawPixel,u8g2,2);
  661. x+=size;
  662. i+=2;
  663. }
  664. return 0;
  665. }
  666. #ifdef LUAT_USE_GTFONT_UTF8
  667. extern unsigned short unicodetogb2312 ( unsigned short chr);
  668. static uint8_t utf8_state;
  669. static uint16_t encoding;
  670. static uint16_t utf8_next(uint8_t b)
  671. {
  672. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  673. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  674. if ( utf8_state == 0 )
  675. {
  676. if ( b >= 0xfc ) /* 6 byte sequence */
  677. {
  678. utf8_state = 5;
  679. b &= 1;
  680. }
  681. else if ( b >= 0xf8 )
  682. {
  683. utf8_state = 4;
  684. b &= 3;
  685. }
  686. else if ( b >= 0xf0 )
  687. {
  688. utf8_state = 3;
  689. b &= 7;
  690. }
  691. else if ( b >= 0xe0 )
  692. {
  693. utf8_state = 2;
  694. b &= 15;
  695. }
  696. else if ( b >= 0xc0 )
  697. {
  698. utf8_state = 1;
  699. b &= 0x01f;
  700. }
  701. else
  702. {
  703. /* do nothing, just use the value as encoding */
  704. return b;
  705. }
  706. encoding = b;
  707. return 0x0fffe;
  708. }
  709. else
  710. {
  711. utf8_state--;
  712. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  713. encoding<<=6;
  714. b &= 0x03f;
  715. encoding |= b;
  716. if ( utf8_state != 0 )
  717. return 0x0fffe; /* nothing to do yet */
  718. }
  719. return encoding;
  720. }
  721. /*
  722. 使用gtfont显示UTF8字符串
  723. @api u8g2.drawGtfontUtf8(str,size,x,y)
  724. @string str 显示字符串
  725. @int size 字体大小 (支持16-192号大小字体)
  726. @int x 横坐标
  727. @int y 竖坐标
  728. @usage
  729. u8g2.drawGtfontUtf8("啊啊啊",32,0,0)
  730. */
  731. static int l_u8g2_draw_gtfont_utf8(lua_State *L) {
  732. unsigned char buf[128];
  733. int len;
  734. int i = 0;
  735. uint8_t strhigh,strlow ;
  736. uint16_t e,str;
  737. const char *fontCode = luaL_checklstring(L, 1,&len);
  738. unsigned char size = luaL_checkinteger(L, 2);
  739. int x = luaL_checkinteger(L, 3);
  740. int y = luaL_checkinteger(L, 4);
  741. for(;;){
  742. e = utf8_next((uint8_t)*fontCode);
  743. if ( e == 0x0ffff )
  744. break;
  745. fontCode++;
  746. if ( e != 0x0fffe ){
  747. uint16_t str = unicodetogb2312(e);
  748. get_font(buf, 1, str, size, size, size);
  749. gtfont_draw_w(buf , x ,y , size , size,gtfont_u8g2_DrawPixel,u8g2,2);
  750. x+=size;
  751. }
  752. }
  753. return 0;
  754. }
  755. #endif // LUAT_USE_GTFONT_UTF8
  756. #endif // LUAT_USE_GTFONT
  757. #include "rotable2.h"
  758. static const rotable_Reg_t reg_u8g2[] =
  759. {
  760. { "begin", ROREG_FUNC(l_u8g2_begin)},
  761. { "init", ROREG_FUNC(l_u8g2_begin)}, // 兼容disp.init函数
  762. { "close", ROREG_FUNC(l_u8g2_close)},
  763. { "ClearBuffer", ROREG_FUNC(l_u8g2_ClearBuffer)},
  764. { "SendBuffer", ROREG_FUNC(l_u8g2_SendBuffer)},
  765. { "DrawUTF8", ROREG_FUNC(l_u8g2_DrawUTF8)},
  766. { "SetFontMode", ROREG_FUNC(l_u8g2_SetFontMode)},
  767. { "SetFont", ROREG_FUNC(l_u8g2_SetFont)},
  768. { "GetDisplayHeight", ROREG_FUNC(l_u8g2_GetDisplayHeight)},
  769. { "GetDisplayWidth", ROREG_FUNC(l_u8g2_GetDisplayWidth)},
  770. { "SetDrawColor", ROREG_FUNC(l_u8g2_SetDrawColor)},
  771. { "DrawPixel", ROREG_FUNC(l_u8g2_DrawPixel)},
  772. { "DrawLine", ROREG_FUNC(l_u8g2_DrawLine)},
  773. { "DrawCircle", ROREG_FUNC(l_u8g2_DrawCircle)},
  774. { "DrawDisc", ROREG_FUNC(l_u8g2_DrawDisc)},
  775. { "DrawEllipse", ROREG_FUNC(l_u8g2_DrawEllipse)},
  776. { "DrawFilledEllipse", ROREG_FUNC(l_u8g2_DrawFilledEllipse)},
  777. { "DrawBox", ROREG_FUNC(l_u8g2_DrawBox)},
  778. { "DrawFrame", ROREG_FUNC(l_u8g2_DrawFrame)},
  779. { "DrawRBox", ROREG_FUNC(l_u8g2_DrawRBox)},
  780. { "DrawRFrame", ROREG_FUNC(l_u8g2_DrawRFrame)},
  781. { "DrawGlyph", ROREG_FUNC(l_u8g2_DrawGlyph)},
  782. { "DrawTriangle", ROREG_FUNC(l_u8g2_DrawTriangle)},
  783. { "SetBitmapMode",ROREG_FUNC(l_u8g2_SetBitmapMode)},
  784. { "DrawXBM", ROREG_FUNC(l_u8g2_DrawXBM)},
  785. { "DrawDrcode", ROREG_FUNC(l_u8g2_DrawDrcode)},
  786. #ifdef LUAT_USE_GTFONT
  787. { "drawGtfontGb2312", ROREG_FUNC(l_u8g2_draw_gtfont_gb2312)},
  788. #ifdef LUAT_USE_GTFONT_UTF8
  789. { "drawGtfontUtf8", ROREG_FUNC(l_u8g2_draw_gtfont_utf8)},
  790. #endif // LUAT_USE_GTFONT_UTF8
  791. #endif // LUAT_USE_GTFONT
  792. // 默认只带8号字体
  793. { "font_opposansm8", ROREG_PTR((void*)u8g2_font_opposansm8)},
  794. #ifdef USE_U8G2_OPPOSANSM_ENGLISH
  795. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  796. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  797. { "font_opposansm10", ROREG_PTR((void*)u8g2_font_opposansm10)},
  798. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  799. { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
  800. { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
  801. { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
  802. { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
  803. { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
  804. { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
  805. #endif
  806. #ifdef USE_U8G2_OPPOSANSM8_CHINESE
  807. { "font_opposansm8_chinese", ROREG_PTR((void*)u8g2_font_opposansm8_chinese)},
  808. #endif
  809. #ifdef USE_U8G2_OPPOSANSM10_CHINESE
  810. { "font_opposansm10_chinese", ROREG_PTR((void*)u8g2_font_opposansm10_chinese)},
  811. #endif
  812. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  813. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  814. #endif
  815. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  816. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  817. #endif
  818. #ifdef USE_U8G2_OPPOSANSM18_CHINESE
  819. { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
  820. #endif
  821. #ifdef USE_U8G2_OPPOSANSM20_CHINESE
  822. { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
  823. #endif
  824. #ifdef USE_U8G2_OPPOSANSM22_CHINESE
  825. { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
  826. #endif
  827. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  828. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  829. #endif
  830. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  831. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  832. #endif
  833. //@const DRAW_UPPER_RIGHT number 上右
  834. { "DRAW_UPPER_RIGHT", ROREG_INT(U8G2_DRAW_UPPER_RIGHT)},
  835. //@const DRAW_UPPER_LEFT number 上左
  836. { "DRAW_UPPER_LEFT", ROREG_INT(U8G2_DRAW_UPPER_LEFT)},
  837. //@const DRAW_LOWER_LEFT number 下左
  838. { "DRAW_LOWER_LEFT", ROREG_INT(U8G2_DRAW_LOWER_LEFT)},
  839. //@const DRAW_LOWER_RIGHT number 下右
  840. { "DRAW_LOWER_RIGHT", ROREG_INT(U8G2_DRAW_LOWER_RIGHT)},
  841. //@const DRAW_ALL number 全部
  842. { "DRAW_ALL", ROREG_INT(U8G2_DRAW_ALL)},
  843. { NULL, ROREG_INT(0)}
  844. };
  845. LUAMOD_API int luaopen_u8g2( lua_State *L ) {
  846. lua_getglobal(L, "disp"); // disp库已经加载过u8g2库, 那就直接重用
  847. if (lua_isuserdata(L, -1))
  848. return 1;
  849. luat_newlib2(L, reg_u8g2);
  850. return 1;
  851. }
  852. //-------------------------------------------------------------------------------------------------
  853. // 往下是一些U8G2方法的默认实现
  854. uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  855. uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  856. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  857. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  858. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  859. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  860. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf);
  861. 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);
  862. typedef struct luat_u8g2_dev_reg
  863. {
  864. const char* name;
  865. dev_setup_cb devcb;
  866. uint16_t w; // 屏幕宽, 最大值
  867. uint16_t h; // 屏幕高, 最大值
  868. uint16_t spi_i2c; // 使用 I2C 0, 使用 SPI 1
  869. }luat_u8g2_dev_reg_t;
  870. static const luat_u8g2_dev_reg_t devregs[] = {
  871. // ssd1306是默认值
  872. {.name="ssd1306", .w=128, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x64_noname_f}, // ssd1306 128x64,I2C
  873. {.name="ssd1306", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_ssd1306_128x64_noname_f}, // ssd1306 128x64,SPI
  874. {.name="ssd1309", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_ssd1309_128x64_noname2_f}, // ssd1309 128x64,SPI
  875. {.name="ssd1322", .w=256, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_ssd1322_nhd_256x64_f}, // ssd1322 128x64
  876. {.name="sh1106", .w=128, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_sh1106_i2c_128x64_noname_f}, // sh1106 128x64,I2C
  877. {.name="sh1106", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_sh1106_128x64_noname_f}, // sh1106 128x64,SPI
  878. {.name="sh1107", .w=64, .h=128, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x64_noname_f}, // sh1107 64x128
  879. {.name="sh1108", .w=160, .h=160, .spi_i2c=0, .devcb=u8g2_Setup_sh1108_i2c_160x160_f}, // sh1108 160x160
  880. {.name="st7567", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_st7567_jlx12864_f}, // st7567 128x64
  881. {.name="uc1701", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_uc1701_mini12864_f}, // uc1701
  882. {.name="ssd1306_128x32", .w=128, .h=32, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x32_univision_f}, // ssd1306 128x32,I2C
  883. {.name=NULL} // 结尾用,必须加.
  884. };
  885. static const luat_u8g2_dev_reg_t* search_dev_reg(luat_u8g2_conf_t *conf, uint16_t spi_i2c) {
  886. size_t dev_reg_index = 0;
  887. while (devregs[dev_reg_index].name != NULL){
  888. if (devregs[dev_reg_index].spi_i2c == spi_i2c && strcmp(devregs[dev_reg_index].name, conf->cname) == 0) {
  889. return &devregs[dev_reg_index];
  890. }
  891. dev_reg_index ++;
  892. }
  893. return &devregs[0];
  894. }
  895. #ifndef LUAT_COMPILER_NOWEAK
  896. LUAT_WEAK int luat_u8g2_setup(luat_u8g2_conf_t *conf) {
  897. return luat_u8g2_setup_default(conf);
  898. }
  899. LUAT_WEAK uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  900. return u8x8_luat_gpio_and_delay_default(u8x8, msg, arg_int, arg_ptr);
  901. }
  902. LUAT_WEAK uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  903. return u8x8_luat_byte_hw_i2c_default(u8x8, msg, arg_int, arg_ptr);
  904. }
  905. LUAT_WEAK uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  906. return u8x8_luat_byte_4wire_hw_spi_default(u8x8, msg, arg_int, arg_ptr);
  907. }
  908. #endif
  909. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf) {
  910. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  911. const luat_u8g2_dev_reg_t* devreg = NULL;
  912. // LLOGD("conf->pinType %d", conf->pinType);
  913. if (pinType == 1) {
  914. devreg = search_dev_reg(conf, 0);
  915. if (devreg == NULL) {
  916. LLOGD("unkown dev %s", conf->cname);
  917. return -1;
  918. }
  919. devreg->devcb(u8g2, conf->direction, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay_default);
  920. #ifdef U8G2_USE_DYNAMIC_ALLOC
  921. buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  922. u8g2_SetBufferPtr(u8g2, buff_ptr);
  923. #endif
  924. u8g2->u8x8.pins[U8X8_PIN_I2C_CLOCK] = i2c_scl;
  925. u8g2->u8x8.pins[U8X8_PIN_I2C_DATA] = i2c_sda;
  926. u8g2_InitDisplay(u8g2);
  927. u8g2_SetPowerSave(u8g2, 0);
  928. return 0;
  929. }else if (pinType == 2) {
  930. devreg = search_dev_reg(conf, 0);
  931. if (devreg == NULL) {
  932. LLOGD("unkown dev %s", conf->cname);
  933. return -1;
  934. }
  935. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_hw_i2c_default, u8x8_luat_gpio_and_delay_default);
  936. #ifdef U8G2_USE_DYNAMIC_ALLOC
  937. buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  938. u8g2_SetBufferPtr(u8g2, buff_ptr);
  939. #endif
  940. //LLOGD("setup disp i2c.hw");
  941. u8g2_InitDisplay(u8g2);
  942. u8g2_SetPowerSave(u8g2, 0);
  943. return 0;
  944. }else if (pinType == 5) {
  945. devreg = search_dev_reg(conf, 1);
  946. if (devreg == NULL) {
  947. LLOGD("unkown dev %s", conf->cname);
  948. return -1;
  949. }
  950. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_4wire_hw_spi_default, u8x8_luat_gpio_and_delay_default);
  951. #ifdef U8G2_USE_DYNAMIC_ALLOC
  952. buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  953. u8g2_SetBufferPtr(u8g2, buff_ptr);
  954. #endif
  955. 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);
  956. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, spi_cs);
  957. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, spi_dc);
  958. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, spi_res);
  959. u8g2_InitDisplay(u8g2);
  960. u8g2_SetPowerSave(u8g2, 0);
  961. return 0;
  962. }
  963. else {
  964. LLOGI("no such u8g2 mode!!");
  965. }
  966. return -1;
  967. }
  968. LUAT_WEAK int luat_u8g2_close(luat_u8g2_conf_t *conf) {
  969. return 0;
  970. }
  971. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  972. static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes */
  973. static uint8_t buf_idx;
  974. uint8_t *data;
  975. switch(msg)
  976. {
  977. case U8X8_MSG_BYTE_SEND:
  978. data = (uint8_t *)arg_ptr;
  979. while( arg_int > 0 )
  980. {
  981. buffer[buf_idx++] = *data;
  982. data++;
  983. arg_int--;
  984. }
  985. break;
  986. case U8X8_MSG_BYTE_INIT:
  987. //i2c_init(u8x8); /* init i2c communication */
  988. luat_i2c_setup(i2c_id,i2c_speed);
  989. break;
  990. case U8X8_MSG_BYTE_SET_DC:
  991. /* ignored for i2c */
  992. break;
  993. case U8X8_MSG_BYTE_START_TRANSFER:
  994. buf_idx = 0;
  995. break;
  996. case U8X8_MSG_BYTE_END_TRANSFER:
  997. luat_i2c_send(i2c_id, u8x8_GetI2CAddress(u8x8) >> 1, buffer, buf_idx,1);
  998. break;
  999. default:
  1000. return 0;
  1001. }
  1002. return 1;
  1003. }
  1004. int hw_spi_begin(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin )
  1005. {
  1006. luat_spi_t u8g2_spi = {0};
  1007. u8g2_spi.id = spi_id;
  1008. switch(spi_mode)
  1009. {
  1010. case 0: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 0; break;
  1011. case 1: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 0; break;
  1012. case 2: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 1; break;
  1013. case 3: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 1; break;
  1014. }
  1015. u8g2_spi.dataw = 8;
  1016. u8g2_spi.bit_dict = 1;
  1017. u8g2_spi.master = 1;
  1018. u8g2_spi.mode = 1;
  1019. u8g2_spi.bandrate = max_hz;
  1020. u8g2_spi.cs = -1;
  1021. LLOGI("spi_mode:%d bandrate:%d cs_pin:%d",spi_mode,max_hz,cs_pin);
  1022. luat_spi_setup(&u8g2_spi);
  1023. luat_gpio_mode(spi_res,Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1024. luat_gpio_mode(spi_dc,Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1025. return 0;
  1026. }
  1027. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  1028. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1029. switch(msg)
  1030. {
  1031. case U8X8_MSG_BYTE_SEND:
  1032. luat_spi_send(spi_id, (const char*)arg_ptr, arg_int);
  1033. break;
  1034. case U8X8_MSG_BYTE_INIT:
  1035. /* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  1036. /* 0: clock active high, data out on falling edge, clock default value is zero, takover on rising edge */
  1037. /* 1: clock active high, data out on rising edge, clock default value is zero, takover on falling edge */
  1038. /* 2: clock active low, data out on rising edge */
  1039. /* 3: clock active low, data out on falling edge */
  1040. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1041. hw_spi_begin(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]);
  1042. break;
  1043. case U8X8_MSG_BYTE_SET_DC:
  1044. u8x8_gpio_SetDC(u8x8, arg_int);
  1045. break;
  1046. case U8X8_MSG_BYTE_START_TRANSFER:
  1047. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  1048. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  1049. break;
  1050. case U8X8_MSG_BYTE_END_TRANSFER:
  1051. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  1052. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1053. break;
  1054. default:
  1055. return 0;
  1056. }
  1057. return 1;
  1058. }
  1059. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1060. {
  1061. uint8_t i;
  1062. switch(msg)
  1063. {
  1064. case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
  1065. __asm__ volatile("nop");
  1066. break;
  1067. case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  1068. __asm__ volatile("nop");
  1069. break;
  1070. case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  1071. for (uint16_t n = 0; n < 320; n++)
  1072. {
  1073. __asm__ volatile("nop");
  1074. }
  1075. break;
  1076. case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  1077. luat_timer_mdelay(arg_int);
  1078. break;
  1079. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  1080. // Function which implements a delay, arg_int contains the amount of ms
  1081. // set spi pin mode
  1082. if (pinType == 1){
  1083. // set i2c pin mode
  1084. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1085. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1086. }else if (pinType == 3){
  1087. 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
  1088. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1089. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1090. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1091. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1092. }else if (pinType == 4){
  1093. 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
  1094. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1095. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1096. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1097. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1098. }else if (pinType == 5){
  1099. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1100. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1101. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1102. }else if (pinType == 6){
  1103. // set 8080 pin mode
  1104. luat_gpio_mode(u8x8->pins[U8X8_PIN_D0],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1105. luat_gpio_mode(u8x8->pins[U8X8_PIN_D1],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1106. luat_gpio_mode(u8x8->pins[U8X8_PIN_D2],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1107. luat_gpio_mode(u8x8->pins[U8X8_PIN_D3],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1108. luat_gpio_mode(u8x8->pins[U8X8_PIN_D4],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1109. luat_gpio_mode(u8x8->pins[U8X8_PIN_D5],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1110. luat_gpio_mode(u8x8->pins[U8X8_PIN_D6],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1111. luat_gpio_mode(u8x8->pins[U8X8_PIN_D7],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1112. luat_gpio_mode(u8x8->pins[U8X8_PIN_E],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1113. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1114. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1115. }
  1116. // // set menu pin mode
  1117. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_HOME],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1118. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_SELECT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1119. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_PREV],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1120. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_NEXT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1121. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_UP],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1122. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_DOWN],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1123. // // set value
  1124. // luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],Luat_GPIO_HIGH);
  1125. // luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_HIGH);
  1126. // luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_HIGH);
  1127. // luat_gpio_set(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_HIGH);
  1128. // luat_gpio_set(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_HIGH);
  1129. break;
  1130. case U8X8_MSG_DELAY_I2C:
  1131. // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  1132. // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  1133. for (uint16_t n = 0; n < (arg_int<=2?160:40); n++)
  1134. {
  1135. __asm__ volatile("nop");
  1136. }
  1137. break;
  1138. //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
  1139. //case U8X8_MSG_GPIO_SPI_CLOCK:
  1140. //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
  1141. //case U8X8_MSG_GPIO_SPI_DATA:
  1142. case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
  1143. luat_gpio_set(u8x8->pins[U8X8_PIN_D2],arg_int);
  1144. break;
  1145. case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
  1146. luat_gpio_set(u8x8->pins[U8X8_PIN_D3],arg_int);
  1147. break;
  1148. case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
  1149. luat_gpio_set(u8x8->pins[U8X8_PIN_D4],arg_int);
  1150. break;
  1151. case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
  1152. luat_gpio_set(u8x8->pins[U8X8_PIN_D5],arg_int);
  1153. break;
  1154. case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
  1155. luat_gpio_set(u8x8->pins[U8X8_PIN_D6],arg_int);
  1156. break;
  1157. case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
  1158. luat_gpio_set(u8x8->pins[U8X8_PIN_D7],arg_int);
  1159. break;
  1160. case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
  1161. luat_gpio_set(u8x8->pins[U8X8_PIN_E],arg_int);
  1162. break;
  1163. case U8X8_MSG_GPIO_I2C_CLOCK:
  1164. // arg_int=0: Output low at I2C clock pin
  1165. // arg_int=1: Input dir with pullup high for I2C clock pin
  1166. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],arg_int);
  1167. break;
  1168. case U8X8_MSG_GPIO_I2C_DATA:
  1169. // arg_int=0: Output low at I2C data pin
  1170. // arg_int=1: Input dir with pullup high for I2C data pin
  1171. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],arg_int);
  1172. break;
  1173. case U8X8_MSG_GPIO_SPI_CLOCK:
  1174. //Function to define the logic level of the clockline
  1175. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],arg_int);
  1176. break;
  1177. case U8X8_MSG_GPIO_SPI_DATA:
  1178. //Function to define the logic level of the data line to the display
  1179. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],arg_int);
  1180. break;
  1181. case U8X8_MSG_GPIO_CS:
  1182. // Function to define the logic level of the CS line
  1183. luat_gpio_set(u8x8->pins[U8X8_PIN_CS],arg_int);
  1184. break;
  1185. case U8X8_MSG_GPIO_DC:
  1186. //Function to define the logic level of the Data/ Command line
  1187. luat_gpio_set(u8x8->pins[U8X8_PIN_DC],arg_int);
  1188. break;
  1189. case U8X8_MSG_GPIO_RESET:
  1190. //Function to define the logic level of the RESET line
  1191. luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],arg_int);
  1192. break;
  1193. default:
  1194. //A message was received which is not implemented, return 0 to indicate an error
  1195. if ( msg >= U8X8_MSG_GPIO(0) )
  1196. {
  1197. i = u8x8_GetPinValue(u8x8, msg);
  1198. if ( i != U8X8_PIN_NONE )
  1199. {
  1200. if ( u8x8_GetPinIndex(u8x8, msg) < U8X8_PIN_OUTPUT_CNT )
  1201. {
  1202. luat_gpio_set(i, arg_int);
  1203. }
  1204. else
  1205. {
  1206. if ( u8x8_GetPinIndex(u8x8, msg) == U8X8_PIN_OUTPUT_CNT )
  1207. {
  1208. // call yield() for the first pin only, u8x8 will always request all the pins, so this should be ok
  1209. // yield();
  1210. }
  1211. u8x8_SetGPIOResult(u8x8, luat_gpio_get(i) == 0 ? 0 : 1);
  1212. }
  1213. }
  1214. break;
  1215. }
  1216. return 0;
  1217. }
  1218. return 1;
  1219. }