mirror of
https://github.com/SlimeVR/SlimeVR-Tracker-ESP.git
synced 2026-04-06 02:01:57 +02:00
Compare commits
7 Commits
v0.7.2
...
feat/seria
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f46d20f5cb | ||
|
|
939ad705ce | ||
|
|
7d800efdbf | ||
|
|
12f4b22dac | ||
|
|
6cd29c7cea | ||
|
|
33dc84c00b | ||
|
|
37658155c7 |
8
.github/workflows/actions.yml
vendored
8
.github/workflows/actions.yml
vendored
@@ -14,7 +14,7 @@ jobs:
|
||||
format:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
- uses: jidicula/clang-format-action@v4.14.0
|
||||
with:
|
||||
clang-format-version: "17"
|
||||
@@ -33,8 +33,8 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/cache@v4
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions/cache@v5
|
||||
with:
|
||||
path: |
|
||||
~/.cache/pip
|
||||
@@ -58,7 +58,7 @@ jobs:
|
||||
run: python ./ci/build.py
|
||||
|
||||
- name: Upload binaries
|
||||
uses: actions/upload-artifact@v5
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: binaries
|
||||
path: ./build/*.bin
|
||||
|
||||
@@ -96,7 +96,7 @@
|
||||
// Not recommended for production
|
||||
#define ENABLE_INSPECTION false
|
||||
|
||||
#define PROTOCOL_VERSION 21
|
||||
#define PROTOCOL_VERSION 22
|
||||
|
||||
#ifndef FIRMWARE_VERSION
|
||||
#define FIRMWARE_VERSION "UNKNOWN"
|
||||
|
||||
110
src/logging/LogBuffer.cpp
Normal file
110
src/logging/LogBuffer.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include "LogBuffer.h"
|
||||
|
||||
namespace SlimeVR::Logging {
|
||||
|
||||
bool LogBuffer::addLog(const char* message) {
|
||||
size_t len = strlen(message);
|
||||
|
||||
// Need space for message + null terminator + length prefix (2 bytes)
|
||||
size_t required = len + 3;
|
||||
|
||||
if (required > getAvailableSpace()) {
|
||||
// Buffer full, drop the message or overwrite oldest
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write length as 2-byte prefix (little endian)
|
||||
m_Buffer[m_WritePos] = (len >> 8) & 0xFF;
|
||||
m_WritePos = (m_WritePos + 1) % MAX_BUFFER_SIZE;
|
||||
m_Buffer[m_WritePos] = len & 0xFF;
|
||||
m_WritePos = (m_WritePos + 1) % MAX_BUFFER_SIZE;
|
||||
|
||||
// Write message
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
m_Buffer[m_WritePos] = message[i];
|
||||
m_WritePos = (m_WritePos + 1) % MAX_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
// Write null terminator
|
||||
m_Buffer[m_WritePos] = '\0';
|
||||
m_WritePos = (m_WritePos + 1) % MAX_BUFFER_SIZE;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void LogBuffer::processCycle() {
|
||||
size_t bytesWritten = 0;
|
||||
|
||||
while (m_WritePos != m_ReadPos && bytesWritten < MAX_BYTES_PER_CYCLE) {
|
||||
size_t msgLen = getNextMessageLength();
|
||||
|
||||
if (msgLen == 0 || bytesWritten + msgLen > MAX_BYTES_PER_CYCLE) {
|
||||
// Message too large for this cycle or corrupted
|
||||
break;
|
||||
}
|
||||
|
||||
// Skip length prefix
|
||||
m_ReadPos = (m_ReadPos + 2) % MAX_BUFFER_SIZE;
|
||||
|
||||
// Read and print message
|
||||
for (size_t i = 0; i < msgLen; i++) {
|
||||
Serial.write(m_Buffer[m_ReadPos]);
|
||||
m_ReadPos = (m_ReadPos + 1) % MAX_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
// Skip null terminator
|
||||
m_ReadPos = (m_ReadPos + 1) % MAX_BUFFER_SIZE;
|
||||
|
||||
bytesWritten += msgLen;
|
||||
}
|
||||
}
|
||||
|
||||
size_t LogBuffer::getPendingBytes() const {
|
||||
if (m_WritePos >= m_ReadPos) {
|
||||
return m_WritePos - m_ReadPos;
|
||||
} else {
|
||||
return MAX_BUFFER_SIZE - m_ReadPos + m_WritePos;
|
||||
}
|
||||
}
|
||||
|
||||
void LogBuffer::flushAll() {
|
||||
while (m_WritePos != m_ReadPos) {
|
||||
size_t msgLen = getNextMessageLength();
|
||||
|
||||
if (msgLen == 0) {
|
||||
break; // Corrupted buffer
|
||||
}
|
||||
|
||||
// Skip length prefix
|
||||
m_ReadPos = (m_ReadPos + 2) % MAX_BUFFER_SIZE;
|
||||
|
||||
// Read and print message
|
||||
for (size_t i = 0; i < msgLen; i++) {
|
||||
Serial.write(m_Buffer[m_ReadPos]);
|
||||
m_ReadPos = (m_ReadPos + 1) % MAX_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
// Skip null terminator
|
||||
m_ReadPos = (m_ReadPos + 1) % MAX_BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
size_t LogBuffer::getAvailableSpace() const {
|
||||
size_t used = getPendingBytes();
|
||||
return MAX_BUFFER_SIZE - used - 1; // -1 to distinguish full from empty
|
||||
}
|
||||
|
||||
size_t LogBuffer::getNextMessageLength() const {
|
||||
if (m_WritePos == m_ReadPos) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t pos = m_ReadPos;
|
||||
uint8_t highByte = m_Buffer[pos];
|
||||
pos = (pos + 1) % MAX_BUFFER_SIZE;
|
||||
uint8_t lowByte = m_Buffer[pos];
|
||||
|
||||
return (highByte << 8) | lowByte;
|
||||
}
|
||||
|
||||
} // namespace SlimeVR::Logging
|
||||
46
src/logging/LogBuffer.h
Normal file
46
src/logging/LogBuffer.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef LOGGING_LOGBUFFER_H
|
||||
#define LOGGING_LOGBUFFER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace SlimeVR::Logging {
|
||||
|
||||
class LogBuffer {
|
||||
public:
|
||||
static constexpr size_t MAX_BUFFER_SIZE = 4096; // Total buffer size
|
||||
static constexpr size_t MAX_BYTES_PER_CYCLE = 512; // Max bytes to output per cycle
|
||||
|
||||
static LogBuffer& getInstance() {
|
||||
static LogBuffer instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Add a log message to the buffer
|
||||
bool addLog(const char* message);
|
||||
|
||||
// Process buffered logs up to MAX_BYTES_PER_CYCLE
|
||||
void processCycle();
|
||||
|
||||
// Check if there are pending logs
|
||||
bool hasPendingLogs() const { return m_WritePos != m_ReadPos; }
|
||||
|
||||
// Get number of bytes pending
|
||||
size_t getPendingBytes() const;
|
||||
|
||||
// Force flush all pending logs (for critical errors or shutdown)
|
||||
void flushAll();
|
||||
|
||||
private:
|
||||
LogBuffer() : m_Buffer{0}, m_WritePos(0), m_ReadPos(0) {}
|
||||
|
||||
char m_Buffer[MAX_BUFFER_SIZE];
|
||||
volatile size_t m_WritePos;
|
||||
volatile size_t m_ReadPos;
|
||||
|
||||
size_t getAvailableSpace() const;
|
||||
size_t getNextMessageLength() const;
|
||||
};
|
||||
|
||||
} // namespace SlimeVR::Logging
|
||||
|
||||
#endif
|
||||
@@ -63,6 +63,23 @@ void Logger::log(Level level, const char* format, va_list args) const {
|
||||
strcat(buf, m_Tag);
|
||||
}
|
||||
|
||||
Serial.printf("[%-5s] [%s] %s\n", levelToString(level), buf, buffer);
|
||||
// Format complete log message
|
||||
char fullMessage[384]; // Enough for header + message
|
||||
snprintf(fullMessage, sizeof(fullMessage), "[%-5s] [%s] %s\n", levelToString(level), buf, buffer);
|
||||
|
||||
// Add to buffer or write directly for critical messages
|
||||
if (level >= ERROR) {
|
||||
// Critical messages: write immediately and flush buffer
|
||||
LogBuffer::getInstance().flushAll();
|
||||
Serial.print(fullMessage);
|
||||
} else {
|
||||
// Normal messages: add to buffer
|
||||
if (!LogBuffer::getInstance().addLog(fullMessage)) {
|
||||
// Buffer full, try to make space
|
||||
LogBuffer::getInstance().processCycle();
|
||||
// Try again
|
||||
LogBuffer::getInstance().addLog(fullMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace SlimeVR::Logging
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "Level.h"
|
||||
#include "LogBuffer.h"
|
||||
#include "debug.h"
|
||||
|
||||
namespace SlimeVR::Logging {
|
||||
@@ -79,12 +80,20 @@ private:
|
||||
strcat(buf, m_Tag);
|
||||
}
|
||||
|
||||
Serial.printf("[%-5s] [%s] %s", levelToString(level), buf, str);
|
||||
// Build array log message
|
||||
char header[256];
|
||||
snprintf(header, sizeof(header), "[%-5s] [%s] %s", levelToString(level), buf, str);
|
||||
|
||||
// For arrays, we'll output directly to avoid excessive buffering
|
||||
// But for critical levels, flush first
|
||||
if (level >= ERROR) {
|
||||
LogBuffer::getInstance().flushAll();
|
||||
}
|
||||
|
||||
Serial.print(header);
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
Serial.print(array[i]);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
@@ -159,6 +159,9 @@ void loop() {
|
||||
OTA::otaUpdate();
|
||||
networkManager.update();
|
||||
|
||||
// Process buffered logs
|
||||
SlimeVR::Logging::LogBuffer::getInstance().processCycle();
|
||||
|
||||
#if DEBUG_MEASURE_SENSOR_TIME_TAKEN
|
||||
sensorMeasurer.before();
|
||||
#endif
|
||||
|
||||
@@ -664,7 +664,6 @@ void Connection::update() {
|
||||
return;
|
||||
}
|
||||
|
||||
m_LastPacketTimestamp = millis();
|
||||
int len = m_UDP.read(m_Packet, sizeof(m_Packet));
|
||||
|
||||
#ifdef DEBUG_NETWORK
|
||||
@@ -679,6 +678,12 @@ void Connection::update() {
|
||||
(void)packetSize;
|
||||
#endif
|
||||
|
||||
if (static_cast<ReceivePacketType>(m_Packet[3]) == ReceivePacketType::Handshake) {
|
||||
m_Logger.warn("Handshake received again, ignoring");
|
||||
return;
|
||||
}
|
||||
|
||||
m_LastPacketTimestamp = millis();
|
||||
switch (static_cast<ReceivePacketType>(m_Packet[3])) {
|
||||
case ReceivePacketType::HeartBeat:
|
||||
sendHeartbeat();
|
||||
@@ -688,8 +693,7 @@ void Connection::update() {
|
||||
break;
|
||||
|
||||
case ReceivePacketType::Handshake:
|
||||
// Assume handshake successful
|
||||
m_Logger.warn("Handshake received again, ignoring");
|
||||
// handled above
|
||||
break;
|
||||
|
||||
case ReceivePacketType::Command:
|
||||
|
||||
@@ -33,6 +33,7 @@ SensorStatus Sensor::getSensorState() {
|
||||
|
||||
void Sensor::setAcceleration(Vector3 a) {
|
||||
acceleration = a;
|
||||
sensorOffset.sandwich(acceleration);
|
||||
newAcceleration = true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user