From cf079fa333daa51d3d9e4ddce6b8acfb833ac72b Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 8 Oct 2015 20:34:24 -0700 Subject: [PATCH 1/5] Basic hx711 functionality --- app/include/user_modules.h | 1 + app/modules/hx711.c | 81 ++++++++++++++++++++++++++++++++++++++ app/modules/modules.h | 11 +++++- 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 app/modules/hx711.c diff --git a/app/include/user_modules.h b/app/include/user_modules.h index c9e06f13..eb8d21b1 100644 --- a/app/include/user_modules.h +++ b/app/include/user_modules.h @@ -43,6 +43,7 @@ #define LUA_USE_MODULES_SNTP //#define LUA_USE_MODULES_BMP085 #define LUA_USE_MODULES_TSL2561 +#define LUA_USE_MODULES_HX711 #endif /* LUA_USE_MODULES */ diff --git a/app/modules/hx711.c b/app/modules/hx711.c new file mode 100644 index 00000000..c95cfff3 --- /dev/null +++ b/app/modules/hx711.c @@ -0,0 +1,81 @@ +#include "lualib.h" +#include "lauxlib.h" +#include "platform.h" +#include "auxmods.h" +#include "lrotable.h" +#include "c_stdlib.h" +#include "c_string.h" +#include "user_interface.h" +static uint8_t data_pin; +static uint8_t clk_pin; + +/*Lua: init(clk_pin,data_pin)*/ +static int hx711_init(lua_State* L) { + clk_pin = luaL_checkinteger(L,1); + data_pin = luaL_checkinteger(L,2); + MOD_CHECK_ID( gpio, clk_pin ); + MOD_CHECK_ID( gpio, data_pin ); + + platform_gpio_mode(clk_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); + platform_gpio_mode(data_pin, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_FLOAT); + platform_gpio_write(clk_pin,1);//put chip to sleep. + return 0; +} + +#define HX711_MAX_WAIT 1000000 +/*will only read chA@128gain*/ +/*Lua: result = hx711.read()*/ +static int ICACHE_FLASH_ATTR hx711_read(lua_State* L) { + uint32_t i; + int32_t data = 0; + //TODO: double check init has happened first. + + //wakeup hx711 + platform_gpio_write(clk_pin,0); + + //wait for data ready. or time out. + //TODO: set pin inturrupt and come back to it. This may take up to 1/10 sec + // or maybe just make an async version too and have both available. + for (i = 0; i=HX711_MAX_WAIT) { + return luaL_error( L, "ADC timeout!", ( unsigned )0 ); + } + + for (i = 0; i<24 ; i++){ //clock in the 24 bits + platform_gpio_write(clk_pin,1); + platform_gpio_write(clk_pin,0); + data = data<<1; + if (platform_gpio_read(data_pin)==1) { + data = i==0 ? -1 : data|1; //signextend the first bit + } + } + //add 25th clock pulse to prevent protocol error (probably not needed + // since we'll go to sleep immediately after and reset on wakeup.) + platform_gpio_write(clk_pin,1); + platform_gpio_write(clk_pin,0); + //sleep + platform_gpio_write(clk_pin,1); + lua_pushinteger( L, data ); + return 1; +} + +#define MIN_OPT_LEVEL 2 +#include "lrodefs.h" +const LUA_REG_TYPE hx711_map[] = +{ + { LSTRKEY( "init" ), LFUNCVAL( hx711_init )}, + { LSTRKEY( "read" ), LFUNCVAL( hx711_read )}, + //{ LSTRKEY( "write" ), LFUNCVAL( ws2812_writegrb )}, + { LNILKEY, LNILVAL} +}; + +LUALIB_API int luaopen_hx711(lua_State *L) { + // TODO: the below todo was inherited from the ws2812 code but is still valid. + // TODO: Make sure that the GPIO system is initialized + LREGISTER(L, "hx711", hx711_map); + return 1; +} diff --git a/app/modules/modules.h b/app/modules/modules.h index 48781cc9..7292b6e9 100644 --- a/app/modules/modules.h +++ b/app/modules/modules.h @@ -245,6 +245,14 @@ #define ROM_MODULES_TSL2561 #endif +#if defined(LUA_USE_MODULES_HX711) +#define MODULES_HX711 "hx711" +#define ROM_MODULES_HX711 \ + _ROM(MODULES_HX711, luaopen_hx711, hx711_map) +#else +#define ROM_MODULES_HX711 +#endif + #define LUA_MODULES_ROM \ ROM_MODULES_GPIO \ ROM_MODULES_PWM \ @@ -275,6 +283,7 @@ ROM_MODULES_RTCFIFO \ ROM_MODULES_SNTP \ ROM_MODULES_BMP085 \ - ROM_MODULES_TSL2561 + ROM_MODULES_TSL2561 \ + ROM_MODULES_HX711 #endif From c298a5cf6050e91425a15896947dae41911ede94 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 8 Oct 2015 20:43:57 -0700 Subject: [PATCH 2/5] documented hx711 --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 5580c61c..c5f50aae 100644 --- a/README.md +++ b/README.md @@ -573,3 +573,13 @@ value = { true, { foo = "bar" } } json_text = cjson.encode(value) -- Returns: '[true,{"foo":"bar"}]' ``` + +####Read an HX711 load cell ADC. +Note: currently only chanel A with gain 128 is supported. +The HX711 is an inexpensive 24bit ADC with programmable 128x, 64x, and 32x gain. +```lua + -- Initialize the hx711 with clk and data pin assignments + hx711.init(5,6) + -- Read ch A with 128 gain. + raw_data = hx711.read() +``` From 140bcee2e0f6f5092a3dfd73881de56f9a69266d Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 8 Oct 2015 20:44:19 -0700 Subject: [PATCH 3/5] comment cleanup --- app/modules/hx711.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/modules/hx711.c b/app/modules/hx711.c index c95cfff3..75822b9c 100644 --- a/app/modules/hx711.c +++ b/app/modules/hx711.c @@ -9,7 +9,7 @@ static uint8_t data_pin; static uint8_t clk_pin; -/*Lua: init(clk_pin,data_pin)*/ +/*Lua: hx711.init(clk_pin,data_pin)*/ static int hx711_init(lua_State* L) { clk_pin = luaL_checkinteger(L,1); data_pin = luaL_checkinteger(L,2); @@ -36,8 +36,9 @@ static int ICACHE_FLASH_ATTR hx711_read(lua_State* L) { //wait for data ready. or time out. //TODO: set pin inturrupt and come back to it. This may take up to 1/10 sec // or maybe just make an async version too and have both available. + WRITE_PERI_REG(0x60000914, 0x73); //clear WDT... this may take a while. for (i = 0; i Date: Fri, 30 Oct 2015 23:27:31 -0700 Subject: [PATCH 4/5] doc hx711 update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c5f50aae..8b0f375e 100644 --- a/README.md +++ b/README.md @@ -578,8 +578,8 @@ json_text = cjson.encode(value) Note: currently only chanel A with gain 128 is supported. The HX711 is an inexpensive 24bit ADC with programmable 128x, 64x, and 32x gain. ```lua - -- Initialize the hx711 with clk and data pin assignments + -- Initialize the hx711 with clk on pin 5 and data on pin 6 hx711.init(5,6) -- Read ch A with 128 gain. - raw_data = hx711.read() + raw_data = hx711.read(0) ``` From dffbfacd46c9ceea00169b2f3940edf4f3c46ddc Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 30 Oct 2015 23:31:45 -0700 Subject: [PATCH 5/5] most users won't need this so it should be off be default --- app/include/user_modules.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/include/user_modules.h b/app/include/user_modules.h index eb8d21b1..5fd1e752 100644 --- a/app/include/user_modules.h +++ b/app/include/user_modules.h @@ -43,7 +43,7 @@ #define LUA_USE_MODULES_SNTP //#define LUA_USE_MODULES_BMP085 #define LUA_USE_MODULES_TSL2561 -#define LUA_USE_MODULES_HX711 +//#define LUA_USE_MODULES_HX711 #endif /* LUA_USE_MODULES */