luat_lib_u8g2.c 56 KB

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