def __init__(self, adapter_name): self._manager = nuimo.ControllerManager(adapter_name) self._manager.listener = self self._is_running = False # Prevents from considering D-Bus events if we aren't running self._discovery_timeout_timer = None self._controller = None self._required_mac_address = None
def main(): arg_parser = ArgumentParser(description="Nuimo Controller Demo") arg_parser.add_argument( '--adapter', default='hci0', help="Name of Bluetooth adapter, defaults to 'hci0'") arg_commands_group = arg_parser.add_mutually_exclusive_group(required=True) arg_commands_group.add_argument('--discover', action='store_true', help="Lists all nearby Nuimo controllers") arg_commands_group.add_argument( '--connect', metavar='address', type=str, help="Connect to a Nuimo controller with a given MAC address") arg_commands_group.add_argument( '--auto', metavar='address', type=str, help= "Connect and automatically reconnect to a Nuimo controller with a given MAC address" ) arg_commands_group.add_argument( '--disconnect', metavar='address', type=str, help="Disconnect a Nuimo controller with a given MAC address") args = arg_parser.parse_args() global controller_manager controller_manager = nuimo.ControllerManager(adapter_name=args.adapter) if args.discover: controller_manager.listener = ControllerManagerPrintListener() controller_manager.start_discovery() elif args.connect: controller = nuimo.Controller(adapter_name=args.adapter, mac_address=args.connect) controller.listener = ControllerTestListener(controller=controller) controller.connect() elif args.auto: controller = nuimo.Controller(adapter_name=args.adapter, mac_address=args.auto) controller.listener = ControllerTestListener(controller=controller, auto_reconnect=True) controller.connect() elif args.disconnect: controller = nuimo.Controller(adapter_name=args.adapter, mac_address=args.disconnect) if not controller.is_connected(): print("Already disconnected") return controller.listener = ControllerTestListener(controller=controller) controller.disconnect() print("Terminate with Ctrl+C") try: controller_manager.run() except KeyboardInterrupt: pass
def __init__(self, params: Dict[str, str]): import nuimo self.service = "NUIMO" mac = params.get("mac") if mac is None: logging.error("mac parameter for Nuimo undefined, aborting") return adaptername = params.get("adapter_name", "hci1") self.manager = nuimo.ControllerManager(adapter_name='hci0') self.nuimocontroller = nuimo.Controller(manager=self.manager, mac_address=mac) self.nuimocontroller.listener = Nuimo.NuimoControllerListener() self.nuimocontroller.listener.controller = self.nuimocontroller self.nuimocontroller.connect() thread = threading.Thread(target=self.runloop, args=()) thread.daemon = True thread.start()
def _restart_bluetooth(self): """ Restarts BlueZ and hard resets the BT module. Sets the self._bt_restart_underway to false to signal the end of bt restart. It also reinitialises the python objects of this class responsible for interacting with the underlying bt infrastructure. """ hard_reset_module_cmd = "systemctl restart enable-bt-module" restart_bluez_cmd = "systemctl restart bluetooth" logger.debug("Hard restarting systemd bluetooth related services") subprocess.run(hard_reset_module_cmd.split()) subprocess.run(restart_bluez_cmd.split()) # Reinitialise the depend on the bt module # Assumed this is needed, not confirmed logger.debug("Reinitialisng python object after restart") del self._gatt_manager del self._manager self._gatt_manager = gatt.DeviceManager(self._adapter_name) self._manager = nuimo.ControllerManager(self._adapter_name) # This smells like it could cause trouble. # Do doubt it for it wasn't thought of deeply. del self._manager.listener self._manager.listener = self self._controller = None logger.debug("Powering BT module after restart") self._manager.is_adapter_powered = True self._bt_restart_underway = False logger.info("Bluetooth restart done")
def __init__(self, adapter_name): # A quick way to test how often are issues causes by adapter naming logger.debug("Using adapter (self.ble_adapter_name): %s" % adapter_name) import subprocess output = subprocess.check_output("hciconfig") logger.debug("Adapter (from hciconfig): %s " % str(output.split()[0])) self._adapter_name = adapter_name self._gatt_manager = gatt.DeviceManager(self._adapter_name) self._manager = nuimo.ControllerManager(self._adapter_name) self._manager.listener = self self._is_running = False # Prevents from considering D-Bus events if we aren't running self._discovery_timeout_timer = None self._connect_timeout_timer = None self._controller = None self._required_mac_address = None self._discovery_duration = None self._connect_duration = None self._nuimo_connected = False self._nuimo_onboarding_attempts = 0 # A control flag not to do any action while restart # is underway self._bt_restart_underway = False
" * * " " * " " * * " " * * " " * * " "* *") self.controller.display_matrix(matrix, interval=10) print(event.gesture, event.gesture == nuimo.Gesture.ROTATION) if (event.gesture == nuimo.Gesture.ROTATION): print(event.value) if (event.gesture == nuimo.Gesture.BUTTON_PRESS): pass if __name__ == '__main__': manager = nuimo.ControllerManager(adapter_name='hci0') controller = nuimo.Controller(mac_address=NUIMO_ADDRESS, manager=manager) controller.listener = ControllerPrintListener( controller ) # Use an instance of your own nuimo.ControllerListener subclass controller.connect() matrix = nuimo.LedMatrix("* *" " * * " " * * " " * * " " * " " * * " " * * " " * * " "* *")