2023-03-23 16:32:04 +00:00
|
|
|
#include "PowerFSM.h"
|
|
|
|
#include "concurrency/OSThread.h"
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "power.h"
|
|
|
|
|
|
|
|
#include <Adafruit_LIS3DH.h>
|
|
|
|
#include <Adafruit_MPU6050.h>
|
|
|
|
|
2023-03-29 18:04:02 +00:00
|
|
|
#define ACCELEROMETER_CHECK_INTERVAL_MS 100
|
|
|
|
#define ACCELEROMETER_CLICK_THRESHOLD 40
|
|
|
|
|
2023-03-23 16:32:04 +00:00
|
|
|
namespace concurrency
|
|
|
|
{
|
|
|
|
class AccelerometerThread : public concurrency::OSThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AccelerometerThread(ScanI2C::DeviceType type = ScanI2C::DeviceType::NONE) : OSThread("AccelerometerThread")
|
|
|
|
{
|
2023-03-29 18:04:02 +00:00
|
|
|
if (accelerometer_found.port == ScanI2C::I2CPort::NO_I2C) {
|
|
|
|
LOG_DEBUG("AccelerometerThread disabling due to no sensors found\n");
|
2023-03-23 16:32:04 +00:00
|
|
|
disable();
|
|
|
|
return;
|
|
|
|
}
|
2023-03-29 18:04:02 +00:00
|
|
|
|
|
|
|
if (!config.display.wake_on_tap_or_motion && !config.device.double_tap_as_button_press) {
|
|
|
|
LOG_DEBUG("AccelerometerThread disabling due to no interested configurations\n");
|
|
|
|
disable();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-23 16:32:04 +00:00
|
|
|
accleremoter_type = type;
|
|
|
|
LOG_DEBUG("AccelerometerThread initializing\n");
|
|
|
|
|
|
|
|
if (accleremoter_type == ScanI2C::DeviceType::MPU6050 && mpu.begin(accelerometer_found.address)) {
|
|
|
|
LOG_DEBUG("MPU6050 initializing\n");
|
|
|
|
// setup motion detection
|
|
|
|
mpu.setHighPassFilter(MPU6050_HIGHPASS_0_63_HZ);
|
|
|
|
mpu.setMotionDetectionThreshold(1);
|
|
|
|
mpu.setMotionDetectionDuration(20);
|
|
|
|
mpu.setInterruptPinLatch(true); // Keep it latched. Will turn off when reinitialized.
|
|
|
|
mpu.setInterruptPinPolarity(true);
|
|
|
|
} else if (accleremoter_type == ScanI2C::DeviceType::LIS3DH && lis.begin(accelerometer_found.address)) {
|
|
|
|
LOG_DEBUG("LIS3DH initializing\n");
|
|
|
|
lis.setRange(LIS3DH_RANGE_2_G);
|
|
|
|
// Adjust threshhold, higher numbers are less sensitive
|
2023-03-29 18:04:02 +00:00
|
|
|
lis.setClick(config.device.double_tap_as_button_press ? 2 : 1, ACCELEROMETER_CLICK_THRESHOLD);
|
2023-03-23 16:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int32_t runOnce() override
|
|
|
|
{
|
|
|
|
canSleep = true; // Assume we should not keep the board awake
|
|
|
|
|
|
|
|
if (accleremoter_type == ScanI2C::DeviceType::MPU6050 && mpu.getMotionInterruptStatus()) {
|
|
|
|
wakeScreen();
|
|
|
|
} else if (accleremoter_type == ScanI2C::DeviceType::LIS3DH && lis.getClick() > 0) {
|
2023-03-29 18:04:02 +00:00
|
|
|
uint8_t click = lis.getClick();
|
|
|
|
if (!config.device.double_tap_as_button_press) {
|
|
|
|
wakeScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.device.double_tap_as_button_press && (click & 0x20)) {
|
|
|
|
buttonPress();
|
|
|
|
return 500;
|
|
|
|
}
|
2023-03-23 16:32:04 +00:00
|
|
|
}
|
2023-03-29 18:04:02 +00:00
|
|
|
return ACCELEROMETER_CHECK_INTERVAL_MS;
|
2023-03-23 16:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void wakeScreen()
|
|
|
|
{
|
|
|
|
if (powerFSM.getState() == &stateDARK) {
|
2023-03-29 18:04:02 +00:00
|
|
|
LOG_INFO("Tap or motion detected. Turning on screen\n");
|
2023-03-23 16:32:04 +00:00
|
|
|
powerFSM.trigger(EVENT_INPUT);
|
|
|
|
}
|
|
|
|
}
|
2023-03-29 18:04:02 +00:00
|
|
|
|
|
|
|
void buttonPress()
|
|
|
|
{
|
|
|
|
LOG_DEBUG("Double-tap detected. Firing button press\n");
|
|
|
|
powerFSM.trigger(EVENT_PRESS);
|
|
|
|
}
|
|
|
|
|
2023-03-23 16:32:04 +00:00
|
|
|
ScanI2C::DeviceType accleremoter_type;
|
|
|
|
Adafruit_MPU6050 mpu;
|
|
|
|
Adafruit_LIS3DH lis;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace concurrency
|