Refactoring

This commit is contained in:
Brikwerk 2020-05-27 01:15:18 -07:00
commit b3b0a73364
4 changed files with 29 additions and 22 deletions

View file

@ -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

View file

@ -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(

View file

@ -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()

View file

@ -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.
"""