luat_lib_u8g2.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /*
  2. @module u8g2
  3. @summary u8g2图形处理库
  4. @author Dozingfiretruck
  5. @version 1.0
  6. @date 2021.01.25
  7. @demo u8g2
  8. */
  9. #include "luat_base.h"
  10. #include "luat_malloc.h"
  11. #include "luat_u8g2.h"
  12. #include "luat_gpio.h"
  13. #include "luat_timer.h"
  14. #include "luat_i2c.h"
  15. #include "luat_spi.h"
  16. #include "qrcodegen.h"
  17. #include "u8g2.h"
  18. #include "u8g2_luat_fonts.h"
  19. #define LUAT_LOG_TAG "u8g2"
  20. #include "luat_log.h"
  21. static u8g2_t* u8g2 = NULL;
  22. static int u8g2_lua_ref = 0;
  23. static uint8_t i2c_id;
  24. static uint8_t i2c_speed;
  25. static uint8_t i2c_scl;
  26. static uint8_t i2c_sda;
  27. // static uint8_t i2c_addr = 0x3C;
  28. static uint8_t spi_id;
  29. static uint8_t spi_res;
  30. static uint8_t spi_dc;
  31. static uint8_t spi_cs;
  32. static uint8_t * buff_ptr = NULL;
  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. /*
  41. u8g2显示屏初始化
  42. @api u8g2.begin(conf)
  43. @table conf 配置信息
  44. @return int 正常初始化1,已经初始化过2,内存不够3,初始化失败返回4
  45. @usage
  46. -- 初始化硬件i2c的ssd1306
  47. u8g2.begin({ic = "ssd1306",direction = 0,mode="i2c_hw",i2c_id=0}) -- direction 可选0 90 180 270
  48. -- 初始化硬件spi的ssd1306
  49. 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
  50. -- 初始化软件i2c的ssd1306
  51. u8g2.begin({ic = "ssd1306",direction = 0,mode="i2c_sw", i2c_scl=1, i2c_sda=4}) -- 通过PA1 SCL / PA4 SDA模拟
  52. */
  53. static int l_u8g2_begin(lua_State *L) {
  54. if (u8g2 != NULL) {
  55. LLOGW("disp is aready inited");
  56. lua_pushinteger(L, 2);
  57. return 1;
  58. }
  59. u8g2 = (u8g2_t*)lua_newuserdata(L, sizeof(u8g2_t));
  60. if (u8g2 == NULL) {
  61. LLOGE("lua_newuserdata return NULL, out of memory ?!");
  62. lua_pushinteger(L, 3);
  63. return 1;
  64. }
  65. luat_u8g2_conf_t conf = {0};
  66. conf.pinType = 2; // I2C 硬件(或者是个假硬件)
  67. conf.ptr = u8g2;
  68. conf.direction = U8G2_R0;
  69. char mode[8] = {0};
  70. size_t mode_len = 0;
  71. if (lua_istable(L, 1)) {
  72. // 参数解析
  73. lua_pushliteral(L, "ic");
  74. lua_gettable(L, 1);
  75. if (lua_isstring(L, -1)) {
  76. conf.cname = (char*)luaL_checkstring(L, -1);
  77. //LLOGD("using ic: %s",conf.cname);
  78. }
  79. lua_pop(L, 1);
  80. lua_pushliteral(L, "direction");
  81. lua_gettable(L, 1);
  82. if (lua_isinteger(L, -1)) {
  83. int direction = luaL_checkinteger(L, -1);
  84. switch (direction)
  85. {
  86. case 0:
  87. conf.direction = U8G2_R0;
  88. break;
  89. case 90:
  90. conf.direction = U8G2_R1;
  91. break;
  92. case 180:
  93. conf.direction = U8G2_R2;
  94. break;
  95. case 270:
  96. conf.direction = U8G2_R3;
  97. break;
  98. default:
  99. conf.direction = U8G2_R0;
  100. break;
  101. }
  102. }
  103. lua_pop(L, 1);
  104. lua_pushliteral(L, "mode");
  105. lua_gettable(L, 1);
  106. if (!lua_isstring(L, -1)) {
  107. LLOGE("need mode!!!");
  108. return 0;
  109. }
  110. const char* tmp = luaL_checklstring(L, -1, &mode_len);
  111. if (mode_len < 1 || mode_len > 7) {
  112. LLOGE("mode string too short or too long!!");
  113. return 0;
  114. }
  115. memcpy(mode, tmp, strlen(tmp));
  116. lua_pop(L, 1);
  117. conf.pinType = 255;
  118. for (size_t i = 0; i < sizeof(mode_strs) / sizeof(const char*); i++)
  119. {
  120. if (strcmp(mode_strs[i], tmp) == 0) {
  121. conf.pinType = i + 1;
  122. break;
  123. }
  124. }
  125. if (conf.pinType == 255) {
  126. LLOGE("no such mode [%s]", tmp);
  127. return 0;
  128. }
  129. lua_pushliteral(L, "i2c_scl");
  130. lua_gettable(L, 1);
  131. if (lua_isinteger(L, -1)) {
  132. i2c_scl = luaL_checkinteger(L, -1);
  133. }
  134. lua_pop(L, 1);
  135. lua_pushliteral(L, "i2c_sda");
  136. lua_gettable(L, 1);
  137. if (lua_isinteger(L, -1)) {
  138. i2c_sda = luaL_checkinteger(L, -1);
  139. }
  140. lua_pop(L, 1);
  141. lua_pushliteral(L, "i2c_id");
  142. lua_gettable(L, 1);
  143. if (lua_isinteger(L, -1)) {
  144. i2c_id = luaL_checkinteger(L, -1);
  145. }
  146. lua_pop(L, 1);
  147. lua_pushliteral(L, "i2c_speed");
  148. lua_gettable(L, 1);
  149. if (lua_isinteger(L, -1)) {
  150. i2c_speed = luaL_checkinteger(L, -1);
  151. }
  152. lua_pop(L, 1);
  153. lua_pushliteral(L, "spi_id");
  154. lua_gettable(L, 1);
  155. if (lua_isinteger(L, -1)) {
  156. spi_id = luaL_checkinteger(L, -1);
  157. }
  158. lua_pop(L, 1);
  159. lua_pushliteral(L, "spi_res");
  160. lua_gettable(L, 1);
  161. if (lua_isinteger(L, -1)) {
  162. spi_res = luaL_checkinteger(L, -1);
  163. }
  164. lua_pop(L, 1);
  165. lua_pushliteral(L, "spi_dc");
  166. lua_gettable(L, 1);
  167. if (lua_isinteger(L, -1)) {
  168. spi_dc = luaL_checkinteger(L, -1);
  169. }
  170. lua_pop(L, 1);
  171. lua_pushliteral(L, "spi_cs");
  172. lua_gettable(L, 1);
  173. if (lua_isinteger(L, -1)) {
  174. spi_cs = luaL_checkinteger(L, -1);
  175. }
  176. lua_pop(L, 1);
  177. }
  178. LLOGD("driver %s mode %s", conf.cname, mode);
  179. if (luat_u8g2_setup(&conf)) {
  180. u8g2 = NULL;
  181. LLOGW("disp init fail");
  182. lua_pushinteger(L, 4);
  183. return 1; // 初始化失败
  184. }
  185. LLOGD("setup done");
  186. u8g2_lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  187. u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr); // 设置默认字体
  188. lua_pushinteger(L, 1);
  189. return 1;
  190. }
  191. /*
  192. 关闭显示屏
  193. @api u8g2.close()
  194. @usage
  195. -- 关闭disp,再次使用disp相关API的话,需要重新初始化
  196. u8g2.close()
  197. */
  198. static int l_u8g2_close(lua_State *L) {
  199. if (u8g2_lua_ref != 0) {
  200. lua_geti(L, LUA_REGISTRYINDEX, u8g2_lua_ref);
  201. if (lua_isuserdata(L, -1)) {
  202. luaL_unref(L, LUA_REGISTRYINDEX, u8g2_lua_ref);
  203. }
  204. u8g2_lua_ref = 0;
  205. }
  206. // buff也得释放掉
  207. if (buff_ptr != NULL) {
  208. luat_heap_free(buff_ptr);
  209. buff_ptr = NULL;
  210. }
  211. lua_gc(L, LUA_GCCOLLECT, 0);
  212. lua_gc(L, LUA_GCCOLLECT, 0);
  213. u8g2 = NULL;
  214. return 0;
  215. }
  216. /*
  217. 清屏,清除内存帧缓冲区中的所有像素
  218. @api u8g2.ClearBuffer()
  219. @usage
  220. -- 清屏
  221. u8g2.ClearBuffer()
  222. */
  223. static int l_u8g2_ClearBuffer(lua_State *L) {
  224. if (u8g2 == NULL) return 0;
  225. u8g2_ClearBuffer(u8g2);
  226. return 0;
  227. }
  228. /*
  229. 将数据更新到屏幕,将存储器帧缓冲区的内容发送到显示器
  230. @api u8g2.SendBuffer()
  231. @usage
  232. -- 把显示数据更新到屏幕
  233. u8g2.SendBuffer()
  234. */
  235. static int l_u8g2_SendBuffer(lua_State *L) {
  236. if (u8g2 == NULL) return 0;
  237. u8g2_SendBuffer(u8g2);
  238. return 0;
  239. }
  240. /*
  241. 在显示屏上画一段文字,在显示屏上画一段文字,要调用u8g2.SendBuffer()才会更新到屏幕
  242. @api u8g2.DrawUTF8(str, x, y)
  243. @string 文件内容
  244. @int 横坐标
  245. @int 竖坐标
  246. @usage
  247. u8g2.DrawUTF8("wifi is ready", 10, 20)
  248. */
  249. static int l_u8g2_DrawUTF8(lua_State *L) {
  250. if (u8g2 == NULL) {
  251. LLOGW("disp not init yet!!!");
  252. return 0;
  253. }
  254. size_t len;
  255. size_t x, y;
  256. const char* str = luaL_checklstring(L, 1, &len);
  257. x = luaL_checkinteger(L, 2);
  258. y = luaL_checkinteger(L, 3);
  259. u8g2_DrawUTF8(u8g2, x, y, str);
  260. return 0;
  261. }
  262. /*
  263. 设置字体模式
  264. @api u8g2.SetFontMode(mode)
  265. @int mode字体模式,启用(1)或禁用(0)透明模式
  266. @usage
  267. u8g2.SetFontMode(1)
  268. */
  269. static int l_u8g2_SetFontMode(lua_State *L){
  270. if (u8g2 == NULL) return 0;
  271. int font_mode = luaL_checkinteger(L, 1);
  272. if (font_mode < 0) {
  273. lua_pushboolean(L, 0);
  274. }
  275. u8g2_SetFontMode(u8g2, font_mode);
  276. u8g2_SetFontDirection(u8g2, 0);
  277. lua_pushboolean(L, 1);
  278. return 1;
  279. }
  280. /*
  281. 设置字体
  282. @api u8g2.SetFont(font)
  283. @userdata font, u8g2.font_opposansm8 为纯英文8号字体,还有font_opposansm10 font_opposansm12 font_opposansm16 font_opposansm18 font_opposansm20 font_opposansm22 font_opposansm24 font_opposansm32 可选 u8g2.font_opposansm12_chinese 为12x12全中文,还有 font_opposansm16_chinese font_opposansm24_chinese font_opposansm32_chinese 可选, u8g2.font_unifont_t_symbols 为符号.
  284. @usage
  285. -- 设置为中文字体,对之后的drawStr有效,使用中文字体需在 luat_conf_bsp.h开启#define USE_U8G2_OPPOSANSMxx_CHINESE xx为字号 之后重新编译固件
  286. u8g2.SetFont(u8g2.font_opposansm12)
  287. */
  288. static int l_u8g2_SetFont(lua_State *L) {
  289. if (u8g2 == NULL) {
  290. LLOGI("u8g2 not init yet!!!");
  291. lua_pushboolean(L, 0);
  292. return 1;
  293. }
  294. if (!lua_islightuserdata(L, 1)) {
  295. LLOGE("no such font");
  296. return 0;
  297. }
  298. const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
  299. if (ptr == NULL) {
  300. LLOGE("only font pointer is allow");
  301. return 0;
  302. }
  303. u8g2_SetFont(u8g2, ptr);
  304. lua_pushboolean(L, 1);
  305. return 1;
  306. }
  307. /*
  308. 获取显示屏高度
  309. @api u8g2.GetDisplayHeight()
  310. @return int 显示屏高度
  311. @usage
  312. u8g2.GetDisplayHeight()
  313. */
  314. static int l_u8g2_GetDisplayHeight(lua_State *L){
  315. if (u8g2 == NULL) return 0;
  316. lua_pushinteger(L, u8g2_GetDisplayHeight(u8g2));
  317. return 1;
  318. }
  319. /*
  320. 获取显示屏宽度
  321. @api u8g2.GetDisplayWidth()
  322. @return int 显示屏宽度
  323. @usage
  324. u8g2.GetDisplayWidth()
  325. */
  326. static int l_u8g2_GetDisplayWidth(lua_State *L){
  327. if (u8g2 == NULL) return 0;
  328. lua_pushinteger(L, u8g2_GetDisplayWidth(u8g2));
  329. return 1;
  330. }
  331. /*
  332. 为所有绘图功能分配绘图颜色。
  333. @api u8g2.SetDrawColor(c)
  334. @int c为颜色值 0没有色 1有色 2与底色xor
  335. @usage
  336. u8g2.SetDrawColor(0)
  337. */
  338. static int l_u8g2_SetDrawColor(lua_State *L){
  339. if (u8g2 == NULL) return 0;
  340. u8g2_SetDrawColor(u8g2,luaL_checkinteger(L, 1));
  341. return 0;
  342. }
  343. /*
  344. 画一个点.
  345. @api u8g2.DrawPixel(x,y)
  346. @int X位置.
  347. @int Y位置.
  348. @usage
  349. u8g2.DrawPixel(20, 5)
  350. */
  351. static int l_u8g2_DrawPixel(lua_State *L){
  352. if (u8g2 == NULL) return 0;
  353. u8g2_DrawPixel(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2));
  354. return 0;
  355. }
  356. /*
  357. 在两点之间画一条线.
  358. @api u8g2.DrawLine(x0,y0,x1,y1)
  359. @int 第一个点的X位置.
  360. @int 第一个点的Y位置.
  361. @int 第二个点的X位置.
  362. @int 第二个点的Y位置.
  363. @usage
  364. u8g2.DrawLine(20, 5, 5, 32)
  365. */
  366. static int l_u8g2_DrawLine(lua_State *L){
  367. if (u8g2 == NULL) return 0;
  368. u8g2_DrawLine(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  369. return 0;
  370. }
  371. /*
  372. 在x,y位置画一个半径为rad的空心圆.
  373. @api u8g2.DrawCircle(x0,y0,rad,opt)
  374. @int 圆心位置
  375. @int 圆心位置
  376. @int 圆半径.
  377. @int 选择圆的部分或全部.
  378. 右上: 0x01
  379. 左上: 0x02
  380. 左下: 0x04
  381. 右下: 0x08
  382. 完整圆: (0x01|0x02|0x04|0x08)
  383. @usage
  384. u8g2.DrawCircle(60,30,8,15)
  385. */
  386. static int l_u8g2_DrawCircle(lua_State *L){
  387. if (u8g2 == NULL) return 0;
  388. u8g2_DrawCircle(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  389. return 0;
  390. }
  391. /*
  392. 在x,y位置画一个半径为rad的实心圆.
  393. @api u8g2.DrawDisc(x0,y0,rad,opt)
  394. @int 圆心位置
  395. @int 圆心位置
  396. @int 圆半径.
  397. @int 选择圆的部分或全部.
  398. 右上: 0x01
  399. 左上: 0x02
  400. 左下: 0x04
  401. 右下: 0x08
  402. 完整圆: (0x01|0x02|0x04|0x08)
  403. @usage
  404. u8g2.DrawDisc(60,30,8,15)
  405. */
  406. static int l_u8g2_DrawDisc(lua_State *L){
  407. if (u8g2 == NULL) return 0;
  408. u8g2_DrawDisc(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  409. return 0;
  410. }
  411. /*
  412. 在x,y位置画一个半径为rad的空心椭圆.
  413. @api u8g2.DrawEllipse(x0,y0,rx,ry,opt)
  414. @int 圆心位置
  415. @int 圆心位置
  416. @int 椭圆大小
  417. @int 椭圆大小
  418. @int 选择圆的部分或全部.
  419. 右上: 0x01
  420. 左上: 0x02
  421. 左下: 0x04
  422. 右下: 0x08
  423. 完整圆: (0x01|0x02|0x04|0x08)
  424. @usage
  425. u8g2.DrawEllipse(60,30,8,15)
  426. */
  427. static int l_u8g2_DrawEllipse(lua_State *L){
  428. if (u8g2 == NULL) return 0;
  429. u8g2_DrawEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  430. return 0;
  431. }
  432. /*
  433. 在x,y位置画一个半径为rad的实心椭圆.
  434. @api u8g2.DrawFilledEllipse(x0,y0,rx,ry,opt)
  435. @int 圆心位置
  436. @int 圆心位置
  437. @int 椭圆大小
  438. @int 椭圆大小
  439. @int 选择圆的部分或全部.
  440. 右上: 0x01
  441. 左上: 0x02
  442. 左下: 0x04
  443. 右下: 0x08
  444. 完整圆: (0x01|0x02|0x04|0x08)
  445. @usage
  446. u8g2.DrawFilledEllipse(60,30,8,15)
  447. */
  448. static int l_u8g2_DrawFilledEllipse(lua_State *L){
  449. if (u8g2 == NULL) return 0;
  450. u8g2_DrawFilledEllipse(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  451. return 0;
  452. }
  453. /*
  454. 从x / y位置(左上边缘)开始绘制一个框(填充的框).
  455. @api u8g2.DrawBox(x,y,w,h)
  456. @int 左上边缘的X位置
  457. @int 左上边缘的Y位置
  458. @int 盒子的宽度
  459. @int 盒子的高度
  460. @usage
  461. u8g2.DrawBox(3,7,25,15)
  462. */
  463. static int l_u8g2_DrawBox(lua_State *L){
  464. if (u8g2 == NULL) return 0;
  465. u8g2_DrawBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  466. return 0;
  467. }
  468. /*
  469. 从x / y位置(左上边缘)开始绘制一个框(空框).
  470. @api u8g2.DrawFrame(x,y,w,h)
  471. @int 左上边缘的X位置
  472. @int 左上边缘的Y位置
  473. @int 盒子的宽度
  474. @int 盒子的高度
  475. @usage
  476. u8g2.DrawFrame(3,7,25,15)
  477. */
  478. static int l_u8g2_DrawFrame(lua_State *L){
  479. if (u8g2 == NULL) return 0;
  480. u8g2_DrawFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4));
  481. return 0;
  482. }
  483. /*
  484. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的填充框/框架.
  485. @api u8g2.DrawRBox(x,y,w,h,r)
  486. @int 左上边缘的X位置
  487. @int 左上边缘的Y位置
  488. @int 盒子的宽度
  489. @int 盒子的高度
  490. @int 四个边缘的半径
  491. @usage
  492. u8g2.DrawRBox(3,7,25,15)
  493. */
  494. static int l_u8g2_DrawRBox(lua_State *L){
  495. if (u8g2 == NULL) return 0;
  496. u8g2_DrawRBox(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  497. return 0;
  498. }
  499. /*
  500. 绘制一个从x / y位置(左上边缘)开始具有圆形边缘的空框/框架.
  501. @api u8g2.DrawRFrame(x,y,w,h,r)
  502. @int 左上边缘的X位置
  503. @int 左上边缘的Y位置
  504. @int 盒子的宽度
  505. @int 盒子的高度
  506. @int 四个边缘的半径
  507. @usage
  508. u8g2.DrawRFrame(3,7,25,15)
  509. */
  510. static int l_u8g2_DrawRFrame(lua_State *L){
  511. if (u8g2 == NULL) return 0;
  512. u8g2_DrawRFrame(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5));
  513. return 0;
  514. }
  515. /*
  516. 绘制一个图形字符。字符放置在指定的像素位置x和y.
  517. @api u8g2.DrawGlyph(x,y,encoding)
  518. @int 字符在显示屏上的位置
  519. @int 字符在显示屏上的位置
  520. @int 字符的Unicode值
  521. @usage
  522. u8g2.SetFont(u8g2_font_unifont_t_symbols)
  523. u8g2.DrawGlyph(5, 20, 0x2603) -- dec 9731/hex 2603 Snowman
  524. */
  525. static int l_u8g2_DrawGlyph(lua_State *L){
  526. if (u8g2 == NULL) return 0;
  527. u8g2_DrawGlyph(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3));
  528. return 0;
  529. }
  530. /*
  531. 绘制一个三角形(实心多边形).
  532. @api u8g2.DrawTriangle(x0,y0,x1,y1,x2,y2)
  533. @int 点0X位置
  534. @int 点0Y位置
  535. @int 点1X位置
  536. @int 点1Y位置
  537. @int 点2X位置
  538. @int 点2Y位置
  539. @usage
  540. u8g2.DrawTriangle(20,5, 27,50, 5,32)
  541. */
  542. static int l_u8g2_DrawTriangle(lua_State *L){
  543. if (u8g2 == NULL) return 0;
  544. u8g2_DrawTriangle(u8g2,luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),luaL_checkinteger(L, 3),luaL_checkinteger(L, 4),luaL_checkinteger(L, 5),luaL_checkinteger(L, 6));
  545. return 0;
  546. }
  547. /*
  548. 定义位图函数是否将写入背景色
  549. @api u8g2.SetBitmapMode(mode)
  550. @int mode字体模式,启用(1)或禁用(0)透明模式
  551. @usage
  552. u8g2.SetBitmapMode(1)
  553. */
  554. static int l_u8g2_SetBitmapMode(lua_State *L){
  555. if (u8g2 == NULL) return 0;
  556. u8g2_SetBitmapMode(u8g2,luaL_checkinteger(L, 1));
  557. return 0;
  558. }
  559. /*
  560. 绘制位图
  561. @api u8g2.DrawXBM(x, y, w, h, data)
  562. @int X坐标
  563. @int y坐标
  564. @int 位图宽
  565. @int 位图高
  566. @int 位图数据,每一位代表一个像素
  567. @usage
  568. -- 取模使用PCtoLCD2002软件即可
  569. -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
  570. u8g2.DrawXBM(0, 0, 16,16, string.char(
  571. 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
  572. 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
  573. ))
  574. */
  575. static int l_u8g2_DrawXBM(lua_State *L){
  576. if (u8g2 == NULL) return 0;
  577. int x = luaL_checkinteger(L, 1);
  578. int y = luaL_checkinteger(L, 2);
  579. int w = luaL_checkinteger(L, 3);
  580. int h = luaL_checkinteger(L, 4);
  581. size_t len = 0;
  582. const char* data = luaL_checklstring(L, 5, &len);
  583. if (h < 1) return 0; // 行数必须大于0
  584. if (len*8/h < w) return 0; // 起码要填满一行
  585. if (len != h*w/8)return 0;
  586. u8g2_DrawXBM(u8g2, x, y, w, h, (const uint8_t*)data);
  587. lua_pushboolean(L, 1);
  588. return 1;
  589. }
  590. /**
  591. 缓冲区绘制QRCode
  592. @api u8g2.DrawDrcode(x, y, str, size)
  593. @int x坐标
  594. @int y坐标
  595. @string 二维码的内容
  596. @int 可选,显示大小,不可小于21,默认21
  597. @return nil 无返回值
  598. */
  599. static int l_u8g2_DrawDrcode(lua_State *L)
  600. {
  601. size_t len;
  602. int x = luaL_checkinteger(L, 1);
  603. int y = luaL_checkinteger(L, 2);
  604. const char* text = luaL_checklstring(L, 3, &len);
  605. int size = luaL_optinteger(L, 4,21);
  606. uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  607. uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
  608. if (qrcode == NULL || tempBuffer == NULL) {
  609. if (qrcode)
  610. luat_heap_free(qrcode);
  611. if (tempBuffer)
  612. luat_heap_free(tempBuffer);
  613. LLOGE("qrcode out of memory");
  614. return 0;
  615. }
  616. bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_MEDIUM,
  617. qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
  618. if (ok){
  619. int qr_size = qrcodegen_getSize(qrcode);
  620. int scale = size / qr_size ;
  621. int margin = (size - qr_size * scale) / 2;
  622. x+=margin;
  623. y+=margin;
  624. for (int j = 0; j < qr_size; j++) {
  625. for (int i = 0; i < qr_size; i++) {
  626. if (qrcodegen_getModule(qrcode, i, j))
  627. u8g2_DrawBox(u8g2,x+i*scale,y+j*scale,scale,scale);
  628. }
  629. }
  630. }
  631. if (qrcode)
  632. luat_heap_free(qrcode);
  633. if (tempBuffer)
  634. luat_heap_free(tempBuffer);
  635. return 0;
  636. }
  637. #ifdef LUAT_USE_GTFONT
  638. #include "GT5SLCD2E_1A.h"
  639. extern void gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int widt,unsigned int high,int(*point)(void*),void* userdata,int mode);
  640. 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);
  641. static int gtfont_u8g2_DrawPixel(u8g2_t *u8g2, uint16_t x, uint16_t y,uint32_t color){
  642. u8g2_DrawPixel(u8g2,x, y);
  643. return 1;
  644. }
  645. /*
  646. 使用gtfont显示gb2312字符串
  647. @api u8g2.drawGtfontGb2312(str,size,x,y)
  648. @string str 显示字符串
  649. @int size 字体大小 (支持16-192号大小字体)
  650. @int x 横坐标
  651. @int y 竖坐标
  652. @usage
  653. u8g2.drawGtfontGb2312("啊啊啊",32,0,0)
  654. */
  655. static int l_u8g2_draw_gtfont_gb2312(lua_State *L) {
  656. unsigned char buf[128];
  657. int len;
  658. int i = 0;
  659. uint8_t strhigh,strlow ;
  660. uint16_t str;
  661. const char *fontCode = luaL_checklstring(L, 1,&len);
  662. unsigned char size = luaL_checkinteger(L, 2);
  663. int x = luaL_checkinteger(L, 3);
  664. int y = luaL_checkinteger(L, 4);
  665. while ( i < len){
  666. strhigh = *fontCode;
  667. fontCode++;
  668. strlow = *fontCode;
  669. str = (strhigh<<8)|strlow;
  670. fontCode++;
  671. get_font(buf, 1, str, size, size, size);
  672. gtfont_draw_w(buf , x ,y , size , size,gtfont_u8g2_DrawPixel,u8g2,2);
  673. x+=size;
  674. i+=2;
  675. }
  676. return 0;
  677. }
  678. #ifdef LUAT_USE_GTFONT_UTF8
  679. extern unsigned short unicodetogb2312 ( unsigned short chr);
  680. static uint8_t utf8_state;
  681. static uint16_t encoding;
  682. static uint16_t utf8_next(uint8_t b)
  683. {
  684. if ( b == 0 ) /* '\n' terminates the string to support the string list procedures */
  685. return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
  686. if ( utf8_state == 0 )
  687. {
  688. if ( b >= 0xfc ) /* 6 byte sequence */
  689. {
  690. utf8_state = 5;
  691. b &= 1;
  692. }
  693. else if ( b >= 0xf8 )
  694. {
  695. utf8_state = 4;
  696. b &= 3;
  697. }
  698. else if ( b >= 0xf0 )
  699. {
  700. utf8_state = 3;
  701. b &= 7;
  702. }
  703. else if ( b >= 0xe0 )
  704. {
  705. utf8_state = 2;
  706. b &= 15;
  707. }
  708. else if ( b >= 0xc0 )
  709. {
  710. utf8_state = 1;
  711. b &= 0x01f;
  712. }
  713. else
  714. {
  715. /* do nothing, just use the value as encoding */
  716. return b;
  717. }
  718. encoding = b;
  719. return 0x0fffe;
  720. }
  721. else
  722. {
  723. utf8_state--;
  724. /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
  725. encoding<<=6;
  726. b &= 0x03f;
  727. encoding |= b;
  728. if ( utf8_state != 0 )
  729. return 0x0fffe; /* nothing to do yet */
  730. }
  731. return encoding;
  732. }
  733. /*
  734. 使用gtfont显示UTF8字符串
  735. @api u8g2.drawGtfontUtf8(str,size,x,y)
  736. @string str 显示字符串
  737. @int size 字体大小 (支持16-192号大小字体)
  738. @int x 横坐标
  739. @int y 竖坐标
  740. @usage
  741. u8g2.drawGtfontUtf8("啊啊啊",32,0,0)
  742. */
  743. static int l_u8g2_draw_gtfont_utf8(lua_State *L) {
  744. unsigned char buf[128];
  745. int len;
  746. int i = 0;
  747. uint8_t strhigh,strlow ;
  748. uint16_t e,str;
  749. const char *fontCode = luaL_checklstring(L, 1,&len);
  750. unsigned char size = luaL_checkinteger(L, 2);
  751. int x = luaL_checkinteger(L, 3);
  752. int y = luaL_checkinteger(L, 4);
  753. for(;;){
  754. e = utf8_next((uint8_t)*fontCode);
  755. if ( e == 0x0ffff )
  756. break;
  757. fontCode++;
  758. if ( e != 0x0fffe ){
  759. uint16_t str = unicodetogb2312(e);
  760. get_font(buf, 1, str, size, size, size);
  761. gtfont_draw_w(buf , x ,y , size , size,gtfont_u8g2_DrawPixel,u8g2,2);
  762. x+=size;
  763. }
  764. }
  765. return 0;
  766. }
  767. #endif // LUAT_USE_GTFONT_UTF8
  768. #endif // LUAT_USE_GTFONT
  769. #include "rotable2.h"
  770. static const rotable_Reg_t reg_u8g2[] =
  771. {
  772. { "begin", ROREG_FUNC(l_u8g2_begin)},
  773. { "init", ROREG_FUNC(l_u8g2_begin)}, // 兼容disp.init函数
  774. { "close", ROREG_FUNC(l_u8g2_close)},
  775. { "ClearBuffer", ROREG_FUNC(l_u8g2_ClearBuffer)},
  776. { "SendBuffer", ROREG_FUNC(l_u8g2_SendBuffer)},
  777. { "DrawUTF8", ROREG_FUNC(l_u8g2_DrawUTF8)},
  778. { "SetFontMode", ROREG_FUNC(l_u8g2_SetFontMode)},
  779. { "SetFont", ROREG_FUNC(l_u8g2_SetFont)},
  780. { "GetDisplayHeight", ROREG_FUNC(l_u8g2_GetDisplayHeight)},
  781. { "GetDisplayWidth", ROREG_FUNC(l_u8g2_GetDisplayWidth)},
  782. { "SetDrawColor", ROREG_FUNC(l_u8g2_SetDrawColor)},
  783. { "DrawPixel", ROREG_FUNC(l_u8g2_DrawPixel)},
  784. { "DrawLine", ROREG_FUNC(l_u8g2_DrawLine)},
  785. { "DrawCircle", ROREG_FUNC(l_u8g2_DrawCircle)},
  786. { "DrawDisc", ROREG_FUNC(l_u8g2_DrawDisc)},
  787. { "DrawEllipse", ROREG_FUNC(l_u8g2_DrawEllipse)},
  788. { "DrawFilledEllipse", ROREG_FUNC(l_u8g2_DrawFilledEllipse)},
  789. { "DrawBox", ROREG_FUNC(l_u8g2_DrawBox)},
  790. { "DrawFrame", ROREG_FUNC(l_u8g2_DrawFrame)},
  791. { "DrawRBox", ROREG_FUNC(l_u8g2_DrawRBox)},
  792. { "DrawRFrame", ROREG_FUNC(l_u8g2_DrawRFrame)},
  793. { "DrawGlyph", ROREG_FUNC(l_u8g2_DrawGlyph)},
  794. { "DrawTriangle", ROREG_FUNC(l_u8g2_DrawTriangle)},
  795. { "SetBitmapMode",ROREG_FUNC(l_u8g2_SetBitmapMode)},
  796. { "DrawXBM", ROREG_FUNC(l_u8g2_DrawXBM)},
  797. { "DrawDrcode", ROREG_FUNC(l_u8g2_DrawDrcode)},
  798. #ifdef LUAT_USE_GTFONT
  799. { "drawGtfontGb2312", ROREG_FUNC(l_u8g2_draw_gtfont_gb2312)},
  800. #ifdef LUAT_USE_GTFONT_UTF8
  801. { "drawGtfontUtf8", ROREG_FUNC(l_u8g2_draw_gtfont_utf8)},
  802. #endif // LUAT_USE_GTFONT_UTF8
  803. #endif // LUAT_USE_GTFONT
  804. // 默认只带8号字体
  805. { "font_opposansm8", ROREG_PTR((void*)u8g2_font_opposansm8)},
  806. #ifdef USE_U8G2_OPPOSANSM_ENGLISH
  807. { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
  808. { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
  809. { "font_opposansm10", ROREG_PTR((void*)u8g2_font_opposansm10)},
  810. { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
  811. { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
  812. { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
  813. { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
  814. { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
  815. { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
  816. { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
  817. #endif
  818. #ifdef USE_U8G2_OPPOSANSM8_CHINESE
  819. { "font_opposansm8_chinese", ROREG_PTR((void*)u8g2_font_opposansm8_chinese)},
  820. #endif
  821. #ifdef USE_U8G2_OPPOSANSM10_CHINESE
  822. { "font_opposansm10_chinese", ROREG_PTR((void*)u8g2_font_opposansm10_chinese)},
  823. #endif
  824. #ifdef USE_U8G2_OPPOSANSM12_CHINESE
  825. { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
  826. #endif
  827. #ifdef USE_U8G2_OPPOSANSM16_CHINESE
  828. { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
  829. #endif
  830. #ifdef USE_U8G2_OPPOSANSM18_CHINESE
  831. { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
  832. #endif
  833. #ifdef USE_U8G2_OPPOSANSM20_CHINESE
  834. { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
  835. #endif
  836. #ifdef USE_U8G2_OPPOSANSM22_CHINESE
  837. { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
  838. #endif
  839. #ifdef USE_U8G2_OPPOSANSM24_CHINESE
  840. { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
  841. #endif
  842. #ifdef USE_U8G2_OPPOSANSM32_CHINESE
  843. { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
  844. #endif
  845. { NULL, ROREG_INT(0)}
  846. };
  847. LUAMOD_API int luaopen_u8g2( lua_State *L ) {
  848. lua_getglobal(L, "disp"); // disp库已经加载过u8g2库, 那就直接重用
  849. if (lua_isuserdata(L, -1))
  850. return 1;
  851. luat_newlib2(L, reg_u8g2);
  852. return 1;
  853. }
  854. //-------------------------------------------------------------------------------------------------
  855. // 往下是一些U8G2方法的默认实现
  856. uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  857. uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  858. uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  859. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  860. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  861. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  862. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf);
  863. 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);
  864. typedef struct luat_u8g2_dev_reg
  865. {
  866. const char* name;
  867. dev_setup_cb devcb;
  868. uint16_t w; // 屏幕宽, 最大值
  869. uint16_t h; // 屏幕高, 最大值
  870. uint16_t spi_i2c; // 使用 I2C 0, 使用 SPI 1
  871. }luat_u8g2_dev_reg_t;
  872. static const luat_u8g2_dev_reg_t devregs[] = {
  873. // ssd1306是默认值
  874. {.name="ssd1306", .w=128, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x64_noname_f}, // ssd1306 128x64,I2C
  875. {.name="ssd1306", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_ssd1306_128x64_noname_f}, // ssd1306 128x64,SPI
  876. {.name="ssd1322", .w=256, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_ssd1322_nhd_256x64_f}, // ssd1322 128x64
  877. {.name="sh1106", .w=128, .h=64, .spi_i2c=0, .devcb=u8g2_Setup_sh1106_i2c_128x64_noname_f}, // sh1106 128x64,I2C
  878. {.name="sh1106", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_sh1106_128x64_noname_f}, // sh1106 128x64,SPI
  879. {.name="sh1107", .w=64, .h=128, .spi_i2c=0, .devcb=u8g2_Setup_ssd1306_i2c_128x64_noname_f}, // sh1107 64x128
  880. {.name="st7567", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_st7567_jlx12864_f}, // st7567 128x64
  881. {.name="uc1701", .w=128, .h=64, .spi_i2c=1, .devcb=u8g2_Setup_uc1701_mini12864_f}, // uc1701
  882. {.name=NULL} // 结尾用,必须加.
  883. };
  884. static const luat_u8g2_dev_reg_t* search_dev_reg(luat_u8g2_conf_t *conf, uint16_t spi_i2c) {
  885. size_t dev_reg_index = 0;
  886. while (devregs[dev_reg_index].name != NULL){
  887. if (devregs[dev_reg_index].spi_i2c == spi_i2c && strcmp(devregs[dev_reg_index].name, conf->cname) == 0) {
  888. return &devregs[dev_reg_index];
  889. }
  890. dev_reg_index ++;
  891. }
  892. return &devregs[0];
  893. }
  894. #ifndef LUAT_COMPILER_NOWEAK
  895. LUAT_WEAK int luat_u8g2_setup(luat_u8g2_conf_t *conf) {
  896. return luat_u8g2_setup_default(conf);
  897. }
  898. LUAT_WEAK uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  899. return u8x8_luat_gpio_and_delay_default(u8x8, msg, arg_int, arg_ptr);
  900. }
  901. LUAT_WEAK uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  902. return u8x8_luat_byte_hw_i2c_default(u8x8, msg, arg_int, arg_ptr);
  903. }
  904. LUAT_WEAK uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  905. return u8x8_luat_byte_4wire_hw_spi_default(u8x8, msg, arg_int, arg_ptr);
  906. }
  907. #endif
  908. int luat_u8g2_setup_default(luat_u8g2_conf_t *conf) {
  909. u8g2_t* u8g2 = (u8g2_t*)conf->ptr;
  910. const luat_u8g2_dev_reg_t* devreg = NULL;
  911. if (conf->pinType == 1) {
  912. devreg = search_dev_reg(conf, 0);
  913. if (devreg == NULL) {
  914. LLOGD("unkown dev %s", conf->cname);
  915. return -1;
  916. }
  917. devreg->devcb(u8g2, conf->direction, u8x8_byte_sw_i2c, u8x8_luat_gpio_and_delay_default);
  918. #ifdef U8G2_USE_DYNAMIC_ALLOC
  919. buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  920. u8g2_SetBufferPtr(u8g2, buff_ptr);
  921. #endif
  922. u8g2->u8x8.pins[U8X8_PIN_I2C_CLOCK] = i2c_scl;
  923. u8g2->u8x8.pins[U8X8_PIN_I2C_DATA] = i2c_sda;
  924. u8g2_InitDisplay(u8g2);
  925. u8g2_SetPowerSave(u8g2, 0);
  926. return 0;
  927. }else if (conf->pinType == 2) {
  928. devreg = search_dev_reg(conf, 0);
  929. if (devreg == NULL) {
  930. LLOGD("unkown dev %s", conf->cname);
  931. return -1;
  932. }
  933. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_hw_i2c_default, u8x8_luat_gpio_and_delay_default);
  934. #ifdef U8G2_USE_DYNAMIC_ALLOC
  935. buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  936. u8g2_SetBufferPtr(u8g2, buff_ptr);
  937. #endif
  938. //LLOGD("setup disp i2c.hw");
  939. u8g2_InitDisplay(u8g2);
  940. u8g2_SetPowerSave(u8g2, 0);
  941. return 0;
  942. }else if (conf->pinType == 5) {
  943. devreg = search_dev_reg(conf, 1);
  944. if (devreg == NULL) {
  945. LLOGD("unkown dev %s", conf->cname);
  946. return -1;
  947. }
  948. devreg->devcb(u8g2, conf->direction, u8x8_luat_byte_4wire_hw_spi_default, u8x8_luat_gpio_and_delay_default);
  949. #ifdef U8G2_USE_DYNAMIC_ALLOC
  950. buff_ptr = (uint8_t *)luat_heap_malloc(u8g2_GetBufferSize(u8g2));
  951. u8g2_SetBufferPtr(u8g2, buff_ptr);
  952. #endif
  953. 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);
  954. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_CS, spi_cs);
  955. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_DC, spi_dc);
  956. u8x8_SetPin(u8g2_GetU8x8(u8g2), U8X8_PIN_RESET, spi_res);
  957. u8g2_InitDisplay(u8g2);
  958. u8g2_SetPowerSave(u8g2, 0);
  959. return 0;
  960. }
  961. else {
  962. LLOGI("no such u8g2 mode!!");
  963. }
  964. return -1;
  965. }
  966. LUAT_WEAK int luat_u8g2_close(luat_u8g2_conf_t *conf) {
  967. return 0;
  968. }
  969. uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  970. static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes */
  971. static uint8_t buf_idx;
  972. uint8_t *data;
  973. switch(msg)
  974. {
  975. case U8X8_MSG_BYTE_SEND:
  976. data = (uint8_t *)arg_ptr;
  977. while( arg_int > 0 )
  978. {
  979. buffer[buf_idx++] = *data;
  980. data++;
  981. arg_int--;
  982. }
  983. break;
  984. case U8X8_MSG_BYTE_INIT:
  985. //i2c_init(u8x8); /* init i2c communication */
  986. luat_i2c_setup(i2c_id,i2c_speed, 0);
  987. break;
  988. case U8X8_MSG_BYTE_SET_DC:
  989. /* ignored for i2c */
  990. break;
  991. case U8X8_MSG_BYTE_START_TRANSFER:
  992. buf_idx = 0;
  993. break;
  994. case U8X8_MSG_BYTE_END_TRANSFER:
  995. luat_i2c_send(i2c_id, u8x8_GetI2CAddress(u8x8) >> 1, buffer, buf_idx,1);
  996. break;
  997. default:
  998. return 0;
  999. }
  1000. return 1;
  1001. }
  1002. int hw_spi_begin(uint8_t spi_mode, uint32_t max_hz, uint8_t cs_pin )
  1003. {
  1004. luat_spi_t u8g2_spi = {0};
  1005. u8g2_spi.id = spi_id;
  1006. switch(spi_mode)
  1007. {
  1008. case 0: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 0; break;
  1009. case 1: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 0; break;
  1010. case 2: u8g2_spi.CPHA = 0;u8g2_spi.CPOL = 1; break;
  1011. case 3: u8g2_spi.CPHA = 1;u8g2_spi.CPOL = 1; break;
  1012. }
  1013. u8g2_spi.dataw = 8;
  1014. u8g2_spi.bit_dict = 1;
  1015. u8g2_spi.master = 1;
  1016. u8g2_spi.mode = 1;
  1017. u8g2_spi.bandrate = max_hz;
  1018. u8g2_spi.cs = cs_pin;
  1019. // LLOGI("cs_pin=%d",cs_pin);
  1020. luat_spi_setup(&u8g2_spi);
  1021. return 1;
  1022. }
  1023. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  1024. uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  1025. switch(msg)
  1026. {
  1027. case U8X8_MSG_BYTE_SEND:
  1028. luat_spi_send(spi_id, (const char*)arg_ptr, arg_int);
  1029. break;
  1030. case U8X8_MSG_BYTE_INIT:
  1031. /* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  1032. /* 0: clock active high, data out on falling edge, clock default value is zero, takover on rising edge */
  1033. /* 1: clock active high, data out on rising edge, clock default value is zero, takover on falling edge */
  1034. /* 2: clock active low, data out on rising edge */
  1035. /* 3: clock active low, data out on falling edge */
  1036. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1037. hw_spi_begin(u8x8->display_info->spi_mode, u8x8->display_info->sck_clock_hz, u8x8->pins[U8X8_PIN_CS]);
  1038. break;
  1039. case U8X8_MSG_BYTE_SET_DC:
  1040. u8x8_gpio_SetDC(u8x8, arg_int);
  1041. break;
  1042. case U8X8_MSG_BYTE_START_TRANSFER:
  1043. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  1044. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  1045. break;
  1046. case U8X8_MSG_BYTE_END_TRANSFER:
  1047. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  1048. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1049. break;
  1050. default:
  1051. return 0;
  1052. }
  1053. return 1;
  1054. }
  1055. uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1056. {
  1057. uint8_t i;
  1058. switch(msg)
  1059. {
  1060. case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
  1061. __asm__ volatile("nop");
  1062. break;
  1063. case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  1064. __asm__ volatile("nop");
  1065. break;
  1066. case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  1067. for (uint16_t n = 0; n < 320; n++)
  1068. {
  1069. __asm__ volatile("nop");
  1070. }
  1071. break;
  1072. case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  1073. luat_timer_mdelay(arg_int);
  1074. break;
  1075. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  1076. // Function which implements a delay, arg_int contains the amount of ms
  1077. // set spi pin mode
  1078. 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
  1079. luat_gpio_mode(u8x8->pins[U8X8_PIN_SPI_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1080. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1081. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1082. luat_gpio_mode(u8x8->pins[U8X8_PIN_CS],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1083. // set i2c pin mode
  1084. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_DATA],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1085. luat_gpio_mode(u8x8->pins[U8X8_PIN_I2C_CLOCK],Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  1086. // set 8080 pin mode
  1087. luat_gpio_mode(u8x8->pins[U8X8_PIN_D0],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1088. luat_gpio_mode(u8x8->pins[U8X8_PIN_D1],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1089. luat_gpio_mode(u8x8->pins[U8X8_PIN_D2],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1090. luat_gpio_mode(u8x8->pins[U8X8_PIN_D3],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1091. luat_gpio_mode(u8x8->pins[U8X8_PIN_D4],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1092. luat_gpio_mode(u8x8->pins[U8X8_PIN_D5],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1093. luat_gpio_mode(u8x8->pins[U8X8_PIN_D6],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1094. luat_gpio_mode(u8x8->pins[U8X8_PIN_D7],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1095. luat_gpio_mode(u8x8->pins[U8X8_PIN_E],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1096. luat_gpio_mode(u8x8->pins[U8X8_PIN_DC],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1097. luat_gpio_mode(u8x8->pins[U8X8_PIN_RESET],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1098. // set menu pin mode
  1099. luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_HOME],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1100. luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_SELECT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1101. luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_PREV],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1102. luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_NEXT],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1103. luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_UP],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1104. luat_gpio_mode(u8x8->pins[U8X8_PIN_MENU_DOWN],Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  1105. // set value
  1106. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],1);
  1107. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],1);
  1108. luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],1);
  1109. luat_gpio_set(u8x8->pins[U8X8_PIN_DC],1);
  1110. luat_gpio_set(u8x8->pins[U8X8_PIN_CS],1);
  1111. break;
  1112. case U8X8_MSG_DELAY_I2C:
  1113. // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  1114. // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  1115. for (uint16_t n = 0; n < (arg_int<=2?160:40); n++)
  1116. {
  1117. __asm__ volatile("nop");
  1118. }
  1119. break;
  1120. //case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
  1121. //case U8X8_MSG_GPIO_SPI_CLOCK:
  1122. //case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
  1123. //case U8X8_MSG_GPIO_SPI_DATA:
  1124. case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
  1125. luat_gpio_set(u8x8->pins[U8X8_PIN_D2],arg_int);
  1126. break;
  1127. case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
  1128. luat_gpio_set(u8x8->pins[U8X8_PIN_D3],arg_int);
  1129. break;
  1130. case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
  1131. luat_gpio_set(u8x8->pins[U8X8_PIN_D4],arg_int);
  1132. break;
  1133. case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
  1134. luat_gpio_set(u8x8->pins[U8X8_PIN_D5],arg_int);
  1135. break;
  1136. case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
  1137. luat_gpio_set(u8x8->pins[U8X8_PIN_D6],arg_int);
  1138. break;
  1139. case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
  1140. luat_gpio_set(u8x8->pins[U8X8_PIN_D7],arg_int);
  1141. break;
  1142. case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
  1143. luat_gpio_set(u8x8->pins[U8X8_PIN_E],arg_int);
  1144. break;
  1145. case U8X8_MSG_GPIO_I2C_CLOCK:
  1146. // arg_int=0: Output low at I2C clock pin
  1147. // arg_int=1: Input dir with pullup high for I2C clock pin
  1148. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_CLOCK],arg_int);
  1149. break;
  1150. case U8X8_MSG_GPIO_I2C_DATA:
  1151. // arg_int=0: Output low at I2C data pin
  1152. // arg_int=1: Input dir with pullup high for I2C data pin
  1153. luat_gpio_set(u8x8->pins[U8X8_PIN_I2C_DATA],arg_int);
  1154. break;
  1155. case U8X8_MSG_GPIO_SPI_CLOCK:
  1156. //Function to define the logic level of the clockline
  1157. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_CLOCK],arg_int);
  1158. break;
  1159. case U8X8_MSG_GPIO_SPI_DATA:
  1160. //Function to define the logic level of the data line to the display
  1161. luat_gpio_set(u8x8->pins[U8X8_PIN_SPI_DATA],arg_int);
  1162. break;
  1163. case U8X8_MSG_GPIO_CS:
  1164. // Function to define the logic level of the CS line
  1165. luat_gpio_set(u8x8->pins[U8X8_PIN_CS],arg_int);
  1166. break;
  1167. case U8X8_MSG_GPIO_DC:
  1168. //Function to define the logic level of the Data/ Command line
  1169. luat_gpio_set(u8x8->pins[U8X8_PIN_DC],arg_int);
  1170. break;
  1171. case U8X8_MSG_GPIO_RESET:
  1172. //Function to define the logic level of the RESET line
  1173. luat_gpio_set(u8x8->pins[U8X8_PIN_RESET],arg_int);
  1174. break;
  1175. default:
  1176. //A message was received which is not implemented, return 0 to indicate an error
  1177. if ( msg >= U8X8_MSG_GPIO(0) )
  1178. {
  1179. i = u8x8_GetPinValue(u8x8, msg);
  1180. if ( i != U8X8_PIN_NONE )
  1181. {
  1182. if ( u8x8_GetPinIndex(u8x8, msg) < U8X8_PIN_OUTPUT_CNT )
  1183. {
  1184. luat_gpio_set(i, arg_int);
  1185. }
  1186. else
  1187. {
  1188. if ( u8x8_GetPinIndex(u8x8, msg) == U8X8_PIN_OUTPUT_CNT )
  1189. {
  1190. // call yield() for the first pin only, u8x8 will always request all the pins, so this should be ok
  1191. // yield();
  1192. }
  1193. u8x8_SetGPIOResult(u8x8, luat_gpio_get(i) == 0 ? 0 : 1);
  1194. }
  1195. }
  1196. break;
  1197. }
  1198. return 0;
  1199. }
  1200. return 1;
  1201. }