Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,426 changes: 2,426 additions & 0 deletions lib/bmi160/BMI160.cpp

Large diffs are not rendered by default.

768 changes: 768 additions & 0 deletions lib/bmi160/BMI160.h

Large diffs are not rendered by default.

157 changes: 157 additions & 0 deletions src/button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include "button.h"

#include <climits>

#if ESP32
#include <driver/rtc_io.h>
#include <esp_sleep.h>
#endif

#include "GlobalVars.h"

#ifdef ON_OFF_BUTTON_PIN

void IRAM_ATTR buttonInterruptHandler() {
if (OnOffButton::getInstance().buttonPressed) {
return;
}

OnOffButton::getInstance().signalPressStart();
}

void OnOffButton::setup() {
#if ESP8266
digitalWrite(D0, LOW);
pinMode(D0, OUTPUT);
pinMode(ON_OFF_BUTTON_PIN, INPUT);
#endif

#if ESP32
pinMode(
ON_OFF_BUTTON_PIN,
BUTTON_ACTIVE_LEVEL == 0 ? INPUT_PULLUP : INPUT_PULLDOWN
);

esp_deep_sleep_enable_gpio_wakeup(
1 << ON_OFF_BUTTON_PIN,
BUTTON_ACTIVE_LEVEL == 0 ? ESP_GPIO_WAKEUP_GPIO_LOW : ESP_GPIO_WAKEUP_GPIO_HIGH
);

#ifdef BUTTON_IMU_ENABLE_PIN
pinMode(BUTTON_IMU_ENABLE_PIN, OUTPUT);
gpio_hold_dis(static_cast<gpio_num_t>(BUTTON_IMU_ENABLE_PIN));
digitalWrite(BUTTON_IMU_ENABLE_PIN, BUTTON_IMU_ENABLE_ACTIVE_LEVEL);
#endif

gpio_deep_sleep_hold_en();
#endif

attachInterrupt(
ON_OFF_BUTTON_PIN,
buttonInterruptHandler,
BUTTON_ACTIVE_LEVEL == 0 ? FALLING : RISING
);
}

void OnOffButton::tick() {
#ifdef BUTTON_AUTO_SLEEP_TIME_SECONDS
uint64_t autoSleepElapsed = millis() - lastActivityMillis;

if (autoSleepElapsed >= BUTTON_AUTO_SLEEP_TIME_SECONDS * 1e3) {
goToSleep();
}
#endif

#if defined(BUTTON_BATTERY_VOLTAGE_THRESHOLD) && BATTERY_MONITOR == BAT_EXTERNAL
if (battery.getVoltage() >= BUTTON_BATTERY_VOLTAGE_THRESHOLD) {
batteryBad = false;
} else if (!batteryBad) {
batteryBad = true;
batteryBadSinceMillis = millis();
}

if (batteryBad) {
uint64_t batteryBadElapsed = millis() - batteryBadSinceMillis;

if (batteryBadElapsed >= batteryBadTimeoutSeconds * 1e3) {
goToSleep();
}
}
#endif

if (!wasReleasedInitially && !getButton()) {
return;
}
wasReleasedInitially = true;

if (!buttonPressed) {
return;
}

uint64_t timeTaken = millis() - buttonPressStartMillis;

if (getButton() && timeTaken < longPressSeconds * 1e3) {
return;
}

if (timeTaken >= longPressSeconds * 1e3) {
goToSleep();
}

buttonPressed = false;
}

void OnOffButton::onBeforeSleep(std::function<void()> callback) {
callbacks.push_back(callback);
}

void OnOffButton::signalTrackerMoved() { lastActivityMillis = millis(); }

OnOffButton& OnOffButton::getInstance() { return instance; }

bool OnOffButton::getButton() {
static constexpr uint8_t circularBufferBitCount
= sizeof(buttonCircularBuffer) * CHAR_BIT;

bool isPressed = digitalRead(ON_OFF_BUTTON_PIN) == BUTTON_ACTIVE_LEVEL;
buttonCircularBuffer = buttonCircularBuffer << 1 | isPressed;

auto popCount = __builtin_popcount(buttonCircularBuffer);
return popCount >= circularBufferBitCount / 2;
}

