52 lines
1.2 KiB
C++
52 lines
1.2 KiB
C++
#include <Figments.h>
|
|
|
|
class MPU5060: public InputSource {
|
|
const int ACCEL_XOUT_HIGH = 0x3B;
|
|
const int ACCEL_XOUT_LOW = 0x3C;
|
|
const int ACCEL_YOUT_HIGH = 0x3D;
|
|
const int ACCEL_YOUT_LOW = 0x3E;
|
|
const int ACCEL_ZOUT_HIGH = 0x3F;
|
|
const int ACCEL_ZOUT_LOW = 0x40;
|
|
|
|
const int I2C_ADDRESS = 0x68;
|
|
|
|
const int PWR_MGMT_1 = 0x6B;
|
|
const int CONFIG_REG = 0x1A;
|
|
const int ACCEL_CONFIG_REG = 0x1C;
|
|
|
|
public:
|
|
MPU5060();
|
|
|
|
void onStart() override;
|
|
void onStop() override;
|
|
InputEvent read() override;
|
|
|
|
template<typename T, uint8_t Size = 8>
|
|
struct Averager {
|
|
std::array<T, Size> buf;
|
|
unsigned int idx = 0;
|
|
unsigned int count = 0;
|
|
|
|
void add(const T &value) {
|
|
buf[idx] = value;
|
|
idx = (idx + 1) % Size;
|
|
if (count < Size) {
|
|
count += 1;
|
|
}
|
|
}
|
|
|
|
T value() const {
|
|
if (count == 0) {
|
|
return T{};
|
|
}
|
|
long long int sum = 0;
|
|
for(unsigned int i = 0; i < count; i++) {
|
|
sum += buf[i];
|
|
}
|
|
return sum / count;
|
|
}
|
|
};
|
|
|
|
Averager<int16_t, 32> m_value;
|
|
};
|