index.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Sand Table Controller</title>
  7. <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
  8. <link rel="stylesheet" href="../static/style.css">
  9. </head>
  10. <body>
  11. <h1>Sand Table Controller</h1>
  12. <div class="container">
  13. <!-- Left Column -->
  14. <div class="left-column">
  15. <div class="section">
  16. <h2>Serial Connection</h2>
  17. <label for="serial_ports">Available Ports:</label>
  18. <select id="serial_ports"></select>
  19. <div class="button-group">
  20. <button onclick="connectSerial()">Connect</button>
  21. <button onclick="disconnectSerial()">Disconnect</button>
  22. <button onclick="restartSerial()">Restart</button>
  23. </div>
  24. <p id="serial_status" class="status">Status: Not connected</p>
  25. <input type="number" id="speed_input" placeholder="Set speed" min="1" step="1">
  26. <button onclick="changeSpeed()">Set Speed</button>
  27. </div>
  28. <div class="section">
  29. <h2>Quick Actions</h2>
  30. <div class="button-group">
  31. <button onclick="sendHomeCommand()">Home Device</button>
  32. <button onclick="moveToCenter()">Move to Center</button>
  33. <button onclick="moveToPerimeter()">Move to Perimeter</button>
  34. </br>
  35. <button onclick="runClearIn()">Clear from In</button>
  36. <button onclick="runClearOut()">Clear from Out</button>
  37. <div class="coordinate-input">
  38. <label for="theta_input">θ:</label>
  39. <input type="number" id="theta_input" placeholder="Theta">
  40. <label for="rho_input">ρ:</label>
  41. <input type="number" id="rho_input" placeholder="Rho">
  42. <button onclick="sendCoordinate()">Send</button>
  43. </div>
  44. </div>
  45. </div>
  46. <div class="section">
  47. <h2>Preview</h2>
  48. <canvas id="patternPreviewCanvas" style="width: 100%; height: 100%;"></canvas>
  49. <p id="first_coordinate">First Coordinate: Not available</p>
  50. <p id="last_coordinate">Last Coordinate: Not available</p>
  51. </div>
  52. </div>
  53. <!-- Right Column -->
  54. <div class="right-column">
  55. <div class="section">
  56. <h2>Pattern Files</h2>
  57. <input type="text" id="search_pattern" placeholder="Search files..." oninput="searchPatternFiles()">
  58. <ul id="theta_rho_files"></ul>
  59. <div class="pre-execution-toggles">
  60. <h3>Pre-Execution Action</h3>
  61. <label>
  62. <input type="radio" name="pre_execution" value="clear_in" id="clear_in"> Clear from In
  63. </label>
  64. <label>
  65. <input type="radio" name="pre_execution" value="clear_out" id="clear_out"> Clear from Out
  66. </label>
  67. <label>
  68. <input type="radio" name="pre_execution" value="none" id="no_action" checked> None
  69. </label>
  70. </div>
  71. <div class="button-group">
  72. <button id="run_button" disabled>Run Selected File</button>
  73. <button onclick="stopExecution()" class="delete-button">Stop</button>
  74. </div>
  75. </div>
  76. <div class="section">
  77. <h2>Upload new files</h2>
  78. <div class="button-group">
  79. <input type="file" id="upload_file">
  80. <div class="button-group">
  81. <button onclick="uploadThetaRho()">Upload</button>
  82. <button id="delete_selected_button" class="delete-button" onclick="deleteSelectedFile()" disabled>Delete Selected File</button>
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. <div id="status_log">
  90. <h2>Status Log</h2>
  91. <!-- Messages will be appended here -->
  92. </div>
  93. <script>
  94. let selectedFile = null;
  95. function logMessage(message) {
  96. const log = document.getElementById('status_log');
  97. const entry = document.createElement('p');
  98. entry.textContent = message;
  99. log.appendChild(entry);
  100. log.scrollTop = log.scrollHeight; // Keep log scrolled to the bottom
  101. }
  102. async function loadThetaRhoFiles() {
  103. logMessage('Loading Theta-Rho files...');
  104. const response = await fetch('/list_theta_rho_files');
  105. const files = await response.json();
  106. const ul = document.getElementById('theta_rho_files');
  107. ul.innerHTML = ''; // Clear current list
  108. files.forEach(file => {
  109. const li = document.createElement('li');
  110. li.textContent = file;
  111. // Highlight the selected file when clicked
  112. li.onclick = () => selectFile(file, li);
  113. ul.appendChild(li);
  114. });
  115. logMessage('Theta-Rho files loaded successfully.');
  116. }
  117. async function selectFile(file, listItem) {
  118. selectedFile = file;
  119. // Highlight the selected file
  120. document.querySelectorAll('#theta_rho_files li').forEach(li => li.classList.remove('selected'));
  121. listItem.classList.add('selected');
  122. // Enable buttons
  123. document.getElementById('run_button').disabled = false;
  124. document.getElementById('delete_selected_button').disabled = false;
  125. logMessage(`Selected file: ${file}`);
  126. // Fetch and preview the selected file
  127. await previewPattern(file);
  128. }
  129. async function uploadThetaRho() {
  130. const fileInput = document.getElementById('upload_file');
  131. const file = fileInput.files[0];
  132. if (!file) {
  133. logMessage('No file selected for upload.');
  134. return;
  135. }
  136. logMessage(`Uploading file: ${file.name}...`);
  137. const formData = new FormData();
  138. formData.append('file', file);
  139. const response = await fetch('/upload_theta_rho', {
  140. method: 'POST',
  141. body: formData
  142. });
  143. const result = await response.json();
  144. if (result.success) {
  145. logMessage(`File uploaded successfully: ${file.name}`);
  146. loadThetaRhoFiles();
  147. } else {
  148. logMessage(`Failed to upload file: ${file.name}`);
  149. }
  150. }
  151. async function deleteSelectedFile() {
  152. if (!selectedFile) {
  153. logMessage("No file selected for deletion.");
  154. return;
  155. }
  156. const userConfirmed = confirm(`Are you sure you want to delete the selected file "${selectedFile}"?`);
  157. if (!userConfirmed) return;
  158. logMessage(`Deleting file: ${selectedFile}...`);
  159. const response = await fetch('/delete_theta_rho_file', {
  160. method: 'POST',
  161. headers: { 'Content-Type': 'application/json' },
  162. body: JSON.stringify({ file_name: selectedFile }),
  163. });
  164. const result = await response.json();
  165. if (result.success) {
  166. const ul = document.getElementById('theta_rho_files');
  167. const selectedItem = Array.from(ul.children).find(li => li.classList.contains('selected'));
  168. if (selectedItem) selectedItem.remove();
  169. selectedFile = null;
  170. document.getElementById('run_button').disabled = true;
  171. document.getElementById('delete_selected_button').disabled = true;
  172. logMessage(`File deleted successfully: ${result.file_name}`);
  173. } else {
  174. logMessage(`Failed to delete file: ${selectedFile}`);
  175. }
  176. }
  177. async function runThetaRho() {
  178. if (!selectedFile) {
  179. logMessage("No file selected to run.");
  180. return;
  181. }
  182. // Get the selected pre-execution action
  183. const preExecutionAction = document.querySelector('input[name="pre_execution"]:checked').value;
  184. logMessage(`Running file: ${selectedFile} with pre-execution action: ${preExecutionAction}...`);
  185. const response = await fetch('/run_theta_rho', {
  186. method: 'POST',
  187. headers: { 'Content-Type': 'application/json' },
  188. body: JSON.stringify({ file_name: selectedFile, pre_execution: preExecutionAction })
  189. });
  190. const result = await response.json();
  191. if (result.success) {
  192. logMessage(`File running: ${selectedFile}`);
  193. } else {
  194. logMessage(`Failed to run file: ${selectedFile}`);
  195. }
  196. }
  197. async function stopExecution() {
  198. logMessage('Stopping execution...');
  199. const response = await fetch('/stop_execution', { method: 'POST' });
  200. const result = await response.json();
  201. if (result.success) {
  202. logMessage('Execution stopped.');
  203. } else {
  204. logMessage('Failed to stop execution.');
  205. }
  206. }
  207. async function loadSerialPorts() {
  208. const response = await fetch('/list_serial_ports');
  209. const ports = await response.json();
  210. const select = document.getElementById('serial_ports');
  211. select.innerHTML = '';
  212. ports.forEach(port => {
  213. const option = document.createElement('option');
  214. option.value = port;
  215. option.textContent = port;
  216. select.appendChild(option);
  217. });
  218. logMessage('Serial ports loaded.');
  219. }
  220. async function connectSerial() {
  221. const port = document.getElementById('serial_ports').value;
  222. const response = await fetch('/connect_serial', {
  223. method: 'POST',
  224. headers: { 'Content-Type': 'application/json' },
  225. body: JSON.stringify({ port })
  226. });
  227. const result = await response.json();
  228. if (result.success) {
  229. document.getElementById('serial_status').textContent = `Status: Connected to ${port}`;
  230. logMessage(`Connected to serial port: ${port}`);
  231. } else {
  232. logMessage(`Error connecting to serial port: ${result.error}`);
  233. }
  234. }
  235. async function disconnectSerial() {
  236. const response = await fetch('/disconnect_serial', { method: 'POST' });
  237. const result = await response.json();
  238. if (result.success) {
  239. document.getElementById('serial_status').textContent = 'Status: Disconnected';
  240. logMessage('Serial port disconnected.');
  241. } else {
  242. logMessage(`Error disconnecting: ${result.error}`);
  243. }
  244. }
  245. async function restartSerial() {
  246. const port = document.getElementById('serial_ports').value;
  247. const response = await fetch('/restart_serial', {
  248. method: 'POST',
  249. headers: { 'Content-Type': 'application/json' },
  250. body: JSON.stringify({ port })
  251. });
  252. const result = await response.json();
  253. if (result.success) {
  254. document.getElementById('serial_status').textContent = `Status: Restarted connection to ${port}`;
  255. logMessage('Serial connection restarted.');
  256. } else {
  257. logMessage(`Error restarting serial connection: ${result.error}`);
  258. }
  259. }
  260. async function sendHomeCommand() {
  261. const response = await fetch('/send_home', { method: 'POST' });
  262. const result = await response.json();
  263. if (result.success) {
  264. logMessage('HOME command sent successfully.');
  265. } else {
  266. logMessage('Failed to send HOME command.');
  267. }
  268. }
  269. async function runClearIn() {
  270. await runFile('clear_from_in.thr');
  271. }
  272. async function runClearOut() {
  273. await runFile('clear_from_out.thr');
  274. }
  275. async function runFile(fileName) {
  276. const response = await fetch(`/run_theta_rho_file/${fileName}`, { method: 'POST' });
  277. const result = await response.json();
  278. if (result.success) {
  279. logMessage(`Running file: ${fileName}`);
  280. } else {
  281. logMessage(`Failed to run file: ${fileName}`);
  282. }
  283. }
  284. let allFiles = []; // Store all files for filtering
  285. async function loadThetaRhoFiles() {
  286. logMessage('Loading Theta-Rho files...');
  287. const response = await fetch('/list_theta_rho_files');
  288. const files = await response.json();
  289. allFiles = files; // Store the full list of files
  290. displayFiles(allFiles); // Initially display all files
  291. logMessage('Theta-Rho files loaded successfully.');
  292. }
  293. function displayFiles(files) {
  294. const ul = document.getElementById('theta_rho_files');
  295. ul.innerHTML = ''; // Clear current list
  296. files.forEach(file => {
  297. const li = document.createElement('li');
  298. li.textContent = file;
  299. // Highlight the selected file when clicked
  300. li.onclick = () => selectFile(file, li);
  301. ul.appendChild(li);
  302. });
  303. }
  304. function searchPatternFiles() {
  305. const searchInput = document.getElementById('search_pattern').value.toLowerCase();
  306. const filteredFiles = allFiles.filter(file => file.toLowerCase().includes(searchInput));
  307. displayFiles(filteredFiles); // Display only matching files
  308. }
  309. async function moveToCenter() {
  310. logMessage('Moving to center...');
  311. const response = await fetch('/move_to_center', { method: 'POST' });
  312. const result = await response.json();
  313. if (result.success) {
  314. logMessage('Moved to center successfully.');
  315. } else {
  316. logMessage(`Failed to move to center: ${result.error}`);
  317. }
  318. }
  319. async function moveToPerimeter() {
  320. logMessage('Moving to perimeter...');
  321. const response = await fetch('/move_to_perimeter', { method: 'POST' });
  322. const result = await response.json();
  323. if (result.success) {
  324. logMessage('Moved to perimeter successfully.');
  325. } else {
  326. logMessage(`Failed to move to perimeter: ${result.error}`);
  327. }
  328. }
  329. async function sendCoordinate() {
  330. const theta = parseFloat(document.getElementById('theta_input').value);
  331. const rho = parseFloat(document.getElementById('rho_input').value);
  332. if (isNaN(theta) || isNaN(rho)) {
  333. logMessage('Invalid input: θ and ρ must be numbers.');
  334. return;
  335. }
  336. logMessage(`Sending coordinate: θ=${theta}, ρ=${rho}...`);
  337. const response = await fetch('/send_coordinate', {
  338. method: 'POST',
  339. headers: { 'Content-Type': 'application/json' },
  340. body: JSON.stringify({ theta, rho })
  341. });
  342. const result = await response.json();
  343. if (result.success) {
  344. logMessage(`Coordinate executed successfully: θ=${theta}, ρ=${rho}`);
  345. } else {
  346. logMessage(`Failed to execute coordinate: ${result.error}`);
  347. }
  348. }
  349. async function previewPattern(fileName) {
  350. logMessage(`Fetching data to preview file: ${fileName}...`);
  351. const response = await fetch('/preview_thr', {
  352. method: 'POST',
  353. headers: { 'Content-Type': 'application/json' },
  354. body: JSON.stringify({ file_name: fileName })
  355. });
  356. const result = await response.json();
  357. if (result.success) {
  358. const coordinates = result.coordinates;
  359. // Update coordinates display
  360. if (coordinates.length > 0) {
  361. const firstCoord = coordinates[0];
  362. const lastCoord = coordinates[coordinates.length - 1];
  363. document.getElementById('first_coordinate').textContent = `First Coordinate: θ=${firstCoord[0]}, ρ=${firstCoord[1]}`;
  364. document.getElementById('last_coordinate').textContent = `Last Coordinate: θ=${lastCoord[0]}, ρ=${lastCoord[1]}`;
  365. } else {
  366. document.getElementById('first_coordinate').textContent = 'First Coordinate: Not available';
  367. document.getElementById('last_coordinate').textContent = 'Last Coordinate: Not available';
  368. }
  369. renderPattern(coordinates);
  370. } else {
  371. logMessage(`Failed to fetch preview for file: ${result.error}`);
  372. // Clear the coordinate display on error
  373. document.getElementById('first_coordinate').textContent = 'First Coordinate: Not available';
  374. document.getElementById('last_coordinate').textContent = 'Last Coordinate: Not available';
  375. }
  376. }
  377. function renderPattern(coordinates) {
  378. const canvas = document.getElementById('patternPreviewCanvas');
  379. const ctx = canvas.getContext('2d');
  380. // Make canvas full screen
  381. canvas.width = window.innerWidth;
  382. canvas.height = window.innerHeight;
  383. // Clear the canvas
  384. ctx.clearRect(0, 0, canvas.width, canvas.height);
  385. // Convert polar to Cartesian and draw the pattern
  386. const centerX = canvas.width / 2;
  387. const centerY = canvas.height / 2;
  388. const maxRho = Math.max(...coordinates.map(coord => coord[1]));
  389. const scale = Math.min(canvas.width, canvas.height) / (2 * maxRho); // Scale to fit within the screen
  390. ctx.beginPath();
  391. coordinates.forEach(([theta, rho], index) => {
  392. const x = centerX + rho * Math.cos(theta) * scale;
  393. const y = centerY - rho * Math.sin(theta) * scale; // Invert y-axis for canvas
  394. if (index === 0) {
  395. ctx.moveTo(x, y);
  396. } else {
  397. ctx.lineTo(x, y);
  398. }
  399. });
  400. ctx.closePath();
  401. ctx.stroke();
  402. logMessage('Pattern preview rendered at full screen.');
  403. }
  404. async function checkSerialStatus() {
  405. const response = await fetch('/serial_status');
  406. const status = await response.json();
  407. const statusElement = document.getElementById('serial_status');
  408. if (status.connected) {
  409. const port = status.port || 'Unknown'; // Fallback if port is undefined
  410. statusElement.textContent = `Status: Connected to ${port}`;
  411. logMessage(`Reconnected to serial port: ${port}`);
  412. } else {
  413. statusElement.textContent = 'Status: Not connected';
  414. logMessage('No active serial connection.');
  415. }
  416. }
  417. async function changeSpeed() {
  418. const speedInput = document.getElementById('speed_input');
  419. const speed = parseFloat(speedInput.value);
  420. if (isNaN(speed) || speed <= 0) {
  421. logMessage('Invalid speed. Please enter a positive number.');
  422. return;
  423. }
  424. logMessage(`Setting speed to: ${speed}...`);
  425. const response = await fetch('/set_speed', {
  426. method: 'POST',
  427. headers: { 'Content-Type': 'application/json' },
  428. body: JSON.stringify({ speed })
  429. });
  430. const result = await response.json();
  431. if (result.success) {
  432. document.getElementById('speed_status').textContent = `Current Speed: ${speed}`;
  433. logMessage(`Speed set to: ${speed}`);
  434. } else {
  435. logMessage(`Failed to set speed: ${result.error}`);
  436. }
  437. }
  438. // Call this function on page load
  439. checkSerialStatus();
  440. // Initial load of serial ports and Theta-Rho files
  441. loadSerialPorts();
  442. loadThetaRhoFiles();
  443. document.getElementById('run_button').onclick = runThetaRho;
  444. </script>
  445. </body>
  446. </html>