void OnOffButton::signalPressStart() {
buttonPressed = true;
buttonPressStartMillis = millis();
buttonCircularBuffer = UINT64_MAX;
}

void OnOffButton::emitOnBeforeSleep() {
for (auto& callback : callbacks) {
callback();
}
}

void OnOffButton::goToSleep() {
emitOnBeforeSleep();

#if defined(BUTTON_IMU_ENABLE_PIN) && ESP32
digitalWrite(BUTTON_IMU_ENABLE_PIN, LOW);
gpio_hold_en(static_cast<gpio_num_t>(BUTTON_IMU_ENABLE_PIN));
#endif

ledManager.pattern(100, 100, 3);

while (buttonPressed && getButton())
;

#if ESP8266
ESP.deepSleep(0);
#elif ESP32
esp_deep_sleep_start();
#endif
}

OnOffButton OnOffButton::instance;

#endif
70 changes: 70 additions & 0 deletions src/button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
SlimeVR Code is placed under the MIT license
Copyright (c) 2025 Gorbit99 & SlimeVR Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#pragma once

#include <Arduino.h>

#include <cstdint>
#include <functional>
#include <vector>

#include "GlobalVars.h"

#ifdef ON_OFF_BUTTON_PIN

class OnOffButton {
public:
void setup();
void tick();
void onBeforeSleep(std::function<void()> callback);
void signalTrackerMoved();

static OnOffButton& getInstance();

private:
OnOffButton() = default;
static OnOffButton instance;

static constexpr float longPressSeconds = 1.0f;
static constexpr float batteryBadTimeoutSeconds = 10.0f;

bool getButton();
void signalPressStart();
void emitOnBeforeSleep();
void goToSleep();

bool buttonPressed = false;
uint64_t buttonPressStartMillis = 0;
uint64_t buttonCircularBuffer = 0;
std::vector<std::function<void()>> callbacks;
bool batteryBad = false;
uint64_t batteryBadSinceMillis = 0;
bool wasReleasedInitially = false;

uint64_t lastActivityMillis = 0;

friend void IRAM_ATTR buttonInterruptHandler();
};

#endif
8 changes: 8 additions & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@
#define FIRMWARE_VERSION "UNKNOWN"
#endif

#ifndef BUTTON_ACTIVE_LEVEL
#define BUTTON_ACTIVE_LEVEL 0
#endif

#ifndef BUTTON_IMU_ENABLE_ACTIVE_LEVEL
#define BUTTON_IMU_ENABLE_ACTIVE_LEVEL 1
#endif

