From 931aee878b0f4a778f3e8d28e2755300d588bb93 Mon Sep 17 00:00:00 2001 From: Rafal Zajac Date: Mon, 13 Nov 2017 16:29:21 +0100 Subject: [PATCH] Update documentation and fix small issues. --- README.md | 5 +- examples/CMakeLists.txt | 4 +- .../{dht22_temp_hum => dht22}/CMakeLists.txt | 8 +-- examples/{dht22_temp_hum => dht22}/README.md | 2 +- examples/{dht22_temp_hum => dht22}/main.c | 63 ++----------------- examples/ds18b20_search/CMakeLists.txt | 8 +-- examples/ds18b20_search/README.md | 2 +- examples/ds18b20_temp/CMakeLists.txt | 8 +-- examples/ds18b20_temp/README.md | 2 +- examples/ds18b20_temp/main.c | 3 +- .../{sht21_example => sht21}/CMakeLists.txt | 8 +-- examples/{sht21_example => sht21}/README.md | 2 +- examples/{sht21_example => sht21}/main.c | 61 ++---------------- src/esp_dht22/README.md | 5 +- src/esp_dht22/esp_dht22.c | 3 +- src/esp_dht22/include/esp_dht22.h | 2 + src/esp_ds18b20/README.md | 17 +++-- src/esp_ds18b20/esp_ds18b20.c | 12 ++-- src/esp_ds18b20/include/esp_ds18b20.h | 2 +- src/esp_sht21/CMakeLists.txt | 3 +- src/esp_sht21/README.md | 4 +- src/esp_sht21/esp_sht21.c | 1 - src/esp_sht21/include/esp_sht21.h | 3 +- 23 files changed, 65 insertions(+), 163 deletions(-) rename examples/{dht22_temp_hum => dht22}/CMakeLists.txt (81%) rename examples/{dht22_temp_hum => dht22}/README.md (85%) rename examples/{dht22_temp_hum => dht22}/main.c (54%) rename examples/{sht21_example => sht21}/CMakeLists.txt (83%) rename examples/{sht21_example => sht21}/README.md (87%) rename examples/{sht21_example => sht21}/main.c (59%) diff --git a/README.md b/README.md index 8f90e15..92c878b 100644 --- a/README.md +++ b/README.md @@ -31,19 +31,22 @@ $ make install ## Examples. -- [DHT22 get temperature and humidity](examples/dht22_temp_hum) +- [DHT22 get temperature and humidity](examples/dht22) - [DS18B20 get temperature](examples/ds18b20_temp) - [Search for DS18B20](examples/ds18b20_search) +- [SHT21 get temperature and humidity](examples/sht21) # Dependencies. This library depends on: +- https://github.com/rzajac/esp-ecl - https://github.com/rzajac/esp-prot to install dependency run: ``` +$ wget -O - https://raw.githubusercontent.com/rzajac/esp-ecl/master/install.sh | bash $ wget -O - https://raw.githubusercontent.com/rzajac/esp-prot/master/install.sh | bash ``` diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6bb3e6b..86856a6 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -15,5 +15,5 @@ add_subdirectory(ds18b20_search) add_subdirectory(ds18b20_temp) -add_subdirectory(dht22_temp_hum) -add_subdirectory(sht21_example) +add_subdirectory(dht22) +add_subdirectory(sht21) diff --git a/examples/dht22_temp_hum/CMakeLists.txt b/examples/dht22/CMakeLists.txt similarity index 81% rename from examples/dht22_temp_hum/CMakeLists.txt rename to examples/dht22/CMakeLists.txt index 83789d0..3460557 100644 --- a/examples/dht22_temp_hum/CMakeLists.txt +++ b/examples/dht22/CMakeLists.txt @@ -16,16 +16,16 @@ find_package(esp_sdo REQUIRED) find_package(esp_util REQUIRED) -add_executable(dht22_temp_hum main.c ${ESP_USER_CONFIG}) +add_executable(dht22_ex main.c ${ESP_USER_CONFIG}) -target_include_directories(dht22_temp_hum PUBLIC +target_include_directories(dht22_ex PUBLIC ${ESP_USER_CONFIG_DIR} ${esp_sdo_INCLUDE_DIRS} ${esp_util_INCLUDE_DIRS}) -target_link_libraries(dht22_temp_hum +target_link_libraries(dht22_ex ${esp_sdo_LIBRARIES} ${esp_util_LIBRARIES} esp_dht22) -esp_gen_exec_targets(dht22_temp_hum) +esp_gen_exec_targets(dht22_ex) diff --git a/examples/dht22_temp_hum/README.md b/examples/dht22/README.md similarity index 85% rename from examples/dht22_temp_hum/README.md rename to examples/dht22/README.md index 4d7b1c5..379a3d1 100644 --- a/examples/dht22_temp_hum/README.md +++ b/examples/dht22/README.md @@ -7,6 +7,6 @@ Demonstrates how to get temperature and humidity from DHT22 sensor. ``` $ cd build $ cmake .. -$ make dht22_temp_hum_flash +$ make dht22_ex_flash $ miniterm.py /dev/ttyUSB0 74880 ``` diff --git a/examples/dht22_temp_hum/main.c b/examples/dht22/main.c similarity index 54% rename from examples/dht22_temp_hum/main.c rename to examples/dht22/main.c index b43aec7..0bcdcfb 100644 --- a/examples/dht22_temp_hum/main.c +++ b/examples/dht22/main.c @@ -14,67 +14,16 @@ * under the License. */ -#include + #include #include -#include #include +#include +#include +#include os_timer_t timer; -/** - * Rise base to power of. - * - * From: http://bbs.espressif.com/viewtopic.php?t=246 - * - * @param base The number. - * @param exp The exponent. - * - * @return Product. - */ -static int ICACHE_FLASH_ATTR -power(int base, int exp) -{ - int result = 1; - while (exp) { - result *= base; - exp--; - } - - return result; -} - -/** - * Get string representation of float. - * - * From: http://bbs.espressif.com/viewtopic.php?t=246 - * - * Warning: limited to 15 chars & non-reentrant. - * e.g., don't use more than once per os_printf call. - * - * @param num The float to convert to string. - * @param decimals The number of decimal places. - * - * @return The float string representation. - */ -static char *ICACHE_FLASH_ATTR -ftoa(float num, uint8_t decimals) -{ - static char *buf[16]; - - int whole = (int) num; - int decimal = (int) ((num - whole) * power(10, decimals)); - if (decimal < 0) { - // get rid of sign on decimal portion - decimal -= 2 * decimal; - } - - char *pattern[10]; // setup printf pattern for decimal portion - os_sprintf((char *) pattern, "%%d.%%0%dd", decimals); - os_sprintf((char *) buf, (const char *) pattern, whole, decimal); - - return (char *) buf; -} void ICACHE_FLASH_ATTR sys_init_done(void* arg) @@ -90,8 +39,8 @@ sys_init_done(void* arg) return; } - os_printf("Temp: %s\n", ftoa(dev->temp, 2)); - os_printf("Hum: %s\n", ftoa(dev->hum, 2)); + os_printf("Temp: %s\n", esp_util_ftoa(dev->temp, 3)); + os_printf("Hum: %s\n", esp_util_ftoa(dev->hum, 3)); os_printf("--------------------\n"); os_free(dev); } diff --git a/examples/ds18b20_search/CMakeLists.txt b/examples/ds18b20_search/CMakeLists.txt index 433b27f..4600095 100644 --- a/examples/ds18b20_search/CMakeLists.txt +++ b/examples/ds18b20_search/CMakeLists.txt @@ -18,20 +18,20 @@ find_package(esp_ow REQUIRED) find_package(esp_eb REQUIRED) find_package(esp_util REQUIRED) -add_executable(ds18b20_search main.c ${ESP_USER_CONFIG}) +add_executable(ds18b20_search_ex main.c ${ESP_USER_CONFIG}) -target_include_directories(ds18b20_search PUBLIC +target_include_directories(ds18b20_search_ex PUBLIC ${ESP_USER_CONFIG_DIR} ${esp_sdo_INCLUDE_DIRS} ${esp_ow_INCLUDE_DIRS} ${esp_eb_INCLUDE_DIRS} ${esp_util_INCLUDE_DIRS}) -target_link_libraries(ds18b20_search +target_link_libraries(ds18b20_search_ex ${esp_sdo_LIBRARIES} ${esp_ow_LIBRARIES} ${esp_eb_LIBRARIES} ${esp_util_LIBRARIES} esp_ds18b20) -esp_gen_exec_targets(ds18b20_search) +esp_gen_exec_targets(ds18b20_search_ex) diff --git a/examples/ds18b20_search/README.md b/examples/ds18b20_search/README.md index f44edce..eb6f50f 100644 --- a/examples/ds18b20_search/README.md +++ b/examples/ds18b20_search/README.md @@ -7,6 +7,6 @@ Demonstrates how to search OneWire bus for DS18b20. ``` $ cd build $ cmake .. -$ make ds18b20_search_flash +$ make ds18b20_search_ex_flash $ miniterm.py /dev/ttyUSB0 74880 ``` diff --git a/examples/ds18b20_temp/CMakeLists.txt b/examples/ds18b20_temp/CMakeLists.txt index 51a7e15..6937f01 100644 --- a/examples/ds18b20_temp/CMakeLists.txt +++ b/examples/ds18b20_temp/CMakeLists.txt @@ -18,20 +18,20 @@ find_package(esp_ow REQUIRED) find_package(esp_eb REQUIRED) find_package(esp_util REQUIRED) -add_executable(ds18b20_temp main.c ${ESP_USER_CONFIG}) +add_executable(ds18b20_temp_ex main.c ${ESP_USER_CONFIG}) -target_include_directories(ds18b20_temp PUBLIC +target_include_directories(ds18b20_temp_ex PUBLIC ${ESP_USER_CONFIG_DIR} ${esp_sdo_INCLUDE_DIRS} ${esp_ow_INCLUDE_DIRS} ${esp_eb_INCLUDE_DIRS} ${esp_util_INCLUDE_DIRS}) -target_link_libraries(ds18b20_temp +target_link_libraries(ds18b20_temp_ex ${esp_sdo_LIBRARIES} ${esp_ow_LIBRARIES} ${esp_eb_LIBRARIES} ${esp_util_LIBRARIES} esp_ds18b20) -esp_gen_exec_targets(ds18b20_temp) +esp_gen_exec_targets(ds18b20_temp_ex) diff --git a/examples/ds18b20_temp/README.md b/examples/ds18b20_temp/README.md index 591a2cb..38b4f44 100644 --- a/examples/ds18b20_temp/README.md +++ b/examples/ds18b20_temp/README.md @@ -7,6 +7,6 @@ Demonstrates how to search OneWire bus for DS18b20. ``` $ cd build $ cmake .. -$ make ds18b20_temp_flash +$ make ds18b20_temp_ex_flash $ miniterm.py /dev/ttyUSB0 74880 ``` diff --git a/examples/ds18b20_temp/main.c b/examples/ds18b20_temp/main.c index ab0e721..7a92777 100644 --- a/examples/ds18b20_temp/main.c +++ b/examples/ds18b20_temp/main.c @@ -15,13 +15,12 @@ */ -#include -#include #include #include #include #include #include +#include // List of found devices on the OneWire bus. static esp_ow_device *root = NULL; diff --git a/examples/sht21_example/CMakeLists.txt b/examples/sht21/CMakeLists.txt similarity index 83% rename from examples/sht21_example/CMakeLists.txt rename to examples/sht21/CMakeLists.txt index d7d86bf..3d5a735 100644 --- a/examples/sht21_example/CMakeLists.txt +++ b/examples/sht21/CMakeLists.txt @@ -17,18 +17,18 @@ find_package(esp_sdo REQUIRED) find_package(esp_i2c REQUIRED) find_package(esp_util REQUIRED) -add_executable(sht21_example main.c ${ESP_USER_CONFIG}) +add_executable(sht21_ex main.c ${ESP_USER_CONFIG}) -target_include_directories(sht21_example PUBLIC +target_include_directories(sht21_ex PUBLIC ${ESP_USER_CONFIG_DIR} ${esp_sdo_INCLUDE_DIRS} ${esp_i2c_INCLUDE_DIRS} ${esp_util_INCLUDE_DIRS}) -target_link_libraries(sht21_example +target_link_libraries(sht21_ex ${esp_sdo_LIBRARIES} ${esp_i2c_LIBRARIES} ${esp_util_LIBRARIES} esp_sht21) -esp_gen_exec_targets(sht21_example) +esp_gen_exec_targets(sht21_ex) diff --git a/examples/sht21_example/README.md b/examples/sht21/README.md similarity index 87% rename from examples/sht21_example/README.md rename to examples/sht21/README.md index 7644cca..bd0f86c 100644 --- a/examples/sht21_example/README.md +++ b/examples/sht21/README.md @@ -10,6 +10,6 @@ Demonstrates how to: ``` $ cd build $ cmake .. -$ make sht21_example_flash +$ make sht21_ex_flash $ miniterm.py /dev/ttyUSB0 74880 ``` diff --git a/examples/sht21_example/main.c b/examples/sht21/main.c similarity index 59% rename from examples/sht21_example/main.c rename to examples/sht21/main.c index 1b6d538..5d06e64 100644 --- a/examples/sht21_example/main.c +++ b/examples/sht21/main.c @@ -14,70 +14,17 @@ * under the License. */ + #include #include +#include #include -#include #define SCL GPIO0 #define SDA GPIO2 os_timer_t timer; -/** - * Rise base to power of. - * - * From: http://bbs.espressif.com/viewtopic.php?t=246 - * - * @param base The number. - * @param exp The exponent. - * - * @return Product. - */ -static int ICACHE_FLASH_ATTR -power(int base, int exp) -{ - int result = 1; - while (exp) { - result *= base; - exp--; - } - - return result; -} - -/** - * Get string representation of float. - * - * From: http://bbs.espressif.com/viewtopic.php?t=246 - * - * Warning: limited to 15 chars & non-reentrant. - * e.g., don't use more than once per os_printf call. - * - * @param num The float to convert to string. - * @param decimals The number of decimal places. - * - * @return The float string representation. - */ -static char *ICACHE_FLASH_ATTR -ftoa(float num, uint8_t decimals) -{ - static char *buf[16]; - - int whole = (int) num; - int decimal = (int) ((num - whole) * power(10, decimals)); - if (decimal < 0) { - // get rid of sign on decimal portion - decimal -= 2 * decimal; - } - - char *pattern[10]; // setup printf pattern for decimal portion - os_sprintf((char *) pattern, "%%d.%%0%dd", decimals); - os_sprintf((char *) buf, (const char *) pattern, whole, decimal); - - return (char *) buf; -} - void ICACHE_FLASH_ATTR run_sht21() { @@ -107,12 +54,12 @@ run_sht21() err = esp_sht21_get_rh(&value); if (err != ESP_I2C_OK) os_printf("Get RH error: %d\n", err); - os_printf("Humidity: %s%%\n", ftoa(value, 2)); + os_printf("Humidity: %s%%\n", esp_util_ftoa(value, 2)); err = esp_sht21_get_temp_last(&value); if (err != ESP_I2C_OK) os_printf("Get TEMP error: %d\n", err); - os_printf("Temperature: %s deg. C\n", ftoa(value, 2)); + os_printf("Temperature: %s deg. C\n", esp_util_ftoa(value, 2)); } void ICACHE_FLASH_ATTR diff --git a/src/esp_dht22/README.md b/src/esp_dht22/README.md index ca2d183..310e577 100644 --- a/src/esp_dht22/README.md +++ b/src/esp_dht22/README.md @@ -8,6 +8,5 @@ Before you can get the temperature and humidity from DHT22 you have to call `esp_dht22_init`. You need to call it only once unless you change the GPIO pin setup somewhere else in your code. - -See driver documentation in [esp_dht22.h](include/esp_dht22.h) header file -for more details. +See [example program](../../examples/dht22) and driver documentation +in [esp_dht22.h](include/esp_dht22.h) header file for more details. diff --git a/src/esp_dht22/esp_dht22.c b/src/esp_dht22/esp_dht22.c index 352ebb3..7cabc5b 100644 --- a/src/esp_dht22/esp_dht22.c +++ b/src/esp_dht22/esp_dht22.c @@ -14,16 +14,17 @@ * under the License. */ + #include #include #include #include #define BUS_LOW(gpio_num) (GPIO_OUT_EN_S = (0x1 << (gpio_num))) -#define BUS_HIGH(gpio_num) (GPIO_OUT_EN_C = (0x1 << (gpio_num))) #define BUS_RELEASE(gpio_num) (GPIO_OUT_EN_C = (0x1 << (gpio_num))) #define BUS_READ(gpio_num) ((GPIO_IN & (0x1 << (gpio_num))) != 0) + /** * Measure number of 10us slots the state was kept. * diff --git a/src/esp_dht22/include/esp_dht22.h b/src/esp_dht22/include/esp_dht22.h index 83cd057..6a416c2 100644 --- a/src/esp_dht22/include/esp_dht22.h +++ b/src/esp_dht22/include/esp_dht22.h @@ -14,6 +14,7 @@ * under the License. */ + #ifndef ESP_DHT22_H #define ESP_DHT22_H @@ -40,6 +41,7 @@ typedef enum { void ets_isr_mask(unsigned intr); void ets_isr_unmask(unsigned intr); + /** * Initialize DHT22. * diff --git a/src/esp_ds18b20/README.md b/src/esp_ds18b20/README.md index 81f1332..9f4f3ec 100644 --- a/src/esp_ds18b20/README.md +++ b/src/esp_ds18b20/README.md @@ -18,7 +18,7 @@ anymore. Most of the operations with the DS18B20 library is based on passing pointers to the `esp_ow_device` structure which has pointer to `esp_ds18b20_st` status structure. The `esp_ds18b20_st` keeps track of last scratch pad read -the last temperature conversion. +and last temperature conversion. ``` esp_ow_device *device; @@ -34,12 +34,11 @@ esp_ow_dev *device = esp_ds18b20_new_dev(rom); ``` -With DS18B20 temperature measurement takes between 94 and 750ms depending -on resolution. You don't want to block the CPU for that long waiting for -example using some kind of delay. That's why library is using event bus -(esp_eb) to emmit events when the temperature conversion is ready to read. -Check [example program](../../examples/ds18b20_temp) to see how it should be -done. +Temperature measurement with DS18B20 takes between 94 and 750ms depending +on resolution. You don't want to block the CPU for that long using some kind +of delay. That's why library is using event bus (esp_eb) to emmit events when +the temperature conversion is ready. -See driver documentation in [esp_ds18b20.h](include/esp_ds18b20.h) header file -for more details. +Check [example program](../../examples/ds18b20_temp) to see how it should be +done and driver documentation in [esp_ds18b20.h](include/esp_ds18b20.h) +header file for more details. diff --git a/src/esp_ds18b20/esp_ds18b20.c b/src/esp_ds18b20/esp_ds18b20.c index 8746511..ac13f13 100644 --- a/src/esp_ds18b20/esp_ds18b20.c +++ b/src/esp_ds18b20/esp_ds18b20.c @@ -217,10 +217,15 @@ esp_ds18b20_search(uint8_t gpio_num, bool in_alert, esp_ow_device **list) esp_ow_device *ICACHE_FLASH_ATTR esp_ds18b20_new_dev(uint8_t *rom) { - esp_ow_device *device = os_zalloc(sizeof(esp_ow_device)); - esp_ds18b20_st *st = os_zalloc(sizeof(esp_ds18b20_st)); + esp_ow_device *device = esp_ow_new_dev(rom); + if (device == NULL) return NULL; + + esp_ds18b20_st *st = os_zalloc(sizeof(esp_ds18b20_st)); + if (st == NULL) { + os_free(device); + return NULL; + } - os_memcpy(device->rom, rom, 8); st->last_temp = ESP_DS18B20_TEMP_ERR; st->retries = -1; device->custom = st; @@ -228,7 +233,6 @@ esp_ds18b20_new_dev(uint8_t *rom) return device; } - esp_ow_device *ICACHE_FLASH_ATTR esp_ds18b20_get(uint8_t gpio_num) { diff --git a/src/esp_ds18b20/include/esp_ds18b20.h b/src/esp_ds18b20/include/esp_ds18b20.h index f5318e7..f5b6939 100644 --- a/src/esp_ds18b20/include/esp_ds18b20.h +++ b/src/esp_ds18b20/include/esp_ds18b20.h @@ -95,7 +95,7 @@ esp_ds18b20_search(uint8_t gpio_num, bool in_alert, esp_ow_device **list); * * @param rom The pointer to 8 byte ROM address * - * @return The device. + * @return The device or NULL on error. */ esp_ow_device *ICACHE_FLASH_ATTR esp_ds18b20_new_dev(uint8_t *rom); diff --git a/src/esp_sht21/CMakeLists.txt b/src/esp_sht21/CMakeLists.txt index 8985324..3181da8 100644 --- a/src/esp_sht21/CMakeLists.txt +++ b/src/esp_sht21/CMakeLists.txt @@ -29,5 +29,4 @@ target_include_directories(esp_sht21 PUBLIC esp_gen_lib(esp_sht21 ${ESP_CMAKE_FIND_DIR} - ${esp_i2c_LIBRARIES} - ${esp_tim_LIBRARIES}) + ${esp_i2c_LIBRARIES}) diff --git a/src/esp_sht21/README.md b/src/esp_sht21/README.md index e782677..1d6074d 100644 --- a/src/esp_sht21/README.md +++ b/src/esp_sht21/README.md @@ -14,5 +14,5 @@ Before you can start communicating with SHT21 you have to call `esp_sht21_init`. You need to call it only once unless you change the GPIO pins setup somewhere else in your code. -See driver documentation in [esp_sht21.h](include/esp_sht21.h) header file -for more details. +See [example program](../../examples/sht21) and driver documentation in +[esp_sht21.h](include/esp_sht21.h) header file for more details. diff --git a/src/esp_sht21/esp_sht21.c b/src/esp_sht21/esp_sht21.c index 783b87d..6548496 100644 --- a/src/esp_sht21/esp_sht21.c +++ b/src/esp_sht21/esp_sht21.c @@ -15,7 +15,6 @@ */ #include -#include static uint8_t ICACHE_FLASH_ATTR calc_crc(uint8_t init, const uint8_t *data, uint8_t len) diff --git a/src/esp_sht21/include/esp_sht21.h b/src/esp_sht21/include/esp_sht21.h index d842323..178a1af 100644 --- a/src/esp_sht21/include/esp_sht21.h +++ b/src/esp_sht21/include/esp_sht21.h @@ -18,8 +18,8 @@ #ifndef ESP_SHT21_H #define ESP_SHT21_H -#include #include +#include #define ESP_SHT21_ADDRESS 0x40 // Measure relative humidity. Hold Master Mode. @@ -56,6 +56,7 @@ // RH: 11bit TEMP: 11bit #define ESP_SHT21_RES0 0x3 + /** * Initialize SHT21. *