sync
This commit is contained in:
@@ -10,7 +10,10 @@
|
|||||||
#include <fs.h>
|
#include <fs.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(audio, LOG_LEVEL_INF);
|
#define AUDIO_THREAD_STACK_SIZE 2048
|
||||||
|
#define AUDIO_THREAD_PRIORITY 5
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(audio, LOG_LEVEL_DBG);
|
||||||
|
|
||||||
/* Dauer eines Blocks in ms (4096 Bytes / (16kHz * 2 Kanäle * 2 Bytes)) = 64 ms */
|
/* Dauer eines Blocks in ms (4096 Bytes / (16kHz * 2 Kanäle * 2 Bytes)) = 64 ms */
|
||||||
#define BLOCK_DURATION_MS ((AUDIO_BLOCK_SIZE * 1000) / (AUDIO_SAMPLE_RATE * 2 * (AUDIO_WORD_WIDTH / 8)))
|
#define BLOCK_DURATION_MS ((AUDIO_BLOCK_SIZE * 1000) / (AUDIO_SAMPLE_RATE * 2 * (AUDIO_WORD_WIDTH / 8)))
|
||||||
|
|||||||
@@ -6,8 +6,6 @@
|
|||||||
#define AUDIO_EVENT_STOP BIT(1)
|
#define AUDIO_EVENT_STOP BIT(1)
|
||||||
#define AUDIO_EVENT_SYNC BIT(8)
|
#define AUDIO_EVENT_SYNC BIT(8)
|
||||||
|
|
||||||
#define AUDIO_THREAD_STACK_SIZE 2048
|
|
||||||
#define AUDIO_THREAD_PRIORITY 5
|
|
||||||
#define AUDIO_EVENTS_MASK (AUDIO_EVENT_PLAY | AUDIO_EVENT_STOP | AUDIO_EVENT_SYNC)
|
#define AUDIO_EVENTS_MASK (AUDIO_EVENT_PLAY | AUDIO_EVENT_STOP | AUDIO_EVENT_SYNC)
|
||||||
|
|
||||||
#define AUDIO_BLOCK_SIZE 8192 /* 512 Samples Stereo (16-bit) = 8192 Bytes */
|
#define AUDIO_BLOCK_SIZE 8192 /* 512 Samples Stereo (16-bit) = 8192 Bytes */
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
LOG_MODULE_REGISTER(protocol, LOG_LEVEL_DBG);
|
LOG_MODULE_REGISTER(protocol, LOG_LEVEL_DBG);
|
||||||
|
|
||||||
#define PROTOCOL_STACK_SIZE 2048
|
#define PROTOCOL_STACK_SIZE 2048
|
||||||
#define PROTOCOL_PRIORITY 5
|
#define PROTOCOL_PRIORITY 6
|
||||||
#define BUFFER_SIZE 256
|
#define BUFFER_SIZE 256
|
||||||
|
|
||||||
static uint8_t buffer[BUFFER_SIZE];
|
static uint8_t buffer[BUFFER_SIZE];
|
||||||
|
|||||||
@@ -1,19 +1,49 @@
|
|||||||
// src/lib/buzzerActions.ts
|
|
||||||
import { nan } from 'astro:schema';
|
|
||||||
import { buzzer } from './buzzerStore';
|
import { buzzer } from './buzzerStore';
|
||||||
|
import { get } from 'svelte/store';
|
||||||
|
|
||||||
let activePort: SerialPort | null = null;
|
type Task = {
|
||||||
/**
|
command: string;
|
||||||
* Hilfsfunktion für die Kommunikation
|
priority: number; // 0 = Hintergrund, 1 = User-Aktion
|
||||||
*/
|
resolve: (lines: string[]) => void;
|
||||||
async function sendCommand(port: SerialPort, command: string): Promise<string[]> {
|
key?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
class SerialQueue {
|
||||||
|
private queue: Task[] = [];
|
||||||
|
private isProcessing = false;
|
||||||
|
private port: SerialPort | null = null;
|
||||||
|
|
||||||
|
setPort(port: SerialPort | null) { this.port = port; }
|
||||||
|
|
||||||
|
async add(command: string, priority = 1, key?: string): Promise<string[]> {
|
||||||
|
if (key) {
|
||||||
|
this.queue = this.queue.filter(t => t.key !== key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const task = { command, priority, resolve, key };
|
||||||
|
if (priority === 1) {
|
||||||
|
const lastUserTaskIndex = this.queue.findLastIndex(t => t.priority === 1);
|
||||||
|
this.queue.splice(lastUserTaskIndex + 1, 0, task);
|
||||||
|
} else {
|
||||||
|
this.queue.push(task);
|
||||||
|
}
|
||||||
|
this.process();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async process() {
|
||||||
|
if (this.isProcessing || !this.port || this.queue.length === 0) return;
|
||||||
|
this.isProcessing = true;
|
||||||
|
|
||||||
|
const task = this.queue.shift()!;
|
||||||
|
try {
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
const writer = port.writable.getWriter();
|
const writer = this.port.writable.getWriter();
|
||||||
const reader = port.readable.getReader();
|
const reader = this.port.readable.getReader();
|
||||||
|
|
||||||
try {
|
await writer.write(encoder.encode(task.command + "\n"));
|
||||||
await writer.write(encoder.encode(command + "\n"));
|
|
||||||
writer.releaseLock();
|
writer.releaseLock();
|
||||||
|
|
||||||
let raw = "";
|
let raw = "";
|
||||||
@@ -23,15 +53,29 @@ async function sendCommand(port: SerialPort, command: string): Promise<string[]>
|
|||||||
raw += decoder.decode(value);
|
raw += decoder.decode(value);
|
||||||
if (raw.includes("OK")) break;
|
if (raw.includes("OK")) break;
|
||||||
}
|
}
|
||||||
return raw.split('\n').map(l => l.trim()).filter(l => l && l !== "OK" && !l.startsWith(command));
|
|
||||||
} finally {
|
|
||||||
reader.releaseLock();
|
reader.releaseLock();
|
||||||
|
task.resolve(raw.split('\n').map(l => l.trim()).filter(l => l && l !== "OK" && !l.startsWith(task.command)));
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Queue Error:", e);
|
||||||
|
} finally {
|
||||||
|
this.isProcessing = false;
|
||||||
|
this.process();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WICHTIG: Hier muss "export" stehen!
|
const queue = new SerialQueue();
|
||||||
|
|
||||||
|
// --- EXPORTE ---
|
||||||
|
|
||||||
|
export function setActivePort(port: SerialPort | null) {
|
||||||
|
queue.setPort(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Diese Funktion fehlte im letzten Block!
|
||||||
export async function updateDeviceInfo(port: SerialPort) {
|
export async function updateDeviceInfo(port: SerialPort) {
|
||||||
const lines = await sendCommand(port, "info");
|
// Wir nutzen hier direkt die Queue für Konsistenz
|
||||||
|
const lines = await queue.add("info", 1);
|
||||||
if (lines.length > 0) {
|
if (lines.length > 0) {
|
||||||
const parts = lines[0].split(';');
|
const parts = lines[0].split(';');
|
||||||
if (parts.length >= 6) {
|
if (parts.length >= 6) {
|
||||||
@@ -52,76 +96,53 @@ export async function updateDeviceInfo(port: SerialPort) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WICHTIG: Auch hier "export"
|
export async function refreshFileList() {
|
||||||
export async function refreshFileList(port: SerialPort) {
|
const lines = await queue.add("ls /lfs/a", 1, 'ls');
|
||||||
if (!port) {
|
const audioFiles = lines.map(line => {
|
||||||
if (!activePort) return;
|
|
||||||
port = activePort;
|
|
||||||
}
|
|
||||||
let audioSize = 0;
|
|
||||||
let sysSize = 0;
|
|
||||||
const audioFiles: {name: string, size: string, crc32: number, isSystem: boolean}[] = [];
|
|
||||||
|
|
||||||
async function scanDir(path: string) {
|
|
||||||
const lines = await sendCommand(port, `ls ${path}`);
|
|
||||||
for (const line of lines) {
|
|
||||||
const parts = line.split(',');
|
const parts = line.split(',');
|
||||||
if (parts.length < 3) continue;
|
if (parts.length < 3) return null;
|
||||||
const [type, sizeStr, name] = parts;
|
const [type, size, name] = parts;
|
||||||
const size = parseInt(sizeStr);
|
|
||||||
const fullPath = `${path}/${name}`;
|
|
||||||
|
|
||||||
if (type === 'D') {
|
|
||||||
await scanDir(fullPath);
|
|
||||||
} else if (type === 'F') {
|
|
||||||
if (path.startsWith('/lfs/a')) {
|
|
||||||
audioSize += size;
|
|
||||||
console.log("Audio-Datei gefunden:", name);
|
|
||||||
audioFiles.push({ name, size: (size / 1024).toFixed(1) + " KB", crc32: NaN, isSystem: false, isSynced: false });
|
|
||||||
} else if (path.startsWith('/lfs/sys')) {
|
|
||||||
sysSize += size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await scanDir('/lfs');
|
|
||||||
|
|
||||||
buzzer.update(s => {
|
|
||||||
const bytesToMB = 1024 * 1024;
|
|
||||||
const usedMB = s.storage.total - s.storage.available;
|
|
||||||
const audioMB = audioSize / bytesToMB;
|
|
||||||
const sysMB = sysSize / bytesToMB;
|
|
||||||
const unknownMB = usedMB - audioMB - sysMB;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...s,
|
name,
|
||||||
files: audioFiles,
|
size: (parseInt(size) / 1024).toFixed(1) + " KB",
|
||||||
storage: { ...s.storage, usedAudio: audioMB, usedSys: sysMB, unknown: unknownMB > 0 ? unknownMB : 0 }
|
crc32: 0,
|
||||||
|
isSystem: false
|
||||||
};
|
};
|
||||||
});
|
}).filter(f => f !== null) as any[];
|
||||||
|
|
||||||
|
buzzer.update(s => ({ ...s, files: audioFiles }));
|
||||||
|
startBackgroundCrcCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setActivePort(port: SerialPort) {
|
async function startBackgroundCrcCheck() {
|
||||||
activePort = port;
|
const currentFiles = get(buzzer).files;
|
||||||
|
for (const file of currentFiles) {
|
||||||
|
if (!file.crc32) {
|
||||||
|
const response = await queue.add(`check /lfs/a/${file.name}`, 0);
|
||||||
|
if (response.length > 0) {
|
||||||
|
const match = response[0].match(/0x([0-9a-fA-F]+)/);
|
||||||
|
if (match) {
|
||||||
|
const crc = parseInt(match[1], 16);
|
||||||
|
updateFileCrc(file.name, crc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateFileCrc(name: string, crc: number) {
|
||||||
|
buzzer.update(s => ({
|
||||||
|
...s,
|
||||||
|
files: s.files.map(f => f.name === name ? { ...f, crc32: crc } : f)
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function playFile(filename: string) {
|
export async function playFile(filename: string) {
|
||||||
if (!activePort) {
|
return queue.add(`play /lfs/a/${filename}`, 1, 'play');
|
||||||
console.warn("Kein aktiver Port zum Abspielen der Datei.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
console.log(`Starte Wiedergabe: ${filename}`);
|
|
||||||
// Kommando: play /lfs/a/filename
|
|
||||||
await sendCommand(activePort, `play /lfs/a/${filename}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteFile(filename: string) {
|
export async function deleteFile(filename: string) {
|
||||||
if (!activePort || !confirm(`Datei ${filename} wirklich löschen?`)) return;
|
if (!confirm(`Datei ${filename} wirklich löschen?`)) return;
|
||||||
|
await queue.add(`rm /lfs/a/${filename}`, 1);
|
||||||
// Kommando: rm /lfs/a/filename
|
await refreshFileList();
|
||||||
await sendCommand(activePort, `rm /lfs/a/${filename}`);
|
|
||||||
|
|
||||||
// Liste nach dem Löschen aktualisieren
|
|
||||||
await refreshFileList(activePort);
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user