Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • Teckids/hacknfun/libs/mynit
  • schubisu/esp-init
2 results
Show changes
Commits on Source (5)
from kivy.app import App from kivy.app import App
from kivy.clock import mainthread
from kivy.logger import Logger from kivy.logger import Logger
from kivy.properties import ObjectProperty from kivy.properties import ObjectProperty
from kivy.uix.button import Button from kivy.uix.button import Button
...@@ -12,17 +13,30 @@ class DeviceListWidget(Widget): ...@@ -12,17 +13,30 @@ class DeviceListWidget(Widget):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._devices_buttons_map = {} self._previous_addresses = {}
def update_device_buttons(self, devices): @mainthread
Logger.debug("Updating device buttons") def update_device_buttons(self, buttons_map):
self._devices_buttons_map = devices Logger.info("Updating device buttons")
self.device_buttons.clear_widgets() for address, info in buttons_map.items():
for label, device in self._devices_buttons_map.items(): if address in self._previous_addresses:
Logger.debug("Adding button for %s" % label) Logger.info("Button for %s already there, skipping" % address)
btn = Button(text=label) continue
label, cb_connect = info
Logger.info("Adding button for %s (%s)" % (address, label))
btn = Button(text=label, on_press=cb_connect)
self.device_buttons.add_widget(btn) self.device_buttons.add_widget(btn)
self._previous_addresses[address] = btn
for address in self._previous_addresses:
if address not in self._previous_addresses:
Logger.info("%s no longer in device list, removing button" % address)
self.remove_widget(self._previous_addresses[address])
del self._previous_addresses[address]
class MynitCompanionApp(App): class MynitCompanionApp(App):
...@@ -32,4 +46,21 @@ class MynitCompanionApp(App): ...@@ -32,4 +46,21 @@ class MynitCompanionApp(App):
self._bluetooth = MynitBluetooth(self) self._bluetooth = MynitBluetooth(self)
def update_device_list(self, devices): def update_device_list(self, devices):
self.widget.update_device_buttons({device.getAddress(): device for device in devices}) Logger.info("Updating device buttons")
buttons_map = {}
for device in devices:
if not device.getName():
Logger.info("Device %s skipped due to missing name" % device.getAddress())
continue
def cb_connect(*args, **kwargs):
self._bluetooth.set_device(device)
buttons_map[device.getAddress()] = (device.getName(), cb_connect)
self.widget.update_device_buttons(buttons_map)
def build(self):
self.widget = DeviceListWidget()
return self.widget
...@@ -16,7 +16,7 @@ class MynitBluetooth(BluetoothDispatcher): ...@@ -16,7 +16,7 @@ class MynitBluetooth(BluetoothDispatcher):
self._app = app self._app = app
self._available_devices = set() self._available_devices = set()
self._current_device = None self._device = None
self._characteristics = None, None self._characteristics = None, None
Logger.info("Starting BLE scanning") Logger.info("Starting BLE scanning")
...@@ -24,26 +24,35 @@ class MynitBluetooth(BluetoothDispatcher): ...@@ -24,26 +24,35 @@ class MynitBluetooth(BluetoothDispatcher):
self.start_scan() self.start_scan()
def on_device(self, device, rssi, advertisement): def on_device(self, device, rssi, advertisement):
Logger.debug("BLE device discovered: %s" % device.getAddress()) Logger.info("BLE device discovered: %s" % device.getAddress())
self.stop_scan()
if device not in self._available_devices: if device not in self._available_devices:
Logger.info("New BLE device discovered: %s" % device.getAddress()) Logger.info("New BLE device discovered: %s (%s)" % (device.getAddress(), device.getName()))
self._available_devices.add(device) self._available_devices.add(device)
self._app.update_device_list(self._available_devices) self._app.update_device_list(self._available_devices)
def set_device(self, device):
self._device = device
self.stop_scan()
def on_scan_completed(self):
if self._device:
self.connect_gatt(self._device)
def on_connection_state_change(self, status, state): def on_connection_state_change(self, status, state):
Logger.debug("Connection state changed") Logger.info("Connection state changed - state: %d, status: %d" % (state, status))
if status == GATT_SUCCESS and state: if status == GATT_SUCCESS:
Logger.info("Connection successful, discovering services") Logger.info("Connection successful, discovering services")
self._current_device = device
self.discover_services() self.discover_services()
else: else:
Logger.warning("Connection failed, disconnecting and resetting") Logger.warning("Connection failed, disconnecting and resetting")
self.close_gatt() self.close_gatt()
self._current_device = None
self._characteristics = None, None self._characteristics = None, None
self._device = None
self.start_scan()
def on_services(self, status, services): def on_services(self, status, services):
Logger.info("Services discovered: %s" % services)
self._characteristics = (services.search(_UART_SERVICE_TX_UUID), services.search(_UART_SERVICE_RX_UUID)) self._characteristics = (services.search(_UART_SERVICE_TX_UUID), services.search(_UART_SERVICE_RX_UUID))
#:kivy 1.0.9 #:kivy 1.0.9
DeviceListWidget: <DeviceListWidget>:
device_buttons: device_buttons device_buttons: device_buttons
BoxLayout: BoxLayout:
......