da03 commited on
Commit
274b099
·
1 Parent(s): 2b99d5a
Files changed (2) hide show
  1. main.py +30 -0
  2. static/index.html +18 -0
main.py CHANGED
@@ -235,6 +235,30 @@ async def websocket_endpoint(websocket: WebSocket):
235
  input_queue = asyncio.Queue()
236
  is_processing = False
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  async def process_input(data):
239
  nonlocal previous_frame, hidden_states, keys_down, frame_num, frame_count, is_processing
240
 
@@ -377,6 +401,12 @@ async def websocket_endpoint(websocket: WebSocket):
377
  await websocket.send_json({"type": "heartbeat_response"})
378
  continue
379
 
 
 
 
 
 
 
380
  # Add the input to our queue
381
  await input_queue.put(data)
382
  print(f"[{receive_time:.3f}] Received input. Queue size now: {input_queue.qsize()}")
 
235
  input_queue = asyncio.Queue()
236
  is_processing = False
237
 
238
+ # Add a function to reset the simulation
239
+ async def reset_simulation():
240
+ nonlocal previous_frame, hidden_states, keys_down, frame_num, is_processing, input_queue
241
+
242
+ # Clear the input queue
243
+ while not input_queue.empty():
244
+ try:
245
+ input_queue.get_nowait()
246
+ input_queue.task_done()
247
+ except asyncio.QueueEmpty:
248
+ break
249
+
250
+ # Reset all state variables
251
+ previous_frame = padding_image
252
+ hidden_states = None
253
+ keys_down = set()
254
+ frame_num = -1
255
+ is_processing = False
256
+
257
+ print(f"[{time.perf_counter():.3f}] Simulation reset to initial state")
258
+
259
+ # Send confirmation to client
260
+ await websocket.send_json({"type": "reset_confirmed"})
261
+
262
  async def process_input(data):
263
  nonlocal previous_frame, hidden_states, keys_down, frame_num, frame_count, is_processing
264
 
 
401
  await websocket.send_json({"type": "heartbeat_response"})
402
  continue
403
 
404
+ # Handle reset command
405
+ if data.get("type") == "reset":
406
+ print(f"[{receive_time:.3f}] Received reset command")
407
+ await reset_simulation()
408
+ continue
409
+
410
  # Add the input to our queue
411
  await input_queue.put(data)
412
  print(f"[{receive_time:.3f}] Received input. Queue size now: {input_queue.qsize()}")
static/index.html CHANGED
@@ -14,6 +14,9 @@
14
  <body>
15
  <canvas id="displayCanvas" width="512" height="384"></canvas>
16
 
 
 
 
17
  <script>
18
  const canvas = document.getElementById('displayCanvas');
19
  const ctx = canvas.getContext('2d');
@@ -61,6 +64,9 @@
61
  //isProcessing = false; // Reset the processing flag when we get a response
62
  };
63
  img.src = 'data:image/png;base64,' + data.image;
 
 
 
64
  }
65
  };
66
  }
@@ -212,6 +218,18 @@
212
  }
213
  }
214
  });
 
 
 
 
 
 
 
 
 
 
 
 
215
  </script>
216
  </body>
217
  </html>
 
14
  <body>
15
  <canvas id="displayCanvas" width="512" height="384"></canvas>
16
 
17
+ <!-- Add this button somewhere in your UI, perhaps near other controls -->
18
+ <button id="resetButton" class="control-button">Reset Simulation</button>
19
+
20
  <script>
21
  const canvas = document.getElementById('displayCanvas');
22
  const ctx = canvas.getContext('2d');
 
64
  //isProcessing = false; // Reset the processing flag when we get a response
65
  };
66
  img.src = 'data:image/png;base64,' + data.image;
67
+ } else if (data.type === "reset_confirmed") {
68
+ console.log("Simulation reset confirmed by server");
69
+ // Optionally update UI to reflect reset state
70
  }
71
  };
72
  }
 
218
  }
219
  }
220
  });
221
+
222
+ // Add event listener for the reset button
223
+ document.getElementById('resetButton').addEventListener('click', function() {
224
+ if (socket && socket.readyState === WebSocket.OPEN) {
225
+ console.log("Sending reset command to server");
226
+ socket.send(JSON.stringify({
227
+ type: "reset"
228
+ }));
229
+ } else {
230
+ console.error("WebSocket not connected, cannot reset");
231
+ }
232
+ });
233
  </script>
234
  </body>
235
  </html>