Skip to content

Commit

Permalink
bitcraze#473 Moved interface selection into connectivity manager
Browse files Browse the repository at this point in the history
  • Loading branch information
krichardsson committed Mar 3, 2021
1 parent 355c400 commit d89eb52
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
45 changes: 40 additions & 5 deletions src/cfclient/ui/connectivity_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,16 @@ class ConnectivityManager(QObject):
'connect_button',
'scan_button'])

CONNECTION_STATE_DISCONNECTED = 0
CONNECTION_STATE_CONNECTING = 1
CONNECTION_STATE_CONNECTED = 2
CONNECTION_STATE_SCANNING = 3

INTERFACE_PROMPT_TEXT = 'Select an interface'

connect_button_clicked = pyqtSignal()
scan_button_clicked = pyqtSignal(object)
interface_combo_current_index_changed = pyqtSignal(object)
connection_state_changed = pyqtSignal(object)

def __init__(self):
QObject.__init__(self)
Expand All @@ -59,7 +66,10 @@ def register_ui_elements(self, ui_elements):
ui_elements.interface_combo.currentIndexChanged['QString'].connect(
self._interface_combo_current_index_changed_handler)

def set_state_disconnected(self, can_connect):
def set_state_disconnected(self):
self.connection_state_changed.emit(self.CONNECTION_STATE_DISCONNECTED)

can_connect = self.get_interface() is not None
for ui_elements in self._ui_elements:
ui_elements.connect_button.setText("Connect")
ui_elements.connect_button.setToolTip("Connect to the Crazyflie on the selected interface (Ctrl+I)")
Expand All @@ -70,6 +80,7 @@ def set_state_disconnected(self, can_connect):
ui_elements.interface_combo.setEnabled(True)

def set_state_connected(self):
self.connection_state_changed.emit(self.CONNECTION_STATE_CONNECTED)
for ui_elements in self._ui_elements:
ui_elements.connect_button.setText("Disconnect")
ui_elements.connect_button.setToolTip("Disconnect from the Crazyflie (Ctrl+I)")
Expand All @@ -78,6 +89,7 @@ def set_state_connected(self):
ui_elements.interface_combo.setEnabled(False)

def set_state_connecting(self):
self.connection_state_changed.emit(self.CONNECTION_STATE_CONNECTING)
for ui_elements in self._ui_elements:
ui_elements.connect_button.setText("Cancel")
ui_elements.connect_button.setToolTip("Cancel connecting to the Crazyflie")
Expand All @@ -86,6 +98,7 @@ def set_state_connecting(self):
ui_elements.interface_combo.setEnabled(False)

def set_state_scanning(self):
self.connection_state_changed.emit(self.CONNECTION_STATE_SCANNING)
for ui_elements in self._ui_elements:
ui_elements.connect_button.setText("Connect")
ui_elements.connect_button.setEnabled(False)
Expand All @@ -94,6 +107,13 @@ def set_state_scanning(self):
ui_elements.address_spinner.setEnabled(False)
ui_elements.interface_combo.setEnabled(False)

def set_enable(self, enable):
for ui_elements in self._ui_elements:
ui_elements.connect_button.setEnabled(enable)
ui_elements.scan_button.setEnabled(enable)
ui_elements.address_spinner.setEnabled(enable)
ui_elements.interface_combo.setEnabled(enable)

def set_address(self, address):
for ui_elements in self._ui_elements:
ui_elements.address_spinner.setValue(address)
Expand All @@ -105,12 +125,27 @@ def get_address(self):
return 0

def set_interfaces(self, interface_items, index):
new_index = 0
if index is not None:
new_index = index + 1

for ui_elements in self._ui_elements:
combo = ui_elements.interface_combo

combo.clear()
combo.addItem(self.INTERFACE_PROMPT_TEXT)
combo.addItems(interface_items)
combo.setCurrentIndex(index)
combo.setCurrentIndex(new_index)

def get_interface(self):
if len(self._ui_elements) > 0:
interface = self._ui_elements[0].interface_combo.currentText()
if interface == self.INTERFACE_PROMPT_TEXT:
self._selected_interface = None
else:
return interface
else:
return None

def _connect_button_click_handler(self):
self.connect_button_clicked.emit()
Expand Down Expand Up @@ -140,9 +175,9 @@ def _address_edited_handler(self):
ui_elements.address_spinner.setValue(value)

def _interface_combo_current_index_changed_handler(self, interface):
can_connect = interface != self.INTERFACE_PROMPT_TEXT
for ui_elements in self._ui_elements:
combo = ui_elements.interface_combo
if combo.currentText != interface:
combo.setCurrentText(interface)

self.interface_combo_current_index_changed.emit(interface)
ui_elements.connect_button.setEnabled(can_connect)
35 changes: 10 additions & 25 deletions src/cfclient/ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@

