Added support for Analog Devices ADXL345 accelerometer (#1252)
This commit is contained in:
parent
bb9e210441
commit
be10ccdb6b
|
@ -18,6 +18,7 @@
|
||||||
// See https://github.com/nodemcu/nodemcu-firmware/pull/1127 for discussions.
|
// See https://github.com/nodemcu/nodemcu-firmware/pull/1127 for discussions.
|
||||||
// New modules should be disabled by default and added in alphabetical order.
|
// New modules should be disabled by default and added in alphabetical order.
|
||||||
#define LUA_USE_MODULES_ADC
|
#define LUA_USE_MODULES_ADC
|
||||||
|
//#define LUA_USE_MODULES_ADXL345
|
||||||
//#define LUA_USE_MODULES_AM2320
|
//#define LUA_USE_MODULES_AM2320
|
||||||
//#define LUA_USE_MODULES_APA102
|
//#define LUA_USE_MODULES_APA102
|
||||||
#define LUA_USE_MODULES_BIT
|
#define LUA_USE_MODULES_BIT
|
||||||
|
@ -61,6 +62,5 @@
|
||||||
//#define LUA_USE_MODULES_WS2801
|
//#define LUA_USE_MODULES_WS2801
|
||||||
//#define LUA_USE_MODULES_WS2812
|
//#define LUA_USE_MODULES_WS2812
|
||||||
|
|
||||||
|
|
||||||
#endif /* LUA_CROSS_COMPILER */
|
#endif /* LUA_CROSS_COMPILER */
|
||||||
#endif /* __USER_MODULES_H__ */
|
#endif /* __USER_MODULES_H__ */
|
||||||
|
|
|
@ -0,0 +1,95 @@
|
||||||
|
/*
|
||||||
|
* Driver for Analog Devices ADXL345 accelerometer.
|
||||||
|
*
|
||||||
|
* Code based on BMP085 driver.
|
||||||
|
*/
|
||||||
|
#include "module.h"
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "platform.h"
|
||||||
|
#include "c_stdlib.h"
|
||||||
|
#include "c_string.h"
|
||||||
|
|
||||||
|
static const uint32_t adxl345_i2c_id = 0;
|
||||||
|
static const uint8_t adxl345_i2c_addr = 0x53;
|
||||||
|
|
||||||
|
static uint8_t ICACHE_FLASH_ATTR r8u(uint32_t id, uint8_t reg) {
|
||||||
|
uint8_t ret;
|
||||||
|
|
||||||
|
platform_i2c_send_start(id);
|
||||||
|
platform_i2c_send_address(id, adxl345_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
|
||||||
|
platform_i2c_send_byte(id, reg);
|
||||||
|
platform_i2c_send_stop(id);
|
||||||
|
platform_i2c_send_start(id);
|
||||||
|
platform_i2c_send_address(id, adxl345_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
|
||||||
|
ret = platform_i2c_recv_byte(id, 0);
|
||||||
|
platform_i2c_send_stop(id);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ICACHE_FLASH_ATTR adxl345_init(lua_State* L) {
|
||||||
|
|
||||||
|
uint32_t sda;
|
||||||
|
uint32_t scl;
|
||||||
|
uint8_t devid;
|
||||||
|
|
||||||
|
sda = luaL_checkinteger(L, 1);
|
||||||
|
scl = luaL_checkinteger(L, 2);
|
||||||
|
|
||||||
|
luaL_argcheck(L, sda > 0 && scl > 0, 1, "no i2c for D0");
|
||||||
|
|
||||||
|
platform_i2c_setup(adxl345_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
|
||||||
|
|
||||||
|
devid = r8u(adxl345_i2c_id, 0x00);
|
||||||
|
|
||||||
|
if (devid != 229) {
|
||||||
|
return luaL_error(L, "device not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable sensor
|
||||||
|
platform_i2c_send_start(adxl345_i2c_id);
|
||||||
|
platform_i2c_send_address(adxl345_i2c_id, adxl345_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
|
||||||
|
platform_i2c_send_byte(adxl345_i2c_id, 0x2D);
|
||||||
|
platform_i2c_send_byte(adxl345_i2c_id, 0x08);
|
||||||
|
platform_i2c_send_stop(adxl345_i2c_id);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ICACHE_FLASH_ATTR adxl345_read(lua_State* L) {
|
||||||
|
|
||||||
|
uint8_t data[6];
|
||||||
|
int x,y,z;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
platform_i2c_send_start(adxl345_i2c_id);
|
||||||
|
platform_i2c_send_address(adxl345_i2c_id, adxl345_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER);
|
||||||
|
platform_i2c_send_byte(adxl345_i2c_id, 0x32);
|
||||||
|
platform_i2c_send_start(adxl345_i2c_id);
|
||||||
|
platform_i2c_send_address(adxl345_i2c_id, adxl345_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER);
|
||||||
|
|
||||||
|
for (i=0; i<5; i++) {
|
||||||
|
data[i] = platform_i2c_recv_byte(adxl345_i2c_id, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
data[5] = platform_i2c_recv_byte(adxl345_i2c_id, 0);
|
||||||
|
|
||||||
|
platform_i2c_send_stop(adxl345_i2c_id);
|
||||||
|
|
||||||
|
x = (int16_t) ((data[1] << 8) | data[0]);
|
||||||
|
y = (int16_t) ((data[3] << 8) | data[2]);
|
||||||
|
z = (int16_t) ((data[5] << 8) | data[4]);
|
||||||
|
|
||||||
|
lua_pushinteger(L, x);
|
||||||
|
lua_pushinteger(L, y);
|
||||||
|
lua_pushinteger(L, z);
|
||||||
|
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const LUA_REG_TYPE adxl345_map[] = {
|
||||||
|
{ LSTRKEY( "read" ), LFUNCVAL( adxl345_read )},
|
||||||
|
{ LSTRKEY( "init" ), LFUNCVAL( adxl345_init )},
|
||||||
|
{ LNILKEY, LNILVAL}
|
||||||
|
};
|
||||||
|
|
||||||
|
NODEMCU_MODULE(ADXL345, "adxl345", adxl345_map, NULL);
|
|
@ -0,0 +1,36 @@
|
||||||
|
# ADXL345 Module
|
||||||
|
| Since | Origin / Contributor | Maintainer | Source |
|
||||||
|
| :----- | :-------------------- | :---------- | :------ |
|
||||||
|
| 2016-04-08 | [Jason Schmidlapp](https://github.com/jschmidlapp) | [Jason Schmidlapp](https://github.com/jschmidlapp) | [adxl345.c](../../../app/modules/adxl345.c)|
|
||||||
|
|
||||||
|
|
||||||
|
This module provides access to the [ADXL345](https://www.sparkfun.com/products/9836) triple axis accelerometer.
|
||||||
|
|
||||||
|
## adxl345.read()
|
||||||
|
Samples the sensor and returns X,Y and Z data from the accelerometer.
|
||||||
|
|
||||||
|
#### Syntax
|
||||||
|
`adxl345.read()`
|
||||||
|
|
||||||
|
#### Returns
|
||||||
|
X,Y,Z data (integers)
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
```lua
|
||||||
|
adxl345.init(1, 2)
|
||||||
|
local x,y,z = adxl345.read()
|
||||||
|
print(string.format("X = %d, Y = %d, Z = %d", x, y, z))
|
||||||
|
```
|
||||||
|
|
||||||
|
## adxl345.init()
|
||||||
|
Initializes the module and sets the pin configuration.
|
||||||
|
|
||||||
|
#### Syntax
|
||||||
|
`adxl345.init(sda, scl)`
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
- `sda` data pin
|
||||||
|
- `scl` clock pin
|
||||||
|
|
||||||
|
#### Returns
|
||||||
|
`nil`
|
|
@ -34,6 +34,7 @@ pages:
|
||||||
- Support: 'en/support.md'
|
- Support: 'en/support.md'
|
||||||
- Modules:
|
- Modules:
|
||||||
- 'adc': 'en/modules/adc.md'
|
- 'adc': 'en/modules/adc.md'
|
||||||
|
- 'adxl345': 'en/modules/adxl345.md'
|
||||||
- 'am2320': 'en/modules/am2320.md'
|
- 'am2320': 'en/modules/am2320.md'
|
||||||
- 'apa102': 'en/modules/apa102.md'
|
- 'apa102': 'en/modules/apa102.md'
|
||||||
- 'bit': 'en/modules/bit.md'
|
- 'bit': 'en/modules/bit.md'
|
||||||
|
|
Loading…
Reference in New Issue