Skip to content

Commit

Permalink
Add define switches for threading
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomona committed Mar 28, 2024
1 parent 582df6a commit 65e4052
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
#define TARGET_LOOPTIME_MICROS (samplingRateInMillis * 1000)
#endif

// Enable threading for higher performance on ESP32 variants
#define SENSOR_THREADING true

// Packet bundling/aggregation
#define PACKET_BUNDLING PACKET_BUNDLING_BUFFERED
// Extra tunable for PACKET_BUNDLING_BUFFERED (10000us = 10ms timeout, 100hz target)
Expand Down
6 changes: 3 additions & 3 deletions src/sensors/SensorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ namespace SlimeVR
sensor->postSetup();
}
}
#if ESP32
#if ESP32 && SENSOR_THREADING
for (auto & sensor : m_Sensors) {
sensor->updateMutex = xSemaphoreCreateMutex();
}
xTaskCreateUniversal(updateSensors, "sensors", 16*1024, this, 10, &sensorTask, ARDUINO_RUNNING_CORE);
#endif
}

#if ESP32
#if ESP32 && SENSOR_THREADING
void SensorManager::updateSensors(void * pxParameter) {
SensorManager *pthis = (SensorManager *)pxParameter;
for (;;) {
Expand All @@ -217,7 +217,7 @@ namespace SlimeVR

void SensorManager::update()
{
#if !ESP32
#if !(ESP32 && SENSOR_THREADING)
// Gather IMU data
bool allIMUGood = true;
for (auto sensor : m_Sensors) {
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/SensorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace SlimeVR
void swapI2C(uint8_t scl, uint8_t sda);

uint32_t m_LastBundleSentAtMicros = micros();
#if ESP32
#if ESP32 && SENSOR_THREADING
TaskHandle_t sensorTask = NULL;
static void updateSensors(void * pvParameters);
#endif
Expand Down
12 changes: 6 additions & 6 deletions src/sensors/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ SensorStatus Sensor::getSensorState() {
}

void Sensor::setAcceleration(Vector3 a) {
#if ESP32
#if ESP32 && SENSOR_THREADING
xSemaphoreTake(updateMutex, portMAX_DELAY);
#endif
acceleration = a;
newAcceleration = true;
#if ESP32
#if ESP32 && SENSOR_THREADING
xSemaphoreGive(updateMutex);
#endif
}

void Sensor::setFusedRotation(Quat r) {
#if ESP32
#if ESP32 && SENSOR_THREADING
xSemaphoreTake(updateMutex, portMAX_DELAY);
#endif
fusedRotation = r * sensorOffset;
Expand All @@ -50,13 +50,13 @@ void Sensor::setFusedRotation(Quat r) {
newFusedRotation = true;
lastFusedRotationSent = fusedRotation;
}
#if ESP32
#if ESP32 && SENSOR_THREADING
xSemaphoreGive(updateMutex);
#endif
}

void Sensor::sendData() {
#if ESP32
#if ESP32 && SENSOR_THREADING
Quat lquat;
xSemaphoreTake(updateMutex, portMAX_DELAY);
if (newFusedRotation) {
Expand All @@ -81,7 +81,7 @@ void Sensor::sendData() {
#endif

#if SEND_ACCELERATION
#if ESP32
#if ESP32 && SENSOR_THREADING
Vector3 laccel;
xSemaphoreTake(updateMutex, portMAX_DELAY);
if (newAcceleration) {
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Sensor
uint8_t sclPin = 0;
uint8_t sdaPin = 0;

#if ESP32
#if ESP32 && SENSOR_THREADING
SemaphoreHandle_t updateMutex = NULL;
#endif
private:
Expand Down

0 comments on commit 65e4052

Please sign in to comment.