🎩 Adds lots of magic

This commit is contained in:
Rune Harlyk
2024-06-09 21:23:29 +02:00
committed by Rune Harlyk
parent 69733beb5e
commit 88f9c0e5fb
8 changed files with 111 additions and 49 deletions
+16 -13
View File
@@ -170,20 +170,23 @@ export type CameraSettings = {
hmirror: boolean;
};
export type servo = {
channel: number;
name: string;
inverted: boolean;
angle: number;
center_angle: number;
// min_pwm: number;
// max_pwm: number;
// min_angle: number;
// max_angle: number;
};
export type File = number;
export interface Directory {
[key: string]: File | Directory;
}
}
export type Servo = {
name: string;
channel: number;
inverted: boolean;
angle: number;
center_angle: number;
};
export type ServoConfiguration = {
is_active: boolean;
servo_pwm_frequency: number;
servo_oscillator_frequency: number;
servos: Servo[];
};
@@ -1,6 +1,6 @@
<script lang="ts">
import type { servo } from "$lib/models";
export let servo: servo;
import type { Servo } from "$lib/models";
export let servo: Servo;
</script>
<div>
+35 -23
View File
@@ -1,39 +1,51 @@
<script lang="ts">
import SettingsCard from "$lib/components/SettingsCard.svelte";
import type { servo } from "$lib/models";
import type { ServoConfiguration } from "$lib/models";
import MotorOutline from '~icons/mdi/motor-outline';
import Servo from './servo.svelte';
import { api } from "$lib/api";
import Spinner from "$lib/components/Spinner.svelte";
let direction = 1
let angle = 0
let min_pwm = 1000
let max_pwm = 2000
let servo_config: ServoConfiguration
let min_angle = 0
let max_angle = 0
$: updateServoConfig(servo_config)
let servos:servo[] = [
{
channel: 0,
name: "Front right hip",
inverted: false,
angle: angle,
min_pwm: min_pwm,
max_pwm: max_pwm,
min_angle: min_angle,
max_angle: max_angle,
center_angle: 0
const updateServoConfig = async (servo_config: ServoConfiguration) => {
let result = await api.post('/api/servo/configuration', servo_config)
}
const getServoConfig = async () => {
let result = await api.get<ServoConfiguration>('/api/servo/configuration')
if (result.isOk()) {
servo_config = result.inner
return result.inner
}
]
}
</script>
<SettingsCard collapsible={false}>
<MotorOutline slot="icon" class="lex-shrink-0 mr-2 h-6 w-6 self-end" />
<span slot="title">Servo</span>
{#each servos as servo}
<Servo {servo} />
{#await getServoConfig() }
<Spinner />
{:then _}
<div class="flex flex-col">
<h2 class="text-lg">General servo configuration</h2>
<span class="mb-1 flex justify-between items-center">
Servo Oscillator Frequency <input type="number" bind:value={servo_config.servo_oscillator_frequency} class="input input-bordered input-sm max-w-xs"/>
</span>
<span class="flex justify-between items-center mb-1">
Servo PWM Frequency <input type="number" bind:value={servo_config.servo_pwm_frequency} class="input input-bordered input-sm max-w-xs"/>
</span>
<span class="flex justify-between items-center gap-1">
Is active <input type="checkbox" bind:value={servo_config.is_active} class="toggle"/>
</span>
</div>
<div class="divider"></div>
{/each}
{#each servo_config.servos as servo}
<Servo {servo} />
<div class="divider"></div>
{/each}
{/await}
</SettingsCard>
@@ -9,7 +9,7 @@
let filename = '';
const getFiles = async () => {
const result = await api.get<Directory>('/api/files/list')
const result = await api.get<Directory>('/api/files')
if (result.isOk()) {
return result.inner;
}
+7 -1
View File
@@ -54,4 +54,10 @@ build_flags =
-D FACTORY_MQTT_MAX_TOPIC_LENGTH=128
; JWT Secret
-D FACTORY_JWT_SECRET=\"#{random}-#{random}\" ; supports placeholders
-D FACTORY_JWT_SECRET=\"#{random}-#{random}\" ; supports placeholders
; Servo settings
-D FACTORY_SERVO_NUM=12
-D FACTORY_SERVO_OSCILLATOR_FREQUENCY=27000000
-D FACTORY_SERVO_PWM_FREQUENCY=50
-D FACTORY_SERVO_CENTER_ANGLE=90
@@ -5,7 +5,8 @@
#include <PsychicHttp.h>
#include <SecurityManager.h>
#define FILE_EXPLORER_SERVICE_PATH "/api/files/list"
#define FILE_EXPLORER_SERVICE_PATH "/api/files"
#define FILE_EXPLORER_DELETE_SERVICE_PATH "/api/files/delete"
class FileExplorer
{
@@ -20,6 +21,9 @@ class FileExplorer
_server->on(FILE_EXPLORER_SERVICE_PATH, HTTP_GET,
_securityManager->wrapRequest(std::bind(&FileExplorer::explore, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED));
_server->on(FILE_EXPLORER_DELETE_SERVICE_PATH, HTTP_POST,
_securityManager->wrapCallback(std::bind(&FileExplorer::deleteFile, this, std::placeholders::_1, std::placeholders::_2),
AuthenticationPredicates::IS_AUTHENTICATED));
ESP_LOGV("APStatus", "Registered GET endpoint: %s", FILE_EXPLORER_SERVICE_PATH);
}
@@ -27,11 +31,23 @@ class FileExplorer
private:
PsychicHttpServer *_server;
SecurityManager *_securityManager;
esp_err_t explore(PsychicRequest *request)
{
return request->reply(200, "application/json", listFiles("/").c_str());
}
esp_err_t deleteFile(PsychicRequest *request, JsonVariant &json)
{
if (json.is<JsonObject>())
{
String filename = json["file"];
ESP_LOGI("FileExplorer", "Deleting file: %s", filename.c_str());
return ESPFS.remove(filename.c_str()) ? request->reply(200) : request->reply(500);
}
return request->reply(400);
}
String listFiles(const String &directory, bool isRoot = true)
{
File root = ESPFS.open(directory.startsWith("/") ? directory : "/" + directory);
+32 -7
View File
@@ -6,11 +6,25 @@
#include <Adafruit_PWMServoDriver.h>
#define SERVO_OSCILLATOR_FREQUENCY 27000000
#define SERVO_FREQ 50
#define SERVO_CONFIG_FILE "/config/servoConfig.json"
#define SERVO_CONFIGURATION_SETTINGS_PATH "/api/servo/configuration"
#ifndef FACTORY_SERVO_NUM
#define FACTORY_SERVO_NUM 12
#endif
#ifndef FACTORY_SERVO_PWM_FREQUENCY
#define FACTORY_SERVO_PWM_FREQUENCY 50
#endif
#ifndef FACTORY_SERVO_OSCILLATOR_FREQUENCY
#define FACTORY_SERVO_OSCILLATOR_FREQUENCY 27000000
#endif
#ifndef FACTORY_SERVO_CENTER_ANGLE
#define FACTORY_SERVO_CENTER_ANGLE 90
#endif
struct servo_t
{
String name;
@@ -22,8 +36,8 @@ struct servo_t
class ServoConfiguration {
public:
int32_t servo_oscillator_frequency {SERVO_OSCILLATOR_FREQUENCY};
int32_t servo_pwm_frequency {SERVO_FREQ};
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 {false};
@@ -47,9 +61,9 @@ class ServoConfiguration {
}
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.is_active = root["is_active"] | false;
settings.servo_pwm_frequency = root["servo_pwm_frequency"] | FACTORY_SERVO_PWM_FREQUENCY;
settings.servo_oscillator_frequency = root["servo_oscillator_frequency"] | FACTORY_SERVO_OSCILLATOR_FREQUENCY;
settings.servos_config.clear();
JsonArray servos = root["servos"];
@@ -70,6 +84,17 @@ class ServoConfiguration {
settings.servos_config.push_back(new_servo);
i++;
}
} else {
for (int8_t i = 0; i < FACTORY_SERVO_NUM; i++) {
ESP_LOGI("WiFiSettings", "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
});
}
}
return StateUpdateResult::CHANGED;
};
@@ -118,7 +118,7 @@ void WiFiSettingsService::connectToWiFi()
}
else if (scanResult == 0)
{
ESP_LOGW("WiFiSettingsService", "No networks found.");
ESP_LOGI("WiFiSettingsService", "No networks found.");
}
else
{