🪄 Formats ESP32SvelteKit
This commit is contained in:
@@ -25,9 +25,9 @@
|
||||
#define FACTORY_SERVO_CENTER_ANGLE 90
|
||||
#endif
|
||||
|
||||
#define SERVO_STATE_SPEED_MS 20
|
||||
#define SERVO_MIN 150 // This is the 'minimum' pulse length count (out of 4096)
|
||||
#define SERVO_MAX 650 // This is the 'maximum' pulse length count (out of 4096)
|
||||
#define SERVO_STATE_SPEED_MS 20
|
||||
#define SERVO_MIN 150 // This is the 'minimum' pulse length count (out of 4096)
|
||||
#define SERVO_MAX 650 // This is the 'maximum' pulse length count (out of 4096)
|
||||
|
||||
enum SERVO_STATE { SERVO_STATE_ACTIVE, SERVO_STATE_SWEEPING_FORWARD, SERVO_STATE_SWEEPING_BACKWARD };
|
||||
|
||||
@@ -41,18 +41,17 @@ struct servo_t {
|
||||
};
|
||||
|
||||
class ServoConfiguration {
|
||||
public:
|
||||
int32_t servo_oscillator_frequency{FACTORY_SERVO_OSCILLATOR_FREQUENCY};
|
||||
int32_t servo_pwm_frequency{FACTORY_SERVO_PWM_FREQUENCY};
|
||||
public:
|
||||
int32_t servo_oscillator_frequency {FACTORY_SERVO_OSCILLATOR_FREQUENCY};
|
||||
int32_t servo_pwm_frequency {FACTORY_SERVO_PWM_FREQUENCY};
|
||||
std::vector<servo_t> servos_config;
|
||||
bool is_active{true};
|
||||
bool is_active {true};
|
||||
const int8_t servo_invert[12] = {-1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1};
|
||||
|
||||
static void read(ServoConfiguration &settings, JsonObject &root) {
|
||||
root["is_active"] = settings.is_active;
|
||||
root["servo_pwm_frequency"] = settings.servo_pwm_frequency;
|
||||
root["servo_oscillator_frequency"] =
|
||||
settings.servo_oscillator_frequency;
|
||||
root["servo_oscillator_frequency"] = settings.servo_oscillator_frequency;
|
||||
|
||||
JsonArray servos = root["servos"].to<JsonArray>();
|
||||
|
||||
@@ -68,14 +67,10 @@ class ServoConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonObject &root,
|
||||
ServoConfiguration &settings) {
|
||||
static StateUpdateResult update(JsonObject &root, ServoConfiguration &settings) {
|
||||
settings.is_active = root["is_active"] | settings.is_active;
|
||||
settings.servo_pwm_frequency =
|
||||
root["servo_pwm_frequency"] | settings.servo_pwm_frequency;
|
||||
settings.servo_oscillator_frequency =
|
||||
root["servo_oscillator_frequency"] |
|
||||
settings.servo_oscillator_frequency;
|
||||
settings.servo_pwm_frequency = root["servo_pwm_frequency"] | settings.servo_pwm_frequency;
|
||||
settings.servo_oscillator_frequency = root["servo_oscillator_frequency"] | settings.servo_oscillator_frequency;
|
||||
|
||||
JsonArray servos = root["servos"];
|
||||
|
||||
@@ -83,15 +78,15 @@ class ServoConfiguration {
|
||||
ESP_LOGI("ControllerSettings", "No servos found, adding default servos");
|
||||
for (int8_t i = 0; i < FACTORY_SERVO_NUM; i++) {
|
||||
ESP_LOGI("ControllerSettings", "Adding servo %d", i);
|
||||
settings.servos_config.push_back(
|
||||
servo_t{.name = "Servo " + String(i),
|
||||
.channel = i,
|
||||
.inverted = 1,
|
||||
.angle = 0,
|
||||
.center_angle = FACTORY_SERVO_CENTER_ANGLE
|
||||
// ,
|
||||
// .state = SERVO_STATE_ACTIVE
|
||||
});
|
||||
settings.servos_config.push_back(servo_t {
|
||||
.name = "Servo " + String(i),
|
||||
.channel = i,
|
||||
.inverted = 1,
|
||||
.angle = 0,
|
||||
.center_angle = FACTORY_SERVO_CENTER_ANGLE
|
||||
// ,
|
||||
// .state = SERVO_STATE_ACTIVE
|
||||
});
|
||||
}
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
@@ -99,22 +94,18 @@ class ServoConfiguration {
|
||||
for (auto new_servo_json : servos) {
|
||||
JsonObject servo_config = new_servo_json.as<JsonObject>();
|
||||
int8_t channel = servo_config["channel"] | -1;
|
||||
servo_t *servo =
|
||||
get_servo_by_channel(settings.servos_config, channel);
|
||||
servo_t *servo = get_servo_by_channel(settings.servos_config, channel);
|
||||
|
||||
if (servo != nullptr) {
|
||||
servo->name = servo_config["name"].as<String>() || servo->name;
|
||||
if (servo_config["inverted"])
|
||||
servo->inverted = servo_config["inverted"];
|
||||
if (servo_config["inverted"]) servo->inverted = servo_config["inverted"];
|
||||
if (servo_config["angle"].is<int16_t>()) {
|
||||
servo->angle = servo_config["angle"].as<int16_t>();
|
||||
servo->state = SERVO_STATE_ACTIVE;
|
||||
}
|
||||
if (servo_config["center_angle"].is<int16_t>())
|
||||
servo->center_angle =
|
||||
servo_config["center_angle"].as<int16_t>();
|
||||
if (servo_config["sweep"])
|
||||
servo->state = SERVO_STATE_SWEEPING_FORWARD;
|
||||
servo->center_angle = servo_config["center_angle"].as<int16_t>();
|
||||
if (servo_config["sweep"]) servo->state = SERVO_STATE_SWEEPING_FORWARD;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -131,8 +122,7 @@ class ServoConfiguration {
|
||||
|
||||
return StateUpdateResult::CHANGED;
|
||||
};
|
||||
static servo_t *get_servo_by_channel(std::vector<servo_t> &servos_config,
|
||||
int8_t channel_id) {
|
||||
static servo_t *get_servo_by_channel(std::vector<servo_t> &servos_config, int8_t channel_id) {
|
||||
for (auto &servo : servos_config) {
|
||||
if (servo.channel == channel_id) {
|
||||
return &servo;
|
||||
@@ -142,20 +132,16 @@ class ServoConfiguration {
|
||||
}
|
||||
};
|
||||
|
||||
class ServoController : public Adafruit_PWMServoDriver,
|
||||
public StatefulService<ServoConfiguration> {
|
||||
public:
|
||||
ServoController(PsychicHttpServer *server, FS *fs,
|
||||
SecurityManager *securityManager, EventSocket *socket)
|
||||
class ServoController : public Adafruit_PWMServoDriver, public StatefulService<ServoConfiguration> {
|
||||
public:
|
||||
ServoController(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager, EventSocket *socket)
|
||||
: Adafruit_PWMServoDriver(),
|
||||
_server(server),
|
||||
_securityManager(securityManager),
|
||||
_eventEndpoint(ServoConfiguration::read, ServoConfiguration::update,
|
||||
this, socket, EVENT_CONFIGURATION_SETTINGS),
|
||||
_fsPersistence(ServoConfiguration::read, ServoConfiguration::update,
|
||||
this, fs, SERVO_CONFIG_FILE) {
|
||||
addUpdateHandler([&](const String &originId) { updateServos(); },
|
||||
false);
|
||||
_eventEndpoint(ServoConfiguration::read, ServoConfiguration::update, this, socket,
|
||||
EVENT_CONFIGURATION_SETTINGS),
|
||||
_fsPersistence(ServoConfiguration::read, ServoConfiguration::update, this, fs, SERVO_CONFIG_FILE) {
|
||||
addUpdateHandler([&](const String &originId) { updateServos(); }, false);
|
||||
}
|
||||
|
||||
void configure() {
|
||||
@@ -164,8 +150,7 @@ class ServoController : public Adafruit_PWMServoDriver,
|
||||
setOscillatorFrequency(_state.servo_oscillator_frequency);
|
||||
setPWMFreq(_state.servo_pwm_frequency);
|
||||
deactivate();
|
||||
ESP_LOGI("ServoController",
|
||||
"Configured with oscillator frequency %d and PWM frequency %d",
|
||||
ESP_LOGI("ServoController", "Configured with oscillator frequency %d and PWM frequency %d",
|
||||
_state.servo_oscillator_frequency, _state.servo_pwm_frequency);
|
||||
}
|
||||
|
||||
@@ -195,7 +180,7 @@ class ServoController : public Adafruit_PWMServoDriver,
|
||||
}
|
||||
}
|
||||
|
||||
void setAngle(servo_t* servo) {
|
||||
void setAngle(servo_t *servo) {
|
||||
int8_t channel = servo->channel;
|
||||
bool invert = servo->inverted;
|
||||
int16_t angle = invert ? 180 - servo->angle : servo->angle;
|
||||
@@ -203,9 +188,7 @@ class ServoController : public Adafruit_PWMServoDriver,
|
||||
setPWM(channel, 0, angleToPwm(angle));
|
||||
}
|
||||
|
||||
uint16_t angleToPwm(int angle) {
|
||||
return map(angle, 0, 180, SERVO_MIN, SERVO_MAX);
|
||||
}
|
||||
uint16_t angleToPwm(int angle) { return map(angle, 0, 180, SERVO_MIN, SERVO_MAX); }
|
||||
|
||||
void updateServoState() {
|
||||
for (auto &servo : _state.servos_config) {
|
||||
@@ -233,13 +216,13 @@ class ServoController : public Adafruit_PWMServoDriver,
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
SecurityManager *_securityManager;
|
||||
EventEndpoint<ServoConfiguration> _eventEndpoint;
|
||||
FSPersistence<ServoConfiguration> _fsPersistence;
|
||||
|
||||
bool is_active{true};
|
||||
bool is_active {true};
|
||||
unsigned long _lastUpdate;
|
||||
constexpr static int ServoInterval = 2;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user