Skip to content
Snippets Groups Projects
Commit f9ac9143 authored by Denver Pallis's avatar Denver Pallis :radioactive:
Browse files

voluxcontroller: start/stop test joystick thread on __enter__/__exit__

parent a1f13d90
No related branches found
No related tags found
1 merge request!5Feature/voluxcontroller
from voluxcontroller import VoluxController
from time import sleep
with VoluxController() as controller:
while True:
sleep(1 / 60)
......@@ -2,6 +2,7 @@ from approxeng.input.selectbinder import ControllerResource
from time import sleep
from volux.module import VoluxSource
from typing import Any
from threading import Thread
def _prepare():
......@@ -12,6 +13,38 @@ def _cleanup():
return
def _joystick_thread(controller_instance):
# while controller instance is running (controlled by start/stop methods)
while controller_instance.running:
print("joystick thread running!")
sleep(1)
# try:
# with ControllerResource() as joystick:
# print("Found a joystick and connected")
# while joystick.connected:
# # Do stuff with your joystick here!
# # ....
# # ....
# print("do stuff!")
# sleep(1)
# # Joystick disconnected...
# print("Connection to joystick lost")
# except IOError:
# # No joystick found, wait for a bit before trying again
# print("Unable to find any joysticks")
# sleep(1.0)
# self.joystick = ControllerResource()
# self.joystick.__enter__()
# # HACK: `ControllerResource.__exit__()`'s arguments aren't even used, so
# # ... just pass in some empty strings so we don't get an error about
# # ... missing arguments
# self.joystick.__exit__("", "", "")
# self.joystick = None # set joystick attrib back to None
class VoluxController(VoluxSource):
def __init__(self, polling_rate=1000):
_ms_between_polls = 1 / polling_rate
......@@ -22,21 +55,27 @@ class VoluxController(VoluxSource):
)
def start(self):
raise NotImplementedError("Not yet implemented")
# self.joystick = # TODO
print("starting joystick thread!")
self.running = True
self._t_joystick = Thread(target=_joystick_thread, args=(self,))
self._t_joystick.start()
return True
def stop(self):
self.joystick = None # set joystick attrib back to None
print("stopping joystick thread!")
self.running = False
self._t_joystick.join()
return True
def __enter__(self):
"""Start stream."""
"""Start joystick thread."""
self.start()
return self
def __exit__(
self, exception_type: Any, exception_value: Any, traceback: Any
) -> Any:
"""Stop stream."""
"""Stop joystick thread."""
self.stop()
# NOTE: WARNING! - don't return anything from this method!
# ... Exceptions will be suppressed on exit!
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment