🛸 Adds servo config
This commit is contained in:
@@ -11,14 +11,66 @@
|
||||
#define SERVO_CONFIG_FILE "/config/deviceConfig.json"
|
||||
#define SERVO_CONFIGURATION_SETTINGS_PATH "/api/servo/configuration"
|
||||
|
||||
struct servo_t
|
||||
{
|
||||
String name;
|
||||
int8_t channel;
|
||||
bool inverted;
|
||||
int16_t angle;
|
||||
int16_t center_angle;
|
||||
};
|
||||
|
||||
class ServoConfiguration {
|
||||
public:
|
||||
int32_t servo_oscillator_frequency {SERVO_OSCILLATOR_FREQUENCY};
|
||||
int32_t servo_pwm_frequency {SERVO_FREQ};
|
||||
static void read(ServoConfiguration &settings, JsonObject &root) {}
|
||||
std::vector<servo_t> servos_config;
|
||||
bool is_active {false};
|
||||
|
||||
static void read(ServoConfiguration &settings, JsonObject &root) {
|
||||
root["is_active"] = settings.is_active;
|
||||
root["servo_pwm_frequency"] = settings.servo_oscillator_frequency;
|
||||
root["servo_oscillator_frequency"] = settings.servo_oscillator_frequency;
|
||||
|
||||
JsonArray servos = root["servos"].to<JsonArray>();
|
||||
|
||||
for (auto &servo : settings.servos_config)
|
||||
{
|
||||
JsonObject servo_config = servos.add<JsonObject>();
|
||||
|
||||
servo_config["name"] = servo.name;
|
||||
servo_config["channel"] = servo.channel;
|
||||
servo_config["inverted"] = servo.inverted;
|
||||
servo_config["angle"] = servo.angle;
|
||||
servo_config["center_angle"] = servo.center_angle;
|
||||
}
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonObject &root, ServoConfiguration &settings) {
|
||||
settings.is_active = root["is_active"];
|
||||
settings.servo_pwm_frequency = root["servo_pwm_frequency"];
|
||||
settings.servo_oscillator_frequency = root["servo_oscillator_frequency"];
|
||||
settings.servos_config.clear();
|
||||
|
||||
JsonArray servos = root["servos"];
|
||||
if (root["servos"].is<JsonArray>())
|
||||
{
|
||||
int i = 0;
|
||||
for (auto servo : servos)
|
||||
{
|
||||
JsonObject servo_config = servo.as<JsonObject>();
|
||||
servo_t new_servo;
|
||||
|
||||
new_servo.name = servo_config["name"].as<String>();
|
||||
new_servo.channel = servo_config["channel"];
|
||||
new_servo.inverted = servo_config["inverted"];
|
||||
new_servo.angle = servo_config["angle"];
|
||||
new_servo.center_angle = servo_config["center_angle"];
|
||||
|
||||
settings.servos_config.push_back(new_servo);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return StateUpdateResult::CHANGED;
|
||||
};
|
||||
};
|
||||
@@ -43,17 +95,15 @@ class ServoController : public Adafruit_PWMServoDriver, public StatefulService<S
|
||||
}
|
||||
|
||||
void deactivate() {
|
||||
isActive = false;
|
||||
_state.is_active = false;
|
||||
sleep();
|
||||
}
|
||||
|
||||
void activate() {
|
||||
isActive = true;
|
||||
_state.is_active = true;
|
||||
sleep();
|
||||
}
|
||||
|
||||
bool isActive{false};
|
||||
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
SecurityManager *_securityManager;
|
||||
|
||||
Reference in New Issue
Block a user