Reduce CPU usage
This commit is contained in:
		
							parent
							
								
									1f98a9bf46
								
							
						
					
					
						commit
						a6d66b3418
					
				
					 2 changed files with 37 additions and 35 deletions
				
			
		|  | @ -69,7 +69,6 @@ class JoyDance: | ||||||
|         self.available_shortcuts = set() |         self.available_shortcuts = set() | ||||||
| 
 | 
 | ||||||
|         self.accel_data = [] |         self.accel_data = [] | ||||||
|         self.last_accels = [] |  | ||||||
| 
 | 
 | ||||||
|         self.ws = None |         self.ws = None | ||||||
|         self.disconnected = False |         self.disconnected = False | ||||||
|  | @ -242,34 +241,46 @@ class JoyDance: | ||||||
|         async for message in self.ws: |         async for message in self.ws: | ||||||
|             await self.on_message(message) |             await self.on_message(message) | ||||||
| 
 | 
 | ||||||
|  |     async def sleep_approx(self, target_duration): | ||||||
|  |         tmp_duration = target_duration | ||||||
|  |         x = 0.3 | ||||||
|  |         start = time.time() | ||||||
|  |         while True: | ||||||
|  |             tmp_duration = tmp_duration * x | ||||||
|  |             await asyncio.sleep(tmp_duration) | ||||||
|  | 
 | ||||||
|  |             dt = time.time() - start | ||||||
|  |             if dt >= target_duration: | ||||||
|  |                 break | ||||||
|  | 
 | ||||||
|  |             tmp_duration = target_duration - dt | ||||||
|  | 
 | ||||||
|     async def tick(self): |     async def tick(self): | ||||||
|         sleep_duration = FRAME_DURATION * 0.75 |         sleep_duration = FRAME_DURATION | ||||||
|         last_time = time.time() |  | ||||||
|         frames = 0 |         frames = 0 | ||||||
| 
 | 
 | ||||||
|         while True: |         while True: | ||||||
|             if self.disconnected: |             if self.disconnected: | ||||||
|                 break |                 break | ||||||
| 
 | 
 | ||||||
|             # Make sure it runs at exactly 60 FPS |  | ||||||
|             while True: |  | ||||||
|                 time_now = time.time() |  | ||||||
|                 dt = time_now - last_time |  | ||||||
|                 if dt >= FRAME_DURATION: |  | ||||||
|                     break |  | ||||||
|             last_time = time_now |  | ||||||
|             frames = frames + 1 if frames < 3 else 1 |  | ||||||
| 
 |  | ||||||
|             if not self.should_start_accelerometer: |             if not self.should_start_accelerometer: | ||||||
|                 await asyncio.sleep(sleep_duration), |                 frames = 0 | ||||||
|  |                 await asyncio.sleep(sleep_duration) | ||||||
|                 continue |                 continue | ||||||
| 
 | 
 | ||||||
|             await asyncio.gather( |             last_time = time.time() | ||||||
|                 asyncio.sleep(sleep_duration), |             frames = frames + 1 if frames < 3 else 1 | ||||||
|                 self.collect_accelerometer_data(frames), |  | ||||||
|             ) |  | ||||||
| 
 | 
 | ||||||
|     async def collect_accelerometer_data(self, frames): |             await asyncio.gather( | ||||||
|  |                 self.sleep_approx(sleep_duration), | ||||||
|  |                 self.collect_accelerometer_data(), | ||||||
|  |             ) | ||||||
|  |             await self.send_accelerometer_data(frames) | ||||||
|  | 
 | ||||||
|  |             dt = time.time() - last_time | ||||||
|  |             sleep_duration = FRAME_DURATION - (dt - sleep_duration) | ||||||
|  | 
 | ||||||
|  |     async def collect_accelerometer_data(self): | ||||||
|         if self.disconnected: |         if self.disconnected: | ||||||
|             return |             return | ||||||
| 
 | 
 | ||||||
|  | @ -278,25 +289,15 @@ class JoyDance: | ||||||
|             return |             return | ||||||
| 
 | 
 | ||||||
|         try: |         try: | ||||||
|             start = time.time() |             accels = self.joycon.get_accels()  # (x, y, z) | ||||||
|             max_runtime = FRAME_DURATION * 0.5 |  | ||||||
|             while time.time() - start < max_runtime: |  | ||||||
|                 # Make sure accelerometer axes are changed |  | ||||||
|                 accels = self.joycon.get_accels()  # [(x, y, z),...] |  | ||||||
|                 if accels != self.last_accels: |  | ||||||
|                     self.last_accels = accels |  | ||||||
|                     break |  | ||||||
| 
 | 
 | ||||||
|             # Accelerator axes on phone & Joy-Con are different so we need to swap some axes |             # Accelerator axes on phone & Joy-Con are different so we need to swap some axes | ||||||
|             # https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md |             # https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md | ||||||
|             for accel in accels: |             accel = accels[2] | ||||||
|                 x = accel[1] * -1 |             x = accel[1] * -1 | ||||||
|                 y = accel[0] |             y = accel[0] | ||||||
|                 z = accel[2] |             z = accel[2] | ||||||
| 
 |             self.accel_data.append([x, y, z]) | ||||||
|                 self.accel_data.append([x, y, z]) |  | ||||||
| 
 |  | ||||||
|             await self.send_accelerometer_data(frames), |  | ||||||
|         except OSError: |         except OSError: | ||||||
|             self.disconnect() |             self.disconnect() | ||||||
|             return |             return | ||||||
|  |  | ||||||
|  | @ -15,7 +15,8 @@ WS_SUBPROTOCOLS[WsSubprotocolVersion.V1.value] = 'v1.phonescoring.jd.ubisoft.com | ||||||
| WS_SUBPROTOCOLS[WsSubprotocolVersion.V2.value] = 'v2.phonescoring.jd.ubisoft.com' | WS_SUBPROTOCOLS[WsSubprotocolVersion.V2.value] = 'v2.phonescoring.jd.ubisoft.com' | ||||||
| 
 | 
 | ||||||
| FRAME_DURATION = 1 / 60 | FRAME_DURATION = 1 / 60 | ||||||
| ACCEL_ACQUISITION_FREQ_HZ = 200  # Hz | SEND_FREQ_MS = 0.005 | ||||||
|  | ACCEL_ACQUISITION_FREQ_HZ = 60  # Hz | ||||||
| ACCEL_ACQUISITION_LATENCY = 40  # ms | ACCEL_ACQUISITION_LATENCY = 40  # ms | ||||||
| ACCEL_MAX_RANGE = 8  # ±G | ACCEL_MAX_RANGE = 8  # ±G | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue