cleanup main.cpp, split platform code into a Platform object

This commit is contained in:
2021-03-31 11:50:00 -07:00
parent a6534bcb20
commit 10bbcd6786
21 changed files with 768 additions and 408 deletions

View File

@@ -1,11 +1,13 @@
#include "./Buttons.h"
#include "../Static.h"
void
Buttons::onStart()
{
for(int i = 0; i < 3; i++) {
//Log.info("Bound pin %d to button %d", 2 + i, i);
m_buttons[i].attach(2 + i, INPUT_PULLDOWN);
//m_buttons[i].attach(2 + i, INPUT_PULLDOWN);
m_buttons[i].attach(2 + i, INPUT_PULLUP);
m_buttons[i].interval(15);
}
}
@@ -35,3 +37,5 @@ Buttons::read()
}
return InputEvent{};
}
STATIC_ALLOC(Buttons);

View File

@@ -0,0 +1,65 @@
#include "MPU6050.h"
#include <Wire.h>
#include "../Config.h"
#include "../Static.h"
MPU5060::MPU5060() : InputSource("MPU5060") {}
void
MPU5060::onStart()
{
Wire.begin();
// Turn on the sensor
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(PWR_MGMT_1);
Wire.write(0);
Wire.endTransmission(true);
// Configure the filter
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(CONFIG_REG);
Wire.write(3);
Wire.endTransmission(true);
// Configure the accel range
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(ACCEL_CONFIG_REG);
// 4G
Wire.write(2 << 3);
Wire.endTransmission(true);
}
void
MPU5060::onStop()
{
Wire.beginTransmission(I2C_ADDRESS);
// Turn off the sensor
Wire.write(PWR_MGMT_1);
Wire.write(1);
Wire.endTransmission(true);
//Wire.end();
}
InputEvent
MPU5060::read()
{
EVERY_N_MILLISECONDS(5) {
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(ACCEL_XOUT_HIGH);
Wire.endTransmission(false);
Wire.requestFrom(I2C_ADDRESS, 6);
const int16_t accelX = Wire.read() << 8 | Wire.read();
const int16_t accelY = Wire.read() << 8 | Wire.read();
const int16_t accelZ = Wire.read() << 8 | Wire.read();
const uint16_t accelSum = abs(accelX) + abs(accelY) + abs(accelZ);
const uint16_t delta = abs(m_value.value() - accelSum);
m_value.add(accelSum);
if (delta > 32) {
return InputEvent{InputEvent::Acceleration, delta};
}
}
return InputEvent{};
}
STATIC_ALLOC(MPU5060);

View File

@@ -1,4 +1,4 @@
#include <Wire.h>
#include <Figments.h>
class MPU5060: public InputSource {
const int ACCEL_XOUT_HIGH = 0x3B;
@@ -15,57 +15,11 @@ class MPU5060: public InputSource {
const int ACCEL_CONFIG_REG = 0x1C;
public:
MPU5060() : InputSource("MPU5060", HardwareConfig::HAS_MPU_6050 ? Task::Running : Task::Stopped) {}
void onStart() override {
Wire.begin();
MPU5060();
// Turn on the sensor
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(PWR_MGMT_1);
Wire.write(0);
Wire.endTransmission(true);
// Configure the filter
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(CONFIG_REG);
Wire.write(3);
Wire.endTransmission(true);
// Configure the accel range
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(ACCEL_CONFIG_REG);
// 4G
Wire.write(2 << 3);
Wire.endTransmission(true);
}
void onStop() override {
Wire.beginTransmission(I2C_ADDRESS);
// Turn off the sensor
Wire.write(PWR_MGMT_1);
Wire.write(1);
Wire.endTransmission(true);
//Wire.end();
}
InputEvent read() override {
EVERY_N_MILLISECONDS(5) {
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(ACCEL_XOUT_HIGH);
Wire.endTransmission(false);
Wire.requestFrom(I2C_ADDRESS, 6);
const int16_t accelX = Wire.read() << 8 | Wire.read();
const int16_t accelY = Wire.read() << 8 | Wire.read();
const int16_t accelZ = Wire.read() << 8 | Wire.read();
const uint16_t accelSum = abs(accelX) + abs(accelY) + abs(accelZ);
const uint16_t delta = abs(m_value.value() - accelSum);
m_value.add(accelSum);
if (delta > 32) {
return InputEvent{InputEvent::Acceleration, delta};
}
}
return InputEvent{};
}
void onStart() override;
void onStop() override;
InputEvent read() override;
template<typename T, uint8_t Size = 8>
struct Averager {