bme680.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // ***************************************************************************
  2. // Port of BMP680 module for ESP8266 with nodeMCU
  3. //
  4. // Written by Lukas Voborsky, @voborsky
  5. // ***************************************************************************
  6. // #define NODE_DEBUG
  7. #include "module.h"
  8. #include "lauxlib.h"
  9. #include "platform.h"
  10. #include "user_interface.h"
  11. #include <math.h>
  12. #include "bme680_defs.h"
  13. #define DEFAULT_HEATER_DUR 100
  14. #define DEFAULT_HEATER_TEMP 300
  15. #define DEFAULT_AMBIENT_TEMP 23
  16. static const uint32_t bme680_i2c_id = BME680_CHIP_ID_ADDR;
  17. static uint8_t bme680_i2c_addr = BME680_I2C_ADDR_PRIMARY;
  18. os_timer_t bme680_timer; // timer for forced mode readout
  19. int lua_connected_readout_ref; // callback when readout is ready
  20. static struct bme680_calib_data bme680_data;
  21. static uint8_t bme680_mode = 0; // stores oversampling settings
  22. static uint8 os_temp = 0;
  23. static uint8 os_pres = 0;
  24. static uint8 os_hum = 0; // stores humidity oversampling settings
  25. static uint16_t heatr_dur;
  26. static int8_t amb_temp = 23; //DEFAULT_AMBIENT_TEMP;
  27. static uint32_t bme680_h = 0;
  28. static double bme680_hc = 1.0;
  29. // return 0 if good
  30. static int r8u_n(uint8_t reg, int n, uint8_t *buff) {
  31. int i;
  32. platform_i2c_send_start(bme680_i2c_id);
  33. platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  34. platform_i2c_send_byte(bme680_i2c_id, reg);
  35. // platform_i2c_send_stop(bme680_i2c_id); // doco says not needed
  36. platform_i2c_send_start(bme680_i2c_id);
  37. platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  38. while (n-- > 0)
  39. *buff++ = platform_i2c_recv_byte(bme680_i2c_id, n > 0);
  40. platform_i2c_send_stop(bme680_i2c_id);
  41. return 0;
  42. }
  43. static uint8_t w8u(uint8_t reg, uint8_t val) {
  44. platform_i2c_send_start(bme680_i2c_id);
  45. platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  46. platform_i2c_send_byte(bme680_i2c_id, reg);
  47. platform_i2c_send_byte(bme680_i2c_id, val);
  48. platform_i2c_send_stop(bme680_i2c_id);
  49. }
  50. static uint8_t r8u(uint8_t reg) {
  51. uint8_t ret[1];
  52. r8u_n(reg, 1, ret);
  53. return ret[0];
  54. }
  55. // replace 'dev->calib.' with 'bme680_data.'
  56. // replace 'dev->amb_temp' with 'amb_temp'
  57. /**\mainpage
  58. * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions are met:
  62. *
  63. * Redistributions of source code must retain the above copyright
  64. * notice, this list of conditions and the following disclaimer.
  65. *
  66. * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. *
  70. * Neither the name of the copyright holder nor the names of the
  71. * contributors may be used to endorse or promote products derived from
  72. * this software without specific prior written permission.
  73. *
  74. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  75. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  76. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  77. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  78. * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  79. * OR CONTRIBUTORS BE LIABLE FOR ANY
  80. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  81. * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
  82. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  83. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  84. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  85. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  86. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  87. * ANY WAY OUT OF THE USE OF THIS
  88. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  89. *
  90. * The information provided is believed to be accurate and reliable.
  91. * The copyright holder assumes no responsibility
  92. * for the consequences of use
  93. * of such information nor for any infringement of patents or
  94. * other rights of third parties which may result from its use.
  95. * No license is granted by implication or otherwise under any patent or
  96. * patent rights of the copyright holder.
  97. *
  98. * File bme680.c
  99. * @date 19 Jun 2018
  100. * @version 3.5.9
  101. *
  102. */
  103. static uint8_t calc_heater_res(uint16_t temp)
  104. {
  105. uint8_t heatr_res;
  106. int32_t var1;
  107. int32_t var2;
  108. int32_t var3;
  109. int32_t var4;
  110. int32_t var5;
  111. int32_t heatr_res_x100;
  112. if (temp > 400) /* Cap temperature */
  113. temp = 400;
  114. var1 = (((int32_t) amb_temp * bme680_data.par_gh3) / 1000) * 256;
  115. var2 = (bme680_data.par_gh1 + 784) * (((((bme680_data.par_gh2 + 154009) * temp * 5) / 100) + 3276800) / 10);
  116. var3 = var1 + (var2 / 2);
  117. var4 = (var3 / (bme680_data.res_heat_range + 4));
  118. var5 = (131 * bme680_data.res_heat_val) + 65536;
  119. heatr_res_x100 = (int32_t) (((var4 / var5) - 250) * 34);
  120. heatr_res = (uint8_t) ((heatr_res_x100 + 50) / 100);
  121. return heatr_res;
  122. }
  123. static uint8_t calc_heater_dur(uint16_t dur)
  124. {
  125. uint8_t factor = 0;
  126. uint8_t durval;
  127. if (dur >= 0xfc0) {
  128. durval = 0xff; /* Max duration*/
  129. } else {
  130. while (dur > 0x3F) {
  131. dur = dur / 4;
  132. factor += 1;
  133. }
  134. durval = (uint8_t) (dur + (factor * 64));
  135. }
  136. return durval;
  137. }
  138. static int16_t calc_temperature(uint32_t temp_adc)
  139. {
  140. int64_t var1;
  141. int64_t var2;
  142. int64_t var3;
  143. int16_t calc_temp;
  144. var1 = ((int32_t) temp_adc >> 3) - ((int32_t) bme680_data.par_t1 << 1);
  145. var2 = (var1 * (int32_t) bme680_data.par_t2) >> 11;
  146. var3 = ((var1 >> 1) * (var1 >> 1)) >> 12;
  147. var3 = ((var3) * ((int32_t) bme680_data.par_t3 << 4)) >> 14;
  148. bme680_data.t_fine = (int32_t) (var2 + var3);
  149. calc_temp = (int16_t) (((bme680_data.t_fine * 5) + 128) >> 8);
  150. return calc_temp;
  151. }
  152. static uint32_t calc_pressure(uint32_t pres_adc)
  153. {
  154. int32_t var1;
  155. int32_t var2;
  156. int32_t var3;
  157. int32_t pressure_comp;
  158. var1 = (((int32_t)bme680_data.t_fine) >> 1) - 64000;
  159. var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) *
  160. (int32_t)bme680_data.par_p6) >> 2;
  161. var2 = var2 + ((var1 * (int32_t)bme680_data.par_p5) << 1);
  162. var2 = (var2 >> 2) + ((int32_t)bme680_data.par_p4 << 16);
  163. var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) *
  164. ((int32_t)bme680_data.par_p3 << 5)) >> 3) +
  165. (((int32_t)bme680_data.par_p2 * var1) >> 1);
  166. var1 = var1 >> 18;
  167. var1 = ((32768 + var1) * (int32_t)bme680_data.par_p1) >> 15;
  168. pressure_comp = 1048576 - pres_adc;
  169. pressure_comp = (int32_t)((pressure_comp - (var2 >> 12)) * ((uint32_t)3125));
  170. if (pressure_comp >= BME680_MAX_OVERFLOW_VAL)
  171. pressure_comp = ((pressure_comp / var1) << 1);
  172. else
  173. pressure_comp = ((pressure_comp << 1) / var1);
  174. var1 = ((int32_t)bme680_data.par_p9 * (int32_t)(((pressure_comp >> 3) *
  175. (pressure_comp >> 3)) >> 13)) >> 12;
  176. var2 = ((int32_t)(pressure_comp >> 2) *
  177. (int32_t)bme680_data.par_p8) >> 13;
  178. var3 = ((int32_t)(pressure_comp >> 8) * (int32_t)(pressure_comp >> 8) *
  179. (int32_t)(pressure_comp >> 8) *
  180. (int32_t)bme680_data.par_p10) >> 17;
  181. pressure_comp = (int32_t)(pressure_comp) + ((var1 + var2 + var3 +
  182. ((int32_t)bme680_data.par_p7 << 7)) >> 4);
  183. return (uint32_t)pressure_comp;
  184. }
  185. static uint32_t calc_humidity(uint16_t hum_adc)
  186. {
  187. int32_t var1;
  188. int32_t var2;
  189. int32_t var3;
  190. int32_t var4;
  191. int32_t var5;
  192. int32_t var6;
  193. int32_t temp_scaled;
  194. int32_t calc_hum;
  195. temp_scaled = (((int32_t) bme680_data.t_fine * 5) + 128) >> 8;
  196. var1 = (int32_t) (hum_adc - ((int32_t) ((int32_t) bme680_data.par_h1 * 16)))
  197. - (((temp_scaled * (int32_t) bme680_data.par_h3) / ((int32_t) 100)) >> 1);
  198. var2 = ((int32_t) bme680_data.par_h2
  199. * (((temp_scaled * (int32_t) bme680_data.par_h4) / ((int32_t) 100))
  200. + (((temp_scaled * ((temp_scaled * (int32_t) bme680_data.par_h5) / ((int32_t) 100))) >> 6)
  201. / ((int32_t) 100)) + (int32_t) (1 << 14))) >> 10;
  202. var3 = var1 * var2;
  203. var4 = (int32_t) bme680_data.par_h6 << 7;
  204. var4 = ((var4) + ((temp_scaled * (int32_t) bme680_data.par_h7) / ((int32_t) 100))) >> 4;
  205. var5 = ((var3 >> 14) * (var3 >> 14)) >> 10;
  206. var6 = (var4 * var5) >> 1;
  207. calc_hum = (((var3 + var6) >> 10) * ((int32_t) 1000)) >> 12;
  208. if (calc_hum > 100000) /* Cap at 100%rH */
  209. calc_hum = 100000;
  210. else if (calc_hum < 0)
  211. calc_hum = 0;
  212. return (uint32_t) calc_hum;
  213. }
  214. /**static variables */
  215. /**Look up table 1 for the possible gas range values */
  216. uint32_t lookupTable1[16] = { UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2147483647),
  217. UINT32_C(2147483647), UINT32_C(2126008810), UINT32_C(2147483647), UINT32_C(2130303777),
  218. UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2143188679), UINT32_C(2136746228),
  219. UINT32_C(2147483647), UINT32_C(2126008810), UINT32_C(2147483647), UINT32_C(2147483647) };
  220. /**Look up table 2 for the possible gas range values */
  221. uint32_t lookupTable2[16] = { UINT32_C(4096000000), UINT32_C(2048000000), UINT32_C(1024000000), UINT32_C(512000000),
  222. UINT32_C(255744255), UINT32_C(127110228), UINT32_C(64000000), UINT32_C(32258064), UINT32_C(16016016),
  223. UINT32_C(8000000), UINT32_C(4000000), UINT32_C(2000000), UINT32_C(1000000), UINT32_C(500000),
  224. UINT32_C(250000), UINT32_C(125000) };
  225. static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range)
  226. {
  227. int64_t var1;
  228. uint64_t var2;
  229. int64_t var3;
  230. uint32_t calc_gas_res;
  231. var1 = (int64_t) ((1340 + (5 * (int64_t) bme680_data.range_sw_err)) *
  232. ((int64_t) lookupTable1[gas_range])) >> 16;
  233. var2 = (((int64_t) ((int64_t) gas_res_adc << 15) - (int64_t) (16777216)) + var1);
  234. var3 = (((int64_t) lookupTable2[gas_range] * (int64_t) var1) >> 9);
  235. calc_gas_res = (uint32_t) ((var3 + ((int64_t) var2 >> 1)) / (int64_t) var2);
  236. return calc_gas_res;
  237. }
  238. uint16_t calc_dur()
  239. {
  240. uint32_t tph_dur; /* Calculate in us */
  241. /* TPH measurement duration */
  242. tph_dur = ((uint32_t) (os_temp + os_pres + os_hum) * UINT32_C(1963));
  243. tph_dur += UINT32_C(477 * 4); /* TPH switching duration */
  244. tph_dur += UINT32_C(477 * 5); /* Gas measurement duration */
  245. tph_dur += UINT32_C(500); /* Get it to the closest whole number.*/
  246. tph_dur /= UINT32_C(1000); /* Convert to ms */
  247. tph_dur += UINT32_C(1); /* Wake up duration of 1ms */
  248. NODE_DBG("tpc_dur: %d\n", tph_dur);
  249. /* The remaining time should be used for heating */
  250. return heatr_dur + (uint16_t) tph_dur;
  251. }
  252. /* This part of code is coming from the original bme680.c driver by Bosch.
  253. * END */
  254. static double ln(double x) {
  255. double y = (x-1)/(x+1);
  256. double y2 = y*y;
  257. double r = 0;
  258. for (int8_t i=33; i>0; i-=2) { //we've got the power
  259. r = 1.0/(double)i + y2 * r;
  260. }
  261. return 2*y*r;
  262. }
  263. static double bme280_qfe2qnh(int32_t qfe, int32_t h) {
  264. double hc;
  265. if (bme680_h == h) {
  266. hc = bme680_hc;
  267. } else {
  268. hc = pow((double)(1.0 - 2.25577e-5 * h), (double)(-5.25588));
  269. bme680_hc = hc; bme680_h = h;
  270. }
  271. double qnh = (double)qfe * hc;
  272. return qnh;
  273. }
  274. static int bme680_lua_setup(lua_State* L) {
  275. uint8_t ack;
  276. bme680_i2c_addr = BME680_I2C_ADDR_PRIMARY;
  277. platform_i2c_send_start(bme680_i2c_id);
  278. ack = platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  279. platform_i2c_send_stop(bme680_i2c_id);
  280. if (!ack) {
  281. NODE_DBG("No ACK on address: %x\n", bme680_i2c_addr);
  282. bme680_i2c_addr = BME680_I2C_ADDR_SECONDARY;
  283. platform_i2c_send_start(bme680_i2c_id);
  284. ack = platform_i2c_send_address(bme680_i2c_id, bme680_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  285. platform_i2c_send_stop(bme680_i2c_id);
  286. if (!ack) {
  287. NODE_DBG("No ACK on address: %x\n", bme680_i2c_addr);
  288. return 0;
  289. }
  290. }
  291. uint8_t chipid = r8u(BME680_CHIP_ID_ADDR);
  292. NODE_DBG("chip_id: %x\n", chipid);
  293. #define r16uLE_buf(reg) (uint16_t)(((uint16_t)reg[1] << 8) | (uint16_t)reg[0])
  294. #define r16sLE_buf(reg) (int16_t)(r16uLE_buf(reg))
  295. uint8_t buff[BME680_COEFF_SIZE], *reg;
  296. r8u_n(BME680_COEFF_ADDR1, BME680_COEFF_ADDR1_LEN, buff);
  297. r8u_n(BME680_COEFF_ADDR2, BME680_COEFF_ADDR2_LEN, &buff[BME680_COEFF_ADDR1_LEN]);
  298. reg = buff + 1;
  299. bme680_data.par_t2 = r16sLE_buf(reg); reg+=2; // #define BME680_T3_REG (3)
  300. bme680_data.par_t3 = (int8_t) reg[0]; reg+=2; // #define BME680_P1_LSB_REG (5)
  301. bme680_data.par_p1 = r16uLE_buf(reg); reg+=2; // #define BME680_P2_LSB_REG (7)
  302. bme680_data.par_p2 = r16sLE_buf(reg); reg+=2; // #define BME680_P3_REG (9)
  303. bme680_data.par_p3 = (int8_t) reg[0]; reg+=2; // #define BME680_P4_LSB_REG (11)
  304. bme680_data.par_p4 = r16sLE_buf(reg); reg+=2; // #define BME680_P5_LSB_REG (13)
  305. bme680_data.par_p5 = r16sLE_buf(reg); reg+=2; // #define BME680_P7_REG (15)
  306. bme680_data.par_p7 = (int8_t) reg[0]; reg++; // #define BME680_P6_REG (16)
  307. bme680_data.par_p6 = (int8_t) reg[0]; reg+=3; // #define BME680_P8_LSB_REG (19)
  308. bme680_data.par_p8 = r16sLE_buf(reg); reg+=2; // #define BME680_P9_LSB_REG (21)
  309. bme680_data.par_p9 = r16sLE_buf(reg); reg+=2; // #define BME680_P10_REG (23)
  310. bme680_data.par_p10 = (int8_t) reg[0]; reg+=2; // #define BME680_H2_MSB_REG (25)
  311. bme680_data.par_h2 = (uint16_t) (((uint16_t) reg[0] << BME680_HUM_REG_SHIFT_VAL)
  312. | ((reg[1]) >> BME680_HUM_REG_SHIFT_VAL)); reg++; // #define BME680_H1_LSB_REG (26)
  313. bme680_data.par_h1 = (uint16_t) (((uint16_t) reg[1] << BME680_HUM_REG_SHIFT_VAL)
  314. | (reg[0] & BME680_BIT_H1_DATA_MSK)); reg+=2; // #define BME680_H3_REG (28)
  315. bme680_data.par_h3 = (int8_t) reg[0]; reg++; // #define BME680_H4_REG (29)
  316. bme680_data.par_h4 = (int8_t) reg[0]; reg++; // #define BME680_H5_REG (30)
  317. bme680_data.par_h5 = (int8_t) reg[0]; reg++; // #define BME680_H6_REG (31)
  318. bme680_data.par_h6 = (uint8_t) reg[0]; reg++; // #define BME680_H7_REG (32)
  319. bme680_data.par_h7 = (int8_t) reg[0]; reg++; // #define BME680_T1_LSB_REG (33)
  320. bme680_data.par_t1 = r16uLE_buf(reg); reg+=2; // #define BME680_GH2_LSB_REG (35)
  321. bme680_data.par_gh2 = r16sLE_buf(reg); reg+=2; // #define BME680_GH1_REG (37)
  322. bme680_data.par_gh1 = reg[0]; reg++; // #define BME680_GH3_REG (38)
  323. bme680_data.par_gh3 = reg[0];
  324. #undef r16uLE_buf
  325. #undef r16sLE_buf
  326. /* Other coefficients */
  327. bme680_data.res_heat_range = ((r8u(BME680_ADDR_RES_HEAT_RANGE_ADDR) & BME680_RHRANGE_MSK) / 16);
  328. bme680_data.res_heat_val = (int8_t) r8u(BME680_ADDR_RES_HEAT_VAL_ADDR);
  329. bme680_data.range_sw_err = ((int8_t) r8u(BME680_ADDR_RANGE_SW_ERR_ADDR) & (int8_t) BME680_RSERROR_MSK) / 16;
  330. NODE_DBG("par_T: %d\t%d\t%d\n", bme680_data.par_t1, bme680_data.par_t2, bme680_data.par_t3);
  331. NODE_DBG("par_P: %d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", bme680_data.par_p1, bme680_data.par_p2, bme680_data.par_p3, bme680_data.par_p4, bme680_data.par_p5, bme680_data.par_p6, bme680_data.par_p7, bme680_data.par_p8, bme680_data.par_p9, bme680_data.par_p10);
  332. NODE_DBG("par_H: %d\t%d\t%d\t%d\t%d\t%d\t%d\n", bme680_data.par_h1, bme680_data.par_h2, bme680_data.par_h3, bme680_data.par_h4, bme680_data.par_h5, bme680_data.par_h6, bme680_data.par_h7);
  333. NODE_DBG("par_GH: %d\t%d\t%d\n", bme680_data.par_gh1, bme680_data.par_gh2, bme680_data.par_gh3);
  334. NODE_DBG("res_heat_range, res_heat_val, range_sw_err: %d\t%d\t%d\n", bme680_data.res_heat_range, bme680_data.res_heat_val, bme680_data.range_sw_err);
  335. uint8_t full_init = !lua_isnumber(L, 7)?1:lua_tointeger(L, 7); // 7-th parameter: init the chip too
  336. if (full_init) {
  337. uint8_t filter;
  338. uint8_t const bit3 = 0b111;
  339. uint8_t const bit2 = 0b11;
  340. //bme680.setup([temp_oss, press_oss, humi_oss, heater_temp, heater_duration, IIR_filter])
  341. os_temp = (!lua_isnumber(L, 1)?BME680_OS_2X:(luaL_checkinteger(L, 1)&bit3)); // 1-st parameter: temperature oversampling
  342. os_pres = (!lua_isnumber(L, 2)?BME680_OS_16X:(luaL_checkinteger(L, 2)&bit3)); // 2-nd parameter: pressure oversampling
  343. os_hum = (!lua_isnumber(L, 3))?BME680_OS_1X:(luaL_checkinteger(L, 3)&bit3);
  344. bme680_mode = BME680_SLEEP_MODE | (os_pres << 2) | (os_temp << 5);
  345. os_hum = os_hum; // 3-rd parameter: humidity oversampling
  346. filter = ((!lua_isnumber(L, 6)?BME680_FILTER_SIZE_31:(luaL_checkinteger(L, 6)&bit3)) << 2); // 6-th parameter: IIR filter
  347. NODE_DBG("mode: %x\nhumidity oss: %x\nconfig: %x\n", bme680_mode, os_hum, filter);
  348. heatr_dur = (!lua_isnumber(L, 5)?DEFAULT_HEATER_DUR:(luaL_checkinteger(L, 5))); // 5-th parameter: heater duration
  349. w8u(BME680_GAS_WAIT0_ADDR, calc_heater_dur(heatr_dur));
  350. w8u(BME680_RES_HEAT0_ADDR, calc_heater_res((!lua_isnumber(L, 4)?DEFAULT_HEATER_TEMP:(luaL_checkinteger(L, 4))))); // 4-th parameter: heater temperature
  351. w8u(BME680_CONF_ODR_FILT_ADDR, BME680_SET_BITS_POS_0(r8u(BME680_CONF_ODR_FILT_ADDR), BME680_FILTER, filter)); // #define BME680_CONF_ODR_FILT_ADDR UINT8_C(0x75)
  352. // set heater on
  353. w8u(BME680_CONF_HEAT_CTRL_ADDR, BME680_SET_BITS_POS_0(r8u(BME680_CONF_HEAT_CTRL_ADDR), BME680_HCTRL, 1));
  354. w8u(BME680_CONF_T_P_MODE_ADDR, bme680_mode);
  355. w8u(BME680_CONF_OS_H_ADDR, BME680_SET_BITS_POS_0(r8u(BME680_CONF_OS_H_ADDR), BME680_OSH, os_hum));
  356. w8u(BME680_CONF_ODR_RUN_GAS_NBC_ADDR, 1 << 4 | 0 & bit3);
  357. }
  358. lua_pushinteger(L, 1);
  359. return 1;
  360. }
  361. static void bme280_readoutdone (void *arg)
  362. {
  363. NODE_DBG("timer out\n");
  364. lua_State *L = lua_getstate();
  365. lua_rawgeti (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
  366. luaL_unref (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
  367. os_timer_disarm (&bme680_timer);
  368. luaL_pcallx (L, 0, 0);
  369. }
  370. static int bme680_lua_startreadout(lua_State* L) {
  371. uint32_t delay;
  372. if (lua_isnumber(L, 1)) {
  373. delay = luaL_checkinteger(L, 1);
  374. if (!delay) {delay = calc_dur();} // if delay is 0 then set the default delay
  375. }
  376. if (!lua_isnoneornil(L, 2)) {
  377. lua_pushvalue(L, 2);
  378. lua_connected_readout_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  379. } else {
  380. lua_connected_readout_ref = LUA_NOREF;
  381. }
  382. w8u(BME680_CONF_OS_H_ADDR, os_hum);
  383. w8u(BME680_CONF_T_P_MODE_ADDR, (bme680_mode & 0xFC) | BME680_FORCED_MODE);
  384. NODE_DBG("control old: %x, control: %x, delay: %d\n", bme680_mode, (bme680_mode & 0xFC) | BME680_FORCED_MODE, delay);
  385. if (lua_connected_readout_ref != LUA_NOREF) {
  386. NODE_DBG("timer armed\n");
  387. os_timer_disarm (&bme680_timer);
  388. os_timer_setfn (&bme680_timer, (os_timer_func_t *)bme280_readoutdone, L);
  389. os_timer_arm (&bme680_timer, delay, 0); // trigger callback when readout is ready
  390. }
  391. return 0;
  392. }
  393. // Return nothing on failure
  394. // Return T, QFE, H if no altitude given
  395. // Return T, QFE, H, QNH if altitude given
  396. static int bme680_lua_read(lua_State* L) {
  397. uint8_t buff[BME680_FIELD_LENGTH] = { 0 };
  398. uint8_t gas_range;
  399. uint32_t adc_temp;
  400. uint32_t adc_pres;
  401. uint16_t adc_hum;
  402. uint16_t adc_gas_res;
  403. uint8_t status;
  404. uint32_t qfe;
  405. uint8_t calc_qnh = lua_isnumber(L, 1);
  406. r8u_n(BME680_FIELD0_ADDR, BME680_FIELD_LENGTH, buff);
  407. status = buff[0] & BME680_NEW_DATA_MSK;
  408. /* read the raw data from the sensor */
  409. adc_pres = (uint32_t) (((uint32_t) buff[2] * 4096) | ((uint32_t) buff[3] * 16) | ((uint32_t) buff[4] / 16));
  410. adc_temp = (uint32_t) (((uint32_t) buff[5] * 4096) | ((uint32_t) buff[6] * 16) | ((uint32_t) buff[7] / 16));
  411. adc_hum = (uint16_t) (((uint32_t) buff[8] * 256) | (uint32_t) buff[9]);
  412. adc_gas_res = (uint16_t) ((uint32_t) buff[13] * 4 | (((uint32_t) buff[14]) / 64));
  413. gas_range = buff[14] & BME680_GAS_RANGE_MSK;
  414. status |= buff[14] & BME680_GASM_VALID_MSK;
  415. status |= buff[14] & BME680_HEAT_STAB_MSK;
  416. NODE_DBG("status, new_data, gas_range, gasm_valid: 0x%x, 0x%x, 0x%x, 0x%x\n", status, status & BME680_NEW_DATA_MSK, buff[14] & BME680_GAS_RANGE_MSK, buff[14] & BME680_GASM_VALID_MSK);
  417. if (!(status & BME680_NEW_DATA_MSK)) {
  418. return 0;
  419. }
  420. int16_t temp = calc_temperature(adc_temp);
  421. amb_temp = temp / 100;
  422. lua_pushinteger(L, temp);
  423. qfe = calc_pressure(adc_pres);
  424. lua_pushinteger(L, qfe);
  425. lua_pushinteger(L, calc_humidity(adc_hum));
  426. lua_pushinteger(L, calc_gas_resistance(adc_gas_res, gas_range));
  427. if (calc_qnh) { // have altitude
  428. int32_t h = luaL_checkinteger(L, 1);
  429. double qnh = bme280_qfe2qnh(qfe, h);
  430. lua_pushinteger(L, (int32_t)(qnh + 0.5));
  431. return 5;
  432. }
  433. return 4;
  434. }
  435. static int bme680_lua_qfe2qnh(lua_State* L) {
  436. if (!lua_isnumber(L, 2)) {
  437. return luaL_error(L, "wrong arg range");
  438. }
  439. int32_t qfe = luaL_checkinteger(L, 1);
  440. int32_t h = luaL_checkinteger(L, 2);
  441. double qnh = bme280_qfe2qnh(qfe, h);
  442. lua_pushinteger(L, (int32_t)(qnh + 0.5));
  443. return 1;
  444. }
  445. static int bme680_lua_altitude(lua_State* L) {
  446. if (!lua_isnumber(L, 2)) {
  447. return luaL_error(L, "wrong arg range");
  448. }
  449. int32_t P = luaL_checkinteger(L, 1);
  450. int32_t qnh = luaL_checkinteger(L, 2);
  451. double h = (1.0 - pow((double)P/(double)qnh, 1.0/5.25588)) / 2.25577e-5 * 100.0;
  452. lua_pushinteger(L, (int32_t)(h + (((h<0)?-1:(h>0)) * 0.5)));
  453. return 1;
  454. }
  455. static int bme680_lua_dewpoint(lua_State* L) {
  456. if (!lua_isnumber(L, 2)) {
  457. return luaL_error(L, "wrong arg range");
  458. }
  459. double H = luaL_checkinteger(L, 1)/100000.0;
  460. double T = luaL_checkinteger(L, 2)/100.0;
  461. const double c243 = 243.5;
  462. const double c17 = 17.67;
  463. double c = ln(H) + ((c17 * T) / (c243 + T));
  464. double d = (c243 * c)/(c17 - c) * 100.0;
  465. lua_pushinteger(L, (int32_t)(d + (((d<0)?-1:(d>0)) * 0.5)));
  466. return 1;
  467. }
  468. LROT_BEGIN(bme680, NULL, 0)
  469. LROT_FUNCENTRY( setup, bme680_lua_setup )
  470. LROT_FUNCENTRY( startreadout, bme680_lua_startreadout )
  471. LROT_FUNCENTRY( qfe2qnh, bme680_lua_qfe2qnh )
  472. LROT_FUNCENTRY( altitude, bme680_lua_altitude )
  473. LROT_FUNCENTRY( dewpoint, bme680_lua_dewpoint )
  474. LROT_FUNCENTRY( read, bme680_lua_read )
  475. LROT_END(bme680, NULL, 0)
  476. NODEMCU_MODULE(BME680, "bme680", bme680, NULL);