luat_lib_i2c.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. @module i2c
  3. @summary I2C操作
  4. @version 1.0
  5. @date 2020.03.30
  6. */
  7. #include "luat_base.h"
  8. #include "luat_log.h"
  9. #include "luat_timer.h"
  10. #include "luat_malloc.h"
  11. #include "luat_i2c.h"
  12. #include "luat_gpio.h"
  13. #define LUAT_LOG_TAG "luat.i2c"
  14. #include "luat_log.h"
  15. static void i2c_soft_start(luat_ei2c *ei2c)
  16. {
  17. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  18. luat_timer_us_delay(5);
  19. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  20. luat_timer_us_delay(5);
  21. luat_gpio_set(ei2c->sda, Luat_GPIO_LOW);
  22. luat_timer_us_delay(5);
  23. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  24. luat_timer_us_delay(5);
  25. }
  26. static void i2c_soft_stop(luat_ei2c *ei2c)
  27. {
  28. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  29. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  30. luat_timer_us_delay(5);
  31. luat_gpio_set(ei2c->sda, Luat_GPIO_LOW);
  32. luat_timer_us_delay(5);
  33. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  34. luat_timer_us_delay(5);
  35. luat_gpio_set(ei2c->sda, Luat_GPIO_HIGH);
  36. luat_timer_us_delay(5);
  37. }
  38. static unsigned char i2c_soft_wait_ack(luat_ei2c *ei2c)
  39. {
  40. luat_gpio_set(ei2c->sda, Luat_GPIO_HIGH);
  41. luat_gpio_mode(ei2c->sda, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, 1);
  42. luat_timer_us_delay(5);
  43. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  44. luat_timer_us_delay(15);
  45. int max_wait_time = 3000;
  46. while(max_wait_time--)
  47. {
  48. if(luat_gpio_get(ei2c->sda) == Luat_GPIO_LOW)
  49. {
  50. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  51. return 1;
  52. }
  53. luat_timer_us_delay(1);
  54. }
  55. i2c_soft_stop(ei2c);
  56. return 0;
  57. }
  58. static void i2c_soft_send_ack(luat_ei2c *ei2c)
  59. {
  60. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0);
  61. luat_timer_us_delay(5);
  62. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  63. luat_timer_us_delay(5);
  64. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  65. }
  66. static void i2c_soft_send_noack(luat_ei2c *ei2c)
  67. {
  68. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  69. luat_timer_us_delay(5);
  70. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  71. luat_timer_us_delay(5);
  72. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  73. }
  74. static void i2c_soft_send_byte(luat_ei2c *ei2c,unsigned char data)
  75. {
  76. unsigned char i = 8;
  77. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0);
  78. while(i--)
  79. {
  80. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  81. luat_timer_us_delay(10);
  82. if(data & 0x80)
  83. {
  84. luat_gpio_set(ei2c->sda, Luat_GPIO_HIGH);
  85. }
  86. else
  87. {
  88. luat_gpio_set(ei2c->sda, Luat_GPIO_LOW);
  89. }
  90. luat_timer_us_delay(5);
  91. data <<= 1;
  92. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  93. luat_timer_us_delay(5);
  94. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  95. luat_timer_us_delay(5);
  96. }
  97. }
  98. static char i2c_soft_recv_byte(luat_ei2c *ei2c)
  99. {
  100. unsigned char i = 8;
  101. unsigned char data = 0;
  102. luat_gpio_set(ei2c->sda, Luat_GPIO_HIGH);
  103. luat_gpio_mode(ei2c->sda, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, 1);
  104. while(i--)
  105. {
  106. data <<= 1;
  107. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  108. luat_timer_us_delay(5);
  109. luat_gpio_set(ei2c->scl, Luat_GPIO_HIGH);
  110. luat_timer_us_delay(5);
  111. if(luat_gpio_get(ei2c->sda))
  112. data |= 0x01;
  113. }
  114. luat_gpio_set(ei2c->scl, Luat_GPIO_LOW);
  115. return(data);
  116. }
  117. static char i2c_soft_recv(luat_ei2c *ei2c,unsigned char addr, char *buff, size_t len)
  118. {
  119. size_t i;
  120. i2c_soft_start(ei2c);
  121. i2c_soft_send_byte(ei2c,(addr<<1)+1);
  122. if(!i2c_soft_wait_ack(ei2c))
  123. {
  124. i2c_soft_stop(ei2c);
  125. return -1;
  126. }
  127. luat_timer_us_delay(2000);
  128. for(i = 0; i < len; i++)
  129. {
  130. *buff++ = i2c_soft_recv_byte(ei2c);
  131. if(i < (len - 1))
  132. i2c_soft_send_ack(ei2c);
  133. }
  134. i2c_soft_send_noack(ei2c);
  135. i2c_soft_stop(ei2c);
  136. return 0;
  137. }
  138. static char i2c_soft_send(luat_ei2c *ei2c,unsigned char addr,char *data, size_t len)
  139. {
  140. size_t i;
  141. i2c_soft_start(ei2c);
  142. i2c_soft_send_byte(ei2c,addr<<1);
  143. if(!i2c_soft_wait_ack(ei2c))
  144. {
  145. i2c_soft_stop(ei2c);
  146. return -1;
  147. }
  148. for(i = 0; i < len; i++)
  149. {
  150. i2c_soft_send_byte(ei2c,data[i]);
  151. if(!i2c_soft_wait_ack(ei2c))
  152. {
  153. i2c_soft_stop(ei2c);
  154. return -1;
  155. }
  156. }
  157. i2c_soft_stop(ei2c);
  158. return 0;
  159. }
  160. /*
  161. i2c编号是否存在
  162. @api i2c.exist(id)
  163. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  164. @return int 存在就返回1,否则返回0
  165. @usage
  166. -- 检查i2c1是否存在
  167. if i2c.exist(1) then
  168. log.info("存在 i2c1")
  169. end
  170. */
  171. static int l_i2c_exist(lua_State *L) {
  172. int re = luat_i2c_exist(luaL_checkinteger(L, 1));
  173. lua_pushinteger(L, re);
  174. return 1;
  175. }
  176. /*
  177. i2c初始化
  178. @api i2c.setup(id, speed, slaveAddr)
  179. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  180. @int I2C速度, 例如i2c.FAST
  181. @int 从设备地址(7位), 例如0x38
  182. @return int 成功就返回1,否则返回0
  183. @usage
  184. -- 初始化i2c1
  185. if i2c.setup(1, i2c.FAST, 0x38) == 1 then
  186. log.info("存在 i2c1")
  187. else
  188. i2c.close(1) -- 关掉
  189. end
  190. */
  191. static int l_i2c_setup(lua_State *L) {
  192. int re = luat_i2c_setup(luaL_checkinteger(L, 1), luaL_optinteger(L, 2, 0), luaL_optinteger(L, 3, 0));
  193. lua_pushinteger(L, re == 0 ? luaL_optinteger(L, 2, 0) : -1);
  194. return 1;
  195. }
  196. /*
  197. 新建一个软件i2c对象
  198. @api i2c.createSoft(scl,sda,slaveAddr)
  199. @int i2c SCL引脚编号
  200. @int i2c SDA引脚编号
  201. @int 从设备地址(7位), 例如0x38
  202. @return 软件I2C对象 可当作i2c的id使用
  203. @usage
  204. -- 注意!这个接口是软件模拟i2c,速度可能会比硬件的慢
  205. -- 不需要调用i2c.close接口
  206. -- 初始化软件i2c
  207. local softI2C = i2c.createSoft(1,2,0x38)
  208. i2c.send(softI2C, 0x5C, string.char(0x0F, 0x2F))
  209. */
  210. static int l_i2c_soft(lua_State *L) {
  211. luat_ei2c *ei2c = (luat_ei2c *)lua_newuserdata(L, sizeof(luat_ei2c));
  212. ei2c->scl = luaL_checkinteger(L, 1);
  213. ei2c->sda = luaL_checkinteger(L, 2);
  214. ei2c->addr = luaL_checkinteger(L, 3);
  215. luat_gpio_mode(ei2c->scl, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  216. luat_gpio_mode(ei2c->sda, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 1);
  217. i2c_soft_stop(ei2c);
  218. luaL_setmetatable(L, LUAT_EI2C_TYPE);
  219. return 1;
  220. }
  221. /*
  222. i2c发送数据
  223. @api i2c.send(id, addr, data)
  224. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  225. @int I2C子设备的地址, 7位地址
  226. @string 待发送的数据
  227. @return true/false 发送是否成功
  228. @usage
  229. -- 往i2c1发送2个字节的数据
  230. i2c.send(1, 0x5C, string.char(0x0F, 0x2F))
  231. */
  232. static int l_i2c_send(lua_State *L) {
  233. int id = 0;
  234. if(!lua_isuserdata(L, 1)){
  235. id = luaL_checkinteger(L, 1);
  236. }
  237. int addr = luaL_checkinteger(L, 2);
  238. size_t len = 0;
  239. int result = 0;
  240. if (lua_isstring(L, 3)) {
  241. const char* buff = luaL_checklstring(L, 3, &len);
  242. if(lua_isuserdata(L, 1))
  243. {
  244. luat_ei2c* ei2c = toei2c(L);
  245. result = i2c_soft_send(ei2c, addr, (char*)buff, len);
  246. }
  247. else
  248. {
  249. result = luat_i2c_send(id, addr, (char*)buff, len);
  250. }
  251. }
  252. else if (lua_isinteger(L, 3)) {
  253. len = lua_gettop(L) - 2;
  254. char* buff = (char*)luat_heap_malloc(len+1);
  255. for (size_t i = 0; i < len; i++)
  256. {
  257. buff[i] = lua_tointeger(L, 3+i);
  258. }
  259. if(lua_isuserdata(L, 1))
  260. {
  261. luat_ei2c* ei2c = toei2c(L);
  262. result = i2c_soft_send(ei2c, addr, buff, len);
  263. }
  264. else
  265. {
  266. result = luat_i2c_send(id, addr, buff, len);
  267. }
  268. luat_heap_free(buff);
  269. }
  270. lua_pushboolean(L, result == 0);
  271. return 1;
  272. }
  273. /*
  274. i2c接收数据
  275. @api i2c.recv(id, addr, len)
  276. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  277. @int I2C子设备的地址, 7位地址
  278. @int 接收数据的长度
  279. @return string 收到的数据
  280. @usage
  281. -- 从i2c1读取2个字节的数据
  282. local data = i2c.recv(1, 0x5C, 2)
  283. */
  284. static int l_i2c_recv(lua_State *L) {
  285. int id = 0;
  286. if(!lua_isuserdata(L, 1)){
  287. id = luaL_checkinteger(L, 1);
  288. }
  289. int addr = luaL_checkinteger(L, 2);
  290. int len = luaL_checkinteger(L, 3);
  291. char* buff = (char*)luat_heap_malloc(len);
  292. int result;
  293. if(lua_isuserdata(L, 1))
  294. {
  295. luat_ei2c* ei2c = toei2c(L);
  296. result = i2c_soft_recv(ei2c, addr, buff, len);
  297. }
  298. else
  299. {
  300. result = luat_i2c_recv(id, addr, buff, len);
  301. }
  302. if(result!=0){//如果返回值不为0,说明收失败了
  303. len = 0;
  304. LLOGD("i2c receive result %d", result);
  305. }
  306. lua_pushlstring(L, buff, len);
  307. luat_heap_free(buff);
  308. return 1;
  309. }
  310. /*
  311. i2c写寄存器数据
  312. @api i2c.writeReg(id, addr, reg, data)
  313. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  314. @int I2C子设备的地址, 7位地址
  315. @int 寄存器地址
  316. @string 待发送的数据
  317. @return true/false 发送是否成功
  318. @usage
  319. -- 从i2c1的地址为0x5C的设备的寄存器0x01写入2个字节的数据
  320. i2c.writeReg(1, 0x5C, 0x01, string.char(0x00, 0xF2))
  321. */
  322. static int l_i2c_write_reg(lua_State *L) {
  323. int id = 0;
  324. if(!lua_isuserdata(L, 1)){
  325. id = luaL_checkinteger(L, 1);
  326. }
  327. int addr = luaL_checkinteger(L, 2);
  328. int reg = luaL_checkinteger(L, 3);
  329. size_t len;
  330. const char* lb = luaL_checklstring(L, 4, &len);
  331. char* buff = (char*)luat_heap_malloc(sizeof(char)*len+1);
  332. *buff = (char)reg;
  333. memcpy(buff+1,lb,sizeof(char)+len+1);
  334. int result;
  335. if(lua_isuserdata(L, 1))
  336. {
  337. luat_ei2c* ei2c = toei2c(L);
  338. result = i2c_soft_send(ei2c, addr, buff, len+1);
  339. }
  340. else
  341. {
  342. result = luat_i2c_send(id, addr, buff, len+1);
  343. }
  344. luat_heap_free(buff);
  345. lua_pushboolean(L, result == 0);
  346. return 1;
  347. }
  348. /*
  349. i2c读寄存器数据
  350. @api i2c.readReg(id, addr, reg, len)
  351. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  352. @int I2C子设备的地址, 7位地址
  353. @int 寄存器地址
  354. @int 待接收的数据长度
  355. @return string 收到的数据
  356. @usage
  357. -- 从i2c1的地址为0x5C的设备的寄存器0x01读出2个字节的数据
  358. i2c.readReg(1, 0x5C, 0x01, 2)
  359. */
  360. static int l_i2c_read_reg(lua_State *L) {
  361. int id = 0;
  362. if(!lua_isuserdata(L, 1)){
  363. id = luaL_checkinteger(L, 1);
  364. }
  365. int addr = luaL_checkinteger(L, 2);
  366. int reg = luaL_checkinteger(L, 3);
  367. int len = luaL_checkinteger(L, 4);
  368. char temp = (char)reg;
  369. int result;
  370. if(lua_isuserdata(L, 1))
  371. {
  372. luat_ei2c* ei2c = toei2c(L);
  373. result = i2c_soft_send(ei2c, addr, &temp, 1);
  374. }
  375. else
  376. {
  377. result = luat_i2c_send(id, addr, &temp, 1);
  378. }
  379. if(result!=0){//如果返回值不为0,说明收失败了
  380. LLOGD("i2c send result %d", result);
  381. lua_pushlstring(L, NULL, 0);
  382. return 1;
  383. }
  384. char* buff = (char*)luat_heap_malloc(sizeof(char)*len);
  385. if(lua_isuserdata(L, 1))
  386. {
  387. luat_ei2c* ei2c = toei2c(L);
  388. result = i2c_soft_recv(ei2c, addr, buff, len);
  389. }
  390. else
  391. {
  392. result = luat_i2c_recv(id, addr, buff, len);
  393. }
  394. if(result!=0){//如果返回值不为0,说明收失败了
  395. len = 0;
  396. LLOGD("i2c receive result %d", result);
  397. }
  398. lua_pushlstring(L, buff, len);
  399. luat_heap_free(buff);
  400. return 1;
  401. }
  402. /*
  403. 关闭i2c设备
  404. @api i2c.close(id)
  405. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  406. @return nil 无返回值
  407. @usage
  408. -- 关闭i2c1
  409. i2c.close(1)
  410. */
  411. static int l_i2c_close(lua_State *L) {
  412. int id = luaL_checkinteger(L, 1);
  413. luat_ic2_close(id);
  414. return 0;
  415. }
  416. /*
  417. 从i2c总线读取DHT12的温湿度数据
  418. @api i2c.readDHT12(id)
  419. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  420. @int DHT12的设备地址,默认0x5C
  421. @return boolean 读取成功返回true,否则返回false
  422. @return int 湿度值,单位0.1%, 例如 591 代表 59.1%
  423. @return int 温度值,单位0.1摄氏度, 例如 292 代表 29.2摄氏度
  424. @usage
  425. -- 从i2c0读取DHT12
  426. i2c.setup(0)
  427. local re, H, T = i2c.readDHT12(0)
  428. if re then
  429. log.info("dht12", H, T)
  430. end
  431. */
  432. static int l_i2c_readDHT12(lua_State *L) {
  433. int id = 0;
  434. if(!lua_isuserdata(L, 1)){
  435. id = luaL_checkinteger(L, 1);
  436. }
  437. int addr = luaL_optinteger(L, 2, 0x5C);
  438. char buff[5] = {0};
  439. char temp = 0x00;
  440. int result = -1;
  441. if(lua_isuserdata(L, 1))
  442. {
  443. luat_ei2c* ei2c = toei2c(L);
  444. result = i2c_soft_send(ei2c, addr, &temp, 1);
  445. }
  446. else
  447. {
  448. result = luat_i2c_send(id, addr, &temp, 1);
  449. }
  450. if (result!=0) {
  451. LLOGD("DHT12 i2c bus write fail");
  452. lua_pushboolean(L, 0);
  453. return 1;
  454. }
  455. if(lua_isuserdata(L, 1))
  456. {
  457. luat_ei2c* ei2c = toei2c(L);
  458. result = i2c_soft_recv(ei2c, addr, buff, 5);
  459. }
  460. else
  461. {
  462. result = luat_i2c_recv(id, addr, buff, 5);
  463. }
  464. if (result!=0) {
  465. lua_pushboolean(L, 0);
  466. return 1;
  467. }
  468. else {
  469. if (buff[0] == 0 && buff[1] == 0 && buff[2] == 0 && buff[3] == 0 && buff[4] == 0) {
  470. LLOGD("DHT12 DATA emtry");
  471. lua_pushboolean(L, 0);
  472. return 1;
  473. }
  474. // 检查crc值
  475. LLOGD("DHT12 DATA %02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4]);
  476. uint8_t crc_act = (uint8_t)buff[0] + (uint8_t)buff[1] + (uint8_t)buff[2] + (uint8_t)buff [3];
  477. uint8_t crc_exp = (uint8_t)buff[4];
  478. if (crc_act != crc_exp) {
  479. LLOGD("DATA12 DATA crc not ok");
  480. lua_pushboolean(L, 0);
  481. return 1;
  482. }
  483. lua_pushboolean(L, 1);
  484. lua_pushinteger(L, (uint8_t)buff[0] * 10 + (uint8_t)buff[1]);
  485. if (((uint8_t)buff[2]) > 127)
  486. lua_pushinteger(L, ((uint8_t)buff[2] - 128) * -10 + (uint8_t)buff[3]);
  487. else
  488. lua_pushinteger(L, (uint8_t)buff[2] * 10 + (uint8_t)buff[3]);
  489. return 3;
  490. }
  491. }
  492. /*
  493. 从i2c总线读取DHT30的温湿度数据(由"好奇星"贡献)
  494. @api i2c.readSHT30(id,addr)
  495. @int 设备id, 例如i2c1的id为1, i2c2的id为2
  496. @int 设备addr,SHT30的设备地址,默认0x44 bit7
  497. @return boolean 读取成功返回true,否则返回false
  498. @return int 湿度值,单位0.1%, 例如 591 代表 59.1%
  499. @return int 温度值,单位0.1摄氏度, 例如 292 代表 29.2摄氏度
  500. @usage
  501. -- 从i2c0读取SHT30
  502. i2c.setup(0)
  503. local re, H, T = i2c.readSHT30(0)
  504. if re then
  505. log.info("sht30", H, T)
  506. end
  507. */
  508. static int l_i2c_readSHT30(lua_State *L){
  509. char buff[7] = {0x2c, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00};
  510. float temp = 0x00;
  511. float hum = 0x00;
  512. int result = -1;
  513. if(lua_isuserdata(L, 1))
  514. {
  515. luat_ei2c* ei2c = toei2c(L);
  516. i2c_soft_send(ei2c, ei2c->addr, buff, 2);
  517. luat_timer_mdelay(13);
  518. result = i2c_soft_recv(ei2c, ei2c->addr, buff, 6);
  519. }
  520. else
  521. {
  522. int id = luaL_optinteger(L, 1, 0);
  523. int addr = luaL_optinteger(L, 2, 0x44);
  524. luat_i2c_send(id, addr, &buff, 2);
  525. luat_timer_mdelay(1);
  526. result = luat_i2c_recv(id, addr, buff, 6);
  527. }
  528. if (result!=0) {
  529. lua_pushboolean(L, 0);
  530. return 1;
  531. }
  532. else {
  533. if (buff[0] == 0 && buff[1] == 0 && buff[2] == 0 && buff[3] == 0 && buff[4] == 0) {
  534. LLOGD("SHT30 DATA emtry");
  535. lua_pushboolean(L, 0);
  536. return 1;
  537. }
  538. // 检查crc值
  539. //LLOGD("SHT30 DATA %02X %02X %02X %02X %02X %02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  540. temp = ((((buff[0] * 256) + buff[1]) * 175) / 6553.5) - 450;
  541. hum = ((((buff[3] * 256) + buff[4]) * 100) / 6553.5);
  542. //LLOGD("\r\n SHT30 %d deg %d Hum ", temp, hum);
  543. // 跳过CRC
  544. // uint8_t crc_act = (uint8_t)buff[0] + (uint8_t)buff[1] + (uint8_t)buff[2] + (uint8_t)buff [3];
  545. // uint8_t crc_exp = (uint8_t)buff[4];
  546. // if (crc_act != crc_exp) {
  547. // LLOGD("DATA12 DATA crc not ok");
  548. // lua_pushboolean(L, 0);
  549. // return 1;
  550. // }
  551. // Convert the data
  552. lua_pushboolean(L, 1);
  553. lua_pushinteger(L, (int)hum);
  554. lua_pushinteger(L, (int)temp);
  555. return 3;
  556. // 华氏度
  557. // fTemp = (cTemp * 1.8) + 32;
  558. }
  559. }
  560. #include "rotable.h"
  561. static const rotable_Reg reg_i2c[] =
  562. {
  563. { "exist", l_i2c_exist, 0},
  564. { "setup", l_i2c_setup, 0},
  565. { "createSoft", l_i2c_soft, 0},
  566. #ifdef __F1C100S__
  567. #else
  568. { "send", l_i2c_send, 0},
  569. { "recv", l_i2c_recv, 0},
  570. #endif
  571. { "writeReg", l_i2c_write_reg, 0},
  572. { "readReg", l_i2c_read_reg, 0},
  573. { "close", l_i2c_close, 0},
  574. { "readDHT12", l_i2c_readDHT12, 0},
  575. { "readSHT30", l_i2c_readSHT30, 0},
  576. { "FAST", NULL, 1},
  577. { "SLOW", NULL, 0},
  578. { NULL, NULL, 0}
  579. };
  580. LUAMOD_API int luaopen_i2c( lua_State *L ) {
  581. luat_newlib(L, reg_i2c);
  582. luaL_newmetatable(L, LUAT_EI2C_TYPE);
  583. lua_pop(L, 1);
  584. return 1;
  585. }