浏览代码

Hdc1080 Module Add (#1880)

* Hdc1080 Module Add

* for Float/Integer Build fixes

* disable module for default

* document fix

* Deprecate init() in favor of setup()

* Fix grammar and code sample

* Deprecate init() in favor of setup()

* Update hdc1080.md

* Update hdc1080.md

* Mini fix

* Update user_modules.h
Metin KOC 7 年之前
父节点
当前提交
9d11543fa9
共有 4 个文件被更改,包括 196 次插入0 次删除
  1. 2 0
      app/include/user_modules.h
  2. 136 0
      app/modules/hdc1080.c
  3. 56 0
      docs/en/modules/hdc1080.md
  4. 2 0
      mkdocs.yml

+ 2 - 0
app/include/user_modules.h

@@ -33,6 +33,7 @@
 #define LUA_USE_MODULES_FILE
 //#define LUA_USE_MODULES_GDBSTUB
 #define LUA_USE_MODULES_GPIO
+//#define LUA_USE_MODULES_HDC1080
 //#define LUA_USE_MODULES_HMC5883L
 //#define LUA_USE_MODULES_HTTP
 //#define LUA_USE_MODULES_HX711
@@ -76,3 +77,4 @@
 
 #endif  /* LUA_CROSS_COMPILER */
 #endif	/* __USER_MODULES_H__ */
+

+ 136 - 0
app/modules/hdc1080.c

@@ -0,0 +1,136 @@
+/*
+ * Driver for TI Texas Instruments HDC1080 Temperature/Humidity Sensor.
+ * Code By Metin KOC 
+ * Sixfab Inc. metin@sixfab.com
+ * Code based on ADXL345 driver.
+ */
+#include "module.h"
+#include "lauxlib.h"
+#include "platform.h"
+#include "c_stdlib.h"
+#include "c_string.h"
+#include "c_math.h"
+
+static const uint32_t hdc1080_i2c_id = 0;
+static const uint8_t hdc1080_i2c_addr = 0x40;
+
+
+#define HDC1080_TEMPERATURE_REGISTER	0X00
+#define HDC1080_HUMIDITY_REGISTER	0X01
+#define HDC1080_CONFIG_REGISTER		0X02
+
+
+static int hdc1080_setup(lua_State* L) {
+
+	// Configure Sensor
+    platform_i2c_send_start(hdc1080_i2c_id);
+    platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
+    platform_i2c_send_byte(hdc1080_i2c_id, HDC1080_CONFIG_REGISTER); 
+    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.
+    platform_i2c_send_byte(hdc1080_i2c_id, 0x00);
+    platform_i2c_send_stop(hdc1080_i2c_id);
+    
+    return 0;
+}
+
+static int hdc1080_init(lua_State* L) {
+
+	uint32_t sda;
+    uint32_t scl;
+    
+    platform_print_deprecation_note("hdc1080.init() is replaced by hdc1080.setup()", "in the next version");
+    
+    if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
+        return luaL_error(L, "wrong arg range");
+    }
+
+    sda = luaL_checkinteger(L, 1);
+    scl = luaL_checkinteger(L, 2);
+    
+    if (scl == 0 || sda == 0) {
+        return luaL_error(L, "no i2c for D0");
+    }
+
+    platform_i2c_setup(hdc1080_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
+    
+    // remove sda and scl parameters from stack
+    lua_remove(L, 1);
+    lua_remove(L, 1);
+    
+    return hdc1080_setup(L);
+}
+
+static int hdc1080_read(lua_State* L) {
+
+    uint8_t data[2];
+    
+    #ifdef LUA_NUMBER_INTEGRAL
+    	int temp;
+    	int humidity;
+    #else
+    	float temp;
+    	float humidity;
+    #endif
+    
+    int i;
+
+    platform_i2c_send_start(hdc1080_i2c_id);
+    platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
+    platform_i2c_send_byte(hdc1080_i2c_id, HDC1080_TEMPERATURE_REGISTER);
+    platform_i2c_send_stop(hdc1080_i2c_id);
+
+    os_delay_us(7000);
+
+    platform_i2c_send_start(hdc1080_i2c_id);
+    platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
+
+    for (i=0; i<2; i++) {
+		data[i] = platform_i2c_recv_byte(hdc1080_i2c_id, 1);
+    }
+
+    platform_i2c_send_stop(hdc1080_i2c_id);
+
+	#ifdef LUA_NUMBER_INTEGRAL
+    	temp = ((((data[0]<<8)|data[1])*165)>>16)-40;
+    	lua_pushinteger(L, (int)temp);
+    #else
+    	temp = ((float)((data[0]<<8)|data[1])/(float)pow(2,16))*165.0f-40.0f;
+    	lua_pushnumber(L, temp);
+    #endif
+    
+    
+    platform_i2c_send_start(hdc1080_i2c_id);
+    platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
+    platform_i2c_send_byte(hdc1080_i2c_id, HDC1080_HUMIDITY_REGISTER);
+    platform_i2c_send_stop(hdc1080_i2c_id);
+
+    os_delay_us(7000);
+
+    platform_i2c_send_start(hdc1080_i2c_id);
+    platform_i2c_send_address(hdc1080_i2c_id, hdc1080_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
+
+    for (i=0; i<2; i++) {
+		data[i] = platform_i2c_recv_byte(hdc1080_i2c_id, 1);
+    }
+
+    platform_i2c_send_stop(hdc1080_i2c_id);
+
+	#ifdef LUA_NUMBER_INTEGRAL
+    	humidity = ((((data[0]<<8)|data[1]))*100)>>16;
+    	lua_pushinteger(L, (int)humidity);
+    #else
+    	humidity = ((float)((data[0]<<8)|data[1])/(float)pow(2,16))*100.0f;
+    	lua_pushnumber(L, humidity);
+    #endif
+    
+    return 2;
+}
+
+static const LUA_REG_TYPE hdc1080_map[] = {
+    { LSTRKEY( "read"  ),        LFUNCVAL( hdc1080_read )},
+    { LSTRKEY( "setup" ),        LFUNCVAL( hdc1080_setup )},
+    { LSTRKEY( "init" ),         LFUNCVAL( hdc1080_init )},
+    { LNILKEY, LNILVAL}
+};
+
+NODEMCU_MODULE(HDC1080, "hdc1080", hdc1080_map, NULL);

+ 56 - 0
docs/en/modules/hdc1080.md

@@ -0,0 +1,56 @@
+# HDC1080 Module
+| Since  | Origin / Contributor  | Maintainer  | Source  |
+| :----- | :-------------------- | :---------- | :------ |
+| 2017-04-01 | [Metin KOC](https://github.com/saucompeng) | [Metin KOC](https://github.com/saucompeng) | [hdc1080.c](../../../app/modules/hdc1080.c)|
+
+
+This module provides access to the [HDC1080](http://www.ti.com/product/HDC1080) low power, high accuracy digital humidity sensor with temperature sensor.
+
+## hdc1080.read()
+Samples the sensor then returns temperature and humidity value.
+
+#### Syntax
+`hdc1080.read()`
+
+#### Returns
+Temperature data in centigrade and humidity data in percentage (0-100) (integer/float)
+
+#### Example
+```lua
+local sda, scl = 1, 2
+i2c.setup(0, sda, scl, i2c.SLOW)  -- call i2c.setup() only once
+hdc1080.setup()
+local temperature,humidity = hdc1080.read()
+print(temperature)
+print(humidity)
+```
+
+## hdc1080.setup()
+Initializes the module.
+
+#### Syntax
+`hdc1080.setup()`
+
+#### Parameters
+- None
+
+#### Returns
+`nil`
+
+
+## hdc1080.init(sda,scl)
+Initializes the module and sets the pin configuration.
+
+!!! attention
+
+	 This function is deprecated and will be removed in upcoming releases. Use `hdc1080.setup()` instead.
+
+#### Syntax
+`hdc1080.init(sda, scl)`
+
+#### Parameters
+- `sda` data pin
+- `scl` clock pin
+
+#### Returns
+`nil`

+ 2 - 0
mkdocs.yml

@@ -52,6 +52,7 @@ pages:
         - 'file': 'en/modules/file.md'
         - 'gdbstub': 'en/modules/gdbstub.md'
         - 'gpio': 'en/modules/gpio.md'
+        - 'hdc1080': 'en/modules/hdc1080.md'
         - 'hmc5883l': 'en/modules/hmc5883l.md'
         - 'http': 'en/modules/http.md'
         - 'hx711' : 'en/modules/hx711.md'
@@ -94,3 +95,4 @@ pages:
         - 'xpt2046': 'en/modules/xpt2046.md'
 #- Deutsch:
 #    - Home: 'de/index.md'
+