Adds callback based event bus

This commit is contained in:
Rune Harlyk
2026-02-09 22:45:25 +01:00
parent eba00f98cd
commit 5863598fbc
37 changed files with 671 additions and 816 deletions
+8 -11
View File
@@ -3,12 +3,12 @@
#include <esp_http_server.h>
#include <features.h>
#include <template/stateful_service.h>
#include <template/stateful_proto_endpoint.h>
#include <template/stateful_persistence.h>
#include <eventbus.hpp>
#include <settings/camera_settings.h>
class WebServer;
namespace Camera {
#define USE_DVP_CAMERA (USE_CAMERA && !CONFIG_IDF_TARGET_ESP32P4)
@@ -25,11 +25,7 @@ void safe_sensor_return();
#define PART_BOUNDARY "frame"
class CameraService
#if USE_DVP_CAMERA
: public StatefulService<CameraSettings>
#endif
{
class CameraService {
public:
CameraService();
@@ -38,11 +34,12 @@ class CameraService
esp_err_t cameraStill(httpd_req_t *request);
esp_err_t cameraStream(httpd_req_t *request);
#if USE_DVP_CAMERA
StatefulProtoEndpoint<CameraSettings, api_CameraSettings> protoEndpoint;
void registerRoutes(WebServer &server);
#if USE_DVP_CAMERA
private:
FSPersistencePB<CameraSettings> _persistence;
CameraSettings _settings {};
SubscriptionHandle _settingsHandle;
void updateCamera();
#endif
};
+5 -19
View File
@@ -1,8 +1,6 @@
#pragma once
#include <template/stateful_persistence.h>
#include <template/stateful_service.h>
#include <template/stateful_proto_endpoint.h>
#include <eventbus.hpp>
#include <utils/math_utils.h>
#include <utils/timing.h>
#include <filesystem.h>
@@ -21,12 +19,9 @@
#include <peripherals/barometer.h>
#include <peripherals/gesture.h>
/*
* Ultrasonic Sensor Settings
*/
#define MAX_DISTANCE 200
class Peripherals : public StatefulService<PeripheralsConfiguration> {
class Peripherals {
public:
Peripherals();
@@ -42,21 +37,14 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
void getIMUProto(socket_message_IMUData &data);
void getSettingsProto(socket_message_PeripheralSettingsData &data);
/* IMU FUNCTIONS */
bool readImu();
bool readMag();
bool readBMP();
bool readGesture();
void readSonar();
float angleX();
float angleY();
float angleZ();
gesture_t takeGesture();
@@ -66,14 +54,12 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
bool calibrateIMU();
StatefulProtoEndpoint<PeripheralsConfiguration, api_PeripheralSettings> protoEndpoint;
private:
FSPersistencePB<PeripheralsConfiguration> _persistence;
PeripheralsConfiguration _settings {};
SubscriptionHandle _settingsHandle;
SemaphoreHandle_t _accessMutex;
inline void beginTransaction() { xSemaphoreTakeRecursive(_accessMutex, portMAX_DELAY); }
inline void endTransaction() { xSemaphoreGiveRecursive(_accessMutex); }
#if FT_ENABLED(USE_MPU6050 || USE_BNO055)
@@ -97,4 +83,4 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
std::list<uint8_t> addressList;
bool i2c_active = false;
};
};
+17 -23
View File
@@ -2,11 +2,10 @@
#define ServoController_h
#include <peripherals/drivers/pca9685.h>
#include <template/stateful_persistence.h>
#include <template/stateful_proto_endpoint.h>
#include <template/stateful_service.h>
#include <eventbus.hpp>
#include <utils/math_utils.h>
#include <platform_shared/api.pb.h>
#include <platform_shared/message.pb.h>
#ifndef FACTORY_SERVO_PWM_FREQUENCY
#define FACTORY_SERVO_PWM_FREQUENCY 50
@@ -34,25 +33,18 @@ inline ServoSettings ServoSettings_defaults() {
return settings;
}
inline void ServoSettings_read(const ServoSettings &settings, ServoSettings &proto) { proto = settings; }
inline StateUpdateResult ServoSettings_update(const ServoSettings &proto, ServoSettings &settings) {
settings = proto;
return StateUpdateResult::CHANGED;
}
class ServoController : public StatefulService<ServoSettings> {
class ServoController {
public:
ServoController()
: protoEndpoint(ServoSettings_read, ServoSettings_update, this,
API_REQUEST_EXTRACTOR(servo_settings, ServoSettings),
API_RESPONSE_ASSIGNER(servo_settings, ServoSettings)),
_persistence(ServoSettings_read, ServoSettings_update, this, SERVO_SETTINGS_FILE, api_ServoSettings_fields,
api_ServoSettings_size, ServoSettings_defaults()) {}
void begin() {
_persistence.readFromFS();
_settings = EventBus::instance().peek<ServoSettings>();
initializePCA();
_settingsHandle =
EventBus::instance().subscribe<ServoSettings>([this](const ServoSettings& s) { _settings = s; });
servoPwmHandle_ = EventBus::instance().subscribe<socket_message_ServoPWMData>(
[this](const socket_message_ServoPWMData& data) { setServoPWM(data.servo_id, data.servo_pwm); });
servoStateHandle_ = EventBus::instance().subscribe<socket_message_ServoStateData>(
[this](const socket_message_ServoStateData& data) { data.active ? activate() : deactivate(); });
}
void pcaWrite(int index, int value) {
@@ -103,7 +95,7 @@ class ServoController : public StatefulService<ServoSettings> {
uint16_t pwms[12];
for (int i = 0; i < 12; i++) {
angles[i] = lerp(angles[i], target_angles[i], 0.1);
auto &servo = state().servos[i];
auto& servo = _settings.servos[i];
float angle = servo.direction * angles[i] + servo.center_angle;
uint16_t pwm = angle * servo.conversion + servo.center_pwm;
pwms[i] = pwm = std::clamp<uint16_t>(pwm, 125, 600);
@@ -115,8 +107,6 @@ class ServoController : public StatefulService<ServoSettings> {
if (control_state == SERVO_CONTROL_STATE::ANGLE) calculatePWM();
}
StatefulProtoEndpoint<ServoSettings, ServoSettings> protoEndpoint;
private:
void initializePCA() {
_pca.begin();
@@ -124,8 +114,8 @@ class ServoController : public StatefulService<ServoSettings> {
_pca.setPWMFreq(FACTORY_SERVO_PWM_FREQUENCY);
_pca.sleep();
}
FSPersistencePB<ServoSettings> _persistence;
ServoSettings _settings {};
PCA9685Driver _pca;
SERVO_CONTROL_STATE control_state = SERVO_CONTROL_STATE::DEACTIVATED;
@@ -133,6 +123,10 @@ class ServoController : public StatefulService<ServoSettings> {
bool is_active {false};
float angles[12] = {0, 90, -145, 0, 90, -145, 0, 90, -145, 0, 90, -145};
float target_angles[12] = {0, 90, -145, 0, 90, -145, 0, 90, -145, 0, 90, -145};
SubscriptionHandle _settingsHandle;
SubscriptionHandle servoPwmHandle_;
SubscriptionHandle servoStateHandle_;
};
#endif