Przeglądaj źródła

add play/pause button

Tuan Nguyen 1 rok temu
rodzic
commit
37a6fc70ff
3 zmienionych plików z 57 dodań i 4 usunięć
  1. 26 4
      app.py
  2. 30 0
      static/main.js
  3. 1 0
      templates/index.html

+ 26 - 4
app.py

@@ -24,6 +24,9 @@ os.makedirs(THETA_RHO_DIR, exist_ok=True)
 ser = None
 ser_port = None  # Global variable to store the serial port name
 stop_requested = False
+pause_requested = False
+pause_condition = threading.Condition()
+
 serial_lock = threading.Lock()
 
 PLAYLISTS_FILE = os.path.join(os.getcwd(), "playlists.json")
@@ -156,6 +159,12 @@ def run_theta_rho_file(file_path):
         if stop_requested:
             print("Execution stopped by user after completing the current batch.")
             break
+        
+        with pause_condition:
+            while pause_requested:
+                print("Execution paused...")
+                pause_condition.wait()  # This will block execution until notified
+        
         batch = coordinates[i:i + batch_size]
         if i == 0:
             send_coordinate_batch(ser, batch)
@@ -498,10 +507,23 @@ def serial_status():
         'connected': ser.is_open if ser else False,
         'port': ser_port  # Include the port name
     })
-
-if not os.path.exists(PLAYLISTS_FILE):
-    with open(PLAYLISTS_FILE, "w") as f:
-        json.dump({}, f, indent=2)
+    
+@app.route('/pause_execution', methods=['POST'])
+def pause_execution():
+    """Pause the current execution."""
+    global pause_requested
+    with pause_condition:
+        pause_requested = True
+    return jsonify({'success': True, 'message': 'Execution paused'})
+
+@app.route('/resume_execution', methods=['POST'])
+def resume_execution():
+    """Resume execution after pausing."""
+    global pause_requested
+    with pause_condition:
+        pause_requested = False
+        pause_condition.notify_all()  # Unblock the waiting thread
+    return jsonify({'success': True, 'message': 'Execution resumed'})
 
 def load_playlists():
     """

+ 30 - 0
static/main.js

@@ -241,6 +241,36 @@ async function stopExecution() {
     }
 }
 
+let isPaused = false;
+
+function togglePausePlay() {
+    const button = document.getElementById("pausePlayButton");
+
+    if (isPaused) {
+        // Resume execution
+        fetch('/resume_execution', { method: 'POST' })
+            .then(response => response.json())
+            .then(data => {
+                if (data.success) {
+                    isPaused = false;
+                    button.innerHTML = "⏸"; // Change to pause icon
+                }
+            })
+            .catch(error => console.error("Error resuming execution:", error));
+    } else {
+        // Pause execution
+        fetch('/pause_execution', { method: 'POST' })
+            .then(response => response.json())
+            .then(data => {
+                if (data.success) {
+                    isPaused = true;
+                    button.innerHTML = "▶"; // Change to play icon
+                }
+            })
+            .catch(error => console.error("Error pausing execution:", error));
+    }
+}
+
 function removeCurrentPattern() {
     if (!selectedFile) {
         logMessage('No file selected to remove.', LOG_TYPE.ERROR);

+ 1 - 0
templates/index.html

@@ -178,6 +178,7 @@
 
             <div class="action-buttons">
                 <button onclick="stopExecution()" class="cancel">Stop Current Pattern</button>
+                <button id="pausePlayButton" onclick="togglePausePlay()" class="cancel">⏸</button> <!-- Default: Pause Icon -->
                 <button onclick="runClearIn()">Clear In</button>
                 <button onclick="runClearOut()">Clear Out</button>
                 <button onclick="moveToCenter()">Move to Center</button>