#ifndef USE_RUNTIME_CALIBRATION
#define USE_RUNTIME_CALIBRATION true
#endif
Expand Down
15 changes: 15 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "GlobalVars.h"
#include "Wire.h"
#include "batterymonitor.h"
#include "button.h"
#include "credentials.h"
#include "debugging/TimeTaken.h"
#include "globals.h"
Expand Down Expand Up @@ -64,6 +65,11 @@ void setup() {
Serial.println();
Serial.println();

#ifdef ON_OFF_BUTTON_PIN
OnOffButton::getInstance().setup();
OnOffButton::getInstance().onBeforeSleep([]() { sensorManager.deinitAll(); });
#endif

logger.info("SlimeVR v" FIRMWARE_VERSION " starting up...");

statusManager.setStatus(SlimeVR::Status::LOADING, true);
Expand Down Expand Up @@ -136,6 +142,15 @@ void loop() {
battery.Loop();
ledManager.update();
I2CSCAN::update();

#ifdef ON_OFF_BUTTON_PIN
OnOffButton::getInstance().tick();

if (!sensorManager.allAtRest()) {
OnOffButton::getInstance().signalTrackerMoved();
}
#endif

#ifdef TARGET_LOOPTIME_MICROS
long elapsed = (micros() - loopTime);
if (elapsed < TARGET_LOOPTIME_MICROS) {
Expand Down
16 changes: 16 additions & 0 deletions src/sensors/SensorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ class SensorManager {
return SensorTypeID::Unknown;
}

void deinitAll() {
for (auto& sensor : m_Sensors) {
sensor->deinit();
}
}

bool allAtRest() {
for (auto& sensor : m_Sensors) {
if (!sensor->isAtRest()) {
return false;
}
}

return true;
}

private:
SlimeVR::Logging::Logger m_Logger;

Expand Down
2 changes: 2 additions & 0 deletions src/sensors/bno055sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@ void BNO055Sensor::motionLoop() {
}

void BNO055Sensor::startCalibration(int calibrationType) {}

void BNO055Sensor::deinit() { imu.enterSuspendMode(); }
1 change: 1 addition & 0 deletions src/sensors/bno055sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class BNO055Sensor : public Sensor {
void motionSetup() override final;
void motionLoop() override final;
void startCalibration(int calibrationType) override final;
void deinit() final;

private:
Adafruit_BNO055 imu;
Expand Down
8 changes: 8 additions & 0 deletions src/sensors/bno080sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ void BNO080Sensor::motionSetup() {
// enableRawGyro only for reading the Temperature every 1 second (0.5°C steps)
imu.enableRawGyro(1000);

#ifdef BUTTON_AUTO_SLEEP_TIME_SECONDS
imu.enableStabilityClassifier(500);
#endif

lastReset = 0;
lastData = millis();
working = true;
Expand Down Expand Up @@ -382,6 +386,10 @@ void BNO080Sensor::startCalibration(int calibrationType) {
// that is disabled 30 seconds after startup
}

void BNO080Sensor::deinit() { imu.softReset(); }

bool BNO080Sensor::isAtRest() { return imu.getStabilityClassifier() == 1; }

bool BNO080Sensor::isFlagSupported(SensorToggles toggle) const {
return toggle == SensorToggles::MagEnabled;
}
2 changes: 2 additions & 0 deletions src/sensors/bno080sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class BNO080Sensor : public Sensor {
void sendData() override final;
void startCalibration(int calibrationType) override final;
SensorStatus getSensorState() override final;
void deinit() final;
bool isAtRest() final;
bool isFlagSupported(SensorToggles toggle) const final;
void sendTempIfNeeded();

Expand Down
2 changes: 2 additions & 0 deletions src/sensors/icm20948sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,3 +993,5 @@ ICM_20948_Status_e ICM_20948::initializeDMP(void) {
return worstResult;
}
#endif // OVERRIDEDMPSETUP

void ICM20948Sensor::deinit() { imu.swReset(); }
1 change: 1 addition & 0 deletions src/sensors/icm20948sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class ICM20948Sensor : public Sensor {
void checkSensorTimeout();
void readRotation();
void readFIFOToEnd();
void deinit() final;

#define OVERRIDEDMPSETUP true
// TapDetector tapDetector;
Expand Down
2 changes: 2 additions & 0 deletions src/sensors/mpu6050sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,5 @@ void MPU6050Sensor::startCalibration(int calibrationType) {

ledManager.off();
}

void MPU6050Sensor::deinit() { imu.reset(); }
1 change: 1 addition & 0 deletions src/sensors/mpu6050sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class MPU6050Sensor : public Sensor {
void motionSetup() override final;
void motionLoop() override final;
void startCalibration(int calibrationType) override final;
void deinit() final;

private:
MPU6050 imu{};
Expand Down
2 changes: 2 additions & 0 deletions src/sensors/mpu9250sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,5 @@ bool MPU9250Sensor::getNextSample(
swapFifoData(buffer);
return true;
}

void MPU9250Sensor::deinit() { imu.reset(); }
1 change: 1 addition & 0 deletions src/sensors/mpu9250sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class MPU9250Sensor : public Sensor {
void motionLoop() override final;
void startCalibration(int calibrationType) override final;
void getMPUScaled();
void deinit() final;

private:
MPU9250 imu{};
Expand Down
2 changes: 2 additions & 0 deletions src/sensors/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class Sensor {
TPSCounter m_tpsCounter;
TPSCounter m_dataCounter;
SlimeVR::SensorInterface* m_hwInterface = nullptr;
virtual void deinit() {}
virtual bool isAtRest() { return false; }

protected:
SlimeVR::Sensors::RegisterInterface& m_RegisterInterface;
Expand Down
Loading