logger = logging.getLogger(__name__)

INTERFACE_PROMPT_TEXT = 'Select an interface'

(main_window_class,
main_windows_base_class) = (uic.loadUiType(cfclient.module_path +
'/ui/main.ui'))
Expand Down Expand Up @@ -187,7 +185,6 @@ def __init__(self, *args):

self._connectivity_manager.connect_button_clicked.connect(self._connect)
self._connectivity_manager.scan_button_clicked.connect(self._scan)
self._connectivity_manager.interface_combo_current_index_changed.connect(self.interfaceChanged)

self._auto_reconnect_enabled = Config().get("auto_reconnect")
self.autoReconnectCheckBox.toggled.connect(
Expand Down Expand Up @@ -231,8 +228,6 @@ def __init__(self, *args):
self.linkQualitySignal.connect(
lambda percentage: self.linkQualityBar.setValue(percentage))

self._selected_interface = None

# Parse the log configuration files
self.logConfigReader = LogConfigReader(self.cf)

Expand Down Expand Up @@ -394,17 +389,8 @@ def disable_input(self, disable):
"""
self._disable_input = disable

def interfaceChanged(self, interface):
if interface == INTERFACE_PROMPT_TEXT:
self._selected_interface = None
else:
self._selected_interface = interface
self._update_ui_state()

def foundInterfaces(self, interfaces):
selected_interface = self._selected_interface

interface_items = [INTERFACE_PROMPT_TEXT]
selected_interface = self._connectivity_manager.get_interface()

formatted_interfaces = []
for i in interfaces:
Expand All @@ -413,7 +399,6 @@ def foundInterfaces(self, interfaces):
else:
interface = i[0]
formatted_interfaces.append(interface)
interface_items += formatted_interfaces

if self._initial_scan:
self._initial_scan = False
Expand All @@ -432,31 +417,31 @@ def foundInterfaces(self, interfaces):
if len(interfaces) == 1 and selected_interface is None:
selected_interface = interfaces[0][0]

newIndex = 0
newIndex = None
if selected_interface is not None:
try:
newIndex = formatted_interfaces.index(selected_interface) + 1
newIndex = formatted_interfaces.index(selected_interface)
except ValueError:
pass

self._connectivity_manager.set_interfaces(interface_items, newIndex)
self._connectivity_manager.set_interfaces(formatted_interfaces, newIndex)

self.uiState = UIState.DISCONNECTED
self._update_ui_state()

def _update_ui_state(self):
if self.uiState == UIState.DISCONNECTED:
self.setWindowTitle("Not connected")
canConnect = self._selected_interface is not None
canConnect = self._connectivity_manager.get_interface() is not None
self.menuItemConnect.setText("Connect to Crazyflie")
self.menuItemConnect.setEnabled(canConnect)
self._connectivity_manager.set_state_disconnected(canConnect)
self._connectivity_manager.set_state_disconnected()
self.batteryBar.setValue(3000)
self._menu_cf2_config.setEnabled(False)
self.linkQualityBar.setValue(0)
self.logConfigAction.setEnabled(False)
elif self.uiState == UIState.CONNECTED:
s = "Connected on %s" % self._selected_interface
s = "Connected on %s" % self._connectivity_manager.get_interface()
self.setWindowTitle(s)
self.menuItemConnect.setText("Disconnect")
self.menuItemConnect.setEnabled(True)
Expand All @@ -467,7 +452,7 @@ def _update_ui_state(self):
if len(self.cf.mem.get_mems(MemoryElement.TYPE_I2C)) > 0:
self._menu_cf2_config.setEnabled(True)
elif self.uiState == UIState.CONNECTING:
s = "Connecting to {} ...".format(self._selected_interface)
s = "Connecting to {} ...".format(self._connectivity_manager.get_interface())
self.setWindowTitle(s)
self.menuItemConnect.setText("Cancel")
self.menuItemConnect.setEnabled(True)
Expand Down Expand Up @@ -535,7 +520,7 @@ def _connected(self):
self.uiState = UIState.CONNECTED
self._update_ui_state()

Config().set("link_uri", str(self._selected_interface))
Config().set("link_uri", str(self._connectivity_manager.get_interface()))

lg = LogConfig("Battery", 1000)
lg.add_variable("pm.vbat", "float")
Expand Down Expand Up @@ -606,7 +591,7 @@ def _connect(self):
self.uiState = UIState.DISCONNECTED
self._update_ui_state()
else:
self.cf.open_link(self._selected_interface)
self.cf.open_link(self._connectivity_manager.get_interface())

def _scan(self, address):
self.uiState = UIState.SCANNING
Expand Down

0 comments on commit d89eb52

Please sign in to comment.