public void connect() { hub = new Hub("com.example.hello-myo"); System.out.println("Attempting to find a Myo..."); log.info("Attempting to find a Myo"); myo = hub.waitForMyo(10000); if (myo == null) { // throw new RuntimeException("Unable to find a Myo!"); log.info("Unable to find a Myo"); } System.out.println("Connected to a Myo armband!"); log.info("Connected to a Myo armband"); hub.addListener(this); if (hubThread == null) { hubThread = new HubThread(this); hubThread.start(); } }
public static void main(String[] args) { LoggingFactory.getInstance().configure(); LoggingFactory.getInstance().setLevel(Level.INFO); try { MyoThalmic myo = (MyoThalmic) Runtime.start("myo", "MyoThalmic"); myo.test(); Hub hub = new Hub("com.example.hello-myo"); System.out.println("Attempting to find a Myo..."); log.info("Attempting to find a Myo"); Myo myodevice = hub.waitForMyo(10000); if (myodevice == null) { throw new RuntimeException("Unable to find a Myo!"); } System.out.println("Connected to a Myo armband!"); log.info("Connected to a Myo armband"); DataCollector dataCollector = new DataCollector(); hub.addListener(dataCollector); while (true) { hub.run(1000 / 20); // System.out.print(dataCollector); System.out.println("yaw is " + dataCollector.getYaw()); System.out.println("roll is " + dataCollector.getRoll()); System.out.println("pitch is " + dataCollector.getPitch()); Runtime.start("gui", "GUIService"); } } catch (Exception e) { Logging.logError(e); } }
public void connectMyo() { try { LOGGER.info("Connecting to Myo"); updateMyoStatus("Attempting to find a Myo..."); Myo myo = hub.waitForMyo(10000); if (myo == null) { LOGGER.error("Unable to connect to Myo"); throw new RuntimeException("Unable to find a Myo!"); } myo.setStreamEmg(StreamEmgType.STREAM_EMG_ENABLED); LOGGER.info("EMG Stream enabled"); updateMyoStatus("Connected to a Myo armband!"); jsonDataCollector = new JsonDataCollector(); jsonDataCollector.addListsner(new FileMyoDataCollector("c:\\tmp\\myo")); jsonDataCollector.addListsner(new SocketServerCollector()); hub.addListener(jsonDataCollector); LOGGER.info("Listener added"); indicatorMyo.setIndicatorStyle(SimpleIndicator.IndicatorStyle.GREEN); startButton.setDisable(false); startButton.setDefaultButton(true); Task<ObservableList<String>> task = new Task<ObservableList<String>>() { @Override protected ObservableList<String> call() throws Exception { while (true) { List<RecordListener> collect = jsonDataCollector .getListeners() .stream() .filter(s -> s instanceof SocketServerCollector) .collect(Collectors.toList()); List<String> connections = collect .stream() .map(s -> ((SocketServerCollector) s).channels) .flatMap(c -> c.stream().map(f -> mapRemoteAddress(f))) .collect(Collectors.toList()); updateValue(FXCollections.observableList(connections)); Thread.sleep(10); } } }; Task<Boolean> booleanTask = new Task<Boolean>() { @Override protected Boolean call() throws Exception { while (true) { List<RecordListener> collect = jsonDataCollector .getListeners() .stream() .filter(s -> s instanceof SocketServerCollector) .collect(Collectors.toList()); long count = collect .stream() .map(s -> ((SocketServerCollector) s).channels) .flatMap(Collection::stream) .filter(AsynchronousSocketChannel::isOpen) .count(); updateValue(count > 0 ? Boolean.TRUE : Boolean.FALSE); Thread.sleep(10); } } }; Task<SimpleIndicator.IndicatorStyle> stringTask = new Task<SimpleIndicator.IndicatorStyle>() { @Override protected SimpleIndicator.IndicatorStyle call() throws Exception { while (true) { List<RecordListener> collect = jsonDataCollector .getListeners() .stream() .filter(s -> s instanceof SocketServerCollector) .collect(Collectors.toList()); long count = collect .stream() .map(s -> ((SocketServerCollector) s).channels) .flatMap(Collection::stream) .filter(AsynchronousSocketChannel::isOpen) .count(); updateValue( count > 0 ? SimpleIndicator.IndicatorStyle.RED : SimpleIndicator.IndicatorStyle.GREEN); Thread.sleep(10); } } }; inboundConnections.itemsProperty().bind(task.valueProperty()); indicatorServer.onProperty().bind(booleanTask.valueProperty()); // try { // indicatorServer.indicatorStyleProperty().bind(stringTask.valueProperty()); // } catch (Throwable e) { // //??Throws a null ppointer but it works?? // } Thread thread = new Thread(task); thread.setDaemon(true); thread.start(); Thread thread2 = new Thread(booleanTask); thread2.setDaemon(true); thread2.start(); Thread thread3 = new Thread(stringTask); thread3.setDaemon(true); thread3.start(); } catch (Exception e) { e.printStackTrace(); updateMyoStatus("Error: " + e.getMessage()); } }