2021-01-17 16:05:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "hardware/i2c.h"
|
|
|
|
#include "hardware/gpio.h"
|
2021-05-14 18:12:37 +01:00
|
|
|
#include "common/pimoroni_common.hpp"
|
|
|
|
#include "common/pimoroni_i2c.hpp"
|
2021-01-17 16:05:28 +00:00
|
|
|
|
|
|
|
namespace pimoroni {
|
|
|
|
|
|
|
|
class MSA301 {
|
2021-05-13 13:12:30 +01:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Constants
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
static const uint8_t DEFAULT_I2C_ADDRESS = 0x26;
|
|
|
|
|
|
|
|
static const uint8_t SOFT_RESET = 0x00;
|
|
|
|
static const uint8_t PART_ID = 0x01;
|
|
|
|
static const uint8_t MOTION_INTERRUPT = 0x09;
|
|
|
|
static const uint8_t DATA_INTERRUPT = 0x0a;
|
|
|
|
static const uint8_t ORIENTATION_STATUS = 0x0c;
|
|
|
|
static const uint8_t RESOLUTION_RANGE = 0x0f;
|
2021-05-13 16:26:08 +01:00
|
|
|
static const uint8_t POWER_MODE_BANDWIDTH = 0x11;
|
2021-05-13 13:12:30 +01:00
|
|
|
static const uint8_t SET_AXIS_POLARITY = 0x12;
|
|
|
|
static const uint8_t INTERRUPT_ENABLE_0 = 0x16;
|
|
|
|
static const uint8_t INTERRUPT_ENABLE_1 = 0x17;
|
|
|
|
static const uint8_t INTERRUPT_LATCH_PERIOD = 0x21;
|
|
|
|
static const uint8_t FREEFALL_DURATION = 0x22;
|
|
|
|
|
2021-01-17 16:05:28 +00:00
|
|
|
|
2021-05-13 16:26:08 +01:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Enums
|
|
|
|
//--------------------------------------------------
|
2021-01-17 16:05:28 +00:00
|
|
|
public:
|
2021-05-13 13:12:30 +01:00
|
|
|
enum Axis {
|
|
|
|
X = 0x02,
|
|
|
|
Y = 0x04,
|
|
|
|
Z = 0x06
|
|
|
|
};
|
2021-01-17 16:05:28 +00:00
|
|
|
|
2021-05-13 13:12:30 +01:00
|
|
|
enum Orientation {
|
2021-05-13 16:26:08 +01:00
|
|
|
PORTRAIT = 0b00,
|
|
|
|
PORTRAIT_INVERTED = 0b01,
|
|
|
|
LANDSCAPE = 0b10,
|
2021-05-13 13:12:30 +01:00
|
|
|
LANDSCAPE_INVERTED = 0b11
|
|
|
|
};
|
|
|
|
|
|
|
|
enum PowerMode {
|
2021-05-13 16:26:08 +01:00
|
|
|
NORMAL = 0b00,
|
|
|
|
LOW = 0b01,
|
2021-05-13 13:12:30 +01:00
|
|
|
SUSPEND = 0b10
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Range {
|
2021-05-13 16:26:08 +01:00
|
|
|
G_2 = 0b00,
|
|
|
|
G_4 = 0b01,
|
|
|
|
G_8 = 0b10,
|
2021-05-13 13:12:30 +01:00
|
|
|
G_16 = 0b11
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Resolution {
|
2021-05-13 16:26:08 +01:00
|
|
|
BITS_14 = 0b0000,
|
|
|
|
BITS_12 = 0b0100,
|
|
|
|
BITS_10 = 0b1000,
|
2021-05-13 13:12:30 +01:00
|
|
|
BITS_8 = 0b1100
|
|
|
|
};
|
|
|
|
|
|
|
|
enum AxisPolarity {
|
2021-05-13 16:26:08 +01:00
|
|
|
INVERT_X = 0b1000,
|
|
|
|
INVERT_Y = 0b0100,
|
|
|
|
INVERT_Z = 0b0010,
|
2021-05-13 13:12:30 +01:00
|
|
|
XY_SWAP = 0b0001
|
|
|
|
};
|
2021-01-17 16:05:28 +00:00
|
|
|
|
2021-05-13 13:12:30 +01:00
|
|
|
enum Interrupt {
|
2021-05-13 16:26:08 +01:00
|
|
|
NONE = 0,
|
2021-05-13 13:12:30 +01:00
|
|
|
ACTIVE = 0b0000111,
|
2021-05-13 16:26:08 +01:00
|
|
|
NEW_DATA = 0b1000000000000,
|
2021-05-13 13:12:30 +01:00
|
|
|
FREEFALL = 0b0100000000000,
|
2021-05-13 16:26:08 +01:00
|
|
|
ORIENTATION = 0b1000000,
|
2021-05-13 13:12:30 +01:00
|
|
|
SINGLE_TAP = 0b0100000,
|
|
|
|
DOUBLE_TAP = 0b0010000,
|
2021-05-13 16:26:08 +01:00
|
|
|
Z_ACTIVE = 0b0000100,
|
|
|
|
Y_ACTIVE = 0b0000010,
|
2021-05-13 13:12:30 +01:00
|
|
|
X_ACTIVE = 0b0000001
|
|
|
|
};
|
|
|
|
|
|
|
|
enum InterruptLatchPeriod {
|
2021-05-13 16:26:08 +01:00
|
|
|
LATCH_1MS = 0b1001,
|
2021-05-13 13:12:30 +01:00
|
|
|
LATCH_2MS = 0b1011,
|
|
|
|
LATCH_25MS = 0b1100,
|
|
|
|
LATCH_50MS = 0b1101,
|
|
|
|
LATCH_100MS = 0b1110,
|
|
|
|
LATCH_250MS = 0b0001,
|
|
|
|
LATCH_500MS = 0b0010,
|
|
|
|
LATCH_1S = 0b0011,
|
|
|
|
LATCH_2S = 0b0100,
|
|
|
|
LATCH_4S = 0b0101,
|
|
|
|
LATCH_8S = 0b0110
|
|
|
|
};
|
|
|
|
|
2021-05-13 16:26:08 +01:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
2021-05-14 18:12:37 +01:00
|
|
|
I2C *i2c;
|
2021-05-17 14:50:49 +01:00
|
|
|
const uint8_t address = DEFAULT_I2C_ADDRESS;
|
|
|
|
uint interrupt = PIN_UNUSED;
|
2021-05-13 16:26:08 +01:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2021-05-17 14:50:49 +01:00
|
|
|
MSA301() : MSA301(new I2C()) {};
|
|
|
|
|
|
|
|
MSA301(I2C *i2c, uint interrupt = PIN_UNUSED) : i2c(i2c), interrupt(interrupt) {}
|
|
|
|
|
2021-05-13 16:26:08 +01:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2021-05-14 17:14:07 +01:00
|
|
|
bool init();
|
2021-05-13 16:26:08 +01:00
|
|
|
void reset();
|
|
|
|
|
2021-05-13 21:28:46 +01:00
|
|
|
i2c_inst_t* get_i2c() const;
|
|
|
|
int get_sda() const;
|
|
|
|
int get_scl() const;
|
|
|
|
int get_int() const;
|
|
|
|
|
2021-05-13 13:12:30 +01:00
|
|
|
uint8_t part_id();
|
|
|
|
float get_axis(Axis axis, uint8_t sample_count = 1);
|
2021-05-13 16:26:08 +01:00
|
|
|
float get_x_axis(uint8_t sample_count = 1);
|
|
|
|
float get_y_axis(uint8_t sample_count = 1);
|
|
|
|
float get_z_axis(uint8_t sample_count = 1);
|
2021-05-13 13:12:30 +01:00
|
|
|
Orientation get_orientation();
|
2021-05-13 16:26:08 +01:00
|
|
|
|
2021-05-13 13:12:30 +01:00
|
|
|
void set_power_mode(MSA301::PowerMode power_mode);
|
|
|
|
void set_range_and_resolution(Range range, MSA301::Resolution resolution);
|
2021-05-13 21:28:46 +01:00
|
|
|
void set_axis_polarity(uint8_t polarity);
|
2021-05-13 16:26:08 +01:00
|
|
|
|
2021-05-13 13:12:30 +01:00
|
|
|
void disable_all_interrupts();
|
2021-05-13 21:28:46 +01:00
|
|
|
void enable_interrupts(uint16_t interrupts);
|
2021-05-13 13:12:30 +01:00
|
|
|
void set_interrupt_latch(InterruptLatchPeriod latch_period, bool reset_latched);
|
2021-05-13 16:26:08 +01:00
|
|
|
bool read_interrupt(Interrupt interrupt);
|
2021-05-13 13:12:30 +01:00
|
|
|
};
|
2021-01-17 16:05:28 +00:00
|
|
|
}
|
2021-05-13 13:12:30 +01:00
|
|
|
|