luat_lib_u8g2.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /*
  2. @module u8g2
  3. @summary u8g2图形处理库
  4. @author Dozingfiretruck
  5. @version 1.0
  6. @date 2021.01.25
  7. */
  8. #include "luat_base.h"
  9. #include "luat_malloc.h"
  10. #include "luat_u8g2.h"
  11. #include "luat_gpio.h"
  12. #include "luat_timer.h"
  13. #include "luat_i2c.h"
  14. #include "luat_spi.h"
  15. #include "u8g2.h"
  16. #define LUAT_LOG_TAG "luat.u8g2"
  17. #include "luat_log.h"
  18. #define font_ncenB08_tr 0
  19. #define font_wqy12_t_gb2312 1
  20. #define font_unifont_t_symbols 2
  21. #define font_open_iconic_weather_6x_t 3
  22. static u8g2_t* u8g2;
  23. static int u8g2_lua_ref;
  24. static uint8_t i2c_id;
  25. static uint8_t i2c_speed;
  26. static uint8_t i2c_addr = 0x3C;
  27. static uint8_t spi_id;
  28. static uint8_t OLED_SPI_PIN_RES;
  29. static uint8_t OLED_SPI_PIN_DC;
  30. static uint8_t OLED_SPI_PIN_CS;
  31. /*
  32. u8g2显示屏初始化
  33. @api u8g2.begin("ssd1306")
  34. @string 配置信息
  35. @return int 正常初始化1,已经初始化过2,内存不够3,初始化失败返回4
  36. @usage
  37. -- 初始化i2c1的ssd1306
  38. u8g2.begin("ssd1306")
  39. */
  40. static int l_u8g2_begin(lua_State *L) {
  41. if (u8g2 != NULL) {
  42. LLOGW("disp is aready inited");
  43. lua_pushinteger(L, 2);
  44. return 1;
  45. }
  46. u8g2 = (u8g2_t*)lua_newuserdata(L, sizeof(u8g2_t));
  47. if (u8g2 == NULL) {
  48. LLOGE("lua_newuserdata return NULL, out of memory ?!");
  49. lua_pushinteger(L, 3);
  50. return 1;
  51. }
  52. // TODO: 暂时只支持SSD1306 12864, I2C接口-> i2c1soft, 软件模拟
  53. luat_u8g2_conf_t conf = {0};
  54. conf.pinType = 2; // I2C 硬件(或者是个假硬件)
  55. conf.ptr = u8g2;
  56. if (lua_istable(L, 1)) {
  57. // 参数解析
  58. lua_pushliteral(L, "mode");
  59. lua_gettable(L, 1);
  60. if (lua_isstring(L, -1)) {
  61. const char* mode = luaL_checkstring(L, -1);
  62. LLOGD("mode = [%s]", mode);
  63. if (strcmp("i2c_sw", mode) == 0) {
  64. LLOGD("using i2c_sw");
  65. conf.pinType = 1;
  66. }
  67. else if (strcmp("i2c_hw", mode) == 0) {
  68. LLOGD("using i2c_hw");
  69. conf.pinType = 2;
  70. }
  71. else if (strcmp("spi_sw_3pin", mode) == 0) {
  72. LLOGD("using spi_sw_3pin");
  73. conf.pinType = 3;
  74. }
  75. else if (strcmp("spi_sw_4pin", mode) == 0) {
  76. LLOGD("using spi_sw_4pin");
  77. conf.pinType = 4;
  78. }
  79. else if (strcmp("spi_hw_4pin", mode) == 0) {
  80. LLOGD("using spi_hw_4pin");
  81. conf.pinType = 5;
  82. }
  83. }
  84. lua_pop(L, 1);
  85. // 解析pin0 ~ pin7
  86. lua_pushliteral(L, "pin0");
  87. lua_gettable(L, 1);
  88. if (lua_isinteger(L, -1)) {
  89. conf.pin0 = luaL_checkinteger(L, -1);
  90. }
  91. lua_pop(L, 1);
  92. lua_pushliteral(L, "pin1");
  93. lua_gettable(L, 1);
  94. if (lua_isinteger(L, -1)) {
  95. conf.pin1 = luaL_checkinteger(L, -1);
  96. }
  97. lua_pop(L, 1);
  98. lua_pushliteral(L, "pin2");
  99. lua_gettable(L, 1);
  100. if (lua_isinteger(L, -1)) {
  101. conf.pin2 = luaL_checkinteger(L, -1);
  102. }
  103. lua_pop(L, 1);
  104. lua_pushliteral(L, "pin3");
  105. lua_gettable(L, 1);
  106. if (lua_isinteger(L, -1)) {
  107. conf.pin3 = luaL_checkinteger(L, -1);
  108. }
  109. lua_pop(L, 1);
  110. lua_pushliteral(L, "i2c_id");
  111. lua_gettable(L, 1);
  112. if (lua_isinteger(L, -1)) {
  113. i2c_id = luaL_checkinteger(L, -1);
  114. }
  115. lua_pop(L, 1);
  116. lua_pushliteral(L, "i2c_speed");
  117. lua_gettable(L, 1);
  118. if (lua_isinteger(L, -1)) {
  119. i2c_speed = luaL_checkinteger(L, -1);
  120. }
  121. lua_pop(L, 1);
  122. lua_pushliteral(L, "i2c_addr");
  123. lua_gettable(L, 1);
  124. if (lua_isinteger(L, -1)) {
  125. i2c_addr = luaL_checkinteger(L, -1);
  126. }
  127. lua_pop(L, 1);
  128. lua_pushliteral(L, "spi_id");
  129. lua_gettable(L, 1);
  130. if (lua_isinteger(L, -1)) {
  131. spi_id = luaL_checkinteger(L, -1);
  132. LLOGD("spi_id=%d", spi_id);
  133. }
  134. lua_pop(L, 1);
  135. lua_pushliteral(L, "OLED_SPI_PIN_RES");
  136. lua_gettable(L, 1);
  137. if (lua_isinteger(L, -1)) {
  138. OLED_SPI_PIN_RES = luaL_checkinteger(L, -1);
  139. LLOGD("OLED_SPI_PIN_RES=%d", OLED_SPI_PIN_RES);
  140. }
  141. lua_pop(L, 1);
  142. lua_pushliteral(L, "OLED_SPI_PIN_DC");
  143. lua_gettable(L, 1);
  144. if (lua_isinteger(L, -1)) {
  145. OLED_SPI_PIN_DC = luaL_checkinteger(L, -1);
  146. LLOGD("OLED_SPI_PIN_DC=%d", OLED_SPI_PIN_DC);
  147. }
  148. lua_pop(L, 1);
  149. lua_pushliteral(L, "OLED_SPI_PIN_CS");
  150. lua_gettable(L, 1);
  151. if (lua_isinteger(L, -1)) {
  152. OLED_SPI_PIN_CS = luaL_checkinteger(L, -1);
  153. LLOGD("OLED_SPI_PIN_CS=%d", OLED_SPI_PIN_CS);
  154. }
  155. lua_pop(L, 1);
  156. // lua_pushliteral(L, "spi_id");
  157. // lua_gettable(L, 1);
  158. // if (lua_isinteger(L, -1)) {
  159. // spi_id = luaL_checkinteger(L, -1);
  160. // }
  161. // lua_pop(L, 1);
  162. // pin4 ~ pin7暂时用不到,先不设置了
  163. }
  164. LLOGD("pinType=%d", conf.pinType);
  165. if (luat_u8g2_setup(&conf)) {
  166. u8g2 = NULL;
  167. LLOGW("disp init fail");
  168. lua_pushinteger(L, 4);
  169. return 1; // 初始化失败
  170. }
  171. u8g2_lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  172. u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr); // 设置默认字体
  173. lua_pushinteger(L, 1);
  174. return 1;
  175. }
  176. /*
  177. 关闭显示屏
  178. @api u8g2.close()
  179. @usage
  180. -- 关闭disp,再次使用disp相关API的话,需要重新初始化
  181. u8g2.close()
  182. */
  183. static int l_u8g2_close(lua_State *L) {
  184. if (u8g2_lua_ref != 0) {
  185. lua_geti(L, LUA_REGISTRYINDEX, u8g2_lua_ref);
  186. if (lua_isuserdata(L, -1)) {
  187. luaL_unref(L, LUA_REGISTRYINDEX, u8g2_lua_ref);
  188. }
  189. u8g2_lua_ref = 0;
  190. }
  191. lua_gc(L, LUA_GCCOLLECT, 0);
  192. u8g2 = NULL;
  193. return 0;
  194. }
  195. /*
  196. 清屏,清除内存帧缓冲区中的所有像素
  197. @api u8g2.ClearBuffer()
  198. @usage
  199. -- 清屏
  200. u8g2.ClearBuffer()
  201. */
  202. static int l_u8g2_ClearBuffer(lua_State *L) {
  203. if (u8g2 == NULL) return 0;
  204. u8g2_ClearBuffer(u8g2);
  205. return 0;
  206. }
  207. /*
  208. 将数据更新到屏幕,将存储器帧缓冲区的内容发送到显示器
  209. @api u8g2.SendBuffer()
  210. @usage
  211. -- 把显示数据更新到屏幕
  212. u8g2.SendBuffer()
  213. */
  214. static int l_u8g2_SendBuffer(lua_State *L) {
  215. if (u8g2 == NULL) return 0;
  216. u8g2_SendBuffer(u8g2);
  217. return 0;
  218. }
  219. /*
  220. 在显示屏上画一段文字,在显示屏上画一段文字,要调用u8g2.SendBuffer()才会更新到屏幕
  221. @api u8g2.DrawUTF8(str, x, y)
  222. @string 文件内容
  223. @int 横坐标
  224. @int 竖坐标
  225. @usage
  226. u8g2.DrawUTF8("wifi is ready", 10, 20)
  227. */
  228. static int l_u8g2_DrawUTF8(lua_State *L) {
  229. if (u8g2 == NULL) {
  230. LLOGW("disp not init yet!!!");
  231. return 0;
  232. }
  233. size_t len;
  234. size_t x, y;
  235. const char* str = luaL_checklstring(L, 1, &len);
  236. x = luaL_checkinteger(L, 2);
  237. y = luaL_checkinteger(L, 3);
  238. u8g2_DrawUTF8(u8g2, x, y, str);
  239. return 0;
  240. }
  241. /*
  242. 设置字体模式
  243. @api u8g2.SetFontMode(mode)
  244. @int mode字体模式,启用(1)或禁用(0)透明模式
  245. @usage
  246. u8g2.SetFontMode(1)
  247. */
  248. static int l_u8g2_SetFontMode(lua_State *L){
  249. if (u8g2 == NULL) return 0;
  250. int font_mode = luaL_checkinteger(L, 1);
  251. if (font_mode < 0) {
  252. lua_pushboolean(L, 0);
  253. }
  254. u8g2_SetFontMode(u8g2, font_mode);
  255. lua_pushboolean(L, 1);
  256. return 1;
  257. }
  258. /*
  259. 设置字体
  260. @api u8g2.SetFont(font)
  261. @string font, "u8g2_font_ncenB08_tr"为纯英文8x8字节,"u8g2_font_wqy12_t_gb2312"为12x12全中文,"u8g2_font_unifont_t_symbols"为符号.
  262. @usage
  263. -- 设置为中文字体,对之后的drawStr有效,使用中文字体需在luat_base.h开启#define USE_U8G2_WQY12_T_GB2312
  264. u8g2.setFont("u8g2_font_wqy12_t_gb2312")
  265. */
  266. static int l_u8g2_SetFont(lua_State *L) {
  267. if (u8g2 == NULL) {
  268. LLOGI("disp not init yet!!!");
  269. lua_pushboolean(L, 0);
  270. return 1;
  271. }
  272. int font = luaL_checkinteger(L, 1);
  273. switch (font)
  274. {
  275. case font_ncenB08_tr:
  276. LLOGI("font_ncenB08_tr");
  277. u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr);
  278. lua_pushboolean(L, 1);
  279. break;
  280. #if defined USE_U8G2_WQY12_T_GB2312
  281. case font_wqy12_t_gb2312:
  282. LLOGI("font_wqy12_t_gb2312");
  283. u8g2_SetFont(u8g2, u8g2_font_wqy12_t_gb2312);
  284. lua_pushboolean(L, 1);
  285. break;
  286. #endif
  287. #if defined USE_U8G2_UNIFONT_SYMBOLS
  288. case font_unifont_t_symbols:
  289. LLOGI("font_wqy12_t_gb2312");
  290. u8g2_SetFont(u8g2, u8g2_font_unifont_t_symbols);
  291. lua_pushboolean(L, 1);
  292. break;
  293. #endif
  294. #if defined USE_U8G2_ICONIC_WEATHER_6X
  295. case font_open_iconic_weather_6x_t:
  296. LLOGI("font_wqy12_t_gb2312");
  297. u8g2_SetFont(u8g2, u8g2_font_open_iconic_weather_6x_t);
  298. lua_pushboolean(L, 1);
  299. break;
  300. #endif
  301. default:
  302. lua_pushboolean(L, 0);
  303. LLOGI("default");
  304. break;
  305. }
  306. return 0;
  307. }
  308. /*
  309. 获取显示屏高度
  310. @api u8g2.GetDisplayHeight()
  311. @return int 显示屏高度
  312. @usage
  313. u8g2.GetDisplayHeight()
  314. */
  315. static int l_u8g2_GetDisplayHeight(lua_State *L){
  316. if (u8g2 == NULL) return 0;
  317. lua_pushinteger(L, u8g2_GetDisplayHeight(u8g2));
  318. return 1;
  319. }
  320. /*
  321. 获取显示屏宽度
  322. @api u8g2.GetDisplayWidth()
  323. @return int 显示屏宽度
  324. @usage
  325. u8g2.GetDisplayWidth()
  326. */
  327. static int l_u8g2_GetDisplayWidth(lua_State *L){
  328. if (u8g2 == NULL) return 0;
  329. lua_pushinteger(L, u8g2_GetDisplayWidth(u8g2));
  330. return 1;
  331. }
  332. /*
  333. 在两点之间画一条线.
  334. @api u8g2.DrawLine(x0,y0,x1,y1)
  335. @int 第一个点的X位置.
  336. @int 第一个点的Y位置.
  337. @int 第二个点的X位置.
  338. @int 第二个点的Y位置.
  339. @usage
  340. u8g2.DrawLine(20, 5, 5, 32)
  341. */
  342. static int l_u8g2_DrawLine(lua_State *L){
  343. if (u8g2 == NULL) return 0;
  344. u8g2_DrawLine(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  345. return 1;
  346. }
  347. /*
  348. 在x,y位置画一个半径为rad的空心圆.
  349. @api u8g2.DrawCircle(x0,y0,rad,opt)
  350. @int 圆心位置
  351. @int 圆心位置
  352. @int 圆半径.
  353. @int 选择圆的部分或全部.
  354. 右上: 0x01
  355. 左上: 0x02
  356. 左下: 0x04
  357. 右下: 0x08
  358. 完整圆: (0x01|0x02|0x04|0x08)
  359. @usage
  360. u8g2.DrawCircle(60,30,8,15)
  361. */
  362. static int l_u8g2_DrawCircle(lua_State *L){
  363. if (u8g2 == NULL) return 0;
  364. u8g2_DrawCircle(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  365. return 1;
  366. }
  367. /*
  368. 在x,y位置画一个半径为rad的实心圆.
  369. @api u8g2.DrawDisc(x0,y0,rad,opt)
  370. @int 圆心位置
  371. @int 圆心位置
  372. @int 圆半径.
  373. @int 选择圆的部分或全部.
  374. 右上: 0x01
  375. 左上: 0x02
  376. 左下: 0x04
  377. 右下: 0x08
  378. 完整圆: (0x01|0x02|0x04|0x08)
  379. @usage
  380. u8g2.DrawDisc(60,30,8,15)
  381. */
  382. static int l_u8g2_DrawDisc(lua_State *L){
  383. if (u8g2 == NULL) return 0;
  384. u8g2_DrawDisc(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  385. return 1;
  386. }
  387. /*
  388. 在x,y位置画一个半径为rad的空心椭圆.
  389. @api u8g2.DrawEllipse(x0,y0,rx,ry,opt)
  390. @int 圆心位置
  391. @int 圆心位置
  392. @int 椭圆大小
  393. @int 椭圆大小
  394. @int 选择圆的部分或全部.
  395. 右上: 0x01
  396. 左上: 0x02
  397. 左下: 0x04
  398. 右下: 0x08
  399. 完整圆: (0x01|0x02|0x04|0x08)
  400. @usage
  401. u8g2.DrawEllipse(60,30,8,15)
  402. */
  403. static int l_u8g2_DrawEllipse(lua_State *L){
  404. if (u8g2 == NULL) return 0;
  405. u8g2_DrawEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  406. return 1;
  407. }
  408. /*
  409. 在x,y位置画一个半径为rad的实心椭圆.
  410. @api u8g2.DrawFilledEllipse(x0,y0,rx,ry,opt)
  411. @int 圆心位置
  412. @int 圆心位置
  413. @int 椭圆大小
  414. @int 椭圆大小
  415. @int 选择圆的部分或全部.
  416. 右上: 0x01
  417. 左上: 0x02
  418. 左下: 0x04
  419. 右下: 0x08
  420. 完整圆: (0x01|0x02|0x04|0x08)
  421. @usage
  422. u8g2.DrawFilledEllipse(60,30,8,15)
  423. */
  424. static int l_u8g2_DrawFilledEllipse(lua_State *L){
  425. if (u8g2 == NULL) return 0;
  426. u8g2_DrawFilledEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  427. return 1;
  428. }
  429. /*
  430. 从x / y位置(左上边缘)开始绘制一个框(填充的框).
  431. @api u8g2.DrawBox(x,y,w,h)
  432. @int 左上边缘的X位置
  433. @int 左上边缘的Y位置
  434. @int 盒子的宽度
  435. @int 盒子的高度
  436. @usage
  437. u8g2.DrawBox(3,7,25,15)
  438. */
  439. static int l_u8g2_DrawBox(lua_State *L){
  440. if (u8g2 == NULL) return 0;
  441. u8g2_DrawBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  442. return 1;
  443. }
  444. /*
  445. 从x / y位置(左上边缘)开始绘制一个框(空框).
  446. @api u8g2.DrawFrame(x,y,w,h)
  447. @int 左上边缘的X位置
  448. @int 左上边缘的Y位置
  449. @int 盒子的宽度
  450. @int 盒子的高度
  451. @usage
  452. u8g2.DrawFrame(3,7,25,15)
  453. */
  454. static int l_u8g2_DrawFrame(lua_State *L){
  455. if (u8g2 == NULL) return 0;
  456. u8g2_DrawFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  457. return 1;
  458. }
  459. /*
  460. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的填充框/框架.
  461. @api u8g2.DrawRBox(x,y,w,h,r)
  462. @int 左上边缘的X位置
  463. @int 左上边缘的Y位置
  464. @int 盒子的宽度
  465. @int 盒子的高度
  466. @int 四个边缘的半径
  467. @usage
  468. u8g2.DrawRBox(3,7,25,15)
  469. */
  470. static int l_u8g2_DrawRBox(lua_State *L){
  471. if (u8g2 == NULL) return 0;
  472. u8g2_DrawRBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  473. return 1;
  474. }
  475. /*
  476. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的空框/框架.
  477. @api u8g2.DrawRFrame(x,y,w,h,r)
  478. @int 左上边缘的X位置
  479. @int 左上边缘的Y位置
  480. @int 盒子的宽度
  481. @int 盒子的高度
  482. @int 四个边缘的半径
  483. @usage
  484. u8g2.DrawRFrame(3,7,25,15)
  485. */
  486. static int l_u8g2_DrawRFrame(lua_State *L){
  487. if (u8g2 == NULL) return 0;
  488. u8g2_DrawRFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  489. return 1;
  490. }
  491. /*
  492. 绘制一个图形字符。字符放置在指定的像素位置x和y.
  493. @api u8g2.DrawGlyph(x,y,encoding)
  494. @int 字符在显示屏上的位置
  495. @int 字符在显示屏上的位置
  496. @int 字符的Unicode值
  497. @usage
  498. u8g2.SetFont(u8g2_font_unifont_t_symbols)
  499. u8g2.DrawGlyph(5, 20, 0x2603) -- dec 9731/hex 2603 Snowman
  500. */
  501. static int l_u8g2_DrawGlyph(lua_State *L){
  502. if (u8g2 == NULL) return 0;
  503. u8g2_DrawGlyph(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3));
  504. return 1;
  505. }
  506. /*
  507. 绘制一个三角形(实心多边形).
  508. @api u8g2.DrawTriangle(x0,y0,x1,y1,x2,y2)
  509. @int 点0X位置
  510. @int 点0Y位置
  511. @int 点1X位置
  512. @int 点1Y位置
  513. @int 点2X位置
  514. @int 点2Y位置
  515. @usage
  516. u8g2.DrawTriangle(20,5, 27,50, 5,32)
  517. */
  518. static int l_u8g2_DrawTriangle(lua_State *L){
  519. if (u8g2 == NULL) return 0;
  520. 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));
  521. return 1;
  522. }
  523. /*
  524. 定义位图函数是否将写入背景色
  525. @api u8g2.SetBitmapMode(mode)
  526. @int mode字体模式,启用(1)或禁用(0)透明模式
  527. @usage
  528. u8g2.SetBitmapMode(1)
  529. */
  530. static int l_u8g2_SetBitmapMode(lua_State *L){
  531. if (u8g2 == NULL) return 0;
  532. u8g2_SetBitmapMode(u8g2,luaL_checkinteger(L, 1));
  533. return 1;
  534. }
  535. #include "rotable.h"
  536. static const rotable_Reg reg_u8g2[] =
  537. {
  538. { "begin", l_u8g2_begin, 0},
  539. { "close", l_u8g2_close, 0},
  540. { "ClearBuffer", l_u8g2_ClearBuffer, 0},
  541. { "SendBuffer", l_u8g2_SendBuffer, 0},
  542. { "DrawUTF8", l_u8g2_DrawUTF8, 0},
  543. { "SetFontMode", l_u8g2_SetFontMode, 0},
  544. { "SetFont", l_u8g2_SetFont, 0},
  545. { "GetDisplayHeight", l_u8g2_GetDisplayHeight, 0},
  546. { "GetDisplayWidth", l_u8g2_GetDisplayWidth, 0},
  547. { "DrawLine", l_u8g2_DrawLine, 0},
  548. { "DrawCircle", l_u8g2_DrawCircle, 0},
  549. { "DrawDisc", l_u8g2_DrawDisc, 0},
  550. { "DrawEllipse", l_u8g2_DrawEllipse, 0},
  551. { "DrawFilledEllipse", l_u8g2_DrawFilledEllipse, 0},
  552. { "DrawBox", l_u8g2_DrawBox, 0},
  553. { "DrawFrame", l_u8g2_DrawFrame, 0},
  554. { "DrawRBox", l_u8g2_DrawRBox, 0},
  555. { "DrawRFrame", l_u8g2_DrawRFrame, 0},
  556. { "DrawGlyph", l_u8g2_DrawGlyph, 0},
  557. { "DrawTriangle", l_u8g2_DrawTriangle, 0},
  558. { "SetBitmapMode", l_u8g2_SetBitmapMode, 0},
  559. { "font_ncenB08_tr", NULL, font_ncenB08_tr},
  560. { "font_wqy12_t_gb2312", NULL, font_wqy12_t_gb2312},
  561. { "font_unifont_t_symbols", NULL, font_unifont_t_symbols},
  562. { "font_open_iconic_weather_6x_t", NULL, font_open_iconic_weather_6x_t},
  563. { NULL, NULL, 0}
  564. };
  565. LUAMOD_API int luaopen_u8g2( lua_State *L ) {
  566. u8g2_lua_ref = 0;
  567. u8g2 = NULL;
  568. luat_newlib(L, reg_u8g2);
  569. return 1;
  570. }
  571. //-----------------------------
  572. // 往下是一些U8G2方法的默认实现
  573. uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  574. uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  575. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  576. LUAT_WEAK int luat_u8g2_setup(luat_u8g2_conf_t *conf) {
  577. if (conf->pinType == 1) {
  578. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  579. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay);
  580. u8g2->u8x8.pins[U8X8_PIN_I2C_CLOCK] = conf->pin0;
  581. u8g2->u8x8.pins[U8X8_PIN_I2C_DATA] = conf->pin1;
  582. LLOGD("setup disp i2c.sw SCL=%ld SDA=%ld", conf->pin0, conf->pin1);
  583. u8g2_InitDisplay(u8g2);
  584. u8g2_SetPowerSave(u8g2, 0);
  585. return 0;
  586. }
  587. else if (conf->pinType == 2) {
  588. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  589. u8g2_Setup_ssd1306_i2c_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_hw_i2c, u8x8_luat_gpio_and_delay);
  590. LLOGD("setup disp i2c.hw");
  591. u8g2_InitDisplay(u8g2);
  592. u8g2_SetPowerSave(u8g2, 0);
  593. return 0;
  594. }
  595. else if (conf->pinType == 5) {
  596. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  597. u8g2_Setup_ssd1306_128x64_noname_f( u8g2, U8G2_R0, u8x8_luat_byte_4wire_hw_spi, u8x8_luat_gpio_and_delay);
  598. LLOGD("setup disp spi.hw");
  599. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, OLED_SPI_PIN_CS);
  600. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, OLED_SPI_PIN_DC);
  601. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, OLED_SPI_PIN_RES);
  602. u8g2_InitDisplay(u8g2);
  603. u8g2_SetPowerSave(u8g2, 0);
  604. return 0;
  605. }
  606. LLOGI("only i2c sw mode is support, by default impl");
  607. return -1;
  608. }
  609. LUAT_WEAK int luat_u8g2_close(luat_u8g2_conf_t *conf) {
  610. return 0;
  611. }
  612. LUAT_WEAK uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  613. static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes */
  614. static uint8_t buf_idx;
  615. uint8_t *data;
  616. switch(msg)
  617. {
  618. case U8X8_MSG_BYTE_SEND:
  619. data = (uint8_t *)arg_ptr;
  620. while( arg_int > 0 )
  621. {
  622. buffer[buf_idx++] = *data;
  623. data++;
  624. arg_int--;
  625. }
  626. break;
  627. case U8X8_MSG_BYTE_INIT:
  628. //i2c_init(u8x8); /* init i2c communication */
  629. luat_i2c_setup(i2c_id,i2c_speed,NULL);
  630. break;
  631. case U8X8_MSG_BYTE_SET_DC:
  632. /* ignored for i2c */
  633. break;
  634. case U8X8_MSG_BYTE_START_TRANSFER:
  635. buf_idx = 0;
  636. break;
  637. case U8X8_MSG_BYTE_END_TRANSFER:
  638. luat_i2c_send(i2c_id, u8x8_GetI2CAddress(u8x8) >> 1, buffer, buf_idx);
  639. break;
  640. default:
  641. return 0;
  642. }
  643. return 1;
  644. }
  645. int hw_spi_begin(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin )
  646. {
  647. luat_spi_t u8g2_spi = {0};
  648. u8g2_spi.id = spi_id;
  649. switch(spi_mode)
  650. {
  651. case 0: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 0; break;
  652. case 1: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 0; break;
  653. case 2: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 1; break;
  654. case 3: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 1; break;
  655. }
  656. u8g2_spi.dataw = 8;
  657. u8g2_spi.bit_dict = 1;
  658. u8g2_spi.master = 1;
  659. u8g2_spi.mode = 1;
  660. u8g2_spi.bandrate = max_hz;
  661. u8g2_spi.cs = cs_pin;
  662. LLOGI("cs_pin=%d",cs_pin);
  663. luat_spi_setup(&u8g2_spi);
  664. return 1;
  665. }
  666. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  667. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  668. uint8_t i;
  669. uint8_t *data;
  670. uint8_t tx[256];
  671. uint8_t rx[256];
  672. static uint8_t buf_idx;
  673. static uint8_t buffer_tx[256];
  674. switch(msg)
  675. {
  676. case U8X8_MSG_BYTE_SEND:
  677. data = (uint8_t *)arg_ptr;
  678. while( arg_int > 0)
  679. {
  680. buffer_tx[buf_idx++] = (uint8_t)*data;
  681. luat_spi_send(spi_id, (uint8_t*)data, 1);
  682. data++;
  683. arg_int--;
  684. }
  685. //luat_spi_send(spi_id, (uint8_t*)data, arg_int);
  686. break;
  687. case U8X8_MSG_BYTE_INIT:
  688. /* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  689. /* 0: clock active high, data out on falling edge, clock default value is zero, takover on rising edge */
  690. /* 1: clock active high, data out on rising edge, clock default value is zero, takover on falling edge */
  691. /* 2: clock active low, data out on rising edge */
  692. /* 3: clock active low, data out on falling edge */
  693. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  694. hw_spi_begin(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]);
  695. break;
  696. case U8X8_MSG_BYTE_SET_DC:
  697. u8x8_gpio_SetDC(u8x8, arg_int);
  698. break;
  699. case U8X8_MSG_BYTE_START_TRANSFER:
  700. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  701. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  702. break;
  703. case U8X8_MSG_BYTE_END_TRANSFER:
  704. memset( tx, 0, ARRAY_SIZE(tx)*sizeof(uint8_t) );
  705. memset( rx, 0, ARRAY_SIZE(rx)*sizeof(uint8_t) );
  706. for (i = 0; i < buf_idx; ++i)
  707. {
  708. tx[i] = buffer_tx[i];
  709. }
  710. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  711. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  712. buf_idx = 0;
  713. break;
  714. default:
  715. return 0;
  716. }
  717. return 1;
  718. }
  719. LUAT_WEAK uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  720. {
  721. switch(msg)
  722. {
  723. case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
  724. __asm__ volatile("nop");
  725. break;
  726. case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  727. __asm__ volatile("nop");
  728. break;
  729. case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  730. for (uint16_t n = 0; n < 320; n++)
  731. {
  732. __asm__ volatile("nop");
  733. }
  734. break;
  735. case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  736. luat_timer_mdelay(arg_int);
  737. break;
  738. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  739. // Function which implements a delay, arg_int contains the amount of ms
  740. // set spi pin mode
  741. if (u8x8->pins[U8X8_PIN_SPI_CLOCK] != 255) {
  742. 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
  743. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  744. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  745. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  746. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  747. }
  748. // set i2c pin mode
  749. if (u8x8->pins[U8X8_PIN_I2C_DATA] != 255) {
  750. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  751. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  752. }
  753. // set 8080 pin mode
  754. if (u8x8->pins[U8X8_PIN_D0] != 255) {
  755. luat_gpio_mode(u8x8->pins[U8X8_PIN_D0],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  756. luat_gpio_mode(u8x8->pins[U8X8_PIN_D1],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  757. luat_gpio_mode(u8x8->pins[U8X8_PIN_D2],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  758. luat_gpio_mode(u8x8->pins[U8X8_PIN_D3],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  759. luat_gpio_mode(u8x8->pins[U8X8_PIN_D4],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  760. luat_gpio_mode(u8x8->pins[U8X8_PIN_D5],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  761. luat_gpio_mode(u8x8->pins[U8X8_PIN_D6],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  762. luat_gpio_mode(u8x8->pins[U8X8_PIN_D7],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  763. luat_gpio_mode(u8x8->pins[U8X8_PIN_E],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  764. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  765. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  766. }
  767. // set value
  768. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],1);
  769. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],1);
  770. luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],1);
  771. luat_gpio_set(u8x8->pins[U8X8_PIN_DC],1);
  772. luat_gpio_set(u8x8->pins[U8X8_PIN_CS],1);
  773. break;
  774. case U8X8_MSG_DELAY_I2C:
  775. // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  776. // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  777. for (uint16_t n = 0; n < (arg_int<=2?160:40); n++)
  778. {
  779. __asm__ volatile("nop");
  780. }
  781. break;
  782. //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
  783. //case U8X8_MSG_GPIO_SPI_CLOCK:
  784. //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
  785. //case U8X8_MSG_GPIO_SPI_DATA:
  786. case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
  787. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D2],1);
  788. else luat_gpio_set(u8x8->pins[U8X8_PIN_D2],0);
  789. break;
  790. case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
  791. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D3],1);
  792. else luat_gpio_set(u8x8->pins[U8X8_PIN_D3],0);
  793. break;
  794. case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
  795. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D4],1);
  796. else luat_gpio_set(u8x8->pins[U8X8_PIN_D4],0);
  797. break;
  798. case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
  799. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D5],1);
  800. else luat_gpio_set(u8x8->pins[U8X8_PIN_D5],0);
  801. break;
  802. case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
  803. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D6],1);
  804. else luat_gpio_set(u8x8->pins[U8X8_PIN_D6],0);
  805. break;
  806. case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
  807. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_D7],1);
  808. else luat_gpio_set(u8x8->pins[U8X8_PIN_D7],0);
  809. break;
  810. case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
  811. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_E],1);
  812. else luat_gpio_set(u8x8->pins[U8X8_PIN_E],0);
  813. break;
  814. case U8X8_MSG_GPIO_I2C_CLOCK:
  815. // arg_int=0: Output low at I2C clock pin
  816. // arg_int=1: Input dir with pullup high for I2C clock pin
  817. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],1);
  818. else luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],0);
  819. break;
  820. case U8X8_MSG_GPIO_I2C_DATA:
  821. // arg_int=0: Output low at I2C data pin
  822. // arg_int=1: Input dir with pullup high for I2C data pin
  823. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],1);
  824. else luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],0);
  825. break;
  826. case U8X8_MSG_GPIO_SPI_CLOCK:
  827. //Function to define the logic level of the clockline
  828. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],1);
  829. else luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],0);
  830. break;
  831. case U8X8_MSG_GPIO_SPI_DATA:
  832. //Function to define the logic level of the data line to the display
  833. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],1);
  834. else luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],0);
  835. break;
  836. case U8X8_MSG_GPIO_CS:
  837. // Function to define the logic level of the CS line
  838. if(arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_CS],1);
  839. else luat_gpio_set(u8x8->pins[U8X8_PIN_CS],0);
  840. break;
  841. case U8X8_MSG_GPIO_DC:
  842. //Function to define the logic level of the Data/ Command line
  843. if(arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_DC],1);
  844. else luat_gpio_set(u8x8->pins[U8X8_PIN_DC],0);
  845. break;
  846. case U8X8_MSG_GPIO_RESET:
  847. //Function to define the logic level of the RESET line
  848. if (arg_int) luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],1);
  849. else luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],0);
  850. break;
  851. default:
  852. //A message was received which is not implemented, return 0 to indicate an error
  853. return 0;
  854. }
  855. return 1;
  856. }