Commit 5a6b4f3d authored by Christian Eichler's avatar Christian Eichler
Browse files

ws-only: Fixup accelerometer.{cpp,h}

parent ee787985
......@@ -45,110 +45,109 @@
#define REG_ACTTHS 0x3E
#define REG_ACTDUR 0x3F
Accelerometer::Accelerometer(int8_t cspin) : _cs(cspin) { }
Accelerometer::Accelerometer(int8_t cspin) : _cs(cspin) { }
bool Accelerometer::begin(accel_dataRate_t dataRate, accel_range_t range) {
digitalWrite(_cs, HIGH);
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
pinMode(_cs, OUTPUT);
// hardware SPI
SPI.begin();
// hardware SPI
SPI.begin();
/* Check connection */
uint8_t deviceid = readRegister8(REG_WHOAMI);
if (deviceid != 0x33) {
/* No LIS3DH detected ... return false */
return false;
}
/* Check connection */
uint8_t deviceid = readRegister8(REG_WHOAMI);
if (deviceid != 0x33) {
/* No LIS3DH detected ... return false */
return false;
}
// enable all axes, normal mode
writeRegister8(REG_CTRL1, 0x0f | (dataRate << 4));
// enable all axes, normal mode
writeRegister8(REG_CTRL1, 0x0f | (dataRate << 4));
// Block data update
writeRegister8(REG_CTRL4, 0x80 | (range << 4));
// Block data update
writeRegister8(REG_CTRL4, 0x80 | (range << 4));
writeRegister8(REG_CTRL5, 0x0a); // latch interrupt on int1 & int2
writeRegister8(REG_CTRL5, 0x0a); // latch interrupt on int1 & int2
return true;
return true;
}
void Accelerometer::read(void) {
// read x y z at once
// read x y z at once
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW);
SPI.transfer(REG_OUT_X_L | 0x80 | 0x40); // read multiple, bit 7&6 high
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW);
SPI.transfer(REG_OUT_X_L | 0x80 | 0x40); // read multiple, bit 7&6 high
SPI.transfer(0xFF);
x = SPI.transfer(0xFF);
SPI.transfer(0xFF);
y = SPI.transfer(0xFF);
SPI.transfer(0xFF);
z = SPI.transfer(0xFF);
SPI.transfer(0xFF);
x = SPI.transfer(0xFF);
SPI.transfer(0xFF);
y = SPI.transfer(0xFF);
SPI.transfer(0xFF);
z = SPI.transfer(0xFF);
digitalWrite(_cs, HIGH);
SPI.endTransaction();
digitalWrite(_cs, HIGH);
SPI.endTransaction();
}
void Accelerometer::setMovementInterrupt(uint8_t movethresh, uint8_t duration) {
uint8_t r = readRegister8(REG_CTRL3);
r |= (0x20); // turn on IA2
writeRegister8(REG_CTRL3, r);
uint8_t r = readRegister8(REG_CTRL3);
r |= (0x20); // turn on IA2
writeRegister8(REG_CTRL3, r);
writeRegister8(REG_INT2THS, movethresh); // arbitrary
writeRegister8(REG_INT2DUR, duration); // arbitrary
writeRegister8(REG_INT2THS, movethresh); // arbitrary
writeRegister8(REG_INT2DUR, duration); // arbitrary
writeRegister8(REG_INT2CFG, 0x7f); // turn on all axes + 6D
writeRegister8(REG_INT2CFG, 0x7f); // turn on all axes + 6D
}
uint8_t Accelerometer::getMovement(void) {
return readRegister8(REG_INT2SRC);
return readRegister8(REG_INT2SRC);
}
void Accelerometer::setClickInterrupt(uint8_t mask, uint8_t clickthresh, uint8_t timelimit, uint8_t timelatency, uint8_t timewindow) {
uint8_t r = readRegister8(REG_CTRL3);
if (mask == 0) {
//disable int
r &= ~(0x80); // turn off I1_CLICK
writeRegister8(REG_CTRL3, r);
writeRegister8(REG_CLICKCFG, 0);
return;
}
// else...
r |= (0x80); // turn on I1_CLICK
writeRegister8(REG_CTRL3, r);
writeRegister8(REG_CLICKCFG, ((mask & 1) != 0 ? 0x15 : 0) | ((mask & 2) != 0 ? 0x2a : 0)); // turn on all axes & tap
writeRegister8(REG_CLICKTHS, clickthresh); // arbitrary
writeRegister8(REG_TIMELIMIT, timelimit); // arbitrary
writeRegister8(REG_TIMELATENCY, timelatency); // arbitrary
writeRegister8(REG_TIMEWINDOW, timewindow); // arbitrary
uint8_t r = readRegister8(REG_CTRL3);
if (mask == 0) {
//disable int
r &= ~(0x80); // turn off I1_CLICK
writeRegister8(REG_CTRL3, r);
writeRegister8(REG_CLICKCFG, 0);
return;
}
// else...
r |= (0x80); // turn on I1_CLICK
writeRegister8(REG_CTRL3, r);
writeRegister8(REG_CLICKCFG, ((mask & 1) != 0 ? 0x15 : 0) | ((mask & 2) != 0 ? 0x2a : 0)); // turn on all axes & tap
writeRegister8(REG_CLICKTHS, clickthresh); // arbitrary
writeRegister8(REG_TIMELIMIT, timelimit); // arbitrary
writeRegister8(REG_TIMELATENCY, timelatency); // arbitrary
writeRegister8(REG_TIMEWINDOW, timewindow); // arbitrary
}
uint8_t Accelerometer::getClick(void) {
return readRegister8(REG_CLICKSRC);
return readRegister8(REG_CLICKSRC);
}
void Accelerometer::writeRegister8(uint8_t reg, uint8_t value) {
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW);
SPI.transfer(reg & ~0x80); // write, bit 7 low
SPI.transfer(value);
digitalWrite(_cs, HIGH);
SPI.endTransaction(); // release the SPI bus
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW);
SPI.transfer(reg & ~0x80); // write, bit 7 low
SPI.transfer(value);
digitalWrite(_cs, HIGH);
SPI.endTransaction(); // release the SPI bus
}
uint8_t Accelerometer::readRegister8(uint8_t reg) {
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW);
SPI.transfer(reg | 0x80); // read, bit 7 high
uint8_t value = SPI.transfer(0);
digitalWrite(_cs, HIGH);
SPI.endTransaction(); // release the SPI bus
return value;
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(_cs, LOW);
SPI.transfer(reg | 0x80); // read, bit 7 high
uint8_t value = SPI.transfer(0);
digitalWrite(_cs, HIGH);
SPI.endTransaction(); // release the SPI bus
return value;
}
......@@ -9,51 +9,51 @@
#include <SPI.h>
class Accelerometer {
typedef enum {
RANGE_16_G = 0b11, // +/- 16g
RANGE_8_G = 0b10, // +/- 8g
RANGE_4_G = 0b01, // +/- 4g
RANGE_2_G = 0b00 // +/- 2g (default value)
} accel_range_t;
typedef enum {
AXIS_X = 0x0,
AXIS_Y = 0x1,
AXIS_Z = 0x2,
} accel_axis_t;
/* Used with register 0x2A (ACCEL_REG_CTRL_REG1) to set bandwidth */
typedef enum {
DATARATE_400_HZ = 0b0111, // 400Hz
DATARATE_200_HZ = 0b0110, // 200Hz
DATARATE_100_HZ = 0b0101, // 100Hz
DATARATE_50_HZ = 0b0100, // 50Hz
DATARATE_25_HZ = 0b0011, // 25Hz
DATARATE_10_HZ = 0b0010, // 10 Hz
DATARATE_1_HZ = 0b0001, // 1 Hz
DATARATE_POWERDOWN = 0,
DATARATE_LOWPOWER_1K6HZ = 0b1000,
DATARATE_LOWPOWER_5KHZ = 0b1001,
} accel_dataRate_t;
typedef enum {
RANGE_16_G = 0b11, // +/- 16g
RANGE_8_G = 0b10, // +/- 8g
RANGE_4_G = 0b01, // +/- 4g
RANGE_2_G = 0b00 // +/- 2g (default value)
} accel_range_t;
typedef enum {
AXIS_X = 0x0,
AXIS_Y = 0x1,
AXIS_Z = 0x2,
} accel_axis_t;
/* Used with register 0x2A (ACCEL_REG_CTRL_REG1) to set bandwidth */
typedef enum {
DATARATE_400_HZ = 0b0111, // 400Hz
DATARATE_200_HZ = 0b0110, // 200Hz
DATARATE_100_HZ = 0b0101, // 100Hz
DATARATE_50_HZ = 0b0100, // 50Hz
DATARATE_25_HZ = 0b0011, // 25Hz
DATARATE_10_HZ = 0b0010, // 10Hz
DATARATE_1_HZ = 0b0001, // 1Hz
DATARATE_POWERDOWN = 0,
DATARATE_LOWPOWER_1K6HZ = 0b1000,
DATARATE_LOWPOWER_5KHZ = 0b1001,
} accel_dataRate_t;
public:
Accelerometer(int8_t cspin);
Accelerometer(int8_t cspin);
bool begin(accel_dataRate_t dataRate = DATARATE_10_HZ, accel_range_t range = RANGE_2_G);
void read();
bool begin(accel_dataRate_t dataRate = DATARATE_10_HZ, accel_range_t range = RANGE_2_G);
void setMovementInterrupt(uint8_t movethresh = 0x20, uint8_t duration = 0x05);
uint8_t getMovement(void);
void setClickInterrupt(uint8_t mask, uint8_t clickthresh, uint8_t timelimit = 10, uint8_t timelatency = 20, uint8_t timewindow = 255);
uint8_t getClick(void);
void read();
int8_t x, y, z;
void setMovementInterrupt(uint8_t movethresh = 0x20, uint8_t duration = 0x05);
uint8_t getMovement(void);
void setClickInterrupt(uint8_t mask, uint8_t clickthresh, uint8_t timelimit = 10, uint8_t timelatency = 20, uint8_t timewindow = 255);
uint8_t getClick(void);
int8_t x, y, z;
private:
uint8_t readRegister8(uint8_t reg);
void writeRegister8(uint8_t reg, uint8_t value);
int8_t _cs;
uint8_t readRegister8(uint8_t reg);
void writeRegister8(uint8_t reg, uint8_t value);
int8_t _cs;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment