hdc1080.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Driver for TI Texas Instruments HDC1080 Temperature/Humidity Sensor.
  3. * Code By Metin KOC
  4. * Sixfab Inc. metin@sixfab.com
  5. * Code based on ADXL345 driver.
  6. */
  7. #include "module.h"
  8. #include "lauxlib.h"
  9. #include "platform.h"
  10. #include "c_stdlib.h"
  11. #include "c_string.h"
  12. #include "c_math.h"
  13. static const uint32_t hdc1080_i2c_id = 0;
  14. static const uint8_t hdc1080_i2c_addr = 0x40;
  15. #define HDC1080_TEMPERATURE_REGISTER 0X00
  16. #define HDC1080_HUMIDITY_REGISTER 0X01
  17. #define HDC1080_CONFIG_REGISTER 0X02
  18. static int hdc1080_setup(lua_State* L) {
  19. // Configure Sensor
  20. platform_i2c_send_start(hdc1080_i2c_id);
  21. platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  22. platform_i2c_send_byte(hdc1080_i2c_id, HDC1080_CONFIG_REGISTER);
  23. platform_i2c_send_byte(hdc1080_i2c_id, 0x05); //Bit[10] to 1 for 11 bit resolution , Set Bit[9:8] to 01 for 11 bit resolution.
  24. platform_i2c_send_byte(hdc1080_i2c_id, 0x00);
  25. platform_i2c_send_stop(hdc1080_i2c_id);
  26. return 0;
  27. }
  28. static int hdc1080_init(lua_State* L) {
  29. uint32_t sda;
  30. uint32_t scl;
  31. platform_print_deprecation_note("hdc1080.init() is replaced by hdc1080.setup()", "in the next version");
  32. if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
  33. return luaL_error(L, "wrong arg range");
  34. }
  35. sda = luaL_checkinteger(L, 1);
  36. scl = luaL_checkinteger(L, 2);
  37. if (scl == 0 || sda == 0) {
  38. return luaL_error(L, "no i2c for D0");
  39. }
  40. platform_i2c_setup(hdc1080_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
  41. // remove sda and scl parameters from stack
  42. lua_remove(L, 1);
  43. lua_remove(L, 1);
  44. return hdc1080_setup(L);
  45. }
  46. static int hdc1080_read(lua_State* L) {
  47. uint8_t data[2];
  48. #ifdef LUA_NUMBER_INTEGRAL
  49. int temp;
  50. int humidity;
  51. #else
  52. float temp;
  53. float humidity;
  54. #endif
  55. int i;
  56. platform_i2c_send_start(hdc1080_i2c_id);
  57. platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  58. platform_i2c_send_byte(hdc1080_i2c_id, HDC1080_TEMPERATURE_REGISTER);
  59. platform_i2c_send_stop(hdc1080_i2c_id);
  60. os_delay_us(7000);
  61. platform_i2c_send_start(hdc1080_i2c_id);
  62. platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  63. for (i=0; i<2; i++) {
  64. data[i] = platform_i2c_recv_byte(hdc1080_i2c_id, 1);
  65. }
  66. platform_i2c_send_stop(hdc1080_i2c_id);
  67. #ifdef LUA_NUMBER_INTEGRAL
  68. temp = ((((data[0]<<8)|data[1])*165)>>16)-40;
  69. lua_pushinteger(L, (int)temp);
  70. #else
  71. temp = ((float)((data[0]<<8)|data[1])/(float)pow(2,16))*165.0f-40.0f;
  72. lua_pushnumber(L, temp);
  73. #endif
  74. platform_i2c_send_start(hdc1080_i2c_id);
  75. platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  76. platform_i2c_send_byte(hdc1080_i2c_id, HDC1080_HUMIDITY_REGISTER);
  77. platform_i2c_send_stop(hdc1080_i2c_id);
  78. os_delay_us(7000);
  79. platform_i2c_send_start(hdc1080_i2c_id);
  80. platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
  81. for (i=0; i<2; i++) {
  82. data[i] = platform_i2c_recv_byte(hdc1080_i2c_id, 1);
  83. }
  84. platform_i2c_send_stop(hdc1080_i2c_id);
  85. #ifdef LUA_NUMBER_INTEGRAL
  86. humidity = ((((data[0]<<8)|data[1]))*100)>>16;
  87. lua_pushinteger(L, (int)humidity);
  88. #else
  89. humidity = ((float)((data[0]<<8)|data[1])/(float)pow(2,16))*100.0f;
  90. lua_pushnumber(L, humidity);
  91. #endif
  92. return 2;
  93. }
  94. static const LUA_REG_TYPE hdc1080_map[] = {
  95. { LSTRKEY( "read" ), LFUNCVAL( hdc1080_read )},
  96. { LSTRKEY( "setup" ), LFUNCVAL( hdc1080_setup )},
  97. { LSTRKEY( "init" ), LFUNCVAL( hdc1080_init )},
  98. { LNILKEY, LNILVAL}
  99. };
  100. NODEMCU_MODULE(HDC1080, "hdc1080", hdc1080_map, NULL);