app.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. from flask import Flask, request, jsonify, render_template
  2. import atexit
  3. import os
  4. import serial
  5. import time
  6. import random
  7. import threading
  8. import serial.tools.list_ports
  9. import math
  10. import json
  11. from datetime import datetime
  12. import subprocess
  13. from tqdm import tqdm
  14. app = Flask(__name__)
  15. # Configuration
  16. THETA_RHO_DIR = './patterns'
  17. IGNORE_PORTS = ['/dev/cu.debug-console', '/dev/cu.Bluetooth-Incoming-Port']
  18. CLEAR_PATTERNS = {
  19. "clear_from_in": "./patterns/clear_from_in.thr",
  20. "clear_from_out": "./patterns/clear_from_out.thr",
  21. "clear_sideway": "./patterns/clear_sideway.thr"
  22. }
  23. os.makedirs(THETA_RHO_DIR, exist_ok=True)
  24. # Serial connection (First available will be selected by default)
  25. ser = None
  26. ser_port = None # Global variable to store the serial port name
  27. stop_requested = False
  28. pause_requested = False
  29. pause_condition = threading.Condition()
  30. # Global variables to store device information
  31. arduino_table_name = None
  32. arduino_driver_type = 'Unknown'
  33. # Table status
  34. current_playing_file = None
  35. execution_progress = None
  36. firmware_version = 'Unknown'
  37. current_playing_index = None
  38. current_playlist = None
  39. is_clearing = False
  40. serial_lock = threading.RLock()
  41. PLAYLISTS_FILE = os.path.join(os.getcwd(), "playlists.json")
  42. MOTOR_TYPE_MAPPING = {
  43. "TMC2209": "./firmware/arduino_code_TMC2209/arduino_code_TMC2209.ino",
  44. "DRV8825": "./firmware/arduino_code/arduino_code.ino",
  45. "esp32": "./firmware/esp32/esp32.ino"
  46. }
  47. # Ensure the file exists and contains at least an empty JSON object
  48. if not os.path.exists(PLAYLISTS_FILE):
  49. with open(PLAYLISTS_FILE, "w") as f:
  50. json.dump({}, f, indent=2)
  51. def get_ino_firmware_details(ino_file_path):
  52. """
  53. Extract firmware details, including version and motor type, from the given .ino file.
  54. Args:
  55. ino_file_path (str): Path to the .ino file.
  56. Returns:
  57. dict: Dictionary containing firmware details such as version and motor type, or None if not found.
  58. """
  59. try:
  60. if not ino_file_path:
  61. raise ValueError("Invalid path: ino_file_path is None or empty.")
  62. firmware_details = {"version": None, "motorType": None}
  63. with open(ino_file_path, "r") as file:
  64. for line in file:
  65. # Extract firmware version
  66. if "firmwareVersion" in line:
  67. start = line.find('"') + 1
  68. end = line.rfind('"')
  69. if start != -1 and end != -1 and start < end:
  70. firmware_details["version"] = line[start:end]
  71. # Extract motor type
  72. if "motorType" in line:
  73. start = line.find('"') + 1
  74. end = line.rfind('"')
  75. if start != -1 and end != -1 and start < end:
  76. firmware_details["motorType"] = line[start:end]
  77. if not firmware_details["version"]:
  78. print(f"Firmware version not found in file: {ino_file_path}")
  79. if not firmware_details["motorType"]:
  80. print(f"Motor type not found in file: {ino_file_path}")
  81. return firmware_details if any(firmware_details.values()) else None
  82. except FileNotFoundError:
  83. print(f"File not found: {ino_file_path}")
  84. return None
  85. except Exception as e:
  86. print(f"Error reading .ino file: {str(e)}")
  87. return None
  88. def check_git_updates():
  89. try:
  90. # Fetch the latest updates from the remote repository
  91. subprocess.run(["git", "fetch", "--tags", "--force"], check=True)
  92. # Get the latest tag from the remote
  93. latest_remote_tag = subprocess.check_output(
  94. ["git", "describe", "--tags", "--abbrev=0", "origin/main"]
  95. ).strip().decode()
  96. # Get the latest tag from the local branch
  97. latest_local_tag = subprocess.check_output(
  98. ["git", "describe", "--tags", "--abbrev=0"]
  99. ).strip().decode()
  100. # Count how many tags the local branch is behind
  101. tag_behind_count = 0
  102. if latest_local_tag != latest_remote_tag:
  103. tags = subprocess.check_output(
  104. ["git", "tag", "--merged", "origin/main"], text=True
  105. ).splitlines()
  106. found_local = False
  107. for tag in tags:
  108. if tag == latest_local_tag:
  109. found_local = True
  110. elif found_local:
  111. tag_behind_count += 1
  112. if tag == latest_remote_tag:
  113. break
  114. # Check if there are new commits
  115. updates_available = latest_remote_tag != latest_local_tag
  116. return {
  117. "updates_available": updates_available,
  118. "tag_behind_count": tag_behind_count, # Tags behind
  119. "latest_remote_tag": latest_remote_tag,
  120. "latest_local_tag": latest_local_tag,
  121. }
  122. except subprocess.CalledProcessError as e:
  123. print(f"Error checking Git updates: {e}")
  124. return {
  125. "updates_available": False,
  126. "tag_behind_count": 0,
  127. "latest_remote_tag": None,
  128. "latest_local_tag": None,
  129. }
  130. def list_serial_ports():
  131. """Return a list of available serial ports."""
  132. ports = serial.tools.list_ports.comports()
  133. return [port.device for port in ports if port.device not in IGNORE_PORTS]
  134. def connect_to_serial(port=None, baudrate=115200):
  135. """Automatically connect to the first available serial port or a specified port."""
  136. global ser, ser_port, arduino_table_name, arduino_driver_type, firmware_version
  137. try:
  138. if port is None:
  139. ports = list_serial_ports()
  140. if not ports:
  141. print("No serial port connected")
  142. return False
  143. port = ports[0] # Auto-select the first available port
  144. with serial_lock:
  145. if ser and ser.is_open:
  146. ser.close()
  147. ser = serial.Serial(port, baudrate, timeout=2) # Set timeout to avoid infinite waits
  148. ser_port = port # Store the connected port globally
  149. print(f"Connected to serial port: {port}")
  150. time.sleep(2) # Allow time for the connection to establish
  151. # Read initial startup messages from Arduino
  152. arduino_table_name = None
  153. arduino_driver_type = None
  154. while ser.in_waiting > 0:
  155. line = ser.readline().decode().strip()
  156. print(f"Arduino: {line}") # Print the received message
  157. # Store the device details based on the expected messages
  158. if "Table:" in line:
  159. arduino_table_name = line.replace("Table: ", "").strip()
  160. elif "Drivers:" in line:
  161. arduino_driver_type = line.replace("Drivers: ", "").strip()
  162. elif "Version:" in line:
  163. firmware_version = line.replace("Version: ", "").strip()
  164. # Display stored values
  165. print(f"Detected Table: {arduino_table_name or 'Unknown'}")
  166. print(f"Detected Drivers: {arduino_driver_type or 'Unknown'}")
  167. return True # Successfully connected
  168. except serial.SerialException as e:
  169. print(f"Failed to connect to serial port {port}: {e}")
  170. port = None # Reset the port to try the next available one
  171. print("Max retries reached. Could not connect to a serial port.")
  172. return False
  173. def disconnect_serial():
  174. """Disconnect the current serial connection."""
  175. global ser, ser_port
  176. if ser and ser.is_open:
  177. ser.close()
  178. ser = None
  179. ser_port = None # Reset the port name
  180. def restart_serial(port, baudrate=115200):
  181. """Restart the serial connection."""
  182. disconnect_serial()
  183. connect_to_serial(port, baudrate)
  184. def parse_theta_rho_file(file_path):
  185. """
  186. Parse a theta-rho file and return a list of (theta, rho) pairs.
  187. Normalizes the list so the first theta is always 0.
  188. """
  189. coordinates = []
  190. try:
  191. with open(file_path, 'r') as file:
  192. for line in file:
  193. line = line.strip()
  194. # Skip header or comment lines (starting with '#' or empty lines)
  195. if not line or line.startswith("#"):
  196. continue
  197. # Parse lines with theta and rho separated by spaces
  198. try:
  199. theta, rho = map(float, line.split())
  200. coordinates.append((theta, rho))
  201. except ValueError:
  202. print(f"Skipping invalid line: {line}")
  203. continue
  204. except Exception as e:
  205. print(f"Error reading file: {e}")
  206. return coordinates
  207. # ---- Normalization Step ----
  208. if coordinates:
  209. # Take the first coordinate's theta
  210. first_theta = coordinates[0][0]
  211. # Shift all thetas so the first coordinate has theta=0
  212. normalized = []
  213. for (theta, rho) in coordinates:
  214. normalized.append((theta - first_theta, rho))
  215. # Replace original list with normalized data
  216. coordinates = normalized
  217. return coordinates
  218. def send_coordinate_batch(ser, coordinates):
  219. """Send a batch of theta-rho pairs to the Arduino."""
  220. # print("Sending batch:", coordinates)
  221. batch_str = ";".join(f"{theta:.5f},{rho:.5f}" for theta, rho in coordinates) + ";\n"
  222. ser.write(batch_str.encode())
  223. def send_command(command):
  224. """Send a single command to the Arduino."""
  225. ser.write(f"{command}\n".encode())
  226. print(f"Sent: {command}")
  227. # Wait for "R" acknowledgment from Arduino
  228. while True:
  229. with serial_lock:
  230. if ser.in_waiting > 0:
  231. response = ser.readline().decode().strip()
  232. print(f"Arduino response: {response}")
  233. if response == "R":
  234. print("Command execution completed.")
  235. break
  236. def wait_for_start_time(schedule_hours):
  237. """
  238. Keep checking every 30 seconds if the time is within the schedule to resume execution.
  239. """
  240. global pause_requested
  241. start_time, end_time = schedule_hours
  242. while pause_requested:
  243. now = datetime.now().time()
  244. if start_time <= now < end_time:
  245. print("Resuming execution: Within schedule.")
  246. pause_requested = False
  247. with pause_condition:
  248. pause_condition.notify_all()
  249. break # Exit the loop once resumed
  250. else:
  251. time.sleep(30) # Wait for 30 seconds before checking again
  252. # Function to check schedule based on start and end time
  253. def schedule_checker(schedule_hours):
  254. """
  255. Pauses/resumes execution based on a given time range.
  256. Parameters:
  257. - schedule_hours (tuple): (start_time, end_time) as `datetime.time` objects.
  258. """
  259. global pause_requested
  260. if not schedule_hours:
  261. return # No scheduling restriction
  262. start_time, end_time = schedule_hours
  263. now = datetime.now().time() # Get the current time as `datetime.time`
  264. # Check if we are currently within the scheduled time
  265. if start_time <= now < end_time:
  266. if pause_requested:
  267. print("Starting execution: Within schedule.")
  268. pause_requested = False # Resume execution
  269. with pause_condition:
  270. pause_condition.notify_all()
  271. else:
  272. if not pause_requested:
  273. print("Pausing execution: Outside schedule.")
  274. pause_requested = True # Pause execution
  275. # Start a background thread to periodically check for start time
  276. threading.Thread(target=wait_for_start_time, args=(schedule_hours,), daemon=True).start()
  277. def run_theta_rho_file(file_path, schedule_hours=None):
  278. """Run a theta-rho file by sending data in optimized batches with tqdm ETA tracking."""
  279. global stop_requested, current_playing_file, execution_progress
  280. coordinates = parse_theta_rho_file(file_path)
  281. total_coordinates = len(coordinates)
  282. if total_coordinates < 2:
  283. print("Not enough coordinates for interpolation.")
  284. current_playing_file = None # Clear tracking if failed
  285. execution_progress = None
  286. return
  287. execution_progress = (0, total_coordinates, None) # Initialize progress with ETA as None
  288. batch_size = 10 # Smaller batches may smooth movement further
  289. # before trying to acuire the lock we send the stop command
  290. # so then we will just wait for the lock to be released so we can use the serial
  291. stop_actions()
  292. with serial_lock:
  293. current_playing_file = file_path # Track current playing file
  294. execution_progress = (0, 0, None) # Reset progress (ETA starts as None)
  295. stop_requested = False
  296. with tqdm(total=total_coordinates, unit="coords", desc=f"Executing Pattern {file_path}", dynamic_ncols=True, disable=None) as pbar:
  297. for i in range(0, total_coordinates, batch_size):
  298. if stop_requested:
  299. print("Execution stopped by user after completing the current batch.")
  300. # after stopping we free the stop rquest "lock"
  301. stop_requested= False
  302. break
  303. with pause_condition:
  304. while pause_requested:
  305. print("Execution paused...")
  306. pause_condition.wait() # This will block execution until notified
  307. batch = coordinates[i:i + batch_size]
  308. if i == 0:
  309. send_coordinate_batch(ser, batch)
  310. execution_progress = (i + batch_size, total_coordinates, None) # No ETA yet
  311. pbar.update(batch_size)
  312. continue
  313. while True:
  314. schedule_checker(schedule_hours) # Check if within schedule
  315. if ser.in_waiting > 0:
  316. response = ser.readline().decode().strip()
  317. if response == "R":
  318. send_coordinate_batch(ser, batch)
  319. pbar.update(batch_size) # Update tqdm progress
  320. # Use tqdm's built-in ETA tracking
  321. estimated_remaining_time = pbar.format_dict['elapsed'] / (i + batch_size) * (total_coordinates - (i + batch_size))
  322. # Update execution progress with formatted ETA
  323. execution_progress = (i + batch_size, total_coordinates, estimated_remaining_time)
  324. break
  325. elif response.startswith("IGNORE"): # Retry the previous batch
  326. print("Received IGNORE. Resending the previous batch...")
  327. # Calculate the previous batch indices
  328. prev_start = max(0, i - batch_size) # Ensure we don't go below 0
  329. prev_end = i # End of the previous batch is `i`
  330. previous_batch = coordinates[prev_start:prev_end]
  331. # Resend the previous batch
  332. send_coordinate_batch(ser, previous_batch)
  333. break # Exit the retry loop after resending
  334. else:
  335. print(f"Arduino response: {response}")
  336. reset_theta()
  337. ser.write("FINISHED\n".encode())
  338. # Clear tracking variables when done
  339. current_playing_file = None
  340. execution_progress = None
  341. print("Pattern execution completed.")
  342. def get_clear_pattern_file(clear_pattern_mode, path=None):
  343. """Return a .thr file path based on pattern_name."""
  344. if not clear_pattern_mode or clear_pattern_mode == 'none':
  345. return
  346. print("Clear pattern mode: " + clear_pattern_mode)
  347. if clear_pattern_mode == "random":
  348. # Randomly pick one of the three known patterns
  349. return random.choice(list(CLEAR_PATTERNS.values()))
  350. if clear_pattern_mode == 'adaptive':
  351. _, first_rho = parse_theta_rho_file(path)[0]
  352. if first_rho < 0.5:
  353. return CLEAR_PATTERNS['clear_from_out']
  354. else:
  355. return random.choice([CLEAR_PATTERNS['clear_from_in'], CLEAR_PATTERNS['clear_sideway']])
  356. else:
  357. return CLEAR_PATTERNS[clear_pattern_mode]
  358. def run_theta_rho_files(
  359. file_paths,
  360. pause_time=0,
  361. clear_pattern=None,
  362. run_mode="single",
  363. shuffle=False,
  364. schedule_hours=None
  365. ):
  366. """
  367. Runs multiple .thr files in sequence with options for pausing, clearing, shuffling, and looping.
  368. Parameters:
  369. - file_paths (list): List of file paths to run.
  370. - pause_time (float): Seconds to pause between patterns.
  371. - clear_pattern (str): Specific clear pattern to run ("clear_from_in", "clear_from_out", "clear_sideway", "adaptive", or "random").
  372. - run_mode (str): "single" for one-time run or "indefinite" for looping.
  373. - shuffle (bool): Whether to shuffle the playlist before running.
  374. """
  375. global stop_requested
  376. global current_playlist
  377. global current_playing_index
  378. stop_requested = False # Reset stop flag at the start
  379. if shuffle:
  380. random.shuffle(file_paths)
  381. print("Playlist shuffled.")
  382. current_playlist = file_paths
  383. while True:
  384. for idx, path in enumerate(file_paths):
  385. print("Upcoming pattern: " + path)
  386. current_playing_index = idx
  387. schedule_checker(schedule_hours)
  388. if stop_requested:
  389. print("Execution stopped before starting next pattern.")
  390. return
  391. if clear_pattern:
  392. if stop_requested:
  393. print("Execution stopped before running the next clear pattern.")
  394. return
  395. # Determine the clear pattern to run
  396. clear_file_path = get_clear_pattern_file(clear_pattern, path)
  397. print(f"Running clear pattern: {clear_file_path}")
  398. run_theta_rho_file(clear_file_path, schedule_hours)
  399. if not stop_requested:
  400. # Run the main pattern
  401. print(f"Running pattern {idx + 1} of {len(file_paths)}: {path}")
  402. run_theta_rho_file(path, schedule_hours)
  403. if idx < len(file_paths) -1:
  404. if stop_requested:
  405. print("Execution stopped before running the next clear pattern.")
  406. return
  407. # Pause after each pattern if requested
  408. if pause_time > 0:
  409. print(f"Pausing for {pause_time} seconds...")
  410. time.sleep(pause_time)
  411. # After completing the playlist
  412. if run_mode == "indefinite":
  413. print("Playlist completed. Restarting as per 'indefinite' run mode.")
  414. if pause_time > 0:
  415. print(f"Pausing for {pause_time} seconds before restarting...")
  416. time.sleep(pause_time)
  417. if shuffle:
  418. random.shuffle(file_paths)
  419. print("Playlist reshuffled for the next loop.")
  420. continue
  421. else:
  422. print("Playlist completed.")
  423. break
  424. # Reset theta after execution or stopping
  425. reset_theta()
  426. ser.write("FINISHED\n".encode())
  427. print("All requested patterns completed (or stopped).")
  428. def reset_theta():
  429. """Reset theta on the Arduino."""
  430. ser.write("RESET_THETA\n".encode())
  431. while True:
  432. with serial_lock:
  433. if ser.in_waiting > 0:
  434. response = ser.readline().decode().strip()
  435. print(f"Arduino response: {response}")
  436. if response == "THETA_RESET":
  437. print("Theta successfully reset.")
  438. break
  439. time.sleep(0.5) # Small delay to avoid busy waiting
  440. # Flask API Endpoints
  441. @app.route('/')
  442. def index():
  443. return render_template('index.html')
  444. @app.route('/list_serial_ports', methods=['GET'])
  445. def list_ports():
  446. return jsonify(list_serial_ports())
  447. @app.route('/connect_serial', methods=['POST'])
  448. def connect_serial():
  449. port = request.json.get('port')
  450. if not port:
  451. return jsonify({'error': 'No port provided'}), 400
  452. try:
  453. connect_to_serial(port)
  454. return jsonify({'success': True})
  455. except Exception as e:
  456. return jsonify({'error': str(e)}), 500
  457. @app.route('/disconnect_serial', methods=['POST'])
  458. def disconnect():
  459. try:
  460. disconnect_serial()
  461. return jsonify({'success': True})
  462. except Exception as e:
  463. return jsonify({'error': str(e)}), 500
  464. @app.route('/restart_serial', methods=['POST'])
  465. def restart():
  466. port = request.json.get('port')
  467. if not port:
  468. return jsonify({'error': 'No port provided'}), 400
  469. try:
  470. restart_serial(port)
  471. return jsonify({'success': True})
  472. except Exception as e:
  473. return jsonify({'error': str(e)}), 500
  474. @app.route('/list_theta_rho_files', methods=['GET'])
  475. def list_theta_rho_files():
  476. files = []
  477. for root, _, filenames in os.walk(THETA_RHO_DIR):
  478. for file in filenames:
  479. # Construct the relative file path
  480. relative_path = os.path.relpath(os.path.join(root, file), THETA_RHO_DIR)
  481. files.append(relative_path)
  482. return jsonify(sorted(files))
  483. @app.route('/upload_theta_rho', methods=['POST'])
  484. def upload_theta_rho():
  485. custom_patterns_dir = os.path.join(THETA_RHO_DIR, 'custom_patterns')
  486. os.makedirs(custom_patterns_dir, exist_ok=True) # Ensure the directory exists
  487. file = request.files['file']
  488. if file:
  489. file.save(os.path.join(custom_patterns_dir, file.filename))
  490. return jsonify({'success': True})
  491. return jsonify({'success': False})
  492. @app.route('/run_theta_rho', methods=['POST'])
  493. def run_theta_rho():
  494. file_name = request.json.get('file_name')
  495. pre_execution = request.json.get('pre_execution')
  496. if not file_name:
  497. return jsonify({'error': 'No file name provided'}), 400
  498. file_path = os.path.join(THETA_RHO_DIR, file_name)
  499. if not os.path.exists(file_path):
  500. return jsonify({'error': 'File not found'}), 404
  501. try:
  502. # Build a list of files to run in sequence
  503. files_to_run = []
  504. # Finally, add the main file
  505. files_to_run.append(file_path)
  506. # Run them in one shot using run_theta_rho_files (blocking call)
  507. threading.Thread(
  508. target=run_theta_rho_files,
  509. args=(files_to_run,),
  510. kwargs={
  511. 'pause_time': 0,
  512. 'clear_pattern': pre_execution
  513. }
  514. ).start()
  515. return jsonify({'success': True})
  516. except Exception as e:
  517. return jsonify({'error': str(e)}), 500
  518. def stop_actions():
  519. global pause_requested
  520. with pause_condition:
  521. pause_requested = False
  522. pause_condition.notify_all()
  523. global stop_requested, current_playing_index, current_playlist, is_clearing, current_playing_file, execution_progress
  524. stop_requested = True
  525. current_playing_index = None
  526. current_playlist = None
  527. is_clearing = False
  528. current_playing_file = None
  529. execution_progress = None
  530. @app.route('/stop_execution', methods=['POST'])
  531. def stop_execution():
  532. stop_actions()
  533. return jsonify({'success': True})
  534. @app.route('/send_home', methods=['POST'])
  535. def send_home():
  536. """Send the HOME command to the Arduino."""
  537. try:
  538. send_command("HOME")
  539. return jsonify({'success': True})
  540. except Exception as e:
  541. return jsonify({'error': str(e)}), 500
  542. @app.route('/run_theta_rho_file/<file_name>', methods=['POST'])
  543. def run_specific_theta_rho_file(file_name):
  544. """Run a specific theta-rho file."""
  545. file_path = os.path.join(THETA_RHO_DIR, file_name)
  546. if not os.path.exists(file_path):
  547. return jsonify({'error': 'File not found'}), 404
  548. threading.Thread(target=run_theta_rho_file, args=(file_path,)).start()
  549. return jsonify({'success': True})
  550. @app.route('/delete_theta_rho_file', methods=['POST'])
  551. def delete_theta_rho_file():
  552. data = request.json
  553. file_name = data.get('file_name')
  554. if not file_name:
  555. return jsonify({"success": False, "error": "No file name provided"}), 400
  556. file_path = os.path.join(THETA_RHO_DIR, file_name)
  557. if not os.path.exists(file_path):
  558. return jsonify({"success": False, "error": "File not found"}), 404
  559. try:
  560. os.remove(file_path)
  561. return jsonify({"success": True})
  562. except Exception as e:
  563. return jsonify({"success": False, "error": str(e)}), 500
  564. @app.route('/move_to_center', methods=['POST'])
  565. def move_to_center():
  566. """Move the sand table to the center position."""
  567. try:
  568. if ser is None or not ser.is_open:
  569. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  570. coordinates = [(0, 0)] # Center position
  571. send_coordinate_batch(ser, coordinates)
  572. return jsonify({"success": True})
  573. except Exception as e:
  574. return jsonify({"success": False, "error": str(e)}), 500
  575. @app.route('/move_to_perimeter', methods=['POST'])
  576. def move_to_perimeter():
  577. """Move the sand table to the perimeter position."""
  578. try:
  579. if ser is None or not ser.is_open:
  580. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  581. MAX_RHO = 1
  582. coordinates = [(0, MAX_RHO)] # Perimeter position
  583. send_coordinate_batch(ser, coordinates)
  584. return jsonify({"success": True})
  585. except Exception as e:
  586. return jsonify({"success": False, "error": str(e)}), 500
  587. @app.route('/preview_thr', methods=['POST'])
  588. def preview_thr():
  589. file_name = request.json.get('file_name')
  590. if not file_name:
  591. return jsonify({'error': 'No file name provided'}), 400
  592. file_path = os.path.join(THETA_RHO_DIR, file_name)
  593. if not os.path.exists(file_path):
  594. return jsonify({'error': 'File not found'}), 404
  595. try:
  596. # Parse the .thr file with transformations
  597. coordinates = parse_theta_rho_file(file_path)
  598. return jsonify({'success': True, 'coordinates': coordinates})
  599. except Exception as e:
  600. return jsonify({'error': str(e)}), 500
  601. @app.route('/send_coordinate', methods=['POST'])
  602. def send_coordinate():
  603. """Send a single (theta, rho) coordinate to the Arduino."""
  604. global ser
  605. if ser is None or not ser.is_open:
  606. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  607. try:
  608. data = request.json
  609. theta = data.get('theta')
  610. rho = data.get('rho')
  611. if theta is None or rho is None:
  612. return jsonify({"success": False, "error": "Theta and Rho are required"}), 400
  613. # Send the coordinate to the Arduino
  614. send_coordinate_batch(ser, [(theta, rho)])
  615. return jsonify({"success": True})
  616. except Exception as e:
  617. return jsonify({"success": False, "error": str(e)}), 500
  618. # Expose files for download if needed
  619. @app.route('/download/<filename>', methods=['GET'])
  620. def download_file(filename):
  621. """Download a file from the theta-rho directory."""
  622. return send_from_directory(THETA_RHO_DIR, filename)
  623. @app.route('/serial_status', methods=['GET'])
  624. def serial_status():
  625. global ser, ser_port
  626. return jsonify({
  627. 'connected': ser.is_open if ser else False,
  628. 'port': ser_port # Include the port name
  629. })
  630. @app.route('/pause_execution', methods=['POST'])
  631. def pause_execution():
  632. """Pause the current execution."""
  633. global pause_requested
  634. with pause_condition:
  635. pause_requested = True
  636. return jsonify({'success': True, 'message': 'Execution paused'})
  637. @app.route('/status', methods=['GET'])
  638. def get_status():
  639. """Returns the current status of the sand table."""
  640. global is_clearing
  641. if current_playing_file in CLEAR_PATTERNS.values():
  642. is_clearing = True
  643. else:
  644. is_clearing = False
  645. return jsonify({
  646. "ser_port": ser_port,
  647. "stop_requested": stop_requested,
  648. "pause_requested": pause_requested,
  649. "current_playing_file": current_playing_file,
  650. "execution_progress": execution_progress,
  651. "current_playing_index": current_playing_index,
  652. "current_playlist": current_playlist,
  653. "is_clearing": is_clearing
  654. })
  655. @app.route('/resume_execution', methods=['POST'])
  656. def resume_execution():
  657. """Resume execution after pausing."""
  658. global pause_requested
  659. with pause_condition:
  660. pause_requested = False
  661. pause_condition.notify_all() # Unblock the waiting thread
  662. return jsonify({'success': True, 'message': 'Execution resumed'})
  663. def load_playlists():
  664. """
  665. Load the entire playlists dictionary from the JSON file.
  666. Returns something like: {
  667. "My Playlist": ["file1.thr", "file2.thr"],
  668. "Another": ["x.thr"]
  669. }
  670. """
  671. with open(PLAYLISTS_FILE, "r") as f:
  672. return json.load(f)
  673. def save_playlists(playlists_dict):
  674. """
  675. Save the entire playlists dictionary back to the JSON file.
  676. """
  677. with open(PLAYLISTS_FILE, "w") as f:
  678. json.dump(playlists_dict, f, indent=2)
  679. @app.route("/list_all_playlists", methods=["GET"])
  680. def list_all_playlists():
  681. """
  682. Returns a list of all playlist names.
  683. Example return: ["My Playlist", "Another Playlist"]
  684. """
  685. playlists_dict = load_playlists()
  686. playlist_names = list(playlists_dict.keys())
  687. return jsonify(playlist_names)
  688. @app.route("/get_playlist", methods=["GET"])
  689. def get_playlist():
  690. """
  691. GET /get_playlist?name=My%20Playlist
  692. Returns: { "name": "My Playlist", "files": [... ] }
  693. """
  694. playlist_name = request.args.get("name", "")
  695. if not playlist_name:
  696. return jsonify({"error": "Missing playlist 'name' parameter"}), 400
  697. playlists_dict = load_playlists()
  698. if playlist_name not in playlists_dict:
  699. return jsonify({"error": f"Playlist '{playlist_name}' not found"}), 404
  700. files = playlists_dict[playlist_name] # e.g. ["file1.thr", "file2.thr"]
  701. return jsonify({
  702. "name": playlist_name,
  703. "files": files
  704. })
  705. @app.route("/create_playlist", methods=["POST"])
  706. def create_playlist():
  707. """
  708. POST /create_playlist
  709. Body: { "name": "My Playlist", "files": ["file1.thr", "file2.thr"] }
  710. Creates or overwrites a playlist with the given name.
  711. """
  712. data = request.get_json()
  713. if not data or "name" not in data or "files" not in data:
  714. return jsonify({"success": False, "error": "Playlist 'name' and 'files' are required"}), 400
  715. playlist_name = data["name"]
  716. files = data["files"]
  717. # Load all playlists
  718. playlists_dict = load_playlists()
  719. # Overwrite or create new
  720. playlists_dict[playlist_name] = files
  721. # Save changes
  722. save_playlists(playlists_dict)
  723. return jsonify({
  724. "success": True,
  725. "message": f"Playlist '{playlist_name}' created/updated"
  726. })
  727. @app.route("/modify_playlist", methods=["POST"])
  728. def modify_playlist():
  729. """
  730. POST /modify_playlist
  731. Body: { "name": "My Playlist", "files": ["file1.thr", "file2.thr"] }
  732. Updates (or creates) the existing playlist with a new file list.
  733. You can 404 if you only want to allow modifications to existing playlists.
  734. """
  735. data = request.get_json()
  736. if not data or "name" not in data or "files" not in data:
  737. return jsonify({"success": False, "error": "Playlist 'name' and 'files' are required"}), 400
  738. playlist_name = data["name"]
  739. files = data["files"]
  740. # Load all playlists
  741. playlists_dict = load_playlists()
  742. # Optional: If you want to disallow creating a new playlist here:
  743. # if playlist_name not in playlists_dict:
  744. # return jsonify({"success": False, "error": f"Playlist '{playlist_name}' not found"}), 404
  745. # Overwrite or create new
  746. playlists_dict[playlist_name] = files
  747. # Save
  748. save_playlists(playlists_dict)
  749. return jsonify({"success": True, "message": f"Playlist '{playlist_name}' updated"})
  750. @app.route("/delete_playlist", methods=["DELETE"])
  751. def delete_playlist():
  752. """
  753. DELETE /delete_playlist
  754. Body: { "name": "My Playlist" }
  755. Removes the playlist from the single JSON file.
  756. """
  757. data = request.get_json()
  758. if not data or "name" not in data:
  759. return jsonify({"success": False, "error": "Missing 'name' field"}), 400
  760. playlist_name = data["name"]
  761. playlists_dict = load_playlists()
  762. if playlist_name not in playlists_dict:
  763. return jsonify({"success": False, "error": f"Playlist '{playlist_name}' not found"}), 404
  764. # Remove from dict
  765. del playlists_dict[playlist_name]
  766. save_playlists(playlists_dict)
  767. return jsonify({
  768. "success": True,
  769. "message": f"Playlist '{playlist_name}' deleted"
  770. })
  771. @app.route('/add_to_playlist', methods=['POST'])
  772. def add_to_playlist():
  773. data = request.json
  774. playlist_name = data.get('playlist_name')
  775. pattern = data.get('pattern')
  776. # Load existing playlists
  777. with open('playlists.json', 'r') as f:
  778. playlists = json.load(f)
  779. # Add pattern to the selected playlist
  780. if playlist_name in playlists:
  781. playlists[playlist_name].append(pattern)
  782. with open('playlists.json', 'w') as f:
  783. json.dump(playlists, f)
  784. return jsonify(success=True)
  785. else:
  786. return jsonify(success=False, error='Playlist not found'), 404
  787. @app.route("/run_playlist", methods=["POST"])
  788. def run_playlist():
  789. """
  790. POST /run_playlist
  791. Body (JSON):
  792. {
  793. "playlist_name": "My Playlist",
  794. "pause_time": 1.0, # Optional: seconds to pause between patterns
  795. "clear_pattern": "random", # Optional: "clear_from_in", "clear_from_out", "clear_sideway", "adaptive" or "random"
  796. "run_mode": "single", # 'single' or 'indefinite'
  797. "shuffle": True # true or false
  798. "start_time": ""
  799. "end_time": ""
  800. }
  801. """
  802. data = request.get_json()
  803. # Validate input
  804. if not data or "playlist_name" not in data:
  805. return jsonify({"success": False, "error": "Missing 'playlist_name' field"}), 400
  806. playlist_name = data["playlist_name"]
  807. pause_time = data.get("pause_time", 0)
  808. clear_pattern = data.get("clear_pattern", None)
  809. run_mode = data.get("run_mode", "single") # Default to 'single' run
  810. shuffle = data.get("shuffle", False) # Default to no shuffle
  811. start_time = data.get("start_time", None)
  812. end_time = data.get("end_time", None)
  813. # Validate pause_time
  814. if not isinstance(pause_time, (int, float)) or pause_time < 0:
  815. return jsonify({"success": False, "error": "'pause_time' must be a non-negative number"}), 400
  816. # Validate clear_pattern
  817. valid_patterns = ["clear_from_in", "clear_from_out", "clear_sideway", "random", "adaptive"]
  818. if clear_pattern not in valid_patterns:
  819. clear_pattern = None
  820. # Validate run_mode
  821. if run_mode not in ["single", "indefinite"]:
  822. return jsonify({"success": False, "error": "'run_mode' must be 'single' or 'indefinite'"}), 400
  823. # Validate shuffle
  824. if not isinstance(shuffle, bool):
  825. return jsonify({"success": False, "error": "'shuffle' must be a boolean value"}), 400
  826. schedule_hours = None
  827. if start_time and end_time:
  828. try:
  829. # Convert HH:MM to datetime.time objects
  830. start_time_obj = datetime.strptime(start_time, "%H:%M").time()
  831. end_time_obj = datetime.strptime(end_time, "%H:%M").time()
  832. # Ensure start_time is before end_time
  833. if start_time_obj >= end_time_obj:
  834. return jsonify({"success": False, "error": "'start_time' must be earlier than 'end_time'"}), 400
  835. # Create schedule tuple with full time
  836. schedule_hours = (start_time_obj, end_time_obj)
  837. except ValueError:
  838. return jsonify({"success": False, "error": "Invalid time format. Use HH:MM (e.g., '09:30')"}), 400
  839. # Load playlists
  840. playlists = load_playlists()
  841. if playlist_name not in playlists:
  842. return jsonify({"success": False, "error": f"Playlist '{playlist_name}' not found"}), 404
  843. file_paths = playlists[playlist_name]
  844. file_paths = [os.path.join(THETA_RHO_DIR, file) for file in file_paths]
  845. if not file_paths:
  846. return jsonify({"success": False, "error": f"Playlist '{playlist_name}' is empty"}), 400
  847. # Start the playlist execution in a separate thread
  848. try:
  849. threading.Thread(
  850. target=run_theta_rho_files,
  851. args=(file_paths,),
  852. kwargs={
  853. 'pause_time': pause_time,
  854. 'clear_pattern': clear_pattern,
  855. 'run_mode': run_mode,
  856. 'shuffle': shuffle,
  857. 'schedule_hours': schedule_hours
  858. },
  859. daemon=True # Daemonize thread to exit with the main program
  860. ).start()
  861. return jsonify({"success": True, "message": f"Playlist '{playlist_name}' is now running."})
  862. except Exception as e:
  863. return jsonify({"success": False, "error": str(e)}), 500
  864. @app.route('/set_speed', methods=['POST'])
  865. def set_speed():
  866. """Set the speed for the Arduino."""
  867. global ser
  868. if ser is None or not ser.is_open:
  869. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  870. try:
  871. # Parse the speed value from the request
  872. data = request.json
  873. speed = data.get('speed')
  874. if speed is None:
  875. return jsonify({"success": False, "error": "Speed is required"}), 400
  876. if not isinstance(speed, (int, float)) or speed <= 0:
  877. return jsonify({"success": False, "error": "Invalid speed value"}), 400
  878. # Send the SET_SPEED command to the Arduino
  879. command = f"SET_SPEED {speed}"
  880. send_command(command)
  881. return jsonify({"success": True, "speed": speed})
  882. except Exception as e:
  883. return jsonify({"success": False, "error": str(e)}), 500
  884. @app.route('/get_firmware_info', methods=['GET', 'POST'])
  885. def get_firmware_info():
  886. """
  887. Compare the installed firmware version and motor type with the one in the .ino file.
  888. """
  889. global firmware_version, arduino_driver_type, ser
  890. if ser is None or not ser.is_open:
  891. return jsonify({"success": False, "error": "Arduino not connected or serial port not open"}), 400
  892. try:
  893. if request.method == "GET":
  894. # Attempt to retrieve installed firmware details from the Arduino
  895. time.sleep(0.5)
  896. installed_version = firmware_version
  897. installed_type = arduino_driver_type
  898. # If Arduino provides valid details, proceed with comparison
  899. if installed_version != 'Unknown' and installed_type != 'Unknown':
  900. ino_path = MOTOR_TYPE_MAPPING.get(installed_type)
  901. firmware_details = get_ino_firmware_details(ino_path)
  902. if not firmware_details or not firmware_details.get("version") or not firmware_details.get("motorType"):
  903. return jsonify({"success": False, "error": "Failed to retrieve .ino firmware details"}), 500
  904. update_available = (
  905. installed_version != firmware_details["version"] or
  906. installed_type != firmware_details["motorType"]
  907. )
  908. return jsonify({
  909. "success": True,
  910. "installedVersion": installed_version,
  911. "installedType": installed_type,
  912. "inoVersion": firmware_details["version"],
  913. "inoType": firmware_details["motorType"],
  914. "updateAvailable": update_available
  915. })
  916. # If Arduino details are unknown, indicate the need for POST
  917. return jsonify({
  918. "success": True,
  919. "installedVersion": installed_version,
  920. "installedType": installed_type,
  921. "updateAvailable": False
  922. })
  923. elif request.method == "POST":
  924. motor_type = request.json.get("motorType", None)
  925. if not motor_type or motor_type not in MOTOR_TYPE_MAPPING:
  926. return jsonify({
  927. "success": False,
  928. "error": "Invalid or missing motor type"
  929. }), 400
  930. # Fetch firmware details for the given motor type
  931. ino_path = MOTOR_TYPE_MAPPING[motor_type]
  932. firmware_details = get_ino_firmware_details(ino_path)
  933. if not firmware_details:
  934. return jsonify({
  935. "success": False,
  936. "error": "Failed to retrieve .ino firmware details"
  937. }), 500
  938. return jsonify({
  939. "success": True,
  940. "installedVersion": 'Unknown',
  941. "installedType": motor_type,
  942. "inoVersion": firmware_details["version"],
  943. "inoType": firmware_details["motorType"],
  944. "updateAvailable": True
  945. })
  946. except Exception as e:
  947. return jsonify({"success": False, "error": str(e)}), 500
  948. @app.route('/flash_firmware', methods=['POST'])
  949. def flash_firmware():
  950. """
  951. Flash the pre-compiled firmware to the connected device (Arduino or ESP32).
  952. """
  953. global ser_port
  954. # Ensure the device is connected
  955. if ser_port is None or ser is None or not ser.is_open:
  956. return jsonify({"success": False, "error": "No device connected or connection lost"}), 400
  957. try:
  958. data = request.json
  959. motor_type = data.get("motorType", None)
  960. # Validate motor type
  961. if not motor_type or motor_type not in MOTOR_TYPE_MAPPING:
  962. return jsonify({"success": False, "error": "Invalid or missing motor type"}), 400
  963. # Determine the firmware file
  964. ino_file_path = MOTOR_TYPE_MAPPING[motor_type] # Path to .ino file
  965. hex_file_path = f"{ino_file_path}.hex"
  966. bin_file_path = f"{ino_file_path}.bin" # For ESP32 firmware
  967. # Check the device type
  968. if motor_type.lower() == "esp32":
  969. if not os.path.exists(bin_file_path):
  970. return jsonify({"success": False, "error": f"Firmware binary not found: {bin_file_path}"}), 404
  971. # Flash ESP32 firmware
  972. flash_command = [
  973. "esptool.py",
  974. "--chip", "esp32",
  975. "--port", ser_port,
  976. "--baud", "115200",
  977. "write_flash", "-z", "0x1000", bin_file_path
  978. ]
  979. else:
  980. if not os.path.exists(hex_file_path):
  981. return jsonify({"success": False, "error": f"Hex file not found: {hex_file_path}"}), 404
  982. # Flash Arduino firmware
  983. flash_command = [
  984. "avrdude",
  985. "-v",
  986. "-c", "arduino",
  987. "-p", "atmega328p",
  988. "-P", ser_port,
  989. "-b", "115200",
  990. "-D",
  991. "-U", f"flash:w:{hex_file_path}:i"
  992. ]
  993. # Execute the flash command
  994. flash_process = subprocess.run(flash_command, capture_output=True, text=True)
  995. if flash_process.returncode != 0:
  996. return jsonify({
  997. "success": False,
  998. "error": flash_process.stderr
  999. }), 500
  1000. return jsonify({"success": True, "message": "Firmware flashed successfully"})
  1001. except Exception as e:
  1002. return jsonify({"success": False, "error": str(e)}), 500
  1003. @app.route('/check_software_update', methods=['GET'])
  1004. def check_updates():
  1005. update_info = check_git_updates()
  1006. return jsonify(update_info)
  1007. @app.route('/update_software', methods=['POST'])
  1008. def update_software():
  1009. error_log = []
  1010. def run_command(command, error_message):
  1011. try:
  1012. subprocess.run(command, check=True)
  1013. except subprocess.CalledProcessError as e:
  1014. print(f"{error_message}: {e}")
  1015. error_log.append(error_message)
  1016. # Fetch the latest version tag from remote
  1017. try:
  1018. subprocess.run(["git", "fetch", "--tags"], check=True)
  1019. latest_remote_tag = subprocess.check_output(
  1020. ["git", "describe", "--tags", "--abbrev=0", "origin/main"]
  1021. ).strip().decode()
  1022. except subprocess.CalledProcessError as e:
  1023. error_log.append(f"Failed to fetch tags or get latest remote tag: {e}")
  1024. return jsonify({
  1025. "success": False,
  1026. "error": "Failed to fetch tags or determine the latest version.",
  1027. "details": error_log
  1028. }), 500
  1029. # Checkout the latest tag
  1030. run_command(["git", "checkout", latest_remote_tag, '--force'], f"Failed to checkout version {latest_remote_tag}")
  1031. # Restart Docker containers
  1032. run_command(["docker", "compose", "up", "-d"], "Failed to restart Docker containers")
  1033. # Check if the update was successful
  1034. update_status = check_git_updates()
  1035. if (
  1036. update_status["updates_available"] is False
  1037. and update_status["latest_local_tag"] == update_status["latest_remote_tag"]
  1038. ):
  1039. # Update was successful
  1040. return jsonify({"success": True})
  1041. else:
  1042. # Update failed; include the errors in the response
  1043. return jsonify({
  1044. "success": False,
  1045. "error": "Update incomplete",
  1046. "details": error_log
  1047. }), 500
  1048. def on_exit():
  1049. """Function to execute on application shutdown."""
  1050. print("Shutting down the application...")
  1051. stop_actions()
  1052. time.sleep(5)
  1053. print("Execution stopped and resources cleaned up.")
  1054. # Register the on_exit function
  1055. atexit.register(on_exit)
  1056. if __name__ == '__main__':
  1057. # Auto-connect to serial
  1058. connect_to_serial()
  1059. try:
  1060. app.run(debug=False, host='0.0.0.0', port=8080)
  1061. except KeyboardInterrupt:
  1062. print("Keyboard interrupt received. Shutting down.")
  1063. finally:
  1064. on_exit() # Ensure cleanup if app is interrupted