From 515a7bf1ab829e3246582c69eab06d39ef3ed939 Mon Sep 17 00:00:00 2001 From: vowstar Date: Wed, 17 Jun 2015 16:30:12 +0800 Subject: [PATCH 1/8] Add dhtlib for DHT11/21/22/33/44, port from Arduino. --- app/Makefile | 148 +++++++++++++++---------------- app/dhtlib/Makefile | 48 +++++++++++ app/dhtlib/dht.c | 206 ++++++++++++++++++++++++++++++++++++++++++++ app/dhtlib/dht.h | 63 ++++++++++++++ 4 files changed, 392 insertions(+), 73 deletions(-) create mode 100644 app/dhtlib/Makefile create mode 100644 app/dhtlib/dht.c create mode 100644 app/dhtlib/dht.h diff --git a/app/Makefile b/app/Makefile index f52b890b..fbc69d78 100644 --- a/app/Makefile +++ b/app/Makefile @@ -21,24 +21,25 @@ ifndef PDIR # { GEN_IMAGES= eagle.app.v6.out GEN_BINS= eagle.app.v6.bin SPECIAL_MKTARGETS=$(APP_MKTARGETS) -SUBDIRS= \ - user \ - driver \ - lwip \ - json \ - upgrade \ - platform \ - libc \ - lua \ - coap \ - mqtt \ - u8glib \ - smart \ - wofs \ - modules \ - spiffs \ - cjson \ - crypto \ +SUBDIRS= \ + user \ + driver \ + lwip \ + json \ + upgrade \ + platform \ + libc \ + lua \ + coap \ + mqtt \ + u8glib \ + smart \ + wofs \ + modules \ + spiffs \ + cjson \ + crypto \ + dhtlib endif # } PDIR @@ -47,10 +48,10 @@ LDDIR = ../ld CCFLAGS += -Os -TARGET_LDFLAGS = \ - -nostdlib \ - -Wl,-EL \ - --longcalls \ +TARGET_LDFLAGS = \ + -nostdlib \ + -Wl,-EL \ + --longcalls \ --text-section-literals ifeq ($(FLAVOR),debug) @@ -71,52 +72,53 @@ ifeq ($(APP), 2) LD_FILE = $(LDDIR)/eagle.app.v6.app2.ld endif -COMPONENTS_eagle.app.v6 = \ - user/libuser.a \ - driver/libdriver.a \ - lwip/liblwip.a \ - json/libjson.a \ - upgrade/libupgrade.a \ - platform/libplatform.a \ - libc/liblibc.a \ - lua/liblua.a \ - coap/coap.a \ - mqtt/mqtt.a \ - u8glib/u8glib.a \ - smart/smart.a \ - wofs/wofs.a \ - spiffs/spiffs.a \ - cjson/libcjson.a \ - crypto/libcrypto.a \ +COMPONENTS_eagle.app.v6 = \ + user/libuser.a \ + driver/libdriver.a \ + lwip/liblwip.a \ + json/libjson.a \ + upgrade/libupgrade.a \ + platform/libplatform.a \ + libc/liblibc.a \ + lua/liblua.a \ + coap/coap.a \ + mqtt/mqtt.a \ + u8glib/u8glib.a \ + smart/smart.a \ + wofs/wofs.a \ + spiffs/spiffs.a \ + cjson/libcjson.a \ + crypto/libcrypto.a \ + dhtlib/libdhtlib.a \ modules/libmodules.a -LINKFLAGS_eagle.app.v6 = \ - -L../lib \ - -Wl,--gc-sections \ - -Xlinker -Map=mapfile \ - -nostdlib \ - -T$(LD_FILE) \ - -Wl,--no-check-sections \ - -u call_user_start \ - -Wl,-static \ - -Wl,--start-group \ - -lc \ - -lgcc \ - -lhal \ - -lphy \ - -lpp \ - -lnet80211 \ - -lwpa \ - -lmain \ - -ljson \ - -lsmartconfig \ - -lssl \ - $(DEP_LIBS_eagle.app.v6) \ - -Wl,--end-group \ +LINKFLAGS_eagle.app.v6 = \ + -L../lib \ + -Wl,--gc-sections \ + -Xlinker -Map=mapfile \ + -nostdlib \ + -T$(LD_FILE) \ + -Wl,--no-check-sections \ + -u call_user_start \ + -Wl,-static \ + -Wl,--start-group \ + -lc \ + -lgcc \ + -lhal \ + -lphy \ + -lpp \ + -lnet80211 \ + -lwpa \ + -lmain \ + -ljson \ + -lsmartconfig \ + -lssl \ + $(DEP_LIBS_eagle.app.v6) \ + -Wl,--end-group \ -lm -DEPENDS_eagle.app.v6 = \ - $(LD_FILE) \ +DEPENDS_eagle.app.v6 = \ + $(LD_FILE) \ $(LDDIR)/eagle.rom.addr.v6.ld ############################################################# @@ -127,24 +129,24 @@ DEPENDS_eagle.app.v6 = \ # for a subtree within the makefile rooted therein # -#UNIVERSAL_TARGET_DEFINES = \ +#UNIVERSAL_TARGET_DEFINES = \ # Other potential configuration flags include: # -DTXRX_TXBUF_DEBUG # -DTXRX_RXBUF_DEBUG # -DWLAN_CONFIG_CCX -CONFIGURATION_DEFINES = -D__ets__ \ - -DICACHE_FLASH \ - -DLWIP_OPEN_SRC \ - -DPBUF_RSV_FOR_WLAN \ +CONFIGURATION_DEFINES = -D__ets__ \ + -DICACHE_FLASH \ + -DLWIP_OPEN_SRC \ + -DPBUF_RSV_FOR_WLAN \ -DEBUF_LWIP -DEFINES += \ - $(UNIVERSAL_TARGET_DEFINES) \ +DEFINES += \ + $(UNIVERSAL_TARGET_DEFINES) \ $(CONFIGURATION_DEFINES) -DDEFINES += \ - $(UNIVERSAL_TARGET_DEFINES) \ +DDEFINES += \ + $(UNIVERSAL_TARGET_DEFINES) \ $(CONFIGURATION_DEFINES) diff --git a/app/dhtlib/Makefile b/app/dhtlib/Makefile new file mode 100644 index 00000000..838d3b80 --- /dev/null +++ b/app/dhtlib/Makefile @@ -0,0 +1,48 @@ + +############################################################# +# Required variables for each makefile +# Discard this section from all parent makefiles +# Expected variables (with automatic defaults): +# CSRCS (all "C" files in the dir) +# SUBDIRS (all subdirs with a Makefile) +# GEN_LIBS - list of libs to be generated () +# GEN_IMAGES - list of images to be generated () +# COMPONENTS_xxx - a list of libs/objs in the form +# subdir/lib to be extracted and rolled up into +# a generated lib/image xxx.a () +# +ifndef PDIR +GEN_LIBS = libdhtlib.a +endif + + +############################################################# +# Configuration i.e. compile options etc. +# Target specific stuff (defines etc.) goes in here! +# Generally values applying to a tree are captured in the +# makefile at its root level - these are then overridden +# for a subtree within the makefile rooted therein +# +#DEFINES += + +############################################################# +# Recursion Magic - Don't touch this!! +# +# Each subtree potentially has an include directory +# corresponding to the common APIs applicable to modules +# rooted at that subtree. Accordingly, the INCLUDE PATH +# of a module can only contain the include directories up +# its parent path, and not its siblings +# +# Required for each makefile to inherit from the parent +# + +INCLUDES := $(INCLUDES) -I $(PDIR)include +INCLUDES += -I ./ +INCLUDES += -I ./include +INCLUDES += -I ../include +INCLUDES += -I ../../include +INCLUDES += -I ../platform +PDIR := ../$(PDIR) +sinclude $(PDIR)Makefile + diff --git a/app/dhtlib/dht.c b/app/dhtlib/dht.c new file mode 100644 index 00000000..f1f59784 --- /dev/null +++ b/app/dhtlib/dht.c @@ -0,0 +1,206 @@ +// +// FILE: dht.cpp +// AUTHOR: Rob Tillaart +// VERSION: 0.1.14 +// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino +// URL: http://arduino.cc/playground/Main/DHTLib +// +// HISTORY: +// 0.1.14 replace digital read with faster (~3x) code => more robust low MHz machines. +// 0.1.13 fix negative dht_temperature +// 0.1.12 support DHT33 and DHT44 initial version +// 0.1.11 renamed DHTLIB_TIMEOUT +// 0.1.10 optimized faster WAKEUP + TIMEOUT +// 0.1.09 optimize size: timeout check + use of mask +// 0.1.08 added formula for timeout based upon clockspeed +// 0.1.07 added support for DHT21 +// 0.1.06 minimize footprint (2012-12-27) +// 0.1.05 fixed negative dht_temperature bug (thanks to Roseman) +// 0.1.04 improved readability of code using DHTLIB_OK in code +// 0.1.03 added error values for temp and dht_humidity when read failed +// 0.1.02 added error codes +// 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011) +// 0.1.00 by Rob Tillaart (01/04/2011) +// +// inspired by DHT11 library +// +// Released to the public domain +// + +#include "platform.h" +#include "dht.h" + +#ifndef LOW +#define LOW 0 +#endif /* ifndef LOW */ + +#ifndef HIGH +#define HIGH 1 +#endif /* ifndef HIGH */ + +#define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) ((byte_high << 8) | (byte_low)) + +static double dht_humidity; +static double dht_temperature; + +static uint8_t dht_bits[5]; // buffer to receive data +static int dht_readSensor(uint8_t pin, uint8_t wakeupDelay); + +///////////////////////////////////////////////////// +// +// PUBLIC +// + +// return values: +// DHTLIB_OK +// DHTLIB_ERROR_CHECKSUM +// DHTLIB_ERROR_TIMEOUT +int dht_read11(uint8_t pin) +{ + // READ VALUES + int rv = dht_readSensor(pin, DHTLIB_DHT11_WAKEUP); + if (rv != DHTLIB_OK) + { + dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? + dht_temperature = DHTLIB_INVALID_VALUE; // invalid value + return rv; + } + + // CONVERT AND STORE + dht_humidity = dht_bits[0]; // dht_bits[1] == 0; + dht_temperature = dht_bits[2]; // dht_bits[3] == 0; + + // TEST CHECKSUM + // dht_bits[1] && dht_bits[3] both 0 + uint8_t sum = dht_bits[0] + dht_bits[2]; + if (dht_bits[4] != sum) return DHTLIB_ERROR_CHECKSUM; + + return DHTLIB_OK; +} + + +// return values: +// DHTLIB_OK +// DHTLIB_ERROR_CHECKSUM +// DHTLIB_ERROR_TIMEOUT +int dht_read(uint8_t pin) +{ + // READ VALUES + int rv = dht_readSensor(pin, DHTLIB_DHT_WAKEUP); + if (rv != DHTLIB_OK) + { + dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? + dht_temperature = DHTLIB_INVALID_VALUE; // invalid value + return rv; // propagate error value + } + + // CONVERT AND STORE + dht_humidity = COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1; + dht_temperature = COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1; + if (dht_bits[2] & 0x80) // negative dht_temperature + { + dht_temperature = -dht_temperature; + } + + // TEST CHECKSUM + uint8_t sum = dht_bits[0] + dht_bits[1] + dht_bits[2] + dht_bits[3]; + if (dht_bits[4] != sum) + { + return DHTLIB_ERROR_CHECKSUM; + } + return DHTLIB_OK; +} + +///////////////////////////////////////////////////// +// +// PRIVATE +// + +// return values: +// DHTLIB_OK +// DHTLIB_ERROR_TIMEOUT +int dht_readSensor(uint8_t pin, uint8_t wakeupDelay) +{ + // INIT BUFFERVAR TO RECEIVE DATA + uint8_t mask = 128; + uint8_t idx = 0; + uint8_t i = 0; + + // replace digitalRead() with Direct Port Reads. + // reduces footprint ~100 bytes => portability issue? + // direct port read is about 3x faster + // uint8_t bit = digitalPinToBitMask(pin); + // uint8_t port = digitalPinToPort(pin); + // volatile uint8_t *PIR = portInputRegister(port); + + // EMPTY BUFFER + for (i = 0; i < 5; i++) dht_bits[i] = 0; + + // REQUEST SAMPLE + // pinMode(pin, OUTPUT); + DIRECT_MODE_OUTPUT(pin); + // digitalWrite(pin, LOW); // T-be + DIRECT_WRITE_LOW(pin); + // delay(wakeupDelay); + for (i = 0; i < wakeupDelay; i++) os_delay_us(1000); + // digitalWrite(pin, HIGH); // T-go + DIRECT_WRITE_HIGH(pin); + os_delay_us(40); + // pinMode(pin, INPUT); + DIRECT_MODE_INPUT(pin); + + // GET ACKNOWLEDGE or TIMEOUT + uint16_t loopCntLOW = DHTLIB_TIMEOUT; + while (DIRECT_READ(pin) == LOW ) // T-rel + { + os_delay_us(1); + if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT; + } + + uint16_t loopCntHIGH = DHTLIB_TIMEOUT; + while (DIRECT_READ(pin) != LOW ) // T-reh + { + os_delay_us(1); + if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT; + } + + // READ THE OUTPUT - 40 BITS => 5 BYTES + for (i = 40; i != 0; i--) + { + loopCntLOW = DHTLIB_TIMEOUT; + while (DIRECT_READ(pin) == LOW ) + { + os_delay_us(1); + if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT; + } + + uint32_t t = system_get_time(); + + loopCntHIGH = DHTLIB_TIMEOUT; + while (DIRECT_READ(pin) != LOW ) + { + os_delay_us(1); + if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT; + } + + if ((system_get_time() - t) > 40) + { + dht_bits[idx] |= mask; + } + mask >>= 1; + if (mask == 0) // next byte? + { + mask = 128; + idx++; + } + } + // pinMode(pin, OUTPUT); + DIRECT_MODE_OUTPUT(pin); + // digitalWrite(pin, HIGH); + DIRECT_WRITE_HIGH(pin); + + return DHTLIB_OK; +} +// +// END OF FILE +// \ No newline at end of file diff --git a/app/dhtlib/dht.h b/app/dhtlib/dht.h new file mode 100644 index 00000000..de31bd69 --- /dev/null +++ b/app/dhtlib/dht.h @@ -0,0 +1,63 @@ +// +// FILE: dht.h +// AUTHOR: Rob Tillaart +// VERSION: 0.1.14 +// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino +// URL: http://arduino.cc/playground/Main/DHTLib +// +// HISTORY: +// see dht.cpp file +// + +#ifndef dht_h +#define dht_h + +// #if ARDUINO < 100 +// #include +// #else +// #include +// #endif +#include "c_types.h" + +#define DHT_LIB_VERSION "0.1.14" + +#define DHTLIB_OK 0 +#define DHTLIB_ERROR_CHECKSUM -1 +#define DHTLIB_ERROR_TIMEOUT -2 +#define DHTLIB_INVALID_VALUE -999 + +#define DHTLIB_DHT11_WAKEUP 18 +#define DHTLIB_DHT_WAKEUP 1 + +// max timeout is 100 usec. +// For a 16 Mhz proc 100 usec is 1600 clock cycles +// loops using DHTLIB_TIMEOUT use at least 4 clock cycli +// so 100 us takes max 400 loops +// so by dividing F_CPU by 40000 we "fail" as fast as possible +// ESP8266 uses delay_us get 1us time +#define DHTLIB_TIMEOUT (100) + +// Platform specific I/O definitions + +#define DIRECT_READ(pin) (0x1 & GPIO_INPUT_GET(GPIO_ID_PIN(pin_num[pin]))) +#define DIRECT_MODE_INPUT(pin) GPIO_DIS_OUTPUT(pin_num[pin]) +#define DIRECT_MODE_OUTPUT(pin) +#define DIRECT_WRITE_LOW(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 0)) +#define DIRECT_WRITE_HIGH(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 1)) + +// return values: +// DHTLIB_OK +// DHTLIB_ERROR_CHECKSUM +// DHTLIB_ERROR_TIMEOUT +int dht_read11(uint8_t pin); +int dht_read(uint8_t pin); + +inline int dht_read21(uint8_t pin) { return dht_read(pin); }; +inline int dht_read22(uint8_t pin) { return dht_read(pin); }; +inline int dht_read33(uint8_t pin) { return dht_read(pin); }; +inline int dht_read44(uint8_t pin) { return dht_read(pin); }; + +#endif +// +// END OF FILE +// \ No newline at end of file From 899935e60bec0cb7a2f749386be7dc13f70ba7d6 Mon Sep 17 00:00:00 2001 From: vowstar Date: Thu, 18 Jun 2015 16:31:40 +0800 Subject: [PATCH 2/8] Add DHT Lua Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44). --- app/dhtlib/dht.c | 52 ++++++++++++++++++++ app/dhtlib/dht.h | 11 +++-- app/include/user_modules.h | 1 + app/modules/Makefile | 1 + app/modules/auxmods.h | 31 +++++++----- app/modules/dht.c | 80 +++++++++++++++++++++++++++++++ app/modules/modules.h | 97 +++++++++++++++++++++----------------- app/platform/common.c | 1 + 8 files changed, 215 insertions(+), 59 deletions(-) create mode 100644 app/modules/dht.c diff --git a/app/dhtlib/dht.c b/app/dhtlib/dht.c index f1f59784..323ef805 100644 --- a/app/dhtlib/dht.c +++ b/app/dhtlib/dht.c @@ -27,6 +27,7 @@ // Released to the public domain // +#include "user_interface.h" #include "platform.h" #include "dht.h" @@ -51,6 +52,20 @@ static int dht_readSensor(uint8_t pin, uint8_t wakeupDelay); // PUBLIC // +// return values: +// Humidity +double dht_getHumidity(void) +{ + return dht_humidity; +} + +// return values: +// Temperature +double dht_getTemperature(void) +{ + return dht_temperature; +} + // return values: // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM @@ -111,6 +126,42 @@ int dht_read(uint8_t pin) return DHTLIB_OK; } +// return values: +// DHTLIB_OK +// DHTLIB_ERROR_CHECKSUM +// DHTLIB_ERROR_TIMEOUT +inline int dht_read21(uint8_t pin) +{ + return dht_read(pin); +} + +// return values: +// DHTLIB_OK +// DHTLIB_ERROR_CHECKSUM +// DHTLIB_ERROR_TIMEOUT +inline int dht_read22(uint8_t pin) +{ + return dht_read(pin); +} + +// return values: +// DHTLIB_OK +// DHTLIB_ERROR_CHECKSUM +// DHTLIB_ERROR_TIMEOUT +inline int dht_read33(uint8_t pin) +{ + return dht_read(pin); +} + +// return values: +// DHTLIB_OK +// DHTLIB_ERROR_CHECKSUM +// DHTLIB_ERROR_TIMEOUT +inline int dht_read44(uint8_t pin) +{ + return dht_read(pin); +} + ///////////////////////////////////////////////////// // // PRIVATE @@ -138,6 +189,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay) // REQUEST SAMPLE // pinMode(pin, OUTPUT); + platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP); DIRECT_MODE_OUTPUT(pin); // digitalWrite(pin, LOW); // T-be DIRECT_WRITE_LOW(pin); diff --git a/app/dhtlib/dht.h b/app/dhtlib/dht.h index de31bd69..1a210ebb 100644 --- a/app/dhtlib/dht.h +++ b/app/dhtlib/dht.h @@ -52,10 +52,13 @@ int dht_read11(uint8_t pin); int dht_read(uint8_t pin); -inline int dht_read21(uint8_t pin) { return dht_read(pin); }; -inline int dht_read22(uint8_t pin) { return dht_read(pin); }; -inline int dht_read33(uint8_t pin) { return dht_read(pin); }; -inline int dht_read44(uint8_t pin) { return dht_read(pin); }; +inline int dht_read21(uint8_t pin); +inline int dht_read22(uint8_t pin); +inline int dht_read33(uint8_t pin); +inline int dht_read44(uint8_t pin); + +double dht_getHumidity(void); +double dht_getTemperature(void); #endif // diff --git a/app/include/user_modules.h b/app/include/user_modules.h index 7e2564f7..9f10f8f3 100644 --- a/app/include/user_modules.h +++ b/app/include/user_modules.h @@ -33,6 +33,7 @@ #define LUA_USE_MODULES_CJSON #define LUA_USE_MODULES_CRYPTO #define LUA_USE_MODULES_RC +#define LUA_USE_MODULES_DHT #endif /* LUA_USE_MODULES */ diff --git a/app/modules/Makefile b/app/modules/Makefile index 499b9314..646a375e 100644 --- a/app/modules/Makefile +++ b/app/modules/Makefile @@ -48,6 +48,7 @@ INCLUDES += -I ../wofs INCLUDES += -I ../spiffs INCLUDES += -I ../smart INCLUDES += -I ../cjson +INCLUDES += -I ../dhtlib PDIR := ../$(PDIR) sinclude $(PDIR)Makefile diff --git a/app/modules/auxmods.h b/app/modules/auxmods.h index ece3ff56..98e8277c 100644 --- a/app/modules/auxmods.h +++ b/app/modules/auxmods.h @@ -7,7 +7,7 @@ #include "lua.h" -#define AUXLIB_GPIO "gpio" +#define AUXLIB_GPIO "gpio" LUALIB_API int ( luaopen_gpio )( lua_State *L ); #define AUXLIB_SPI "spi" @@ -46,42 +46,51 @@ LUALIB_API int ( luaopen_cpu )( lua_State* L ); #define AUXLIB_ADC "adc" LUALIB_API int ( luaopen_adc )( lua_State *L ); -#define AUXLIB_RPC "rpc" +#define AUXLIB_RPC "rpc" LUALIB_API int ( luaopen_rpc )( lua_State *L ); #define AUXLIB_BITARRAY "bitarray" LUALIB_API int ( luaopen_bitarray )( lua_State *L ); -#define AUXLIB_ELUA "elua" +#define AUXLIB_ELUA "elua" LUALIB_API int ( luaopen_elua )( lua_State *L ); -#define AUXLIB_I2C "i2c" +#define AUXLIB_I2C "i2c" LUALIB_API int ( luaopen_i2c )( lua_State *L ); -#define AUXLIB_WIFI "wifi" +#define AUXLIB_WIFI "wifi" LUALIB_API int ( luaopen_wifi )( lua_State *L ); -#define AUXLIB_COAP "coap" +#define AUXLIB_COAP "coap" LUALIB_API int ( luaopen_coap )( lua_State *L ); -#define AUXLIB_MQTT "mqtt" +#define AUXLIB_MQTT "mqtt" LUALIB_API int ( luaopen_mqtt )( lua_State *L ); #define AUXLIB_U8G "u8g" LUALIB_API int ( luaopen_u8g )( lua_State *L ); -#define AUXLIB_NODE "node" +#define AUXLIB_NODE "node" LUALIB_API int ( luaopen_node )( lua_State *L ); -#define AUXLIB_FILE "file" +#define AUXLIB_FILE "file" LUALIB_API int ( luaopen_file )( lua_State *L ); -#define AUXLIB_OW "ow" +#define AUXLIB_OW "ow" LUALIB_API int ( luaopen_ow )( lua_State *L ); -#define AUXLIB_CJSON "cjson" +#define AUXLIB_CJSON "cjson" LUALIB_API int ( luaopen_cjson )( lua_State *L ); +#define AUXLIB_CRYPTO "crypto" +LUALIB_API int ( luaopen_crypto )( lua_State *L ); + +#define AUXLIB_RC "rc" +LUALIB_API int ( luaopen_rc )( lua_State *L ); + +#define AUXLIB_DHT "dht" +LUALIB_API int ( luaopen_dht )( lua_State *L ); + // Helper macros #define MOD_CHECK_ID( mod, id )\ if( !platform_ ## mod ## _exists( id ) )\ diff --git a/app/modules/dht.c b/app/modules/dht.c new file mode 100644 index 00000000..91b0afd3 --- /dev/null +++ b/app/modules/dht.c @@ -0,0 +1,80 @@ +// Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44). + +#include "lualib.h" +#include "lauxlib.h" +#include "auxmods.h" +#include "lrotable.h" +#include "cpu_esp8266.h" +#include "dht.h" + +#define NUM_DHT GPIO_PIN_NUM + +// **************************************************************************** +// DHT functions +int platform_dht_exists( unsigned id ) +{ + return ((id < NUM_DHT) && (id > 0)); +} + +// Lua: result = dht.read( id ) +static int dht_lapi_read( lua_State *L ) +{ + unsigned id = luaL_checkinteger( L, 1 ); + MOD_CHECK_ID( dht, id ); + lua_pushinteger( L, dht_read(id) ); + return 1; +} + +// Lua: result = dht.read11( id ) +static int dht_lapi_read11( lua_State *L ) +{ + unsigned id = luaL_checkinteger( L, 1 ); + MOD_CHECK_ID( dht, id ); + lua_pushinteger( L, dht_read11(id) ); + return 1; +} + +// Lua: result = dht.humidity() +static int dht_lapi_humidity( lua_State *L ) +{ + lua_pushinteger( L, dht_getHumidity() ); + return 1; +} + +// Lua: result = dht.temperature() +static int dht_lapi_temperature( lua_State *L ) +{ + lua_pushinteger( L, dht_getTemperature() ); + return 1; +} + + +// Module function map +#define MIN_OPT_LEVEL 2 +#include "lrodefs.h" +const LUA_REG_TYPE dht_map[] = +{ + { LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) }, + { LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) }, + { LSTRKEY( "humidity" ), LFUNCVAL( dht_lapi_humidity ) }, + { LSTRKEY( "temperature" ), LFUNCVAL( dht_lapi_temperature ) }, +#if LUA_OPTIMIZE_MEMORY > 0 + { LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) }, + { LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) }, + { LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) }, +#endif + { LNILKEY, LNILVAL } +}; + +LUALIB_API int luaopen_dht( lua_State *L ) +{ +#if LUA_OPTIMIZE_MEMORY > 0 + return 0; +#else // #if LUA_OPTIMIZE_MEMORY > 0 + luaL_register( L, AUXLIB_DHT, dht_map ); + + // Add the constants + + return 1; +#endif // #if LUA_OPTIMIZE_MEMORY > 0 +} diff --git a/app/modules/modules.h b/app/modules/modules.h index 76a357a8..c9e55193 100644 --- a/app/modules/modules.h +++ b/app/modules/modules.h @@ -6,165 +6,173 @@ #define __MODULES_H__ #if defined(LUA_USE_MODULES_GPIO) -#define MODULES_GPIO "gpio" -#define ROM_MODULES_GPIO \ +#define MODULES_GPIO "gpio" +#define ROM_MODULES_GPIO \ _ROM(MODULES_GPIO, luaopen_gpio, gpio_map) #else #define ROM_MODULES_GPIO #endif #if defined(LUA_USE_MODULES_PWM) -#define MODULES_PWM "pwm" -#define ROM_MODULES_PWM \ +#define MODULES_PWM "pwm" +#define ROM_MODULES_PWM \ _ROM(MODULES_PWM, luaopen_pwm, pwm_map) #else #define ROM_MODULES_PWM #endif #if defined(LUA_USE_MODULES_WIFI) -#define MODULES_WIFI "wifi" -#define ROM_MODULES_WIFI \ +#define MODULES_WIFI "wifi" +#define ROM_MODULES_WIFI \ _ROM(MODULES_WIFI, luaopen_wifi, wifi_map) #else #define ROM_MODULES_WIFI #endif #if defined(LUA_USE_MODULES_NET) -#define MODULES_NET "net" -#define ROM_MODULES_NET \ +#define MODULES_NET "net" +#define ROM_MODULES_NET \ _ROM(MODULES_NET, luaopen_net, net_map) #else #define ROM_MODULES_NET #endif #if defined(LUA_USE_MODULES_COAP) -#define MODULES_COAP "coap" -#define ROM_MODULES_COAP \ +#define MODULES_COAP "coap" +#define ROM_MODULES_COAP \ _ROM(MODULES_COAP, luaopen_coap, coap_map) #else #define ROM_MODULES_COAP #endif #if defined(LUA_USE_MODULES_MQTT) -#define MODULES_MQTT "mqtt" -#define ROM_MODULES_MQTT \ +#define MODULES_MQTT "mqtt" +#define ROM_MODULES_MQTT \ _ROM(MODULES_MQTT, luaopen_mqtt, mqtt_map) #else #define ROM_MODULES_MQTT #endif #if defined(LUA_USE_MODULES_U8G) -#define MODULES_U8G "u8g" -#define ROM_MODULES_U8G \ +#define MODULES_U8G "u8g" +#define ROM_MODULES_U8G \ _ROM(MODULES_U8G, luaopen_u8g, lu8g_map) #else #define ROM_MODULES_U8G #endif #if defined(LUA_USE_MODULES_I2C) -#define MODULES_I2C "i2c" -#define ROM_MODULES_I2C \ +#define MODULES_I2C "i2c" +#define ROM_MODULES_I2C \ _ROM(MODULES_I2C, luaopen_i2c, i2c_map) #else #define ROM_MODULES_I2C #endif #if defined(LUA_USE_MODULES_SPI) -#define MODULES_SPI "spi" -#define ROM_MODULES_SPI \ +#define MODULES_SPI "spi" +#define ROM_MODULES_SPI \ _ROM(MODULES_SPI, luaopen_spi, spi_map) #else #define ROM_MODULES_SPI #endif #if defined(LUA_USE_MODULES_TMR) -#define MODULES_TMR "tmr" -#define ROM_MODULES_TMR \ +#define MODULES_TMR "tmr" +#define ROM_MODULES_TMR \ _ROM(MODULES_TMR, luaopen_tmr, tmr_map) #else #define ROM_MODULES_TMR #endif #if defined(LUA_USE_MODULES_NODE) -#define MODULES_NODE "node" -#define ROM_MODULES_NODE \ +#define MODULES_NODE "node" +#define ROM_MODULES_NODE \ _ROM(MODULES_NODE, luaopen_node, node_map) #else #define ROM_MODULES_NODE #endif #if defined(LUA_USE_MODULES_FILE) -#define MODULES_FILE "file" -#define ROM_MODULES_FILE \ +#define MODULES_FILE "file" +#define ROM_MODULES_FILE \ _ROM(MODULES_FILE, luaopen_file, file_map) #else #define ROM_MODULES_FILE #endif #if defined(LUA_USE_MODULES_ADC) -#define MODULES_ADC "adc" -#define ROM_MODULES_ADC \ +#define MODULES_ADC "adc" +#define ROM_MODULES_ADC \ _ROM(MODULES_ADC, luaopen_adc, adc_map) #else #define ROM_MODULES_ADC #endif #if defined(LUA_USE_MODULES_UART) -#define MODULES_UART "uart" -#define ROM_MODULES_UART \ +#define MODULES_UART "uart" +#define ROM_MODULES_UART \ _ROM(MODULES_UART, luaopen_uart, uart_map) #else #define ROM_MODULES_UART #endif #if defined(LUA_USE_MODULES_OW) -#define MODULES_OW "ow" -#define ROM_MODULES_OW \ +#define MODULES_OW "ow" +#define ROM_MODULES_OW \ _ROM(MODULES_OW, luaopen_ow, ow_map) #else #define ROM_MODULES_OW #endif #if defined(LUA_USE_MODULES_BIT) -#define MODULES_BIT "bit" -#define ROM_MODULES_BIT \ +#define MODULES_BIT "bit" +#define ROM_MODULES_BIT \ _ROM(MODULES_BIT, luaopen_bit, bit_map) #else #define ROM_MODULES_BIT #endif #if defined(LUA_USE_MODULES_WS2812) -#define MODULES_WS2812 "ws2812" -#define ROM_MODULES_WS2812 \ +#define MODULES_WS2812 "ws2812" +#define ROM_MODULES_WS2812 \ _ROM(MODULES_WS2812, luaopen_ws2812, ws2812_map) #else #define ROM_MODULES_WS2812 #endif #if defined(LUA_USE_MODULES_CJSON) -#define MODULES_CJSON "cjson" -#define ROM_MODULES_CJSON \ +#define MODULES_CJSON "cjson" +#define ROM_MODULES_CJSON \ _ROM(MODULES_CJSON, luaopen_cjson, cjson_map) #else #define ROM_MODULES_CJSON #endif #if defined(LUA_USE_MODULES_CRYPTO) -#define MODULES_CRYPTO "crypto" -#define ROM_MODULES_CRYPTO \ +#define MODULES_CRYPTO "crypto" +#define ROM_MODULES_CRYPTO \ _ROM(MODULES_CRYPTO, luaopen_crypto, crypto_map) #else #define ROM_MODULES_CRYPTO #endif #if defined(LUA_USE_MODULES_RC) -#define MODULES_RC "rc" -#define ROM_MODULES_RC \ -_ROM(MODULES_RC, luaopen_rc, rc_map) +#define MODULES_RC "rc" +#define ROM_MODULES_RC \ + _ROM(MODULES_RC, luaopen_rc, rc_map) #else #define ROM_MODULES_RC #endif +#if defined(LUA_USE_MODULES_DHT) +#define MODULES_DHT "dht" +#define ROM_MODULES_DHT \ + _ROM(MODULES_DHT, luaopen_dht, dht_map) +#else +#define ROM_MODULES_DHT +#endif + #define LUA_MODULES_ROM \ ROM_MODULES_GPIO \ ROM_MODULES_PWM \ @@ -183,8 +191,9 @@ _ROM(MODULES_RC, luaopen_rc, rc_map) ROM_MODULES_OW \ ROM_MODULES_BIT \ ROM_MODULES_WS2812 \ - ROM_MODULES_CJSON \ - ROM_MODULES_CRYPTO \ - ROM_MODULES_RC + ROM_MODULES_CJSON \ + ROM_MODULES_CRYPTO \ + ROM_MODULES_RC \ + ROM_MODULES_DHT #endif diff --git a/app/platform/common.c b/app/platform/common.c index fed7b89b..711a8a4a 100644 --- a/app/platform/common.c +++ b/app/platform/common.c @@ -75,6 +75,7 @@ int platform_tmr_exists( unsigned id ) return id < NUM_TMR; } +// **************************************************************************** // I2C support int platform_i2c_exists( unsigned id ) { From af56aea1e859e261b3f8f4416b2b5ce74bfa016b Mon Sep 17 00:00:00 2001 From: vowstar Date: Thu, 18 Jun 2015 17:19:55 +0800 Subject: [PATCH 3/8] Update dhtlib api, support both integer and float version. --- app/dhtlib/dht.c | 4 ++-- app/include/user_version.h | 2 +- app/modules/dht.c | 31 +++++++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/dhtlib/dht.c b/app/dhtlib/dht.c index 323ef805..8471e229 100644 --- a/app/dhtlib/dht.c +++ b/app/dhtlib/dht.c @@ -110,8 +110,8 @@ int dht_read(uint8_t pin) } // CONVERT AND STORE - dht_humidity = COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1; - dht_temperature = COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1; + dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1; + dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1; if (dht_bits[2] & 0x80) // negative dht_temperature { dht_temperature = -dht_temperature; diff --git a/app/include/user_version.h b/app/include/user_version.h index 4db7edca..789e5fea 100644 --- a/app/include/user_version.h +++ b/app/include/user_version.h @@ -7,6 +7,6 @@ #define NODE_VERSION_INTERNAL 0U #define NODE_VERSION "NodeMCU 0.9.6" -#define BUILD_DATE "build 20150617" +#define BUILD_DATE "build 20150618" #endif /* __USER_VERSION_H__ */ diff --git a/app/modules/dht.c b/app/modules/dht.c index 91b0afd3..bda3379d 100644 --- a/app/modules/dht.c +++ b/app/modules/dht.c @@ -22,7 +22,9 @@ static int dht_lapi_read( lua_State *L ) unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( dht, id ); lua_pushinteger( L, dht_read(id) ); - return 1; + lua_pushnumber( L, dht_getTemperature() ); + lua_pushnumber( L, dht_getHumidity() ); + return 3; } // Lua: result = dht.read11( id ) @@ -31,23 +33,42 @@ static int dht_lapi_read11( lua_State *L ) unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( dht, id ); lua_pushinteger( L, dht_read11(id) ); - return 1; + lua_pushnumber( L, dht_getTemperature() ); + lua_pushnumber( L, dht_getHumidity() ); + return 3; } // Lua: result = dht.humidity() static int dht_lapi_humidity( lua_State *L ) { - lua_pushinteger( L, dht_getHumidity() ); + lua_pushnumber( L, dht_getHumidity() ); + return 1; +} + +// Lua: result = dht.humiditydecimal() +static int dht_lapi_humiditydecimal( lua_State *L ) +{ + double value = dht_getHumidity(); + int result = (int)((value - (int)value) * 1000); + lua_pushnumber( L, result ); return 1; } // Lua: result = dht.temperature() static int dht_lapi_temperature( lua_State *L ) { - lua_pushinteger( L, dht_getTemperature() ); + lua_pushnumber( L, dht_getTemperature() ); return 1; } +// Lua: result = dht.temperaturedecimal() +static int dht_lapi_temperaturedecimal( lua_State *L ) +{ + double value = dht_getTemperature(); + int result = (int)((value - (int)value) * 1000); + lua_pushnumber( L, result ); + return 1; +} // Module function map #define MIN_OPT_LEVEL 2 @@ -58,6 +79,8 @@ const LUA_REG_TYPE dht_map[] = { LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) }, { LSTRKEY( "humidity" ), LFUNCVAL( dht_lapi_humidity ) }, { LSTRKEY( "temperature" ), LFUNCVAL( dht_lapi_temperature ) }, + { LSTRKEY( "humiditydecimal" ), LFUNCVAL( dht_lapi_humiditydecimal ) }, + { LSTRKEY( "temperaturedecimal" ), LFUNCVAL( dht_lapi_temperaturedecimal ) }, #if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) }, { LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) }, From 98a2c9fdb638e0e7c417ccdae0cb82d21eb43c4d Mon Sep 17 00:00:00 2001 From: vowstar Date: Thu, 18 Jun 2015 17:36:27 +0800 Subject: [PATCH 4/8] Align all TAB=8 in app/Makefile, make sure it fit for github display. --- app/Makefile | 100 +++++++++++++++++++++++++-------------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/app/Makefile b/app/Makefile index fbc69d78..87331c24 100644 --- a/app/Makefile +++ b/app/Makefile @@ -21,24 +21,24 @@ ifndef PDIR # { GEN_IMAGES= eagle.app.v6.out GEN_BINS= eagle.app.v6.bin SPECIAL_MKTARGETS=$(APP_MKTARGETS) -SUBDIRS= \ - user \ - driver \ - lwip \ - json \ - upgrade \ - platform \ - libc \ - lua \ - coap \ - mqtt \ - u8glib \ - smart \ - wofs \ - modules \ - spiffs \ - cjson \ - crypto \ +SUBDIRS= \ + user \ + driver \ + lwip \ + json \ + upgrade \ + platform \ + libc \ + lua \ + coap \ + mqtt \ + u8glib \ + smart \ + wofs \ + modules \ + spiffs \ + cjson \ + crypto \ dhtlib endif # } PDIR @@ -48,10 +48,10 @@ LDDIR = ../ld CCFLAGS += -Os -TARGET_LDFLAGS = \ +TARGET_LDFLAGS = \ -nostdlib \ -Wl,-EL \ - --longcalls \ + --longcalls \ --text-section-literals ifeq ($(FLAVOR),debug) @@ -72,13 +72,13 @@ ifeq ($(APP), 2) LD_FILE = $(LDDIR)/eagle.app.v6.app2.ld endif -COMPONENTS_eagle.app.v6 = \ +COMPONENTS_eagle.app.v6 = \ user/libuser.a \ driver/libdriver.a \ lwip/liblwip.a \ json/libjson.a \ - upgrade/libupgrade.a \ - platform/libplatform.a \ + upgrade/libupgrade.a \ + platform/libplatform.a \ libc/liblibc.a \ lua/liblua.a \ coap/coap.a \ @@ -92,33 +92,33 @@ COMPONENTS_eagle.app.v6 = \ dhtlib/libdhtlib.a \ modules/libmodules.a -LINKFLAGS_eagle.app.v6 = \ - -L../lib \ +LINKFLAGS_eagle.app.v6 = \ + -L../lib \ -Wl,--gc-sections \ - -Xlinker -Map=mapfile \ - -nostdlib \ + -Xlinker -Map=mapfile \ + -nostdlib \ -T$(LD_FILE) \ - -Wl,--no-check-sections \ - -u call_user_start \ + -Wl,--no-check-sections \ + -u call_user_start \ -Wl,-static \ -Wl,--start-group \ - -lc \ - -lgcc \ - -lhal \ - -lphy \ - -lpp \ - -lnet80211 \ - -lwpa \ - -lmain \ - -ljson \ + -lc \ + -lgcc \ + -lhal \ + -lphy \ + -lpp \ + -lnet80211 \ + -lwpa \ + -lmain \ + -ljson \ -lsmartconfig \ - -lssl \ - $(DEP_LIBS_eagle.app.v6) \ + -lssl \ + $(DEP_LIBS_eagle.app.v6) \ -Wl,--end-group \ -lm -DEPENDS_eagle.app.v6 = \ - $(LD_FILE) \ +DEPENDS_eagle.app.v6 = \ + $(LD_FILE) \ $(LDDIR)/eagle.rom.addr.v6.ld ############################################################# @@ -129,24 +129,24 @@ DEPENDS_eagle.app.v6 = \ # for a subtree within the makefile rooted therein # -#UNIVERSAL_TARGET_DEFINES = \ +#UNIVERSAL_TARGET_DEFINES = \ # Other potential configuration flags include: # -DTXRX_TXBUF_DEBUG # -DTXRX_RXBUF_DEBUG # -DWLAN_CONFIG_CCX -CONFIGURATION_DEFINES = -D__ets__ \ - -DICACHE_FLASH \ - -DLWIP_OPEN_SRC \ +CONFIGURATION_DEFINES = -D__ets__ \ + -DICACHE_FLASH \ + -DLWIP_OPEN_SRC \ -DPBUF_RSV_FOR_WLAN \ -DEBUF_LWIP -DEFINES += \ - $(UNIVERSAL_TARGET_DEFINES) \ +DEFINES += \ + $(UNIVERSAL_TARGET_DEFINES) \ $(CONFIGURATION_DEFINES) -DDEFINES += \ - $(UNIVERSAL_TARGET_DEFINES) \ +DDEFINES += \ + $(UNIVERSAL_TARGET_DEFINES) \ $(CONFIGURATION_DEFINES) From 87341547d7ef9568387f2f13be16ef8109443682 Mon Sep 17 00:00:00 2001 From: vowstar Date: Thu, 18 Jun 2015 20:53:52 +0800 Subject: [PATCH 5/8] Update DHTlib, supported DHT11 or DHTxx automatic detection. Using @MarsTechHAN 's method. New usage: status, temp, humi, tempdec, humidec = dht.read( id ) print( dht.read( id ) ) = dht.read( id ) --- app/dhtlib/dht.c | 58 ++++++++++++++++++++++++++ app/dhtlib/dht.h | 2 + app/modules/dht.c | 104 +++++++++++++++++++++++++++++----------------- 3 files changed, 125 insertions(+), 39 deletions(-) diff --git a/app/dhtlib/dht.c b/app/dhtlib/dht.c index 8471e229..8f1201b7 100644 --- a/app/dhtlib/dht.c +++ b/app/dhtlib/dht.c @@ -66,6 +66,64 @@ double dht_getTemperature(void) return dht_temperature; } +// return values: +// DHTLIB_OK +// DHTLIB_ERROR_CHECKSUM +// DHTLIB_ERROR_TIMEOUT +int dht_read_universal(uint8_t pin) +{ + // READ VALUES + int rv = dht_readSensor(pin, DHTLIB_DHT_UNI_WAKEUP); + if (rv != DHTLIB_OK) + { + dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? + dht_temperature = DHTLIB_INVALID_VALUE; // invalid value + return rv; // propagate error value + } + + // Assume it is DHT11 + // If it is DHT11, both bit[1] and bit[3] is 0 + if ((dht_bits[1] == 0) && (dht_bits[3] == 0)) + { + // It may DHT11 + // CONVERT AND STORE + dht_humidity = dht_bits[0]; // dht_bits[1] == 0; + dht_temperature = dht_bits[2]; // dht_bits[3] == 0; + + // TEST CHECKSUM + // dht_bits[1] && dht_bits[3] both 0 + uint8_t sum = dht_bits[0] + dht_bits[2]; + if (dht_bits[4] != sum) + { + // It may not DHT11 + dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? + dht_temperature = DHTLIB_INVALID_VALUE; // invalid value + // Do nothing + } + else + { + return DHTLIB_OK; + } + } + + // Assume it is not DHT11 + // CONVERT AND STORE + dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1; + dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1; + if (dht_bits[2] & 0x80) // negative dht_temperature + { + dht_temperature = -dht_temperature; + } + + // TEST CHECKSUM + uint8_t sum = dht_bits[0] + dht_bits[1] + dht_bits[2] + dht_bits[3]; + if (dht_bits[4] != sum) + { + return DHTLIB_ERROR_CHECKSUM; + } + return DHTLIB_OK; +} + // return values: // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM diff --git a/app/dhtlib/dht.h b/app/dhtlib/dht.h index 1a210ebb..46fa31c1 100644 --- a/app/dhtlib/dht.h +++ b/app/dhtlib/dht.h @@ -28,6 +28,7 @@ #define DHTLIB_DHT11_WAKEUP 18 #define DHTLIB_DHT_WAKEUP 1 +#define DHTLIB_DHT_UNI_WAKEUP 18 // max timeout is 100 usec. // For a 16 Mhz proc 100 usec is 1600 clock cycles @@ -49,6 +50,7 @@ // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_TIMEOUT +int dht_read_universal(uint8_t pin); int dht_read11(uint8_t pin); int dht_read(uint8_t pin); diff --git a/app/modules/dht.c b/app/modules/dht.c index bda3379d..fcbf5fdb 100644 --- a/app/modules/dht.c +++ b/app/modules/dht.c @@ -16,59 +16,88 @@ int platform_dht_exists( unsigned id ) return ((id < NUM_DHT) && (id > 0)); } -// Lua: result = dht.read( id ) +// Lua: status, temp, humi, tempdec, humidec = dht.read( id ) static int dht_lapi_read( lua_State *L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( dht, id ); - lua_pushinteger( L, dht_read(id) ); - lua_pushnumber( L, dht_getTemperature() ); - lua_pushnumber( L, dht_getHumidity() ); - return 3; + lua_pushinteger( L, dht_read_universal(id) ); + double temp = dht_getTemperature(); + double humi = dht_getHumidity(); + int tempdec = (int)((temp - (int)temp) * 1000); + int humidec = (int)((humi - (int)humi) * 1000); + lua_pushnumber( L, temp ); + lua_pushnumber( L, humi ); + lua_pushnumber( L, tempdec ); + lua_pushnumber( L, humidec ); + return 5; } -// Lua: result = dht.read11( id ) +// Lua: status, temp, humi, tempdec, humidec = dht.read11( id )) static int dht_lapi_read11( lua_State *L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( dht, id ); lua_pushinteger( L, dht_read11(id) ); - lua_pushnumber( L, dht_getTemperature() ); - lua_pushnumber( L, dht_getHumidity() ); - return 3; + double temp = dht_getTemperature(); + double humi = dht_getHumidity(); + int tempdec = (int)((temp - (int)temp) * 1000); + int humidec = (int)((humi - (int)humi) * 1000); + lua_pushnumber( L, temp ); + lua_pushnumber( L, humi ); + lua_pushnumber( L, tempdec ); + lua_pushnumber( L, humidec ); + return 5; } -// Lua: result = dht.humidity() -static int dht_lapi_humidity( lua_State *L ) +// Lua: status, temp, humi, tempdec, humidec = dht.readxx( id )) +static int dht_lapi_readxx( lua_State *L ) { - lua_pushnumber( L, dht_getHumidity() ); - return 1; + unsigned id = luaL_checkinteger( L, 1 ); + MOD_CHECK_ID( dht, id ); + lua_pushinteger( L, dht_read(id) ); + double temp = dht_getTemperature(); + double humi = dht_getHumidity(); + int tempdec = (int)((temp - (int)temp) * 1000); + int humidec = (int)((humi - (int)humi) * 1000); + lua_pushnumber( L, temp ); + lua_pushnumber( L, humi ); + lua_pushnumber( L, tempdec ); + lua_pushnumber( L, humidec ); + return 5; } -// Lua: result = dht.humiditydecimal() -static int dht_lapi_humiditydecimal( lua_State *L ) -{ - double value = dht_getHumidity(); - int result = (int)((value - (int)value) * 1000); - lua_pushnumber( L, result ); - return 1; -} +// // Lua: result = dht.humidity() +// static int dht_lapi_humidity( lua_State *L ) +// { +// lua_pushnumber( L, dht_getHumidity() ); +// return 1; +// } -// Lua: result = dht.temperature() -static int dht_lapi_temperature( lua_State *L ) -{ - lua_pushnumber( L, dht_getTemperature() ); - return 1; -} +// // Lua: result = dht.humiditydecimal() +// static int dht_lapi_humiditydecimal( lua_State *L ) +// { +// double value = dht_getHumidity(); +// int result = (int)((value - (int)value) * 1000); +// lua_pushnumber( L, result ); +// return 1; +// } -// Lua: result = dht.temperaturedecimal() -static int dht_lapi_temperaturedecimal( lua_State *L ) -{ - double value = dht_getTemperature(); - int result = (int)((value - (int)value) * 1000); - lua_pushnumber( L, result ); - return 1; -} +// // Lua: result = dht.temperature() +// static int dht_lapi_temperature( lua_State *L ) +// { +// lua_pushnumber( L, dht_getTemperature() ); +// return 1; +// } + +// // Lua: result = dht.temperaturedecimal() +// static int dht_lapi_temperaturedecimal( lua_State *L ) +// { +// double value = dht_getTemperature(); +// int result = (int)((value - (int)value) * 1000); +// lua_pushnumber( L, result ); +// return 1; +// } // Module function map #define MIN_OPT_LEVEL 2 @@ -77,10 +106,7 @@ const LUA_REG_TYPE dht_map[] = { { LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) }, { LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) }, - { LSTRKEY( "humidity" ), LFUNCVAL( dht_lapi_humidity ) }, - { LSTRKEY( "temperature" ), LFUNCVAL( dht_lapi_temperature ) }, - { LSTRKEY( "humiditydecimal" ), LFUNCVAL( dht_lapi_humiditydecimal ) }, - { LSTRKEY( "temperaturedecimal" ), LFUNCVAL( dht_lapi_temperaturedecimal ) }, + { LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) }, #if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) }, { LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) }, From c56659e84d469b0e7f8d85b3e31f2efc0e27d79b Mon Sep 17 00:00:00 2001 From: vowstar Date: Fri, 19 Jun 2015 01:05:19 +0800 Subject: [PATCH 6/8] Using __attribute__((alias("dht_read"))) reduce code size. --- app/dhtlib/dht.c | 20 ++++---------------- app/dhtlib/dht.h | 8 ++++---- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/app/dhtlib/dht.c b/app/dhtlib/dht.c index 8f1201b7..c717bdaa 100644 --- a/app/dhtlib/dht.c +++ b/app/dhtlib/dht.c @@ -188,37 +188,25 @@ int dht_read(uint8_t pin) // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_TIMEOUT -inline int dht_read21(uint8_t pin) -{ - return dht_read(pin); -} +int dht_read21(uint8_t pin) __attribute__((alias("dht_read"))); // return values: // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_TIMEOUT -inline int dht_read22(uint8_t pin) -{ - return dht_read(pin); -} +int dht_read22(uint8_t pin) __attribute__((alias("dht_read"))); // return values: // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_TIMEOUT -inline int dht_read33(uint8_t pin) -{ - return dht_read(pin); -} +int dht_read33(uint8_t pin) __attribute__((alias("dht_read"))); // return values: // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_TIMEOUT -inline int dht_read44(uint8_t pin) -{ - return dht_read(pin); -} +int dht_read44(uint8_t pin) __attribute__((alias("dht_read"))); ///////////////////////////////////////////////////// // diff --git a/app/dhtlib/dht.h b/app/dhtlib/dht.h index 46fa31c1..ae60c9af 100644 --- a/app/dhtlib/dht.h +++ b/app/dhtlib/dht.h @@ -54,10 +54,10 @@ int dht_read_universal(uint8_t pin); int dht_read11(uint8_t pin); int dht_read(uint8_t pin); -inline int dht_read21(uint8_t pin); -inline int dht_read22(uint8_t pin); -inline int dht_read33(uint8_t pin); -inline int dht_read44(uint8_t pin); +int dht_read21(uint8_t pin); +int dht_read22(uint8_t pin); +int dht_read33(uint8_t pin); +int dht_read44(uint8_t pin); double dht_getHumidity(void); double dht_getTemperature(void); From cda7992c1a27a1794ae4d96ecd468dd743b4b273 Mon Sep 17 00:00:00 2001 From: vowstar Date: Sat, 20 Jun 2015 00:26:59 +0800 Subject: [PATCH 7/8] Fix DHTLIB bug of DHT22, DHT21, DHT33, DHT44. --- app/dhtlib/Makefile | 1 + app/dhtlib/dht.c | 61 ++++++++++++++++++++++++++------------------- app/dhtlib/dht.h | 2 ++ 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/app/dhtlib/Makefile b/app/dhtlib/Makefile index 838d3b80..9ef52484 100644 --- a/app/dhtlib/Makefile +++ b/app/dhtlib/Makefile @@ -42,6 +42,7 @@ INCLUDES += -I ./ INCLUDES += -I ./include INCLUDES += -I ../include INCLUDES += -I ../../include +INCLUDES += -I ../libc INCLUDES += -I ../platform PDIR := ../$(PDIR) sinclude $(PDIR)Makefile diff --git a/app/dhtlib/dht.c b/app/dhtlib/dht.c index c717bdaa..26255219 100644 --- a/app/dhtlib/dht.c +++ b/app/dhtlib/dht.c @@ -29,6 +29,7 @@ #include "user_interface.h" #include "platform.h" +#include "c_stdio.h" #include "dht.h" #ifndef LOW @@ -39,12 +40,12 @@ #define HIGH 1 #endif /* ifndef HIGH */ -#define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) ((byte_high << 8) | (byte_low)) +#define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) (((byte_high) << 8) | (byte_low)) static double dht_humidity; static double dht_temperature; -static uint8_t dht_bits[5]; // buffer to receive data +static uint8_t dht_bytes[5]; // buffer to receive data static int dht_readSensor(uint8_t pin, uint8_t wakeupDelay); ///////////////////////////////////////////////////// @@ -81,19 +82,28 @@ int dht_read_universal(uint8_t pin) return rv; // propagate error value } +#if defined(DHT_DEBUG_BYTES) + int i; + for (i = 0; i < 5; i++) + { + DHT_DEBUG("%02X\n", dht_bytes[i]); + } +#endif // defined(DHT_DEBUG_BYTES) + // Assume it is DHT11 // If it is DHT11, both bit[1] and bit[3] is 0 - if ((dht_bits[1] == 0) && (dht_bits[3] == 0)) + if ((dht_bytes[1] == 0) && (dht_bytes[3] == 0)) { // It may DHT11 // CONVERT AND STORE - dht_humidity = dht_bits[0]; // dht_bits[1] == 0; - dht_temperature = dht_bits[2]; // dht_bits[3] == 0; + DHT_DEBUG("DHT11 method\n"); + dht_humidity = dht_bytes[0]; // dht_bytes[1] == 0; + dht_temperature = dht_bytes[2]; // dht_bytes[3] == 0; // TEST CHECKSUM - // dht_bits[1] && dht_bits[3] both 0 - uint8_t sum = dht_bits[0] + dht_bits[2]; - if (dht_bits[4] != sum) + // dht_bytes[1] && dht_bytes[3] both 0 + uint8_t sum = dht_bytes[0] + dht_bytes[2]; + if (dht_bytes[4] != sum) { // It may not DHT11 dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? @@ -108,16 +118,17 @@ int dht_read_universal(uint8_t pin) // Assume it is not DHT11 // CONVERT AND STORE - dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1; - dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1; - if (dht_bits[2] & 0x80) // negative dht_temperature + DHT_DEBUG("DHTxx method\n"); + dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[0], dht_bytes[1]) * 0.1; + dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[2] & 0x7F, dht_bytes[3]) * 0.1; + if (dht_bytes[2] & 0x80) // negative dht_temperature { dht_temperature = -dht_temperature; } // TEST CHECKSUM - uint8_t sum = dht_bits[0] + dht_bits[1] + dht_bits[2] + dht_bits[3]; - if (dht_bits[4] != sum) + uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3]; + if (dht_bytes[4] != sum) { return DHTLIB_ERROR_CHECKSUM; } @@ -140,13 +151,13 @@ int dht_read11(uint8_t pin) } // CONVERT AND STORE - dht_humidity = dht_bits[0]; // dht_bits[1] == 0; - dht_temperature = dht_bits[2]; // dht_bits[3] == 0; + dht_humidity = dht_bytes[0]; // dht_bytes[1] == 0; + dht_temperature = dht_bytes[2]; // dht_bytes[3] == 0; // TEST CHECKSUM - // dht_bits[1] && dht_bits[3] both 0 - uint8_t sum = dht_bits[0] + dht_bits[2]; - if (dht_bits[4] != sum) return DHTLIB_ERROR_CHECKSUM; + // dht_bytes[1] && dht_bytes[3] both 0 + uint8_t sum = dht_bytes[0] + dht_bytes[2]; + if (dht_bytes[4] != sum) return DHTLIB_ERROR_CHECKSUM; return DHTLIB_OK; } @@ -168,16 +179,16 @@ int dht_read(uint8_t pin) } // CONVERT AND STORE - dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1; - dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1; - if (dht_bits[2] & 0x80) // negative dht_temperature + dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[0], dht_bytes[1]) * 0.1; + dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[2] & 0x7F, dht_bytes[3]) * 0.1; + if (dht_bytes[2] & 0x80) // negative dht_temperature { dht_temperature = -dht_temperature; } // TEST CHECKSUM - uint8_t sum = dht_bits[0] + dht_bits[1] + dht_bits[2] + dht_bits[3]; - if (dht_bits[4] != sum) + uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3]; + if (dht_bytes[4] != sum) { return DHTLIB_ERROR_CHECKSUM; } @@ -231,7 +242,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay) // volatile uint8_t *PIR = portInputRegister(port); // EMPTY BUFFER - for (i = 0; i < 5; i++) dht_bits[i] = 0; + for (i = 0; i < 5; i++) dht_bytes[i] = 0; // REQUEST SAMPLE // pinMode(pin, OUTPUT); @@ -283,7 +294,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay) if ((system_get_time() - t) > 40) { - dht_bits[idx] |= mask; + dht_bytes[idx] |= mask; } mask >>= 1; if (mask == 0) // next byte? diff --git a/app/dhtlib/dht.h b/app/dhtlib/dht.h index ae60c9af..f3b66c4f 100644 --- a/app/dhtlib/dht.h +++ b/app/dhtlib/dht.h @@ -30,6 +30,8 @@ #define DHTLIB_DHT_WAKEUP 1 #define DHTLIB_DHT_UNI_WAKEUP 18 +#define DHT_DEBUG + // max timeout is 100 usec. // For a 16 Mhz proc 100 usec is 1600 clock cycles // loops using DHTLIB_TIMEOUT use at least 4 clock cycli From 3430e5f826e44719276d941ac9e6ae095bb748aa Mon Sep 17 00:00:00 2001 From: vowstar Date: Sat, 20 Jun 2015 01:40:45 +0800 Subject: [PATCH 8/8] Add __attribute__((optimize("O2"))) to ws2812 module. --- app/modules/ws2812.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/modules/ws2812.c b/app/modules/ws2812.c index 0a6cd5b1..21079cc3 100644 --- a/app/modules/ws2812.c +++ b/app/modules/ws2812.c @@ -16,13 +16,13 @@ // -- This WS2812 code must be compiled with -O2 to get the timing right. Read this: // -- http://wp.josh.com/2014/05/13/ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/ // -- The ICACHE_FLASH_ATTR is there to trick the compiler and get the very first pulse width correct. -static void ICACHE_FLASH_ATTR send_ws_0(uint8_t gpio) { +static void ICACHE_FLASH_ATTR __attribute__((optimize("O2"))) send_ws_0(uint8_t gpio){ uint8_t i; i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); i = 9; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); } -static void ICACHE_FLASH_ATTR send_ws_1(uint8_t gpio) { +static void ICACHE_FLASH_ATTR __attribute__((optimize("O2"))) send_ws_1(uint8_t gpio){ uint8_t i; i = 8; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); i = 6; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);