Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions arduino/src/json_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ void sendJsonPong() {
cfg["maxSpeed"] = maxSpeedKmH;
cfg["sensorDistance"] = sensorDistance;
cfg["debugEnabled"] = debugEnabled;
cfg["operatingMode"] = (currentOpMode == OperatingMode::ALIGNMENT) ? "alignment"
: (currentOpMode == OperatingMode::LAPTIMER) ? "laptimer" : "speedcamera";
LapTimer::populatePongConfig(cfg);
Alignment::populatePongConfig(cfg);

String out;
serializeJson(doc, out);
out += '\n';
Expand Down
24 changes: 18 additions & 6 deletions arduino/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@ void setup() {

// ─── Main Loop ───────────────────────────────────────────────────────────────
void loop() {
LapTimer::update();
Alignment::update();

// Process incoming serial commands every iteration
// Process incoming serial commands first every iteration
handleSerial();
if (stringComplete) {
decodeJson(inputString);
}

// ponytail: mutually exclusive execution based on operating mode state machine
if (currentOpMode == OperatingMode::ALIGNMENT) {
Alignment::update();
yield();
return; // Do not process speed passes while actively aligning light barriers
}

if (currentOpMode == OperatingMode::LAPTIMER) {
LapTimer::update();
}

// ── IDLE: wait for either sensor to trigger ──────────────────────────────
if (measState == MeasurementState::IDLE) {
if (millis() - lastMeasurementEndMs < (unsigned long)cooldownMs) {
Expand All @@ -43,6 +51,7 @@ void loop() {
}

if (digitalRead(sensor1) == HIGH) {

firstTriggerUs = micros();
firstSensor = FirstSensor::SENSOR_ONE;
measurementStartMs = millis();
Expand Down Expand Up @@ -118,8 +127,11 @@ void loop() {
sendJsonStatus("legal", speedInKmH, speedTolerance, direction);
}

// ponytail: notify active modules with zero heap allocation or dynamic dispatch
LapTimer::onMeasurement(event);
// ponytail: notify lap timer ONLY if operating in LAPTIMER mode
if (currentOpMode == OperatingMode::LAPTIMER) {
LapTimer::onMeasurement(event);
}


} else {
sendJsonStatus("timeout"); // Speed out of valid range — discard
Expand Down
47 changes: 42 additions & 5 deletions arduino/src/serial_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,51 @@ void handleCommand(JsonDocument &doc) {
} else if (strcmp(command, "getCapabilities") == 0) {
sendJsonCapabilities();

// ponytail: route module-specific commands without bloated registry abstractions
} else if (LapTimer::handleCommand(command, doc)) {
return;
} else if (strcmp(command, "setMode") == 0) {
const char *modeStr = doc["mode"] | "speedcamera";
if (strcmp(modeStr, "alignment") == 0) {
LapTimer::reset();
currentOpMode = OperatingMode::ALIGNMENT;
resetMeasurement();
Alignment::handleCommand("startAlignment", doc);
} else if (strcmp(modeStr, "laptimer") == 0) {
Alignment::reset();
currentOpMode = OperatingMode::LAPTIMER;
resetMeasurement();
LapTimer::handleCommand("startLapSession", doc);
} else {
Alignment::reset();
LapTimer::reset();
currentOpMode = OperatingMode::SPEEDCAMERA;
resetMeasurement();
sendJsonStatus("modeSpeedcamera");
}

} else if (Alignment::handleCommand(command, doc)) {
return;
// ponytail: route module commands with strict operating mode mutual exclusion
} else if (strcmp(command, "startAlignment") == 0) {
LapTimer::reset();
currentOpMode = OperatingMode::ALIGNMENT;
resetMeasurement();
Alignment::handleCommand(command, doc);

} else if (strcmp(command, "stopAlignment") == 0) {
Alignment::handleCommand(command, doc);
currentOpMode = OperatingMode::SPEEDCAMERA;
resetMeasurement();

} else if (strcmp(command, "startLapSession") == 0) {
Alignment::reset();
currentOpMode = OperatingMode::LAPTIMER;
resetMeasurement();
LapTimer::handleCommand(command, doc);

} else if (strcmp(command, "stopLapSession") == 0) {
LapTimer::handleCommand(command, doc);
currentOpMode = OperatingMode::SPEEDCAMERA;
resetMeasurement();

} else {
sendJsonStatus("jsonError");
}
}

5 changes: 5 additions & 0 deletions arduino/src/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ unsigned long lastMeasurementEndMs = 0;
// ─── Speed Measurement Results ────────────────────────────────────────────────
float speedInKmH = 0.0f;
float speedTolerance = 0.0f;

// ─── Operating Mode State ─────────────────────────────────────────────────────
// ponytail: default to SPEEDCAMERA, mutually exclusive with LAPTIMER and ALIGNMENT
OperatingMode currentOpMode = OperatingMode::SPEEDCAMERA;

6 changes: 6 additions & 0 deletions arduino/src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ extern unsigned long lastMeasurementEndMs;
// ─── Speed Measurement Results ────────────────────────────────────────────────
extern float speedInKmH;
extern float speedTolerance;

// ─── Operating Mode State ─────────────────────────────────────────────────────
extern OperatingMode currentOpMode;
void resetMeasurement();


4 changes: 4 additions & 0 deletions arduino/src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
enum class MeasurementState : uint8_t { IDLE, WAITING_FOR_SECOND };
enum class FirstSensor : uint8_t { NONE, SENSOR_ONE, SENSOR_TWO };

// ─── Operating Mode State Machine ─────────────────────────────────────────────
enum class OperatingMode : uint8_t { SPEEDCAMERA, LAPTIMER, ALIGNMENT };

// ─── Measurement Event ────────────────────────────────────────────────────────
// Emitted when a passing vehicle triggers both sensors within valid window.
struct MeasurementEvent {
Expand All @@ -15,3 +18,4 @@ struct MeasurementEvent {
const char *direction; // "forward" | "reverse"
unsigned long boundaryUs; // µs timestamp of first beam break
};

59 changes: 47 additions & 12 deletions headless-rust/frontend/src/components/MobileMenuSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,35 @@ const CORE_NAV_LINKS = [
{ label: "About & Updates", to: "/about", icon: Info },
] as const;

const MODE_META: Record<string, { label: string; badgeClass: string }> = {
speedcamera: {
label: "Speed Camera",
badgeClass: "bg-blue-500/10 text-blue-400 border-blue-500/25",
},
laptimer: {
label: "Lap Timer",
badgeClass: "bg-emerald-500/10 text-emerald-400 border-emerald-500/25",
},
alignment: {
label: "Alignment",
badgeClass: "bg-amber-500/10 text-amber-400 border-amber-500/25",
},
setup: {
label: "Setup",
badgeClass: "bg-purple-500/10 text-purple-400 border-purple-500/25",
},
};

export function MobileMenuSheet({ open, onOpenChange }: MobileMenuSheetProps) {
const connectedPort = useAppStore((s) => s.connectedPort);
const cameraConnected = useAppStore((s) => s.cameraConnected);
const systemState = useAppStore((s) => s.systemState);
const operatingMode = useAppStore((s) => s.operatingMode);

const currentModeMeta = MODE_META[operatingMode] ?? {
label: operatingMode,
badgeClass: "bg-muted text-muted-foreground border-border",
};

const pluginNavItems = pluginRegistry.getNavItems();

Expand All @@ -54,18 +79,28 @@ export function MobileMenuSheet({ open, onOpenChange }: MobileMenuSheetProps) {
<SheetHeader className="p-4 border-b border-border bg-muted/20">
<div className="flex items-center justify-between pr-8">
<SheetTitle className="text-base font-bold tracking-tight">Speedcamera</SheetTitle>
<span
className={cn(
"text-[10px] font-bold uppercase tracking-wider px-2 py-0.5 rounded-full border",
systemState === "ARMED"
? "bg-red-500/20 text-red-400 border-red-500/40"
: systemState === "DISARMED"
? "bg-amber-500/15 text-amber-400 border-amber-500/30"
: "bg-muted text-muted-foreground border-border"
)}
>
{systemState}
</span>
<div className="flex items-center gap-1.5">
<span
className={cn(
"text-[10px] font-semibold px-2 py-0.5 rounded-full border",
currentModeMeta.badgeClass
)}
>
{currentModeMeta.label}
</span>
<span
className={cn(
"text-[10px] font-bold uppercase tracking-wider px-2 py-0.5 rounded-full border",
systemState === "ARMED"
? "bg-red-500/20 text-red-400 border-red-500/40"
: systemState === "DISARMED"
? "bg-amber-500/15 text-amber-400 border-amber-500/30"
: "bg-muted text-muted-foreground border-border"
)}
>
{systemState}
</span>
</div>
</div>
<SheetDescription className="sr-only">Mobile Navigation Menu</SheetDescription>

Expand Down
86 changes: 86 additions & 0 deletions headless-rust/frontend/src/components/ModeChangeGuardDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { HugeiconsIcon } from "@hugeicons/react";
import { Alert02Icon, SecurityCheckIcon } from "@hugeicons/core-free-icons";

interface ModeChangeGuardDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
targetMode: string;
onConfirm: () => void;
onCancel: () => void;
message?: string;
isArmed?: boolean;
}

export function ModeChangeGuardDialog({
open,
onOpenChange,
targetMode,
onConfirm,
onCancel,
message,
isArmed = true,
}: ModeChangeGuardDialogProps) {
const modeLabels: Record<string, string> = {
speedcamera: "Speed Camera",
laptimer: "Lap Timer",
alignment: "Sensor Alignment",
setup: "Setup Wizard",
};

const targetLabel = modeLabels[targetMode] || targetMode;

return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader className="gap-2">
<div className="flex items-center gap-3">
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-amber-500/15 text-amber-600 dark:text-amber-400">
<HugeiconsIcon icon={isArmed ? SecurityCheckIcon : Alert02Icon} size={22} />
</div>
<div>
<DialogTitle className="text-base font-bold">
{isArmed ? "System Is Currently Armed" : "Safeguard Confirmation"}
</DialogTitle>
<DialogDescription className="text-xs text-muted-foreground mt-0.5">
Mode transition safeguard triggered
</DialogDescription>
</div>
</div>
</DialogHeader>

<div className="py-2 text-sm text-foreground/90 space-y-2">
<p>
{message ||
`The speed camera is currently active and armed. To switch to ${targetLabel}, the system must disarm.`}
</p>
<p className="text-xs text-muted-foreground">
Violations will not be captured while in {targetLabel} mode.
</p>
</div>

<DialogFooter className="gap-2 sm:gap-0">
<Button variant="outline" size="sm" onClick={onCancel}>
Abort Switch
</Button>
<Button
variant="destructive"
size="sm"
onClick={onConfirm}
className="font-medium"
>
{isArmed ? "Disarm & Continue" : "Confirm Switch"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
36 changes: 35 additions & 1 deletion headless-rust/frontend/src/components/NavigationMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Link } from "@tanstack/react-router";
import { cn } from "@/lib/utils";
import { pluginRegistry } from "@/plugins";
import { useAppStore } from "@/stores/useAppStore";
import { ConnectionIndicator } from "./ConnectionIndicator";

const CORE_NAV_ITEMS = [
Expand All @@ -12,12 +13,36 @@ const CORE_NAV_ITEMS = [
{ label: "About", to: "/about" },
] as const;

const MODE_META: Record<string, { label: string; badgeClass: string }> = {
speedcamera: {
label: "Speed Camera",
badgeClass: "bg-blue-500/10 text-blue-400 border-blue-500/25",
},
laptimer: {
label: "Lap Timer",
badgeClass: "bg-emerald-500/10 text-emerald-400 border-emerald-500/25",
},
alignment: {
label: "Alignment",
badgeClass: "bg-amber-500/10 text-amber-400 border-amber-500/25",
},
setup: {
label: "Setup",
badgeClass: "bg-purple-500/10 text-purple-400 border-purple-500/25",
},
};

interface NavigationMenuProps {
className?: string;
}

export function NavigationMenu({ className }: NavigationMenuProps) {
const pluginNavItems = pluginRegistry.getNavItems();
const operatingMode = useAppStore((s) => s.operatingMode);
const currentModeMeta = MODE_META[operatingMode] ?? {
label: operatingMode,
badgeClass: "bg-muted text-muted-foreground border-border",
};

const allNavItems = [
CORE_NAV_ITEMS[0], // Home
Expand All @@ -41,7 +66,16 @@ export function NavigationMenu({ className }: NavigationMenuProps) {
</Link>
))}

<div className="ml-auto flex items-center">
<div className="ml-auto flex items-center gap-2">
<span
className={cn(
"text-xs px-2.5 py-0.5 rounded-full border font-medium transition-colors",
currentModeMeta.badgeClass
)}
title={`Active Operating Mode: ${currentModeMeta.label}`}
>
{currentModeMeta.label}
</span>
<ConnectionIndicator />
</div>
</nav>
Expand Down
Loading