* Stop accelerometer thread when double-tap/wake-on-motion disabled at runtime double_tap_as_button_press and wake_on_tap_or_motion are applied live: the OFF->ON edge calls accelerometerThread->start(), but there was no ON->OFF branch, so turning either flag off left the sensor thread running (polling I2C, drawing power) until reboot. Worse, because enabled stayed true, a later OFF->ON edge was a no-op (the enabled==false guard blocked re-start), leaving the feature un-restartable without a reboot. Add the symmetric ON->OFF branch in both handlers. When a flag goes true->off and the other consumer of the shared thread is also off, call accelerometerThread->disable() (stops runOnce polling and clears enabled so a later re-enable can start() again). Each branch checks the other flag first so disabling one feature never stops the sensor while the other still needs it. * Keep accelerometer thread running when its sensor drives the compass * Guard accelerometer thread config toggles against a null thread pointer * AdminModule: factor shared accelerometer start/stop into a helper The device and display config handlers had mirror-image blocks reconciling the shared accelerometer thread. Extract reconcileAccelerometerThread(wasOn, nowOn, otherFeatureOn) so the null guard, edge logic, compass (providesHeading) guard, and rationale live in one place; each call site is now a single call. Behavior is unchanged. Also drops the redundant per-field assignment that the whole-struct `config.device = ...` / `config.display = ...` overwrites anyway. --------- Co-authored-by: Thomas Göttgens <tgoettgens@gmail.com>
195 lines
5.2 KiB
C++
Executable File
195 lines
5.2 KiB
C++
Executable File
#pragma once
|
|
#ifndef _ACCELEROMETER_H_
|
|
#define _ACCELEROMETER_H_
|
|
|
|
#include "configuration.h"
|
|
|
|
#if !defined(ARCH_STM32WL) && !MESHTASTIC_EXCLUDE_I2C && !MESHTASTIC_EXCLUDE_ACCELEROMETER
|
|
|
|
#include "../concurrency/OSThread.h"
|
|
#ifdef HAS_BMA423
|
|
#include "BMA423Sensor.h"
|
|
#endif
|
|
#ifdef HAS_BMI270
|
|
#include "BMI270Sensor.h"
|
|
#endif
|
|
#include "BMM150Sensor.h"
|
|
#include "BMX160Sensor.h"
|
|
#include "ICM20948Sensor.h"
|
|
#include "ICM42607PSensor.h"
|
|
#include "LIS3DHSensor.h"
|
|
#include "LSM6DS3Sensor.h"
|
|
#include "MPU6050Sensor.h"
|
|
#include "MotionSensor.h"
|
|
#ifdef HAS_QMA6100P
|
|
#include "QMA6100PSensor.h"
|
|
#endif
|
|
#ifdef HAS_STK8XXX
|
|
#include "STK8XXXSensor.h"
|
|
#endif
|
|
|
|
extern ScanI2C::DeviceAddress accelerometer_found;
|
|
|
|
class AccelerometerThread : public concurrency::OSThread
|
|
{
|
|
private:
|
|
MotionSensor *sensor = nullptr;
|
|
bool isInitialised = false;
|
|
|
|
public:
|
|
explicit AccelerometerThread(ScanI2C::FoundDevice foundDevice) : OSThread("Accelerometer")
|
|
{
|
|
device = foundDevice;
|
|
init();
|
|
}
|
|
|
|
explicit AccelerometerThread(ScanI2C::DeviceType type) : AccelerometerThread(ScanI2C::FoundDevice{type, accelerometer_found})
|
|
{
|
|
}
|
|
|
|
void start()
|
|
{
|
|
init();
|
|
setIntervalFromNow(0);
|
|
};
|
|
|
|
void calibrate(uint16_t forSeconds)
|
|
{
|
|
if (sensor) {
|
|
sensor->calibrate(forSeconds);
|
|
}
|
|
}
|
|
|
|
// True if the active sensor drives the compass heading in runOnce(). Callers must not
|
|
// disable() the thread in this case, or the compass would freeze until the next reboot.
|
|
bool providesHeading() const { return isInitialised && sensor && sensor->providesHeading(); }
|
|
|
|
protected:
|
|
int32_t runOnce() override
|
|
{
|
|
// Assume we should not keep the board awake
|
|
canSleep = true;
|
|
|
|
if (isInitialised)
|
|
return sensor->runOnce();
|
|
|
|
return MOTION_SENSOR_CHECK_INTERVAL_MS;
|
|
}
|
|
|
|
private:
|
|
ScanI2C::FoundDevice device;
|
|
|
|
void init()
|
|
{
|
|
if (isInitialised)
|
|
return;
|
|
|
|
if (device.address.port == ScanI2C::I2CPort::NO_I2C || device.address.address == 0 || device.type == ScanI2C::NONE) {
|
|
LOG_DEBUG("AccelerometerThread Disable due to no sensors found");
|
|
disable();
|
|
return;
|
|
}
|
|
|
|
switch (device.type) {
|
|
#ifdef HAS_BMA423
|
|
case ScanI2C::DeviceType::BMA423:
|
|
sensor = new BMA423Sensor(device);
|
|
break;
|
|
#endif
|
|
#if __has_include(<Adafruit_MPU6050.h>)
|
|
case ScanI2C::DeviceType::MPU6050:
|
|
sensor = new MPU6050Sensor(device);
|
|
break;
|
|
#endif
|
|
case ScanI2C::DeviceType::BMX160:
|
|
sensor = new BMX160Sensor(device);
|
|
break;
|
|
#if __has_include(<Adafruit_LIS3DH.h>)
|
|
case ScanI2C::DeviceType::LIS3DH:
|
|
sensor = new LIS3DHSensor(device);
|
|
break;
|
|
#endif
|
|
#if __has_include(<Adafruit_LSM6DS3TRC.h>)
|
|
case ScanI2C::DeviceType::LSM6DS3:
|
|
sensor = new LSM6DS3Sensor(device);
|
|
break;
|
|
#endif
|
|
#ifdef HAS_STK8XXX
|
|
case ScanI2C::DeviceType::STK8BAXX:
|
|
sensor = new STK8XXXSensor(device);
|
|
break;
|
|
#endif
|
|
#if __has_include(<ICM_20948.h>)
|
|
case ScanI2C::DeviceType::ICM20948:
|
|
sensor = new ICM20948Sensor(device);
|
|
break;
|
|
#endif
|
|
#if __has_include(<ICM42670P.h>)
|
|
case ScanI2C::DeviceType::ICM42607P:
|
|
sensor = new ICM42607PSensor(device);
|
|
break;
|
|
#endif
|
|
#if __has_include(<DFRobot_BMM150.h>)
|
|
case ScanI2C::DeviceType::BMM150:
|
|
sensor = new BMM150Sensor(device);
|
|
break;
|
|
#endif
|
|
#ifdef HAS_BMI270
|
|
case ScanI2C::DeviceType::BMI270:
|
|
sensor = new BMI270Sensor(device);
|
|
break;
|
|
#endif
|
|
#ifdef HAS_QMA6100P
|
|
case ScanI2C::DeviceType::QMA6100P:
|
|
sensor = new QMA6100PSensor(device);
|
|
break;
|
|
#endif
|
|
default:
|
|
disable();
|
|
return;
|
|
}
|
|
|
|
isInitialised = sensor->init();
|
|
if (!isInitialised) {
|
|
clean();
|
|
}
|
|
LOG_DEBUG("AccelerometerThread::init %s", isInitialised ? "ok" : "failed");
|
|
}
|
|
|
|
// Copy constructor (not implemented / included to avoid cppcheck warnings)
|
|
AccelerometerThread(const AccelerometerThread &other) : OSThread::OSThread("Accelerometer") { this->copy(other); }
|
|
|
|
// Destructor (included to avoid cppcheck warnings)
|
|
virtual ~AccelerometerThread() { clean(); }
|
|
|
|
// Copy assignment (not implemented / included to avoid cppcheck warnings)
|
|
AccelerometerThread &operator=(const AccelerometerThread &other)
|
|
{
|
|
this->copy(other);
|
|
return *this;
|
|
}
|
|
|
|
// Take a very shallow copy (does not copy OSThread state nor the sensor object)
|
|
// If for some reason this is ever used, make sure to call init() after any copy
|
|
void copy(const AccelerometerThread &other)
|
|
{
|
|
if (this != &other) {
|
|
clean();
|
|
this->device = ScanI2C::FoundDevice(other.device.type,
|
|
ScanI2C::DeviceAddress(other.device.address.port, other.device.address.address));
|
|
}
|
|
}
|
|
|
|
// Cleanup resources
|
|
void clean()
|
|
{
|
|
isInitialised = false;
|
|
delete sensor;
|
|
sensor = nullptr;
|
|
}
|
|
};
|
|
|
|
#endif
|
|
|
|
#endif
|