luat_lib_u8g2.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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. /*
  398. 设置字体
  399. @api u8g2.SetFont(font)
  400. @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 为符号.
  401. @usage
  402. -- 设置为中文字体,对之后的drawStr有效
  403. u8g2.SetFont(u8g2.font_opposansm12)
  404. */
  405. static int l_u8g2_SetFont(lua_State *L) {
  406. if (conf == NULL) {
  407. LLOGI("u8g2 not init yet!!!");
  408. lua_pushboolean(L, 0);
  409. return 1;
  410. }
  411. if (!lua_islightuserdata(L, 1)) {
  412. LLOGE("no such font");
  413. return 0;
  414. }
  415. const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
  416. if (ptr == NULL) {
  417. LLOGE("only font pointer is allow");
  418. return 0;
  419. }
  420. u8g2_SetFont(&conf->u8g2, ptr);
  421. lua_pushboolean(L, 1);
  422. return 1;
  423. }
  424. /*
  425. 获取显示屏高度
  426. @api u8g2.GetDisplayHeight()
  427. @return int 显示屏高度
  428. @usage
  429. u8g2.GetDisplayHeight()
  430. */
  431. static int l_u8g2_GetDisplayHeight(lua_State *L){
  432. if (conf == NULL) return 0;
  433. lua_pushinteger(L, u8g2_GetDisplayHeight(&conf->u8g2));
  434. return 1;
  435. }
  436. /*
  437. 获取显示屏宽度
  438. @api u8g2.GetDisplayWidth()
  439. @return int 显示屏宽度
  440. @usage
  441. u8g2.GetDisplayWidth()
  442. */
  443. static int l_u8g2_GetDisplayWidth(lua_State *L){
  444. if (conf == NULL) return 0;
  445. lua_pushinteger(L, u8g2_GetDisplayWidth(&conf->u8g2));
  446. return 1;
  447. }
  448. /*
  449. 为所有绘图功能分配绘图颜色。
  450. @api u8g2.SetDrawColor(c)
  451. @int c为颜色值 0没有色 1有色 2与底色xor
  452. @usage
  453. u8g2.SetDrawColor(0)
  454. */
  455. static int l_u8g2_SetDrawColor(lua_State *L){
  456. if (conf == NULL) return 0;
  457. u8g2_SetDrawColor(&conf->u8g2,luaL_checkinteger(L, 1));
  458. return 0;
  459. }
  460. /*
  461. 画一个点.
  462. @api u8g2.DrawPixel(x,y)
  463. @int X位置.
  464. @int Y位置.
  465. @usage
  466. u8g2.DrawPixel(20, 5)
  467. */
  468. static int l_u8g2_DrawPixel(lua_State *L){
  469. if (conf == NULL) return 0;
  470. u8g2_DrawPixel(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2));
  471. return 0;
  472. }
  473. /*
  474. 在两点之间画一条线.
  475. @api u8g2.DrawLine(x0,y0,x1,y1)
  476. @int 第一个点的X位置.
  477. @int 第一个点的Y位置.
  478. @int 第二个点的X位置.
  479. @int 第二个点的Y位置.
  480. @usage
  481. u8g2.DrawLine(20, 5, 5, 32)
  482. */
  483. static int l_u8g2_DrawLine(lua_State *L){
  484. if (conf == NULL) return 0;
  485. u8g2_DrawLine(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  486. return 0;
  487. }
  488. /*
  489. 在x,y位置画一个半径为rad的空心圆.
  490. @api u8g2.DrawCircle(x0,y0,rad,opt)
  491. @int 圆心位置
  492. @int 圆心位置
  493. @int 圆半径.
  494. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  495. @usage
  496. u8g2.DrawCircle(60,30,8,u8g2.DRAW_ALL)
  497. */
  498. static int l_u8g2_DrawCircle(lua_State *L){
  499. if (conf == NULL) return 0;
  500. u8g2_DrawCircle(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_optinteger(L, 4,U8G2_DRAW_ALL));
  501. return 0;
  502. }
  503. /*
  504. 在x,y位置画一个半径为rad的实心圆.
  505. @api u8g2.DrawDisc(x0,y0,rad,opt)
  506. @int 圆心位置
  507. @int 圆心位置
  508. @int 圆半径.
  509. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  510. @usage
  511. u8g2.DrawDisc(60,30,8,u8g2.DRAW_ALL)
  512. */
  513. static int l_u8g2_DrawDisc(lua_State *L){
  514. if (conf == NULL) return 0;
  515. u8g2_DrawDisc(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_optinteger(L, 4,U8G2_DRAW_ALL));
  516. return 0;
  517. }
  518. /*
  519. 在x,y位置画一个半径为rad的空心椭圆.
  520. @api u8g2.DrawEllipse(x0,y0,rx,ry,opt)
  521. @int 圆心位置
  522. @int 圆心位置
  523. @int 椭圆大小
  524. @int 椭圆大小
  525. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  526. @usage
  527. u8g2.DrawEllipse(60,30,8,u8g2.DRAW_ALL)
  528. */
  529. static int l_u8g2_DrawEllipse(lua_State *L){
  530. if (conf == NULL) return 0;
  531. 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));
  532. return 0;
  533. }
  534. /*
  535. 在x,y位置画一个半径为rad的实心椭圆.
  536. @api u8g2.DrawFilledEllipse(x0,y0,rx,ry,opt)
  537. @int 圆心位置
  538. @int 圆心位置
  539. @int 椭圆大小
  540. @int 椭圆大小
  541. @int 选择圆的部分或全部. 默认全画 可选 u8g2.DRAW_UPPER_RIGHT u8g2.DRAW_UPPER_LEFT u8g2.DRAW_LOWER_LEFT u8g2.DRAW_LOWER_RIGHT u8g2.DRAW_ALL
  542. @usage
  543. u8g2.DrawFilledEllipse(60,30,8,15)
  544. */
  545. static int l_u8g2_DrawFilledEllipse(lua_State *L){
  546. if (conf == NULL) return 0;
  547. 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));
  548. return 0;
  549. }
  550. /*
  551. 从x / y位置(左上边缘)开始绘制一个框(填充的框).
  552. @api u8g2.DrawBox(x,y,w,h)
  553. @int 左上边缘的X位置
  554. @int 左上边缘的Y位置
  555. @int 盒子的宽度
  556. @int 盒子的高度
  557. @usage
  558. u8g2.DrawBox(3,7,25,15)
  559. */
  560. static int l_u8g2_DrawBox(lua_State *L){
  561. if (conf == NULL) return 0;
  562. u8g2_DrawBox(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  563. return 0;
  564. }
  565. /*
  566. 从x / y位置(左上边缘)开始绘制一个框(空框).
  567. @api u8g2.DrawFrame(x,y,w,h)
  568. @int 左上边缘的X位置
  569. @int 左上边缘的Y位置
  570. @int 盒子的宽度
  571. @int 盒子的高度
  572. @usage
  573. u8g2.DrawFrame(3,7,25,15)
  574. */
  575. static int l_u8g2_DrawFrame(lua_State *L){
  576. if (conf == NULL) return 0;
  577. u8g2_DrawFrame(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  578. return 0;
  579. }
  580. /*
  581. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的填充框/框架.
  582. @api u8g2.DrawRBox(x,y,w,h,r)
  583. @int 左上边缘的X位置
  584. @int 左上边缘的Y位置
  585. @int 盒子的宽度
  586. @int 盒子的高度
  587. @int 四个边缘的半径
  588. @usage
  589. u8g2.DrawRBox(3,7,25,15)
  590. */
  591. static int l_u8g2_DrawRBox(lua_State *L){
  592. if (conf == NULL) return 0;
  593. u8g2_DrawRBox(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  594. return 0;
  595. }
  596. /*
  597. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的空框/框架.
  598. @api u8g2.DrawRFrame(x,y,w,h,r)
  599. @int 左上边缘的X位置
  600. @int 左上边缘的Y位置
  601. @int 盒子的宽度
  602. @int 盒子的高度
  603. @int 四个边缘的半径
  604. @usage
  605. u8g2.DrawRFrame(3,7,25,15)
  606. */
  607. static int l_u8g2_DrawRFrame(lua_State *L){
  608. if (conf == NULL) return 0;
  609. u8g2_DrawRFrame(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  610. return 0;
  611. }
  612. /*
  613. 绘制一个图形字符。字符放置在指定的像素位置x和y.
  614. @api u8g2.DrawGlyph(x,y,encoding)
  615. @int 字符在显示屏上的位置
  616. @int 字符在显示屏上的位置
  617. @int 字符的Unicode值
  618. @usage
  619. u8g2.SetFont(u8g2_font_unifont_t_symbols)
  620. u8g2.DrawGlyph(5, 20, 0x2603) -- dec 9731/hex 2603 Snowman
  621. */
  622. static int l_u8g2_DrawGlyph(lua_State *L){
  623. if (conf == NULL) return 0;
  624. u8g2_DrawGlyph(&conf->u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3));
  625. return 0;
  626. }
  627. /*
  628. 绘制一个三角形(实心多边形).
  629. @api u8g2.DrawTriangle(x0,y0,x1,y1,x2,y2)
  630. @int 点0X位置
  631. @int 点0Y位置
  632. @int 点1X位置
  633. @int 点1Y位置
  634. @int 点2X位置
  635. @int 点2Y位置
  636. @usage
  637. u8g2.DrawTriangle(20,5, 27,50, 5,32)
  638. */
  639. static int l_u8g2_DrawTriangle(lua_State *L){
  640. if (conf == NULL) return 0;
  641. 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));
  642. return 0;
  643. }
  644. /*
  645. 定义位图函数是否将写入背景色
  646. @api u8g2.SetBitmapMode(mode)
  647. @int mode字体模式,启用(1)或禁用(0)透明模式
  648. @usage
  649. u8g2.SetBitmapMode(1)
  650. */
  651. static int l_u8g2_SetBitmapMode(lua_State *L){
  652. if (conf == NULL) return 0;
  653. u8g2_SetBitmapMode(&conf->u8g2,luaL_checkinteger(L, 1));
  654. return 0;
  655. }
  656. /*
  657. 绘制位图
  658. @api u8g2.DrawXBM(x, y, w, h, data)
  659. @int X坐标
  660. @int y坐标
  661. @int 位图宽
  662. @int 位图高
  663. @int 位图数据,每一位代表一个像素
  664. @usage
  665. -- 取模使用PCtoLCD2002软件即可
  666. -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
  667. u8g2.DrawXBM(0, 0, 16,16, string.char(
  668. 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
  669. 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
  670. ))
  671. */
  672. static int l_u8g2_DrawXBM(lua_State *L){
  673. if (conf == NULL) return 0;
  674. int x = luaL_checkinteger(L, 1);
  675. int y = luaL_checkinteger(L, 2);
  676. int w = luaL_checkinteger(L, 3);
  677. int h = luaL_checkinteger(L, 4);
  678. size_t len = 0;
  679. const char* data = luaL_checklstring(L, 5, &len);
  680. if (h < 1) return 0; // 行数必须大于0
  681. if (len*8/h < w) return 0; // 起码要填满一行
  682. if (len != h*w/8)return 0;
  683. u8g2_DrawXBM(&conf->u8g2, x, y, w, h, (const uint8_t*)data);
  684. lua_pushboolean(L, 1);
  685. return 1;
  686. }
  687. /**
  688. 缓冲区绘制QRCode
  689. @api u8g2.DrawDrcode(x, y, str, size)
  690. @int x坐标
  691. @int y坐标
  692. @string 二维码的内容
  693. @int 显示大小 (注意:二维码生成大小与要显示内容和纠错等级有关,生成版本为1-40(对应 21x21 - 177x177)的不定大小,如果和设置大小不同会自动在指定的区域中间显示二维码,如二维码未显示请查看日志提示)
  694. @return nil 无返回值
  695. */
  696. static int l_u8g2_DrawDrcode(lua_State *L)
  697. {
  698. size_t len;
  699. int x = luaL_checkinteger(L, 1);
  700. int y = luaL_checkinteger(L, 2);
  701. const char* text = luaL_checklstring(L, 3, &len);
  702. int size = luaL_checkinteger(L, 4);
  703. uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  704. uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  705. if (qrcode == NULL || tempBuffer == NULL) {
  706. if (qrcode)
  707. luat_heap_free(qrcode);
  708. if (tempBuffer)
  709. luat_heap_free(tempBuffer);
  710. LLOGE("qrcode out of memory");
  711. return 0;
  712. }
  713. bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_LOW,
  714. qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
  715. if (ok){
  716. int qr_size = qrcodegen_getSize(qrcode);
  717. if (size < qr_size){
  718. LLOGE("size must be greater than qr_size %d",qr_size);
  719. goto end;
  720. }
  721. int scale = size / qr_size ;
  722. if (!scale)scale = 1;
  723. int margin = (size - qr_size * scale) / 2;
  724. x+=margin;
  725. y+=margin;
  726. for (int j = 0; j < qr_size; j++) {
  727. for (int i = 0; i < qr_size; i++) {
  728. if (qrcodegen_getModule(qrcode, i, j))
  729. u8g2_DrawBox(&conf->u8g2,x+i*scale,y+j*scale,scale,scale);
  730. }
  731. }
  732. }else{
  733. LLOGE("qrcodegen_encodeText false");
  734. }
  735. end:
  736. if (qrcode)
  737. luat_heap_free(qrcode);
  738. if (tempBuffer)
  739. luat_heap_free(tempBuffer);
  740. return 0;
  741. }
  742. /**
  743. 调整屏幕对比度
  744. @api u8g2.SetContrast(contrast)
  745. @int 对比度(取值范围与IC有关,如st7567为0-63)
  746. @return nil 无返回值
  747. */
  748. static int l_u8g2_SetContrast(lua_State *L)
  749. {
  750. if (conf == NULL) return 0;
  751. u8g2_SetContrast(&conf->u8g2,luaL_checkinteger(L, 1));
  752. return 0;
  753. }
  754. #ifdef LUAT_USE_GTFONT
  755. #include "GT5SLCD2E_1A.h"
  756. extern unsigned int gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int size,unsigned int widt,unsigned int high,int(*point)(void*),void* userdata,int mode);
  757. extern void gtfont_draw_gray_hz(unsigned char *data,unsigned short x,unsigned short y,unsigned short w ,unsigned short h,unsigned char grade, unsigned char HB_par,int(*point)(void*,uint16_t, uint16_t, uint32_t),void* userdata,int mode);
  758. static int gtfont_u8g2_DrawPixel(u8g2_t *u8g2, uint16_t x, uint16_t y,uint32_t color){
  759. u8g2_DrawPixel(&conf->u8g2,x, y);
  760. return 1;
  761. }
  762. /*
  763. 使用gtfont显示gb2312字符串
  764. @api u8g2.drawGtfontGb2312(str,size,x,y)
  765. @string str 显示字符串
  766. @int size 字体大小 (支持16-192号大小字体)
  767. @int x 横坐标
  768. @int y 竖坐标
  769. @usage
  770. u8g2.drawGtfontGb2312("啊啊啊",32,0,0)
  771. */
  772. static int l_u8g2_draw_gtfont_gb2312(lua_State *L) {
  773. unsigned char buf[128];
  774. int len;
  775. int i = 0;
  776. uint8_t strhigh,strlow ;
  777. uint16_t str;
  778. const char *fontCode = luaL_checklstring(L, 1,&len);
  779. unsigned char size = luaL_checkinteger(L, 2);
  780. int x = luaL_checkinteger(L, 3);
  781. int y = luaL_checkinteger(L, 4);
  782. while ( i < len){
  783. strhigh = *fontCode;
  784. fontCode++;
  785. strlow = *fontCode;
  786. str = (strhigh<<8)|strlow;
  787. fontCode++;
  788. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  789. if(font_size == 0){
  790. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  791. return 0;
  792. }
  793. gtfont_draw_w(buf , x ,y , font_size,size , size,gtfont_u8g2_DrawPixel,&conf->u8g2,2);
  794. x+=size;
  795. i+=2;
  796. }
  797. return 0;
  798. }
  799. #ifdef LUAT_USE_GTFONT_UTF8
  800. extern unsigned short unicodetogb2312 ( unsigned short chr);
  801. static uint8_t utf8_state;
  802. static uint16_t encoding;
  803. static uint16_t utf8_next(uint8_t b)
  804. {
  805. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  806. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  807. if ( utf8_state == 0 )
  808. {
  809. if ( b >= 0xfc ) /* 6 byte sequence */
  810. {
  811. utf8_state = 5;
  812. b &= 1;
  813. }
  814. else if ( b >= 0xf8 )
  815. {
  816. utf8_state = 4;
  817. b &= 3;
  818. }
  819. else if ( b >= 0xf0 )
  820. {
  821. utf8_state = 3;
  822. b &= 7;
  823. }
  824. else if ( b >= 0xe0 )
  825. {
  826. utf8_state = 2;
  827. b &= 15;
  828. }
  829. else if ( b >= 0xc0 )
  830. {
  831. utf8_state = 1;
  832. b &= 0x01f;
  833. }
  834. else
  835. {
  836. /* do nothing, just use the value as encoding */
  837. return b;
  838. }
  839. encoding = b;
  840. return 0x0fffe;
  841. }
  842. else
  843. {
  844. utf8_state--;
  845. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  846. encoding<<=6;
  847. b &= 0x03f;
  848. encoding |= b;
  849. if ( utf8_state != 0 )
  850. return 0x0fffe; /* nothing to do yet */
  851. }
  852. return encoding;
  853. }
  854. /*
  855. 使用gtfont显示UTF8字符串
  856. @api u8g2.drawGtfontUtf8(str,size,x,y)
  857. @string str 显示字符串
  858. @int size 字体大小 (支持16-192号大小字体)
  859. @int x 横坐标
  860. @int y 竖坐标
  861. @usage
  862. u8g2.drawGtfontUtf8("啊啊啊",32,0,0)
  863. */
  864. static int l_u8g2_draw_gtfont_utf8(lua_State *L) {
  865. unsigned char buf[128];
  866. int len;
  867. int i = 0;
  868. uint8_t strhigh,strlow ;
  869. uint16_t e,str;
  870. const char *fontCode = luaL_checklstring(L, 1,&len);
  871. unsigned char size = luaL_checkinteger(L, 2);
  872. int x = luaL_checkinteger(L, 3);
  873. int y = luaL_checkinteger(L, 4);
  874. for(;;){
  875. e = utf8_next((uint8_t)*fontCode);
  876. if ( e == 0x0ffff )
  877. break;
  878. fontCode++;
  879. if ( e != 0x0fffe ){
  880. uint16_t str = unicodetogb2312(e);
  881. int font_size = get_font(buf, str<0x80?VEC_HZ_ASCII_STY:VEC_BLACK_STY, str, size, size, size);
  882. if(font_size == 0){
  883. LLOGW("get gtfont error size:%d font_size:%d",size,font_size);
  884. return 0;
  885. }
  886. gtfont_draw_w(buf , x ,y , font_size,size , size,gtfont_u8g2_DrawPixel,&conf->u8g2,2);
  887. x+=size;
  888. }
  889. }
  890. return 0;
  891. }
  892. #endif // LUAT_USE_GTFONT_UTF8
  893. #endif // LUAT_USE_GTFONT
  894. /*
  895. 获取底层图像缓冲区
  896. @api u8g2.CopyBuffer(buff)
  897. @userdata zbuff实例,空间要大于等于底层buff的大小
  898. @return int 成功返回buff大小,否则返回nil
  899. @usage
  900. -- 本函数在u8g2初始化之后才能调用
  901. -- 获取大小
  902. local sz = u8g2.CopyBuffer()
  903. -- 拷贝底层buff
  904. local buff = zbuff.create(sz)
  905. u8g2.CopyBuffer(buff)
  906. */
  907. #include "luat_zbuff.h"
  908. static int l_u8g2_CopyBuffer(lua_State *L) {
  909. if (conf == NULL) return 0;
  910. if (conf->buff_ptr == NULL) return 0;
  911. size_t len = u8g2_GetBufferSize(&conf->u8g2);
  912. if (lua_gettop(L) == 0 || lua_isnil(L, 1)) {
  913. lua_pushinteger(L, len);
  914. return 1;
  915. }
  916. luat_zbuff_t* buff = tozbuff(L);
  917. if (buff->len < len) {
  918. LLOGD("zbuff数据长度不够,需要%d 但只有 %d", len, buff->len);
  919. return 0;
  920. }
  921. memcpy(buff->addr, conf->buff_ptr, len);
  922. lua_pushinteger(L, len);
  923. return 1;
  924. }
  925. /*
  926. 设置省电模式
  927. @api u8g2.SetPowerSave(is_enable)
  928. @boolean 启用与否. true 启用, false禁用
  929. @return nil 无返回值
  930. @usage
  931. -- 本API于2023.11.02新增
  932. -- 开启省电
  933. u8g2.SetPowerSave(true)
  934. -- 关闭省电
  935. u8g2.SetPowerSave(false)
  936. */
  937. static int l_u8g2_SetPowerSave(lua_State *L) {
  938. if (conf == NULL) return 0;
  939. int is_enable = 0;
  940. if (lua_isinteger(L, 1)) {
  941. is_enable = lua_tointeger(L, 1);
  942. }
  943. else if (lua_isboolean(L, 1)) {
  944. is_enable = lua_toboolean(L, 1);
  945. }
  946. u8g2_SetPowerSave(&conf->u8g2, is_enable);
  947. return 0;
  948. }
  949. #include "rotable2.h"
  950. static const rotable_Reg_t reg_u8g2[] =
  951. {
  952. { "begin", ROREG_FUNC(l_u8g2_begin)},
  953. { "init", ROREG_FUNC(l_u8g2_begin)}, // 兼容disp.init函数
  954. { "close", ROREG_FUNC(l_u8g2_close)},
  955. { "ClearBuffer", ROREG_FUNC(l_u8g2_ClearBuffer)},
  956. { "SendBuffer", ROREG_FUNC(l_u8g2_SendBuffer)},
  957. { "DrawUTF8", ROREG_FUNC(l_u8g2_DrawUTF8)},
  958. { "DrawButtonUTF8", ROREG_FUNC(l_u8g2_DrawButtonUTF8)},
  959. { "SetFontMode", ROREG_FUNC(l_u8g2_SetFontMode)},
  960. { "SetFont", ROREG_FUNC(l_u8g2_SetFont)},
  961. { "GetDisplayHeight", ROREG_FUNC(l_u8g2_GetDisplayHeight)},
  962. { "GetDisplayWidth", ROREG_FUNC(l_u8g2_GetDisplayWidth)},
  963. { "SetDrawColor", ROREG_FUNC(l_u8g2_SetDrawColor)},
  964. { "DrawPixel", ROREG_FUNC(l_u8g2_DrawPixel)},
  965. { "DrawLine", ROREG_FUNC(l_u8g2_DrawLine)},
  966. { "DrawCircle", ROREG_FUNC(l_u8g2_DrawCircle)},
  967. { "DrawDisc", ROREG_FUNC(l_u8g2_DrawDisc)},
  968. { "DrawEllipse", ROREG_FUNC(l_u8g2_DrawEllipse)},
  969. { "DrawFilledEllipse", ROREG_FUNC(l_u8g2_DrawFilledEllipse)},
  970. { "DrawBox", ROREG_FUNC(l_u8g2_DrawBox)},
  971. { "DrawFrame", ROREG_FUNC(l_u8g2_DrawFrame)},
  972. { "DrawRBox", ROREG_FUNC(l_u8g2_DrawRBox)},
  973. { "DrawRFrame", ROREG_FUNC(l_u8g2_DrawRFrame)},
  974. { "DrawGlyph", ROREG_FUNC(l_u8g2_DrawGlyph)},
  975. { "DrawTriangle", ROREG_FUNC(l_u8g2_DrawTriangle)},
  976. { "SetBitmapMode",ROREG_FUNC(l_u8g2_SetBitmapMode)},
  977. { "DrawXBM", ROREG_FUNC(l_u8g2_DrawXBM)},
  978. { "DrawDrcode", ROREG_FUNC(l_u8g2_DrawDrcode)},
  979. { "SetContrast", ROREG_FUNC(l_u8g2_SetContrast)},
  980. { "CopyBuffer", ROREG_FUNC(l_u8g2_CopyBuffer)},
  981. { "SetPowerSave", ROREG_FUNC(l_u8g2_SetPowerSave)},
  982. #ifdef LUAT_USE_GTFONT
  983. { "drawGtfontGb2312", ROREG_FUNC(l_u8g2_draw_gtfont_gb2312)},
  984. #ifdef LUAT_USE_GTFONT_UTF8
  985. { "drawGtfontUtf8", ROREG_FUNC(l_u8g2_draw_gtfont_utf8)},
  986. #endif // LUAT_USE_GTFONT_UTF8
  987. #endif // LUAT_USE_GTFONT
  988. // 默认只带8号字体
  989. { "font_opposansm8", ROREG_PTR((void*)u8g2_font_opposansm8)},
  990. #ifdef USE_U8G2_OPPOSANSM_ENGLISH
  991. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  992. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  993. { "font_opposansm10", ROREG_PTR((void*)u8g2_font_opposansm10)},
  994. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  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_OPPOSANSM8_CHINESE
  1003. { "font_opposansm8_chinese", ROREG_PTR((void*)u8g2_font_opposansm8_chinese)},
  1004. #endif
  1005. #ifdef USE_U8G2_OPPOSANSM10_CHINESE
  1006. { "font_opposansm10_chinese", ROREG_PTR((void*)u8g2_font_opposansm10_chinese)},
  1007. #endif
  1008. #ifdef USE_U8G2_OPPOSANSM14_CHINESE
  1009. { "font_opposansm14_chinese", ROREG_PTR((void*)u8g2_font_opposansm14_chinese)},
  1010. #endif
  1011. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  1012. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  1013. #endif
  1014. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  1015. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  1016. #endif
  1017. #ifdef USE_U8G2_OPPOSANSM18_CHINESE
  1018. { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
  1019. #endif
  1020. #ifdef USE_U8G2_OPPOSANSM20_CHINESE
  1021. { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
  1022. #endif
  1023. #ifdef USE_U8G2_OPPOSANSM22_CHINESE
  1024. { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
  1025. #endif
  1026. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  1027. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  1028. #endif
  1029. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  1030. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  1031. #endif
  1032. #ifdef USE_U8G2_SARASA_ENGLISH
  1033. { "font_sarasa_m8_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m8_ascii)},
  1034. { "font_sarasa_m10_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m10_ascii)},
  1035. { "font_sarasa_m12_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m12_ascii)},
  1036. { "font_sarasa_m14_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m14_ascii)},
  1037. { "font_sarasa_m16_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m16_ascii)},
  1038. { "font_sarasa_m18_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m18_ascii)},
  1039. { "font_sarasa_m20_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m20_ascii)},
  1040. { "font_sarasa_m22_ascii", ROREG_PTR((void*)u8g2_font_sarasa_m22_ascii)},
  1041. //再大的很少用到先不加了
  1042. #endif
  1043. #ifdef USE_U8G2_SARASA_M8_CHINESE
  1044. { "font_sarasa_m8_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m8_chinese)},
  1045. #endif
  1046. #ifdef USE_U8G2_SARASA_M10_CHINESE
  1047. { "font_sarasa_m10_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m10_chinese)},
  1048. #endif
  1049. #ifdef USE_U8G2_SARASA_M12_CHINESE
  1050. { "font_sarasa_m12_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m12_chinese)},
  1051. #endif
  1052. #ifdef USE_U8G2_SARASA_M14_CHINESE
  1053. { "font_sarasa_m14_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m14_chinese)},
  1054. #endif
  1055. #ifdef USE_U8G2_SARASA_M16_CHINESE
  1056. { "font_sarasa_m16_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m16_chinese)},
  1057. #endif
  1058. #ifdef USE_U8G2_SARASA_M18_CHINESE
  1059. { "font_sarasa_m18_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m18_chinese)},
  1060. #endif
  1061. #ifdef USE_U8G2_SARASA_M20_CHINESE
  1062. { "font_sarasa_m20_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m20_chinese)},
  1063. #endif
  1064. #ifdef USE_U8G2_SARASA_M22_CHINESE
  1065. { "font_sarasa_m22_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m22_chinese)},
  1066. #endif
  1067. #ifdef USE_U8G2_SARASA_M24_CHINESE
  1068. { "font_sarasa_m24_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m24_chinese)},
  1069. #endif
  1070. #ifdef USE_U8G2_SARASA_M26_CHINESE
  1071. { "font_sarasa_m26_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m26_chinese)},
  1072. #endif
  1073. #ifdef USE_U8G2_SARASA_M28_CHINESE
  1074. { "font_sarasa_m28_chinese", ROREG_PTR((void*)u8g2_font_sarasa_m28_chinese)},
  1075. #endif
  1076. //@const DRAW_UPPER_RIGHT number 上右
  1077. { "DRAW_UPPER_RIGHT", ROREG_INT(U8G2_DRAW_UPPER_RIGHT)},
  1078. //@const DRAW_UPPER_LEFT number 上左
  1079. { "DRAW_UPPER_LEFT", ROREG_INT(U8G2_DRAW_UPPER_LEFT)},
  1080. //@const DRAW_LOWER_LEFT number 下左
  1081. { "DRAW_LOWER_LEFT", ROREG_INT(U8G2_DRAW_LOWER_LEFT)},
  1082. //@const DRAW_LOWER_RIGHT number 下右
  1083. { "DRAW_LOWER_RIGHT", ROREG_INT(U8G2_DRAW_LOWER_RIGHT)},
  1084. //@const DRAW_ALL number 全部
  1085. { "DRAW_ALL", ROREG_INT(U8G2_DRAW_ALL)},
  1086. //@const BTN_BW0 number 文本周围没有边框
  1087. { "BTN_BW0", ROREG_INT(U8G2_BTN_BW0)},
  1088. //@const BTN_BW1 number 文本周围的边框,1像素边框宽度
  1089. { "BTN_BW1", ROREG_INT(U8G2_BTN_BW1)},
  1090. //@const BTN_BW2 number 文本周围的边框,2像素边框宽度
  1091. { "BTN_BW2", ROREG_INT(U8G2_BTN_BW2)},
  1092. //@const BTN_BW3 number 文本周围的边框,3像素边框宽度
  1093. { "BTN_BW3", ROREG_INT(U8G2_BTN_BW3)},
  1094. //@const BTN_SHADOW0 number 启用阴影,与框架无间隙
  1095. { "BTN_SHADOW0", ROREG_INT(U8G2_BTN_SHADOW0)},
  1096. //@const BTN_SHADOW1 number 启用阴影,到帧的1像素间隙
  1097. { "BTN_SHADOW1", ROREG_INT(U8G2_BTN_SHADOW1)},
  1098. //@const BTN_SHADOW2 number 启用阴影,到帧的2像素间隙
  1099. { "BTN_SHADOW2", ROREG_INT(U8G2_BTN_SHADOW2)},
  1100. //@const BTN_INV number 反转文本
  1101. { "BTN_INV", ROREG_INT(U8G2_BTN_INV)},
  1102. //@const BTN_HCENTER number 将文本置于框架内的中心,并将参考位置更改为文本的中心
  1103. { "BTN_HCENTER", ROREG_INT(U8G2_BTN_HCENTER)},
  1104. //@const BTN_XFRAME number 在按钮周围绘制第二个1像素框
  1105. { "BTN_XFRAME", ROREG_INT(U8G2_BTN_XFRAME)},
  1106. { NULL, ROREG_INT(0)}
  1107. };
  1108. LUAMOD_API int luaopen_u8g2( lua_State *L ) {
  1109. lua_getglobal(L, "disp"); // disp库已经加载过u8g2库, 那就直接重用
  1110. if (lua_isuserdata(L, -1))
  1111. return 1;
  1112. luat_newlib2(L, reg_u8g2);
  1113. return 1;
  1114. }
  1115. //-------------------------------------------------------------------------------------------------
  1116. // 往下是一些U8G2方法的默认实现
  1117. uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1118. uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1119. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1120. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1121. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1122. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  1123. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf);
  1124. static const luat_u8g2_dev_reg_t* search_dev_reg(luat_u8g2_conf_t *conf, uint16_t spi_i2c) {
  1125. size_t dev_reg_index = 0;
  1126. while (devregs[dev_reg_index].name != NULL){
  1127. if (devregs[dev_reg_index].spi_i2c == spi_i2c && strcmp(devregs[dev_reg_index].name, conf->cname) == 0) {
  1128. return &devregs[dev_reg_index];
  1129. }
  1130. dev_reg_index ++;
  1131. }
  1132. return &devregs[0];
  1133. }
  1134. #ifndef LUAT_COMPILER_NOWEAK
  1135. LUAT_WEAK int luat_u8g2_setup(luat_u8g2_conf_t *conf) {
  1136. return luat_u8g2_setup_default(conf);
  1137. }
  1138. LUAT_WEAK uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1139. return u8x8_luat_gpio_and_delay_default(u8x8, msg, arg_int, arg_ptr);
  1140. }
  1141. LUAT_WEAK uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1142. return u8x8_luat_byte_hw_i2c_default(u8x8, msg, arg_int, arg_ptr);
  1143. }
  1144. LUAT_WEAK uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1145. return u8x8_luat_byte_4wire_hw_spi_default(u8x8, msg, arg_int, arg_ptr);
  1146. }
  1147. #endif
  1148. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf) {
  1149. u8g2_t* u8g2 = &conf->u8g2;
  1150. const luat_u8g2_dev_reg_t* devreg = NULL;
  1151. // LLOGD("pinType %d", pinType);
  1152. devreg = search_dev_reg(conf, pinType == 5?1:0);
  1153. if (devreg == NULL) {
  1154. LLOGD("unkown dev %s", conf->cname);
  1155. return -1;
  1156. }
  1157. // LLOGD("devreg name:%s spi_i2c:%d", devreg->name,devreg->spi_i2c);
  1158. if (pinType == 1) {
  1159. devreg->devcb(u8g2, conf->direction, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay_default);
  1160. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_I2C_CLOCK, i2c_scl);
  1161. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_I2C_DATA, i2c_sda);
  1162. }else if (pinType == 2) {
  1163. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_hw_i2c_default, u8x8_luat_gpio_and_delay_default);
  1164. //LLOGD("setup disp i2c.hw");
  1165. }else if (pinType == 5) {
  1166. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_4wire_hw_spi_default, u8x8_luat_gpio_and_delay_default);
  1167. 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);
  1168. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, spi_cs);
  1169. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, spi_dc);
  1170. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, spi_res);
  1171. } else {
  1172. LLOGI("no such u8g2 mode!!");
  1173. return -1;
  1174. }
  1175. u8g2_InitDisplay(u8g2);
  1176. #ifdef U8G2_USE_DYNAMIC_ALLOC
  1177. conf->buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  1178. u8g2_SetBufferPtr(u8g2, conf->buff_ptr);
  1179. #endif
  1180. u8g2_SetPowerSave(u8g2, 0);
  1181. return 0;
  1182. }
  1183. LUAT_WEAK int luat_u8g2_close(luat_u8g2_conf_t *conf) {
  1184. return 0;
  1185. }
  1186. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1187. static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes */
  1188. static uint8_t buf_idx;
  1189. uint8_t *data;
  1190. switch(msg)
  1191. {
  1192. case U8X8_MSG_BYTE_SEND:
  1193. data = (uint8_t *)arg_ptr;
  1194. while( arg_int > 0 )
  1195. {
  1196. buffer[buf_idx++] = *data;
  1197. data++;
  1198. arg_int--;
  1199. }
  1200. break;
  1201. case U8X8_MSG_BYTE_INIT:
  1202. luat_i2c_setup(i2c_id,i2c_speed);
  1203. break;
  1204. case U8X8_MSG_BYTE_SET_DC:
  1205. /* ignored for i2c */
  1206. break;
  1207. case U8X8_MSG_BYTE_START_TRANSFER:
  1208. buf_idx = 0;
  1209. break;
  1210. case U8X8_MSG_BYTE_END_TRANSFER:
  1211. luat_i2c_send(i2c_id, u8x8_GetI2CAddress(u8x8) >> 1, buffer, buf_idx,1);
  1212. break;
  1213. default:
  1214. return 0;
  1215. }
  1216. return 1;
  1217. }
  1218. int hw_spi_begin(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin )
  1219. {
  1220. luat_spi_t u8g2_spi = {0};
  1221. u8g2_spi.id = spi_id;
  1222. switch(spi_mode)
  1223. {
  1224. case 0: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 0; break;
  1225. case 1: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 0; break;
  1226. case 2: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 1; break;
  1227. case 3: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 1; break;
  1228. }
  1229. u8g2_spi.dataw = 8;
  1230. u8g2_spi.bit_dict = 1;
  1231. u8g2_spi.master = 1;
  1232. u8g2_spi.mode = 0;
  1233. u8g2_spi.bandrate = max_hz;
  1234. u8g2_spi.cs = -1;
  1235. LLOGI("spi_mode:%d bandrate:%d cs_pin:%d",spi_mode,max_hz,cs_pin);
  1236. luat_spi_setup(&u8g2_spi);
  1237. luat_gpio_mode(spi_res,Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1238. luat_gpio_mode(spi_dc,Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1239. return 0;
  1240. }
  1241. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  1242. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1243. // LLOGI("u8x8_luat_byte_4wire_hw_spi_default msg:%d arg_int:%d",msg,arg_int);
  1244. switch(msg)
  1245. {
  1246. case U8X8_MSG_BYTE_SEND:
  1247. luat_spi_send(spi_id, (const char*)arg_ptr, arg_int);
  1248. break;
  1249. case U8X8_MSG_BYTE_INIT:
  1250. /* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  1251. /* 0: clock active high, data out on falling edge, clock default value is zero, takover on rising edge */
  1252. /* 1: clock active high, data out on rising edge, clock default value is zero, takover on falling edge */
  1253. /* 2: clock active low, data out on rising edge */
  1254. /* 3: clock active low, data out on falling edge */
  1255. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1256. hw_spi_begin(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]);
  1257. break;
  1258. case U8X8_MSG_BYTE_SET_DC:
  1259. u8x8_gpio_SetDC(u8x8, arg_int);
  1260. break;
  1261. case U8X8_MSG_BYTE_START_TRANSFER:
  1262. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  1263. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  1264. break;
  1265. case U8X8_MSG_BYTE_END_TRANSFER:
  1266. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  1267. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1268. break;
  1269. default:
  1270. return 0;
  1271. }
  1272. return 1;
  1273. }
  1274. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1275. {
  1276. uint8_t i;
  1277. switch(msg)
  1278. {
  1279. case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
  1280. luat_timer_us_delay(1);
  1281. break;
  1282. case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  1283. luat_timer_us_delay(1);
  1284. break;
  1285. case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  1286. luat_timer_us_delay(1);
  1287. break;
  1288. case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  1289. luat_timer_mdelay(arg_int);
  1290. break;
  1291. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  1292. // Function which implements a delay, arg_int contains the amount of ms
  1293. // set spi pin mode
  1294. if (pinType == 1){
  1295. // set i2c pin mode
  1296. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1297. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1298. }else if (pinType == 3){
  1299. 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
  1300. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1301. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1302. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1303. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1304. }else if (pinType == 4){
  1305. 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
  1306. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1307. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1308. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1309. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1310. }else if (pinType == 5){
  1311. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1312. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1313. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1314. }else if (pinType == 6){
  1315. // set 8080 pin mode
  1316. luat_gpio_mode(u8x8->pins[U8X8_PIN_D0],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1317. luat_gpio_mode(u8x8->pins[U8X8_PIN_D1],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1318. luat_gpio_mode(u8x8->pins[U8X8_PIN_D2],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1319. luat_gpio_mode(u8x8->pins[U8X8_PIN_D3],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1320. luat_gpio_mode(u8x8->pins[U8X8_PIN_D4],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1321. luat_gpio_mode(u8x8->pins[U8X8_PIN_D5],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1322. luat_gpio_mode(u8x8->pins[U8X8_PIN_D6],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1323. luat_gpio_mode(u8x8->pins[U8X8_PIN_D7],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1324. luat_gpio_mode(u8x8->pins[U8X8_PIN_E],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1325. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1326. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1327. }
  1328. // // set menu pin mode
  1329. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_HOME],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1330. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_SELECT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1331. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_PREV],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1332. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_NEXT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1333. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_UP],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1334. // luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_DOWN],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1335. // // set value
  1336. // luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],Luat_GPIO_HIGH);
  1337. // luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_HIGH);
  1338. // luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_HIGH);
  1339. // luat_gpio_set(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_HIGH);
  1340. // luat_gpio_set(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_HIGH);
  1341. break;
  1342. case U8X8_MSG_DELAY_I2C:
  1343. // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  1344. // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  1345. if (arg_int<=2) {
  1346. luat_timer_us_delay(5);
  1347. }
  1348. else {
  1349. luat_timer_us_delay(2);
  1350. }
  1351. break;
  1352. //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
  1353. //case U8X8_MSG_GPIO_SPI_CLOCK:
  1354. //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
  1355. //case U8X8_MSG_GPIO_SPI_DATA:
  1356. case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
  1357. luat_gpio_set(u8x8->pins[U8X8_PIN_D2],arg_int);
  1358. break;
  1359. case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
  1360. luat_gpio_set(u8x8->pins[U8X8_PIN_D3],arg_int);
  1361. break;
  1362. case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
  1363. luat_gpio_set(u8x8->pins[U8X8_PIN_D4],arg_int);
  1364. break;
  1365. case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
  1366. luat_gpio_set(u8x8->pins[U8X8_PIN_D5],arg_int);
  1367. break;
  1368. case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
  1369. luat_gpio_set(u8x8->pins[U8X8_PIN_D6],arg_int);
  1370. break;
  1371. case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
  1372. luat_gpio_set(u8x8->pins[U8X8_PIN_D7],arg_int);
  1373. break;
  1374. case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
  1375. luat_gpio_set(u8x8->pins[U8X8_PIN_E],arg_int);
  1376. break;
  1377. case U8X8_MSG_GPIO_I2C_CLOCK:
  1378. // arg_int=0: Output low at I2C clock pin
  1379. // arg_int=1: Input dir with pullup high for I2C clock pin
  1380. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],arg_int);
  1381. break;
  1382. case U8X8_MSG_GPIO_I2C_DATA:
  1383. // arg_int=0: Output low at I2C data pin
  1384. // arg_int=1: Input dir with pullup high for I2C data pin
  1385. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],arg_int);
  1386. break;
  1387. case U8X8_MSG_GPIO_SPI_CLOCK:
  1388. //Function to define the logic level of the clockline
  1389. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],arg_int);
  1390. break;
  1391. case U8X8_MSG_GPIO_SPI_DATA:
  1392. //Function to define the logic level of the data line to the display
  1393. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],arg_int);
  1394. break;
  1395. case U8X8_MSG_GPIO_CS:
  1396. // Function to define the logic level of the CS line
  1397. luat_gpio_set(u8x8->pins[U8X8_PIN_CS],arg_int);
  1398. break;
  1399. case U8X8_MSG_GPIO_DC:
  1400. //Function to define the logic level of the Data/ Command line
  1401. luat_gpio_set(u8x8->pins[U8X8_PIN_DC],arg_int);
  1402. break;
  1403. case U8X8_MSG_GPIO_RESET:
  1404. //Function to define the logic level of the RESET line
  1405. luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],arg_int);
  1406. break;
  1407. default:
  1408. //A message was received which is not implemented, return 0 to indicate an error
  1409. if ( msg >= U8X8_MSG_GPIO(0) )
  1410. {
  1411. i = u8x8_GetPinValue(u8x8, msg);
  1412. if ( i != U8X8_PIN_NONE )
  1413. {
  1414. if ( u8x8_GetPinIndex(u8x8, msg) < U8X8_PIN_OUTPUT_CNT )
  1415. {
  1416. luat_gpio_set(i, arg_int);
  1417. }
  1418. else
  1419. {
  1420. if ( u8x8_GetPinIndex(u8x8, msg) == U8X8_PIN_OUTPUT_CNT )
  1421. {
  1422. // call yield() for the first pin only, u8x8 will always request all the pins, so this should be ok
  1423. // yield();
  1424. }
  1425. u8x8_SetGPIOResult(u8x8, luat_gpio_get(i) == 0 ? 0 : 1);
  1426. }
  1427. }
  1428. break;
  1429. }
  1430. return 0;
  1431. }
  1432. return 1;
  1433. }