Camera api to protobuf - still and stream not tested
This commit is contained in:
+2
-2
@@ -2,7 +2,7 @@ import { get } from 'svelte/store'
|
||||
import { Err, Ok, type Result } from './utilities'
|
||||
import { apiLocation } from './stores/location-store'
|
||||
import type { MessageFns } from './platform_shared/filesystem'
|
||||
import { Request, Response } from './platform_shared/api'
|
||||
import { Request, Response as ProtoResponse } from './platform_shared/api'
|
||||
import { BinaryWriter } from '@bufbuild/protobuf/wire'
|
||||
|
||||
export const api = {
|
||||
@@ -72,7 +72,7 @@ async function sendRequest<TResponse>(
|
||||
const data = await response.json()
|
||||
return Ok.new(data as TResponse)
|
||||
} else if (contentType && contentType.includes('application/x-protobuf')) {
|
||||
let data: Response = Response.decode(await response.bytes());
|
||||
let data: ProtoResponse = ProtoResponse.decode(await response.bytes());
|
||||
return Ok.new(data as TResponse)
|
||||
} else {
|
||||
// Handle empty object as response
|
||||
|
||||
@@ -76,21 +76,6 @@ export type Rssi = {
|
||||
ssid: string
|
||||
}
|
||||
|
||||
export type CameraSettings = {
|
||||
framesize: number
|
||||
quality: number
|
||||
brightness: number
|
||||
contrast: number
|
||||
saturation: number
|
||||
sharpness: number
|
||||
denoise: number
|
||||
special_effect: number
|
||||
wb_mode: number
|
||||
vflip: boolean
|
||||
hmirror: boolean
|
||||
}
|
||||
|
||||
|
||||
export type Servo = {
|
||||
name: string
|
||||
channel: number
|
||||
|
||||
@@ -1,38 +1,40 @@
|
||||
<script lang="ts">
|
||||
import { api } from '$lib/api'
|
||||
import Spinner from '$lib/components/Spinner.svelte'
|
||||
import type { CameraSettings } from '$lib/types/models'
|
||||
let settings: CameraSettings = $state({
|
||||
brightness: 0,
|
||||
contrast: 0,
|
||||
framesize: 0,
|
||||
vflip: false,
|
||||
hmirror: false,
|
||||
special_effect: 0,
|
||||
quality: 0,
|
||||
saturation: 0,
|
||||
sharpness: 0,
|
||||
denoise: 0,
|
||||
wb_mode: 0
|
||||
})
|
||||
import { CameraSettings, Request, type Response as ProtoResponse } from '$lib/platform_shared/api'
|
||||
|
||||
let settings = $state<CameraSettings>(CameraSettings.create({}))
|
||||
|
||||
const getCameraSettings = async () => {
|
||||
const result = await api.get<CameraSettings>('/api/camera/settings')
|
||||
const result = await api.get<ProtoResponse>('/api/camera/settings')
|
||||
if (result.isErr()) {
|
||||
console.error('An error occurred', result.inner)
|
||||
return
|
||||
}
|
||||
settings = result.inner
|
||||
if (result.inner.cameraSettings) {
|
||||
settings = result.inner.cameraSettings
|
||||
}
|
||||
}
|
||||
|
||||
const updateCameraSettings = async () => {
|
||||
const result = await api.post<CameraSettings>('/api/camera/settings', settings)
|
||||
const request = Request.create({
|
||||
cameraSettings: settings
|
||||
})
|
||||
const result = await api.post_proto<ProtoResponse>('/api/camera/settings', request)
|
||||
if (result.isErr()) {
|
||||
console.error('An error occurred', result.inner)
|
||||
return
|
||||
}
|
||||
settings = result.inner
|
||||
if (result.inner.cameraSettings) {
|
||||
settings = result.inner.cameraSettings
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to convert number (0/1) to boolean for checkbox binding
|
||||
const getVflip = () => settings.vflip !== 0
|
||||
const setVflip = (value: boolean) => (settings.vflip = value ? 1 : 0)
|
||||
const getHmirror = () => settings.hmirror !== 0
|
||||
const setHmirror = (value: boolean) => (settings.hmirror = value ? 1 : 0)
|
||||
</script>
|
||||
|
||||
{#await getCameraSettings()}
|
||||
@@ -78,19 +80,29 @@
|
||||
|
||||
<label class="cursor-pointer flex items-center justify-between">
|
||||
Vertical flip
|
||||
<input type="checkbox" class="toggle" bind:checked={settings.vflip} />
|
||||
<input
|
||||
type="checkbox"
|
||||
class="toggle"
|
||||
checked={getVflip()}
|
||||
onchange={(e) => setVflip(e.currentTarget.checked)}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label class="cursor-pointer flex items-center justify-between">
|
||||
Horizontal flip
|
||||
<input type="checkbox" class="toggle" bind:checked={settings.hmirror} />
|
||||
<input
|
||||
type="checkbox"
|
||||
class="toggle"
|
||||
checked={getHmirror()}
|
||||
onchange={(e) => setHmirror(e.currentTarget.checked)}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label for="special_effect" class="flex items-center">
|
||||
<span class="basis-1/2">Special Effect</span>
|
||||
<select
|
||||
class="select select-bordered select-sm w-full max-w-xs"
|
||||
bind:value={settings.special_effect}
|
||||
bind:value={settings.specialEffect}
|
||||
>
|
||||
<option value={0}>No effect</option>
|
||||
<option value={1}>Negative</option>
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <esp_http_server.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <features.h>
|
||||
#include <template/stateful_persistence.h>
|
||||
#include <template/stateful_endpoint.h>
|
||||
#include <template/stateful_service.h>
|
||||
#include <template/stateful_proto_endpoint.h>
|
||||
#include <template/stateful_persistence_pb.h>
|
||||
|
||||
#include <settings/camera_settings.h>
|
||||
|
||||
@@ -19,6 +19,7 @@ namespace Camera {
|
||||
#endif
|
||||
|
||||
#define PART_BOUNDARY "frame"
|
||||
#define CAMERA_SETTINGS_FILE "/config/cameraSettings.pb"
|
||||
|
||||
camera_fb_t *safe_camera_fb_get();
|
||||
sensor_t *safe_sensor_get();
|
||||
@@ -33,10 +34,10 @@ class CameraService : public StatefulService<CameraSettings> {
|
||||
esp_err_t cameraStill(httpd_req_t *request);
|
||||
esp_err_t cameraStream(httpd_req_t *request);
|
||||
|
||||
StatefulHttpEndpoint<CameraSettings> endpoint;
|
||||
StatefulProtoEndpoint<CameraSettings, api_CameraSettings> protoEndpoint;
|
||||
|
||||
private:
|
||||
FSPersistence<CameraSettings> _persistence;
|
||||
FSPersistencePB<CameraSettings> _persistence;
|
||||
void updateCamera();
|
||||
};
|
||||
} // namespace Camera
|
||||
|
||||
@@ -1,109 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <template/state_result.h>
|
||||
#include <platform_shared/api.pb.h>
|
||||
#include <esp_camera.h>
|
||||
|
||||
namespace Camera {
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <template/state_result.h>
|
||||
#include <esp_camera.h>
|
||||
// Use proto type directly as settings type
|
||||
using CameraSettings = api_CameraSettings;
|
||||
|
||||
class CameraSettings {
|
||||
public:
|
||||
pixformat_t pixformat;
|
||||
framesize_t framesize; // 0 - 10
|
||||
uint8_t quality; // 0 - 63
|
||||
int8_t brightness; //-2 - 2
|
||||
int8_t contrast; //-2 - 2
|
||||
int8_t saturation; //-2 - 2
|
||||
int8_t sharpness; //-2 - 2
|
||||
uint8_t denoise;
|
||||
gainceiling_t gainceiling;
|
||||
uint8_t whitebal;
|
||||
uint8_t special_effect; // 0 - 6
|
||||
uint8_t wb_mode; // 0 - 4
|
||||
uint8_t awb;
|
||||
uint8_t exposure_ctrl;
|
||||
uint8_t awb_gain;
|
||||
uint8_t gain_ctrl;
|
||||
uint8_t aec;
|
||||
uint8_t aec2;
|
||||
int8_t ae_level; //-2 - 2
|
||||
uint16_t aec_value; // 0 - 1200
|
||||
uint8_t agc;
|
||||
uint8_t agc_gain; // 0 - 30
|
||||
uint8_t bpc;
|
||||
uint8_t wpc;
|
||||
uint8_t raw_gma;
|
||||
uint8_t lenc;
|
||||
uint8_t hmirror;
|
||||
uint8_t vflip;
|
||||
uint8_t dcw;
|
||||
uint8_t colorbar;
|
||||
|
||||
static void read(CameraSettings &settings, JsonVariant &root) {
|
||||
root["pixformat"] = settings.pixformat;
|
||||
root["framesize"] = settings.framesize;
|
||||
root["quality"] = settings.quality;
|
||||
root["brightness"] = settings.brightness;
|
||||
root["contrast"] = settings.contrast;
|
||||
root["saturation"] = settings.saturation;
|
||||
root["sharpness"] = settings.sharpness;
|
||||
root["denoise"] = settings.denoise;
|
||||
root["special_effect"] = settings.special_effect;
|
||||
root["wb_mode"] = settings.wb_mode;
|
||||
root["exposure_ctrl"] = settings.exposure_ctrl;
|
||||
root["gain_ctrl"] = settings.gain_ctrl;
|
||||
root["awb"] = settings.awb;
|
||||
root["awb_gain"] = settings.awb_gain;
|
||||
root["aec"] = settings.aec;
|
||||
root["aec2"] = settings.aec2;
|
||||
root["ae_level"] = settings.ae_level;
|
||||
root["aec_value"] = settings.aec_value;
|
||||
root["agc"] = settings.agc;
|
||||
root["agc_gain"] = settings.agc_gain;
|
||||
root["gainceiling"] = settings.gainceiling;
|
||||
root["bpc"] = settings.bpc;
|
||||
root["wpc"] = settings.wpc;
|
||||
root["raw_gma"] = settings.raw_gma;
|
||||
root["lenc"] = settings.lenc;
|
||||
root["hmirror"] = settings.hmirror;
|
||||
root["vflip"] = settings.vflip;
|
||||
root["dcw"] = settings.dcw;
|
||||
root["colorbar"] = settings.colorbar;
|
||||
// Default factory settings
|
||||
inline CameraSettings CameraSettings_defaults() {
|
||||
CameraSettings settings = api_CameraSettings_init_zero;
|
||||
settings.pixformat = PIXFORMAT_JPEG;
|
||||
settings.framesize = FRAMESIZE_VGA;
|
||||
settings.quality = 12;
|
||||
settings.brightness = 0;
|
||||
settings.contrast = 0;
|
||||
settings.saturation = 0;
|
||||
settings.sharpness = 0;
|
||||
settings.denoise = 0;
|
||||
settings.gainceiling = GAINCEILING_2X;
|
||||
settings.whitebal = 1;
|
||||
settings.special_effect = 0;
|
||||
settings.wb_mode = 0;
|
||||
settings.awb = 1;
|
||||
settings.exposure_ctrl = 1;
|
||||
settings.awb_gain = 1;
|
||||
settings.gain_ctrl = 1;
|
||||
settings.aec = 1;
|
||||
settings.aec2 = 0;
|
||||
settings.ae_level = 0;
|
||||
settings.aec_value = 300;
|
||||
settings.agc = 1;
|
||||
settings.agc_gain = 0;
|
||||
settings.bpc = 0;
|
||||
settings.wpc = 1;
|
||||
settings.raw_gma = 1;
|
||||
settings.lenc = 1;
|
||||
settings.hmirror = 0;
|
||||
settings.vflip = 0;
|
||||
settings.dcw = 1;
|
||||
settings.colorbar = 0;
|
||||
return settings;
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonVariant &root, CameraSettings &settings) {
|
||||
settings.pixformat = root["pixformat"];
|
||||
settings.framesize = root["framesize"];
|
||||
settings.brightness = root["brightness"];
|
||||
settings.contrast = root["contrast"];
|
||||
settings.quality = root["quality"];
|
||||
settings.contrast = root["contrast"];
|
||||
settings.saturation = root["saturation"];
|
||||
settings.sharpness = root["sharpness"];
|
||||
settings.denoise = root["denoise"];
|
||||
settings.exposure_ctrl = root["exposure_ctrl"];
|
||||
settings.gain_ctrl = root["gain_ctrl"];
|
||||
settings.special_effect = root["special_effect"];
|
||||
settings.wb_mode = root["wb_mode"];
|
||||
settings.awb = root["awb"];
|
||||
settings.awb_gain = root["awb_gain"];
|
||||
settings.aec = root["aec"];
|
||||
settings.aec2 = root["aec2"];
|
||||
settings.ae_level = root["ae_level"];
|
||||
settings.aec_value = root["aec_value"];
|
||||
settings.agc = root["agc"];
|
||||
settings.agc_gain = root["agc_gain"];
|
||||
settings.gainceiling = root["gainceiling"];
|
||||
settings.bpc = root["bpc"];
|
||||
settings.wpc = root["wpc"];
|
||||
settings.raw_gma = root["raw_gma"];
|
||||
settings.lenc = root["lenc"];
|
||||
settings.hmirror = root["hmirror"];
|
||||
settings.vflip = root["vflip"];
|
||||
settings.dcw = root["dcw"];
|
||||
settings.colorbar = root["colorbar"];
|
||||
// Proto read/update are identity functions since type is the same
|
||||
inline void CameraSettings_read(const CameraSettings& settings, CameraSettings& proto) {
|
||||
proto = settings;
|
||||
}
|
||||
|
||||
inline StateUpdateResult CameraSettings_update(const CameraSettings& proto, CameraSettings& settings) {
|
||||
settings = proto;
|
||||
return StateUpdateResult::CHANGED;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace Camera
|
||||
+3
-4
@@ -49,14 +49,13 @@ void setupServer() {
|
||||
server.onProto("/api/system/sleep", HTTP_POST,
|
||||
[&](httpd_req_t *request, api_Request *protoReq) { return system_service::handleSleep(request); });
|
||||
#if USE_CAMERA
|
||||
// TODO: REMAKE TO PROTO
|
||||
server.on("/api/camera/still", HTTP_GET, [&](httpd_req_t *request) { return cameraService.cameraStill(request); });
|
||||
server.on("/api/camera/stream", HTTP_GET,
|
||||
[&](httpd_req_t *request) { return cameraService.cameraStream(request); });
|
||||
server.on("/api/camera/settings", HTTP_GET,
|
||||
[&](httpd_req_t *request) { return cameraService.endpoint.getState(request); });
|
||||
server.on("/api/camera/settings", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
||||
return cameraService.endpoint.handleStateUpdate(request, json);
|
||||
[&](httpd_req_t *request) { return cameraService.protoEndpoint.getState(request); });
|
||||
server.onProto("/api/camera/settings", HTTP_POST, [&](httpd_req_t *request, api_Request *protoReq) {
|
||||
return cameraService.protoEndpoint.handleStateUpdate(request, protoReq);
|
||||
});
|
||||
#endif
|
||||
server.on("/api/servo/config", HTTP_GET,
|
||||
|
||||
@@ -31,8 +31,12 @@ sensor_t *safe_sensor_get() {
|
||||
void safe_sensor_return() { xSemaphoreGiveRecursive(cameraMutex); }
|
||||
|
||||
CameraService::CameraService()
|
||||
: endpoint(CameraSettings::read, CameraSettings::update, this),
|
||||
_persistence(CameraSettings::read, CameraSettings::update, this, CAMERA_SETTINGS_FILE) {
|
||||
: protoEndpoint(CameraSettings_read, CameraSettings_update, this,
|
||||
API_REQUEST_EXTRACTOR(camera_settings, api_CameraSettings),
|
||||
API_RESPONSE_ASSIGNER(camera_settings, api_CameraSettings)),
|
||||
_persistence(CameraSettings_read, CameraSettings_update, this,
|
||||
CAMERA_SETTINGS_FILE, api_CameraSettings_fields, api_CameraSettings_size,
|
||||
CameraSettings_defaults()) {
|
||||
addUpdateHandler([&](const std::string &originId) { updateCamera(); }, false);
|
||||
}
|
||||
|
||||
@@ -149,14 +153,14 @@ void CameraService::updateCamera() {
|
||||
safe_sensor_return();
|
||||
return;
|
||||
}
|
||||
s->set_pixformat(s, state().pixformat);
|
||||
s->set_framesize(s, state().framesize);
|
||||
s->set_pixformat(s, static_cast<pixformat_t>(state().pixformat));
|
||||
s->set_framesize(s, static_cast<framesize_t>(state().framesize));
|
||||
s->set_brightness(s, state().brightness);
|
||||
s->set_contrast(s, state().contrast);
|
||||
s->set_saturation(s, state().saturation);
|
||||
s->set_sharpness(s, state().sharpness);
|
||||
s->set_denoise(s, state().denoise);
|
||||
s->set_gainceiling(s, state().gainceiling);
|
||||
s->set_gainceiling(s, static_cast<gainceiling_t>(state().gainceiling));
|
||||
s->set_quality(s, state().quality);
|
||||
s->set_colorbar(s, state().colorbar);
|
||||
s->set_awb_gain(s, state().awb_gain);
|
||||
|
||||
@@ -86,6 +86,45 @@ message WifiSettings {
|
||||
|
||||
message WifiSettingsRequest {}
|
||||
|
||||
// =============================================================================
|
||||
// Camera Settings - shared data types
|
||||
// =============================================================================
|
||||
|
||||
message CameraSettings {
|
||||
uint32 pixformat = 1;
|
||||
uint32 framesize = 2; // 0-10
|
||||
uint32 quality = 3; // 0-63
|
||||
int32 brightness = 4; // -2 to 2
|
||||
int32 contrast = 5; // -2 to 2
|
||||
int32 saturation = 6; // -2 to 2
|
||||
int32 sharpness = 7; // -2 to 2
|
||||
uint32 denoise = 8;
|
||||
uint32 gainceiling = 9;
|
||||
uint32 whitebal = 10;
|
||||
uint32 special_effect = 11; // 0-6
|
||||
uint32 wb_mode = 12; // 0-4
|
||||
uint32 awb = 13;
|
||||
uint32 exposure_ctrl = 14;
|
||||
uint32 awb_gain = 15;
|
||||
uint32 gain_ctrl = 16;
|
||||
uint32 aec = 17;
|
||||
uint32 aec2 = 18;
|
||||
int32 ae_level = 19; // -2 to 2
|
||||
uint32 aec_value = 20; // 0-1200
|
||||
uint32 agc = 21;
|
||||
uint32 agc_gain = 22; // 0-30
|
||||
uint32 bpc = 23;
|
||||
uint32 wpc = 24;
|
||||
uint32 raw_gma = 25;
|
||||
uint32 lenc = 26;
|
||||
uint32 hmirror = 27;
|
||||
uint32 vflip = 28;
|
||||
uint32 dcw = 29;
|
||||
uint32 colorbar = 30;
|
||||
}
|
||||
|
||||
message CameraSettingsRequest {}
|
||||
|
||||
// =============================================================================
|
||||
// File System - shared data types
|
||||
// =============================================================================
|
||||
@@ -134,6 +173,8 @@ message Request {
|
||||
FileMkdirRequest file_mkdir_request = 33;
|
||||
WifiSettings wifi_settings = 40;
|
||||
WifiSettingsRequest wifi_settings_request = 41;
|
||||
CameraSettings camera_settings = 50;
|
||||
CameraSettingsRequest camera_settings_request = 51;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,5 +190,6 @@ message Response {
|
||||
ServoSettings servo_settings = 20;
|
||||
FileList file_list = 30;
|
||||
WifiSettings wifi_settings = 40;
|
||||
CameraSettings camera_settings = 50;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -118,5 +118,5 @@ extra_scripts =
|
||||
pre:esp32/scripts/build_app.py
|
||||
lib_compat_mode = strict
|
||||
debug_tool = esp-builtin
|
||||
#debug_init_break = tbreak setup
|
||||
debug_init_break =
|
||||
#upload_port = COM[13] # Only use this when upload port is not correctly detected due to multiple COM ports attached.
|
||||
Reference in New Issue
Block a user