🎩 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
+15 -12
View File
@@ -170,20 +170,23 @@ export type CameraSettings = {
hmirror: boolean; 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 type File = number;
export interface Directory { export interface Directory {
[key: string]: File | 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"> <script lang="ts">
import type { servo } from "$lib/models"; import type { Servo } from "$lib/models";
export let servo: servo; export let servo: Servo;
</script> </script>
<div> <div>
+32 -20
View File
@@ -1,39 +1,51 @@
<script lang="ts"> <script lang="ts">
import SettingsCard from "$lib/components/SettingsCard.svelte"; 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 MotorOutline from '~icons/mdi/motor-outline';
import Servo from './servo.svelte'; import Servo from './servo.svelte';
import { api } from "$lib/api";
import Spinner from "$lib/components/Spinner.svelte";
let direction = 1 let servo_config: ServoConfiguration
let angle = 0
let min_pwm = 1000
let max_pwm = 2000
let min_angle = 0 $: updateServoConfig(servo_config)
let max_angle = 0
let servos:servo[] = [ const updateServoConfig = async (servo_config: ServoConfiguration) => {
{ let result = await api.post('/api/servo/configuration', servo_config)
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 getServoConfig = async () => {
let result = await api.get<ServoConfiguration>('/api/servo/configuration')
if (result.isOk()) {
servo_config = result.inner
return result.inner
}
}
</script> </script>
<SettingsCard collapsible={false}> <SettingsCard collapsible={false}>
<MotorOutline slot="icon" class="lex-shrink-0 mr-2 h-6 w-6 self-end" /> <MotorOutline slot="icon" class="lex-shrink-0 mr-2 h-6 w-6 self-end" />
<span slot="title">Servo</span> <span slot="title">Servo</span>
{#each servos as 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 servo_config.servos as servo}
<Servo {servo} /> <Servo {servo} />
<div class="divider"></div> <div class="divider"></div>
{/each} {/each}
{/await}
</SettingsCard> </SettingsCard>
@@ -9,7 +9,7 @@
let filename = ''; let filename = '';
const getFiles = async () => { const getFiles = async () => {
const result = await api.get<Directory>('/api/files/list') const result = await api.get<Directory>('/api/files')
if (result.isOk()) { if (result.isOk()) {
return result.inner; return result.inner;
} }
+6
View File
@@ -55,3 +55,9 @@ build_flags =
; JWT Secret ; 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 <PsychicHttp.h>
#include <SecurityManager.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 class FileExplorer
{ {
@@ -20,6 +21,9 @@ class FileExplorer
_server->on(FILE_EXPLORER_SERVICE_PATH, HTTP_GET, _server->on(FILE_EXPLORER_SERVICE_PATH, HTTP_GET,
_securityManager->wrapRequest(std::bind(&FileExplorer::explore, this, std::placeholders::_1), _securityManager->wrapRequest(std::bind(&FileExplorer::explore, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED)); 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); ESP_LOGV("APStatus", "Registered GET endpoint: %s", FILE_EXPLORER_SERVICE_PATH);
} }
@@ -27,11 +31,23 @@ class FileExplorer
private: private:
PsychicHttpServer *_server; PsychicHttpServer *_server;
SecurityManager *_securityManager; SecurityManager *_securityManager;
esp_err_t explore(PsychicRequest *request) esp_err_t explore(PsychicRequest *request)
{ {
return request->reply(200, "application/json", listFiles("/").c_str()); 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) String listFiles(const String &directory, bool isRoot = true)
{ {
File root = ESPFS.open(directory.startsWith("/") ? directory : "/" + directory); File root = ESPFS.open(directory.startsWith("/") ? directory : "/" + directory);
+32 -7
View File
@@ -6,11 +6,25 @@
#include <Adafruit_PWMServoDriver.h> #include <Adafruit_PWMServoDriver.h>
#define SERVO_OSCILLATOR_FREQUENCY 27000000
#define SERVO_FREQ 50
#define SERVO_CONFIG_FILE "/config/servoConfig.json" #define SERVO_CONFIG_FILE "/config/servoConfig.json"
#define SERVO_CONFIGURATION_SETTINGS_PATH "/api/servo/configuration" #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 struct servo_t
{ {
String name; String name;
@@ -22,8 +36,8 @@ struct servo_t
class ServoConfiguration { class ServoConfiguration {
public: public:
int32_t servo_oscillator_frequency {SERVO_OSCILLATOR_FREQUENCY}; int32_t servo_oscillator_frequency {FACTORY_SERVO_OSCILLATOR_FREQUENCY};
int32_t servo_pwm_frequency {SERVO_FREQ}; int32_t servo_pwm_frequency {FACTORY_SERVO_PWM_FREQUENCY};
std::vector<servo_t> servos_config; std::vector<servo_t> servos_config;
bool is_active {false}; bool is_active {false};
@@ -47,9 +61,9 @@ 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 = root["is_active"] | false;
settings.servo_pwm_frequency = root["servo_pwm_frequency"]; settings.servo_pwm_frequency = root["servo_pwm_frequency"] | FACTORY_SERVO_PWM_FREQUENCY;
settings.servo_oscillator_frequency = root["servo_oscillator_frequency"]; settings.servo_oscillator_frequency = root["servo_oscillator_frequency"] | FACTORY_SERVO_OSCILLATOR_FREQUENCY;
settings.servos_config.clear(); settings.servos_config.clear();
JsonArray servos = root["servos"]; JsonArray servos = root["servos"];
@@ -70,6 +84,17 @@ class ServoConfiguration {
settings.servos_config.push_back(new_servo); settings.servos_config.push_back(new_servo);
i++; 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; return StateUpdateResult::CHANGED;
}; };
@@ -118,7 +118,7 @@ void WiFiSettingsService::connectToWiFi()
} }
else if (scanResult == 0) else if (scanResult == 0)
{ {
ESP_LOGW("WiFiSettingsService", "No networks found."); ESP_LOGI("WiFiSettingsService", "No networks found.");
} }
else else
{ {