From b3b0a73364f3a719a527efeb3b9c46f5f775711a Mon Sep 17 00:00:00 2001 From: Brikwerk Date: Wed, 27 May 2020 01:15:18 -0700 Subject: [PATCH] Refactoring --- README.md | 8 ++++++-- nxbt/controller/server.py | 29 ++++++++++++++++++++++------- nxbt/manager.py | 12 ------------ scripts/{jc_proxy.py => proxy.py} | 2 +- 4 files changed, 29 insertions(+), 22 deletions(-) delete mode 100644 nxbt/manager.py rename scripts/{jc_proxy.py => proxy.py} (99%) diff --git a/README.md b/README.md index 670790f..86e4c13 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ -# NXBT - Control your Switch Locally or Remotely +# NXBT + +Control a Nintendo Switch Locally or Remotely + +--- This is meant to serve as an all-in-one solution to controlling a Nintendo Switch from a variety of devices. @@ -6,7 +10,7 @@ Functionality is currently under development. ## Prerequisites -- A Bluetooth adapter with a Bluetooth version greather than or equal to 4.0 +- A Bluetooth adapter with a Bluetooth version greater than or equal to 4.0 - A computer running Linux - A Nintendo Switch diff --git a/nxbt/controller/server.py b/nxbt/controller/server.py index 2a4b0ad..ef23d2c 100644 --- a/nxbt/controller/server.py +++ b/nxbt/controller/server.py @@ -23,20 +23,29 @@ class ControllerServer(): self.controller_type, self.bt.address) - def run(self, reconnect_address=None): + def run(self, reconnect_address=None, state=None, task_queue=None): """Runs the mainloop of the controller server. :param reconnect_address: The Bluetooth MAC address of a previously connected to Nintendo Switch, defaults to None :type reconnect_address: string, optional """ + + print(reconnect_address, state, task_queue) + + if state: + state["state"] = "initializing" self.controller.setup() if reconnect_address: - itr, s_itr, ctrl, s_ctrl = self.reconnect(reconnect_address) + itr, s_itr, ctrl, s_ctrl = self.reconnect( + reconnect_address, state=state) else: - itr, s_itr, ctrl, s_ctrl = self.connect() + itr, s_itr, ctrl, s_ctrl = self.connect(state=state) + + if state: + state["state"] = "connected" # Mainloop while True: @@ -66,11 +75,14 @@ class ControllerServer(): else: time.sleep(1/60) - def connect(self): + def connect(self, state=None): """Configures as a specified controller, pairs with a Nintendo Switch, and creates/accepts sockets for communication with the Switch. """ + if state: + state["state"] = "connecting" + # Creating control and interrupt sockets s_ctrl = socket.socket( family=socket.AF_BLUETOOTH, @@ -120,8 +132,8 @@ class ControllerServer(): self.protocol.process_commands(reply) msg = self.protocol.get_report() - #if reply: - # print(format_msg_controller(msg)) + # if reply: + # print(format_msg_controller(msg)) try: itr.sendall(msg) @@ -138,13 +150,16 @@ class ControllerServer(): return itr, s_itr, ctrl, s_ctrl - def reconnect(self, reconnect_address): + def reconnect(self, reconnect_address, state=None): """Attempts to reconnect with a Switch at the given address. :param reconnect_address: The Bluetooth MAC address of the Switch :type reconnect_address: string """ + if state: + state["state"] = "reconnecting" + device_path = self.bt.find_device_by_address(reconnect_address) if not device_path: raise ValueError( diff --git a/nxbt/manager.py b/nxbt/manager.py deleted file mode 100644 index 69aa933..0000000 --- a/nxbt/manager.py +++ /dev/null @@ -1,12 +0,0 @@ -from multiprocessing import Process -from multiprocessing import Queue - -from .controller import ControllerServer -from .controller import ControllerTypes - - -class Nxbt(): - - def __init__(self): - - self.task_queue = Queue() diff --git a/scripts/jc_proxy.py b/scripts/proxy.py similarity index 99% rename from scripts/jc_proxy.py rename to scripts/proxy.py index 4b3ec3c..587a26c 100644 --- a/scripts/jc_proxy.py +++ b/scripts/proxy.py @@ -1,7 +1,7 @@ """ This is a quick and dirty script for recording input from a controller and dumping it into a "messages.txt" file. You'll need to input the -devices Bluetooth MAC address manually and specify the type of +device's Bluetooth MAC address manually and specify the type of controller before this script works. """