luat_lib_u8g2.c 54 KB

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