TableControlPage.tsx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. import { useState, useEffect, useRef } from 'react'
  2. import { toast } from 'sonner'
  3. import { Button } from '@/components/ui/button'
  4. import {
  5. Card,
  6. CardContent,
  7. CardDescription,
  8. CardHeader,
  9. CardTitle,
  10. } from '@/components/ui/card'
  11. import { Input } from '@/components/ui/input'
  12. import { Separator } from '@/components/ui/separator'
  13. import { Badge } from '@/components/ui/badge'
  14. import { Alert, AlertDescription } from '@/components/ui/alert'
  15. import {
  16. Dialog,
  17. DialogContent,
  18. DialogDescription,
  19. DialogHeader,
  20. DialogTitle,
  21. DialogTrigger,
  22. DialogFooter,
  23. } from '@/components/ui/dialog'
  24. import {
  25. Tooltip,
  26. TooltipContent,
  27. TooltipProvider,
  28. TooltipTrigger,
  29. } from '@/components/ui/tooltip'
  30. import {
  31. Select,
  32. SelectContent,
  33. SelectItem,
  34. SelectTrigger,
  35. SelectValue,
  36. } from '@/components/ui/select'
  37. import { apiClient } from '@/lib/apiClient'
  38. export function TableControlPage() {
  39. const [speedInput, setSpeedInput] = useState('')
  40. const [currentSpeed, setCurrentSpeed] = useState<number | null>(null)
  41. const [currentTheta, setCurrentTheta] = useState(0)
  42. const [isLoading, setIsLoading] = useState<string | null>(null)
  43. const [isPatternRunning, setIsPatternRunning] = useState(false)
  44. // Serial terminal state
  45. const [serialPorts, setSerialPorts] = useState<string[]>([])
  46. const [selectedSerialPort, setSelectedSerialPort] = useState('')
  47. const [serialConnected, setSerialConnected] = useState(false)
  48. const [serialCommand, setSerialCommand] = useState('')
  49. const [serialHistory, setSerialHistory] = useState<Array<{ type: 'cmd' | 'resp' | 'error'; text: string; time: string }>>([])
  50. const [serialLoading, setSerialLoading] = useState(false)
  51. const serialOutputRef = useRef<HTMLDivElement>(null)
  52. const serialInputRef = useRef<HTMLInputElement>(null)
  53. // Connect to status WebSocket to get current speed and playback status
  54. useEffect(() => {
  55. let ws: WebSocket | null = null
  56. let shouldReconnect = true
  57. const connect = () => {
  58. if (!shouldReconnect) return
  59. // Don't interrupt an existing connection that's still connecting
  60. if (ws) {
  61. if (ws.readyState === WebSocket.CONNECTING) {
  62. return // Already connecting, wait for it
  63. }
  64. if (ws.readyState === WebSocket.OPEN) {
  65. ws.close()
  66. }
  67. ws = null
  68. }
  69. ws = new WebSocket(apiClient.getWebSocketUrl('/ws/status'))
  70. ws.onopen = () => {
  71. if (!shouldReconnect) {
  72. // Component unmounted while connecting - close the WebSocket now
  73. ws?.close()
  74. }
  75. }
  76. ws.onmessage = (event) => {
  77. if (!shouldReconnect) return
  78. try {
  79. const message = JSON.parse(event.data)
  80. if (message.type === 'status_update' && message.data) {
  81. if (message.data.speed !== null && message.data.speed !== undefined) {
  82. setCurrentSpeed(message.data.speed)
  83. }
  84. // Track if a pattern is running or paused
  85. setIsPatternRunning(message.data.is_running || message.data.is_paused)
  86. }
  87. } catch (error) {
  88. console.error('Failed to parse status:', error)
  89. }
  90. }
  91. }
  92. connect()
  93. // Reconnect when table changes
  94. const unsubscribe = apiClient.onBaseUrlChange(() => {
  95. connect()
  96. })
  97. return () => {
  98. shouldReconnect = false
  99. unsubscribe()
  100. if (ws) {
  101. // Only close if already OPEN - CONNECTING WebSockets will close in onopen
  102. if (ws.readyState === WebSocket.OPEN) {
  103. ws.close()
  104. }
  105. ws = null
  106. }
  107. }
  108. }, [])
  109. const handleAction = async (
  110. action: string,
  111. endpoint: string,
  112. body?: object
  113. ) => {
  114. setIsLoading(action)
  115. try {
  116. const data = await apiClient.post<{ success?: boolean; detail?: string }>(endpoint, body)
  117. if (data.success !== false) {
  118. return { success: true, data }
  119. }
  120. throw new Error(data.detail || 'Action failed')
  121. } catch (error) {
  122. console.error(`Error with ${action}:`, error)
  123. throw error
  124. } finally {
  125. setIsLoading(null)
  126. }
  127. }
  128. // Helper to check if pattern is running and show warning
  129. const checkPatternRunning = (actionName: string): boolean => {
  130. if (isPatternRunning) {
  131. toast.error(`Cannot ${actionName} while a pattern is running. Stop the pattern first.`, {
  132. action: {
  133. label: 'Stop',
  134. onClick: () => handleStop(),
  135. },
  136. })
  137. return true
  138. }
  139. return false
  140. }
  141. const handleHome = async () => {
  142. try {
  143. await handleAction('home', '/send_home')
  144. toast.success('Moving to home position...')
  145. } catch {
  146. toast.error('Failed to move to home position')
  147. }
  148. }
  149. const handleStop = async () => {
  150. try {
  151. await handleAction('stop', '/stop_execution')
  152. toast.success('Execution stopped')
  153. } catch {
  154. // Normal stop failed, try force stop
  155. try {
  156. await handleAction('stop', '/force_stop')
  157. toast.success('Force stopped')
  158. } catch {
  159. toast.error('Failed to stop execution')
  160. }
  161. }
  162. }
  163. const handleReset = async () => {
  164. try {
  165. await handleAction('reset', '/soft_reset')
  166. toast.success('Reset sent. Please home the table.')
  167. } catch {
  168. toast.error('Failed to send reset command')
  169. }
  170. }
  171. const handleMoveToCenter = async () => {
  172. if (checkPatternRunning('move to center')) return
  173. try {
  174. await handleAction('center', '/move_to_center')
  175. toast.success('Moving to center...')
  176. } catch {
  177. toast.error('Failed to move to center')
  178. }
  179. }
  180. const handleMoveToPerimeter = async () => {
  181. if (checkPatternRunning('move to perimeter')) return
  182. try {
  183. await handleAction('perimeter', '/move_to_perimeter')
  184. toast.success('Moving to perimeter...')
  185. } catch {
  186. toast.error('Failed to move to perimeter')
  187. }
  188. }
  189. const handleSetSpeed = async () => {
  190. const speed = parseFloat(speedInput)
  191. if (isNaN(speed) || speed <= 0) {
  192. toast.error('Please enter a valid speed value')
  193. return
  194. }
  195. try {
  196. await handleAction('speed', '/set_speed', { speed })
  197. setCurrentSpeed(speed)
  198. toast.success(`Speed set to ${speed} mm/s`)
  199. setSpeedInput('')
  200. } catch {
  201. toast.error('Failed to set speed')
  202. }
  203. }
  204. const handleClearPattern = async (patternFile: string, label: string) => {
  205. try {
  206. await handleAction(patternFile, '/run_theta_rho', {
  207. file_name: patternFile,
  208. pre_execution: 'none',
  209. })
  210. toast.success(`Running ${label}...`)
  211. } catch (error) {
  212. if (error instanceof Error && error.message.includes('409')) {
  213. toast.error('Another pattern is already running')
  214. } else {
  215. toast.error(`Failed to run ${label}`)
  216. }
  217. }
  218. }
  219. const handleRotate = async (degrees: number) => {
  220. if (checkPatternRunning('align')) return
  221. try {
  222. const radians = degrees * (Math.PI / 180)
  223. const newTheta = currentTheta + radians
  224. await handleAction('rotate', '/send_coordinate', { theta: newTheta, rho: 1 })
  225. setCurrentTheta(newTheta)
  226. toast.info(`Rotated ${degrees}°`)
  227. } catch {
  228. toast.error('Failed to rotate')
  229. }
  230. }
  231. // Serial terminal functions
  232. const fetchSerialPorts = async () => {
  233. try {
  234. const data = await apiClient.get<string[]>('/list_serial_ports')
  235. setSerialPorts(Array.isArray(data) ? data : [])
  236. } catch {
  237. toast.error('Failed to fetch serial ports')
  238. }
  239. }
  240. const fetchMainConnectionStatus = async () => {
  241. try {
  242. // Fetch available ports first to validate against
  243. const portsData = await apiClient.get<string[]>('/list_serial_ports')
  244. const availablePorts = Array.isArray(portsData) ? portsData : []
  245. const data = await apiClient.get<{ connected: boolean; port?: string }>('/serial_status')
  246. if (data.connected && data.port) {
  247. // Only set port if it exists in available ports
  248. // This prevents race conditions where stale port data from a different
  249. // backend (e.g., Mac port on a Pi) could be set and auto-connected
  250. if (availablePorts.includes(data.port)) {
  251. setSelectedSerialPort(data.port)
  252. } else {
  253. console.warn(`Port ${data.port} from status not in available ports, ignoring`)
  254. }
  255. }
  256. } catch {
  257. // Ignore errors
  258. }
  259. }
  260. const handleSerialConnect = async (silent = false) => {
  261. if (!selectedSerialPort) {
  262. if (!silent) toast.error('Please select a serial port')
  263. return
  264. }
  265. setSerialLoading(true)
  266. try {
  267. await apiClient.post('/api/debug-serial/open', { port: selectedSerialPort })
  268. setSerialConnected(true)
  269. addSerialHistory('resp', `Connected to ${selectedSerialPort}`)
  270. if (!silent) toast.success(`Connected to ${selectedSerialPort}`)
  271. } catch (error) {
  272. const errorMsg = error instanceof Error ? error.message : 'Unknown error'
  273. addSerialHistory('error', `Failed to connect: ${errorMsg}`)
  274. if (!silent) toast.error('Failed to connect to serial port')
  275. } finally {
  276. setSerialLoading(false)
  277. }
  278. }
  279. const handleSerialDisconnect = async () => {
  280. setSerialLoading(true)
  281. try {
  282. await apiClient.post('/api/debug-serial/close', { port: selectedSerialPort })
  283. setSerialConnected(false)
  284. addSerialHistory('resp', 'Disconnected')
  285. toast.success('Disconnected from serial port')
  286. } catch {
  287. toast.error('Failed to disconnect')
  288. } finally {
  289. setSerialLoading(false)
  290. }
  291. }
  292. const addSerialHistory = (type: 'cmd' | 'resp' | 'error', text: string) => {
  293. const time = new Date().toLocaleTimeString()
  294. setSerialHistory((prev) => [...prev.slice(-200), { type, text, time }])
  295. setTimeout(() => {
  296. if (serialOutputRef.current) {
  297. serialOutputRef.current.scrollTop = serialOutputRef.current.scrollHeight
  298. }
  299. }, 10)
  300. }
  301. const handleSerialSend = async () => {
  302. if (!serialCommand.trim() || !serialConnected || serialLoading) return
  303. const cmd = serialCommand.trim()
  304. setSerialCommand('')
  305. setSerialLoading(true)
  306. addSerialHistory('cmd', cmd)
  307. try {
  308. const data = await apiClient.post<{ responses?: string[]; detail?: string }>('/api/debug-serial/send', { port: selectedSerialPort, command: cmd })
  309. if (data.responses) {
  310. if (data.responses.length > 0) {
  311. data.responses.forEach((line: string) => addSerialHistory('resp', line))
  312. } else {
  313. addSerialHistory('resp', '(no response)')
  314. }
  315. } else if (data.detail) {
  316. addSerialHistory('error', data.detail || 'Command failed')
  317. }
  318. } catch (error) {
  319. addSerialHistory('error', `Error: ${error}`)
  320. } finally {
  321. setSerialLoading(false)
  322. setTimeout(() => serialInputRef.current?.focus(), 0)
  323. }
  324. }
  325. const handleSerialReset = async () => {
  326. if (!serialConnected || serialLoading) return
  327. setSerialLoading(true)
  328. addSerialHistory('cmd', '[Soft Reset]')
  329. try {
  330. // Send soft reset command (backend auto-detects: $Bye for FluidNC, Ctrl+X for GRBL)
  331. const data = await apiClient.post<{ responses?: string[]; detail?: string }>('/api/debug-serial/send', { port: selectedSerialPort, command: '\x18' })
  332. if (data.responses && data.responses.length > 0) {
  333. data.responses.forEach((line: string) => addSerialHistory('resp', line))
  334. } else {
  335. addSerialHistory('resp', 'Reset sent')
  336. }
  337. toast.success('Reset command sent')
  338. } catch (error) {
  339. addSerialHistory('error', `Reset failed: ${error}`)
  340. toast.error('Failed to send reset')
  341. } finally {
  342. setSerialLoading(false)
  343. }
  344. }
  345. const handleSerialKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
  346. if (e.key === 'Enter' && !e.shiftKey) {
  347. e.preventDefault()
  348. if (!serialLoading) {
  349. handleSerialSend()
  350. }
  351. }
  352. }
  353. // Fetch serial ports and main connection status on mount
  354. useEffect(() => {
  355. fetchSerialPorts()
  356. fetchMainConnectionStatus()
  357. }, [])
  358. return (
  359. <TooltipProvider>
  360. <div className="flex flex-col w-full max-w-5xl mx-auto gap-6 py-3 sm:py-6 px-0 sm:px-4">
  361. {/* Page Header */}
  362. <div className="space-y-0.5 sm:space-y-1 pl-1">
  363. <h1 className="text-xl font-semibold tracking-tight">Table Control</h1>
  364. <p className="text-xs text-muted-foreground">
  365. Manual controls for your sand table
  366. </p>
  367. </div>
  368. <Separator />
  369. {/* Main Controls Grid - 2x2 */}
  370. <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
  371. {/* Primary Actions */}
  372. <Card className="transition-all duration-200 hover:shadow-md hover:border-primary/20">
  373. <CardHeader className="pb-3">
  374. <CardTitle className="text-lg">Primary Actions</CardTitle>
  375. <CardDescription>Calibrate or stop the table</CardDescription>
  376. </CardHeader>
  377. <CardContent>
  378. <div className="grid grid-cols-3 gap-3">
  379. <Tooltip>
  380. <TooltipTrigger asChild>
  381. <Button
  382. onClick={handleHome}
  383. disabled={isLoading === 'home'}
  384. variant="primary"
  385. className="h-16 gap-1 flex-col items-center justify-center"
  386. >
  387. {isLoading === 'home' ? (
  388. <span className="material-icons-outlined animate-spin text-2xl">sync</span>
  389. ) : (
  390. <span className="material-icons-outlined text-2xl">home</span>
  391. )}
  392. <span className="text-xs">Home</span>
  393. </Button>
  394. </TooltipTrigger>
  395. <TooltipContent>Return to home position</TooltipContent>
  396. </Tooltip>
  397. <Tooltip>
  398. <TooltipTrigger asChild>
  399. <Button
  400. onClick={handleStop}
  401. disabled={isLoading === 'stop'}
  402. variant="destructive"
  403. className="h-16 gap-1 flex-col items-center justify-center"
  404. >
  405. {isLoading === 'stop' ? (
  406. <span className="material-icons-outlined animate-spin text-2xl">sync</span>
  407. ) : (
  408. <span className="material-icons-outlined text-2xl">stop_circle</span>
  409. )}
  410. <span className="text-xs">Stop</span>
  411. </Button>
  412. </TooltipTrigger>
  413. <TooltipContent>Gracefully stop</TooltipContent>
  414. </Tooltip>
  415. <Dialog>
  416. <Tooltip>
  417. <TooltipTrigger asChild>
  418. <DialogTrigger asChild>
  419. <Button
  420. disabled={isLoading === 'reset'}
  421. variant="secondary"
  422. className="h-16 gap-1 flex-col items-center justify-center"
  423. >
  424. {isLoading === 'reset' ? (
  425. <span className="material-icons-outlined animate-spin text-2xl">sync</span>
  426. ) : (
  427. <span className="material-icons-outlined text-2xl">restart_alt</span>
  428. )}
  429. <span className="text-xs">Reset</span>
  430. </Button>
  431. </DialogTrigger>
  432. </TooltipTrigger>
  433. <TooltipContent>Send soft reset to controller</TooltipContent>
  434. </Tooltip>
  435. <DialogContent className="sm:max-w-md">
  436. <DialogHeader>
  437. <DialogTitle>Reset Controller?</DialogTitle>
  438. <DialogDescription>
  439. This will send a soft reset to the controller.
  440. </DialogDescription>
  441. </DialogHeader>
  442. <Alert className="flex items-center border-amber-500/50">
  443. <span className="material-icons-outlined text-amber-500 text-base mr-2 shrink-0">warning</span>
  444. <AlertDescription className="text-amber-600 dark:text-amber-400">
  445. Homing is required after resetting. The table will lose its position reference.
  446. </AlertDescription>
  447. </Alert>
  448. <DialogFooter className="gap-2 sm:gap-0">
  449. <DialogTrigger asChild>
  450. <Button variant="outline">Cancel</Button>
  451. </DialogTrigger>
  452. <DialogTrigger asChild>
  453. <Button variant="destructive" onClick={handleReset}>
  454. Reset Controller
  455. </Button>
  456. </DialogTrigger>
  457. </DialogFooter>
  458. </DialogContent>
  459. </Dialog>
  460. </div>
  461. </CardContent>
  462. </Card>
  463. {/* Speed Control */}
  464. <Card className="transition-all duration-200 hover:shadow-md hover:border-primary/20">
  465. <CardHeader className="pb-3">
  466. <div className="flex items-center justify-between">
  467. <div>
  468. <CardTitle className="text-lg">Speed</CardTitle>
  469. <CardDescription>Ball movement speed</CardDescription>
  470. </div>
  471. <Badge variant="secondary" className="font-mono">
  472. {currentSpeed !== null ? `${currentSpeed} mm/s` : '-- mm/s'}
  473. </Badge>
  474. </div>
  475. </CardHeader>
  476. <CardContent>
  477. <div className="flex gap-2">
  478. <Input
  479. type="number"
  480. value={speedInput}
  481. onChange={(e) => setSpeedInput(e.target.value)}
  482. placeholder="mm/s"
  483. min="1"
  484. step="1"
  485. className="flex-1"
  486. onKeyDown={(e) => e.key === 'Enter' && handleSetSpeed()}
  487. />
  488. <Button
  489. onClick={handleSetSpeed}
  490. disabled={isLoading === 'speed' || !speedInput}
  491. className="gap-2"
  492. >
  493. {isLoading === 'speed' ? (
  494. <span className="material-icons-outlined animate-spin">sync</span>
  495. ) : (
  496. <span className="material-icons-outlined">check</span>
  497. )}
  498. Set
  499. </Button>
  500. </div>
  501. </CardContent>
  502. </Card>
  503. {/* Position */}
  504. <Card className="transition-all duration-200 hover:shadow-md hover:border-primary/20">
  505. <CardHeader className="pb-3">
  506. <CardTitle className="text-lg">Position</CardTitle>
  507. <CardDescription>Move ball to a specific location</CardDescription>
  508. </CardHeader>
  509. <CardContent>
  510. <div className="grid grid-cols-3 gap-3">
  511. <Tooltip>
  512. <TooltipTrigger asChild>
  513. <Button
  514. onClick={handleMoveToCenter}
  515. disabled={isLoading === 'center'}
  516. variant="secondary"
  517. className="h-16 gap-1 flex-col items-center justify-center"
  518. >
  519. {isLoading === 'center' ? (
  520. <span className="material-icons-outlined animate-spin text-2xl">sync</span>
  521. ) : (
  522. <span className="material-icons-outlined text-2xl">center_focus_strong</span>
  523. )}
  524. <span className="text-xs">Center</span>
  525. </Button>
  526. </TooltipTrigger>
  527. <TooltipContent>Move ball to center</TooltipContent>
  528. </Tooltip>
  529. <Tooltip>
  530. <TooltipTrigger asChild>
  531. <Button
  532. onClick={handleMoveToPerimeter}
  533. disabled={isLoading === 'perimeter'}
  534. variant="secondary"
  535. className="h-16 gap-1 flex-col items-center justify-center"
  536. >
  537. {isLoading === 'perimeter' ? (
  538. <span className="material-icons-outlined animate-spin text-2xl">sync</span>
  539. ) : (
  540. <span className="material-icons-outlined text-2xl">trip_origin</span>
  541. )}
  542. <span className="text-xs">Perimeter</span>
  543. </Button>
  544. </TooltipTrigger>
  545. <TooltipContent>Move ball to edge</TooltipContent>
  546. </Tooltip>
  547. <Dialog>
  548. <Tooltip>
  549. <TooltipTrigger asChild>
  550. <DialogTrigger asChild>
  551. <Button
  552. variant="secondary"
  553. className="h-16 gap-1 flex-col items-center justify-center"
  554. >
  555. <span className="material-icons-outlined text-2xl">screen_rotation</span>
  556. <span className="text-xs">Align</span>
  557. </Button>
  558. </DialogTrigger>
  559. </TooltipTrigger>
  560. <TooltipContent>Align pattern orientation</TooltipContent>
  561. </Tooltip>
  562. <DialogContent className="sm:max-w-md">
  563. <DialogHeader>
  564. <DialogTitle>Pattern Orientation Alignment</DialogTitle>
  565. <DialogDescription>
  566. Follow these steps to align your patterns with their previews
  567. </DialogDescription>
  568. </DialogHeader>
  569. <div className="space-y-4 py-4">
  570. <ol className="space-y-3 text-sm">
  571. {[
  572. 'Home the table then select move to perimeter. Look at your pattern preview and decide where the "bottom" should be.',
  573. 'Manually move the radial arm or use the rotation buttons below to point 90° to the right of where you want the pattern bottom.',
  574. 'Click the "Home" button to establish this as the reference position.',
  575. 'All patterns will now be oriented according to their previews!',
  576. ].map((step, i) => (
  577. <li key={i} className="flex gap-3">
  578. <Badge
  579. variant="secondary"
  580. className="h-6 w-6 shrink-0 items-center justify-center rounded-full p-0"
  581. >
  582. {i + 1}
  583. </Badge>
  584. <span className="text-muted-foreground">{step}</span>
  585. </li>
  586. ))}
  587. </ol>
  588. <Separator />
  589. <Alert className="flex items-start border-amber-500/50">
  590. <span className="material-icons-outlined text-amber-500 text-base mr-2 shrink-0">
  591. warning
  592. </span>
  593. <AlertDescription className="text-amber-600 dark:text-amber-400">
  594. Only perform this when you want to change the orientation reference.
  595. </AlertDescription>
  596. </Alert>
  597. <div className="space-y-3">
  598. <p className="text-sm font-medium text-center">Fine Adjustment</p>
  599. <div className="flex justify-center gap-2">
  600. <Button
  601. variant="secondary"
  602. onClick={() => handleRotate(-10)}
  603. disabled={isLoading === 'rotate'}
  604. >
  605. <span className="material-icons text-lg mr-1">rotate_left</span>
  606. CCW 10°
  607. </Button>
  608. <Button
  609. variant="secondary"
  610. onClick={() => handleRotate(10)}
  611. disabled={isLoading === 'rotate'}
  612. >
  613. CW 10°
  614. <span className="material-icons text-lg ml-1">rotate_right</span>
  615. </Button>
  616. </div>
  617. <p className="text-xs text-muted-foreground text-center">
  618. Each click rotates 10 degrees
  619. </p>
  620. </div>
  621. </div>
  622. <DialogFooter>
  623. <DialogTrigger asChild>
  624. <Button>Got it</Button>
  625. </DialogTrigger>
  626. </DialogFooter>
  627. </DialogContent>
  628. </Dialog>
  629. </div>
  630. </CardContent>
  631. </Card>
  632. {/* Clear Patterns */}
  633. <Card className="transition-all duration-200 hover:shadow-md hover:border-primary/20">
  634. <CardHeader className="pb-3">
  635. <CardTitle className="text-lg">Clear Sand</CardTitle>
  636. <CardDescription>Erase current pattern from the table</CardDescription>
  637. </CardHeader>
  638. <CardContent>
  639. <div className="grid grid-cols-3 gap-3">
  640. <Tooltip>
  641. <TooltipTrigger asChild>
  642. <Button
  643. onClick={() => handleClearPattern('clear_from_in.thr', 'clear from center')}
  644. disabled={isLoading === 'clear_from_in.thr'}
  645. variant="secondary"
  646. className="h-16 gap-1 flex-col items-center justify-center"
  647. >
  648. {isLoading === 'clear_from_in.thr' ? (
  649. <span className="material-icons-outlined animate-spin text-2xl">sync</span>
  650. ) : (
  651. <span className="material-icons-outlined text-2xl">center_focus_strong</span>
  652. )}
  653. <span className="text-xs">Clear Center</span>
  654. </Button>
  655. </TooltipTrigger>
  656. <TooltipContent>Spiral outward from center</TooltipContent>
  657. </Tooltip>
  658. <Tooltip>
  659. <TooltipTrigger asChild>
  660. <Button
  661. onClick={() => handleClearPattern('clear_from_out.thr', 'clear from perimeter')}
  662. disabled={isLoading === 'clear_from_out.thr'}
  663. variant="secondary"
  664. className="h-16 gap-1 flex-col items-center justify-center"
  665. >
  666. {isLoading === 'clear_from_out.thr' ? (
  667. <span className="material-icons-outlined animate-spin text-2xl">sync</span>
  668. ) : (
  669. <span className="material-icons-outlined text-2xl">all_out</span>
  670. )}
  671. <span className="text-xs">Clear Edge</span>
  672. </Button>
  673. </TooltipTrigger>
  674. <TooltipContent>Spiral inward from edge</TooltipContent>
  675. </Tooltip>
  676. <Tooltip>
  677. <TooltipTrigger asChild>
  678. <Button
  679. onClick={() => handleClearPattern('clear_sideway.thr', 'clear sideways')}
  680. disabled={isLoading === 'clear_sideway.thr'}
  681. variant="secondary"
  682. className="h-16 gap-1 flex-col items-center justify-center"
  683. >
  684. {isLoading === 'clear_sideway.thr' ? (
  685. <span className="material-icons-outlined animate-spin text-2xl">sync</span>
  686. ) : (
  687. <span className="material-icons-outlined text-2xl">swap_horiz</span>
  688. )}
  689. <span className="text-xs">Clear Sideways</span>
  690. </Button>
  691. </TooltipTrigger>
  692. <TooltipContent>Clear with side-to-side motion</TooltipContent>
  693. </Tooltip>
  694. </div>
  695. </CardContent>
  696. </Card>
  697. </div>
  698. {/* Serial Terminal */}
  699. <Card className="transition-all duration-200 hover:shadow-md hover:border-primary/20">
  700. <CardHeader className="pb-3 space-y-3">
  701. <div className="flex items-start justify-between gap-2">
  702. <div className="min-w-0 space-y-2">
  703. <CardTitle className="text-lg flex items-center gap-2">
  704. <span className="material-icons-outlined text-xl">terminal</span>
  705. Serial Terminal
  706. </CardTitle>
  707. <CardDescription className="hidden sm:block">Send raw commands to the table controller</CardDescription>
  708. {/* Warning about pattern interference */}
  709. <Alert className="flex items-center border-amber-500/50 py-2">
  710. <span className="material-icons-outlined text-amber-500 text-base mr-2 shrink-0">warning</span>
  711. <AlertDescription className="text-xs text-amber-600 dark:text-amber-400">
  712. Do not use while a pattern is running. This will interfere with the main connection.
  713. </AlertDescription>
  714. </Alert>
  715. </div>
  716. {/* Clear button - only show on desktop in header */}
  717. <div className="hidden sm:flex items-center gap-1">
  718. {serialHistory.length > 0 && (
  719. <Button
  720. variant="ghost"
  721. size="icon"
  722. onClick={() => setSerialHistory([])}
  723. title="Clear history"
  724. >
  725. <span className="material-icons-outlined">delete_sweep</span>
  726. </Button>
  727. )}
  728. </div>
  729. </div>
  730. {/* Controls row - stacks better on mobile */}
  731. <div className="flex flex-wrap items-center gap-2">
  732. {/* Port selector - auto-refreshes on open */}
  733. <Select
  734. value={selectedSerialPort}
  735. onValueChange={setSelectedSerialPort}
  736. onOpenChange={(open) => open && fetchSerialPorts()}
  737. disabled={serialConnected || serialLoading}
  738. >
  739. <SelectTrigger className="h-9 flex-1 min-w-[180px] max-w-[280px]">
  740. <SelectValue placeholder="Select port..." />
  741. </SelectTrigger>
  742. <SelectContent>
  743. {serialPorts.map((port) => (
  744. <SelectItem key={port} value={port}>{port}</SelectItem>
  745. ))}
  746. </SelectContent>
  747. </Select>
  748. {!serialConnected ? (
  749. <Button
  750. size="sm"
  751. onClick={() => handleSerialConnect()}
  752. disabled={!selectedSerialPort || serialLoading}
  753. title="Connect"
  754. >
  755. {serialLoading ? (
  756. <span className="material-icons-outlined animate-spin sm:mr-1">sync</span>
  757. ) : (
  758. <span className="material-icons-outlined sm:mr-1">power</span>
  759. )}
  760. <span className="hidden sm:inline">Connect</span>
  761. </Button>
  762. ) : (
  763. <>
  764. <Button
  765. size="sm"
  766. variant="destructive"
  767. onClick={handleSerialDisconnect}
  768. disabled={serialLoading}
  769. title="Disconnect"
  770. >
  771. <span className="material-icons-outlined sm:mr-1">power_off</span>
  772. <span className="hidden sm:inline">Disconnect</span>
  773. </Button>
  774. <Button
  775. size="sm"
  776. variant="secondary"
  777. onClick={handleSerialReset}
  778. disabled={serialLoading}
  779. title="Send soft reset to controller"
  780. >
  781. <span className="material-icons-outlined sm:mr-1">restart_alt</span>
  782. <span className="hidden sm:inline">Reset</span>
  783. </Button>
  784. </>
  785. )}
  786. {/* Clear button - show on mobile in controls row */}
  787. {serialHistory.length > 0 && (
  788. <Button
  789. variant="ghost"
  790. size="icon"
  791. className="sm:hidden"
  792. onClick={() => setSerialHistory([])}
  793. title="Clear history"
  794. >
  795. <span className="material-icons-outlined">delete</span>
  796. </Button>
  797. )}
  798. </div>
  799. </CardHeader>
  800. <CardContent>
  801. {/* Output area */}
  802. <div
  803. ref={serialOutputRef}
  804. className="bg-black/90 rounded-md p-3 h-48 overflow-y-auto font-mono text-sm mb-3"
  805. >
  806. {serialHistory.length > 0 ? (
  807. serialHistory.map((entry, i) => (
  808. <div
  809. key={i}
  810. className={`${
  811. entry.type === 'cmd'
  812. ? 'text-cyan-400'
  813. : entry.type === 'error'
  814. ? 'text-red-400'
  815. : 'text-green-400'
  816. }`}
  817. >
  818. <span className="text-gray-500 text-xs mr-2">{entry.time}</span>
  819. {entry.type === 'cmd' ? '> ' : ''}
  820. {entry.text}
  821. </div>
  822. ))
  823. ) : (
  824. <div className="text-gray-500 italic">
  825. {serialConnected
  826. ? 'Ready. Enter a command below (e.g., $, $$, ?, $H)'
  827. : 'Connect to a serial port to send commands'}
  828. </div>
  829. )}
  830. </div>
  831. {/* Input area */}
  832. <div className="flex gap-2">
  833. <Input
  834. ref={serialInputRef}
  835. value={serialCommand}
  836. onChange={(e) => setSerialCommand(e.target.value)}
  837. onKeyDown={handleSerialKeyDown}
  838. disabled={!serialConnected}
  839. readOnly={serialLoading}
  840. placeholder={serialConnected ? 'Enter command (e.g., $, $$, ?, $H)' : 'Connect to send commands'}
  841. className="font-mono text-base h-11"
  842. />
  843. <Button
  844. onClick={handleSerialSend}
  845. disabled={!serialConnected || !serialCommand.trim() || serialLoading}
  846. className="h-11 px-6"
  847. >
  848. {serialLoading ? (
  849. <span className="material-icons-outlined animate-spin">sync</span>
  850. ) : (
  851. <span className="material-icons-outlined">send</span>
  852. )}
  853. </Button>
  854. </div>
  855. </CardContent>
  856. </Card>
  857. </div>
  858. </TooltipProvider>
  859. )
  860. }