luat_lib_u8g2.c 42 KB

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