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. 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 ## 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 computer running Linux
- A Nintendo Switch - A Nintendo Switch

View file

@ -23,20 +23,29 @@ class ControllerServer():
self.controller_type, self.controller_type,
self.bt.address) 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. """Runs the mainloop of the controller server.
:param reconnect_address: The Bluetooth MAC address of a :param reconnect_address: The Bluetooth MAC address of a
previously connected to Nintendo Switch, defaults to None previously connected to Nintendo Switch, defaults to None
:type reconnect_address: string, optional :type reconnect_address: string, optional
""" """
print(reconnect_address, state, task_queue)
if state:
state["state"] = "initializing"
self.controller.setup() self.controller.setup()
if reconnect_address: 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: 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 # Mainloop
while True: while True:
@ -66,11 +75,14 @@ class ControllerServer():
else: else:
time.sleep(1/60) time.sleep(1/60)
def connect(self): def connect(self, state=None):
"""Configures as a specified controller, pairs with a Nintendo Switch, """Configures as a specified controller, pairs with a Nintendo Switch,
and creates/accepts sockets for communication with the Switch. and creates/accepts sockets for communication with the Switch.
""" """
if state:
state["state"] = "connecting"
# Creating control and interrupt sockets # Creating control and interrupt sockets
s_ctrl = socket.socket( s_ctrl = socket.socket(
family=socket.AF_BLUETOOTH, family=socket.AF_BLUETOOTH,
@ -120,8 +132,8 @@ class ControllerServer():
self.protocol.process_commands(reply) self.protocol.process_commands(reply)
msg = self.protocol.get_report() msg = self.protocol.get_report()
#if reply: # if reply:
# print(format_msg_controller(msg)) # print(format_msg_controller(msg))
try: try:
itr.sendall(msg) itr.sendall(msg)
@ -138,13 +150,16 @@ class ControllerServer():
return itr, s_itr, ctrl, s_ctrl 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. """Attempts to reconnect with a Switch at the given address.
:param reconnect_address: The Bluetooth MAC address of the Switch :param reconnect_address: The Bluetooth MAC address of the Switch
:type reconnect_address: string :type reconnect_address: string
""" """
if state:
state["state"] = "reconnecting"
device_path = self.bt.find_device_by_address(reconnect_address) device_path = self.bt.find_device_by_address(reconnect_address)
if not device_path: if not device_path:
raise ValueError( 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 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 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. controller before this script works.
""" """