@Override public void onDestroy() { super.onDestroy(); // We don't want any callbacks when the Service is gone, so unregister the listener. Hub.getInstance().removeListener(mListener); Hub.getInstance().shutdown(); }
@Override public void onCreate() { super.onCreate(); // First, we initialize the Hub singleton with an application identifier. Hub hub = Hub.getInstance(); if (!hub.init(this, getPackageName())) { showToast("Couldn't initialize Hub"); stopSelf(); return; } // Next, register for DeviceListener callbacks. hub.addListener(mListener); // Finally, scan for Myo devices and connect to the first one found. hub.pairWithAnyMyo(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_workout); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayShowTitleEnabled(false); TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); toolbarTitle.setVisibility(View.GONE); // ActionBar actionbar = getSupportActionBar(); // actionbar.setDisplayHomeAsUpEnabled(true); FrameLayout container = (FrameLayout) findViewById(R.id.fragment_container); ExerciseModel workout = (ExerciseModel) getIntent().getExtras().getSerializable("Model"); WorkoutIntroFragment introfragment = new WorkoutIntroFragment(); Bundle newBundle = new Bundle(); newBundle.putSerializable("Model", workout); introfragment.setArguments(newBundle); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.setCustomAnimations(R.anim.fragment_enter_anim, R.anim.fragment_exit_anim); ft.replace(R.id.fragment_container, introfragment, "intro"); // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); Hub hub = Hub.getInstance(); // if (!hub.init(this, getPackageName())) { // // We can't do anything with the Myo device if the Hub can't be initialized, so // exit. // Toast.makeText(this, "Couldn't initialize Hub", Toast.LENGTH_SHORT).show(); // finish(); // return; // } // Next, register for DeviceListener callbacks. hub.addListener(mListener); hub.setLockingPolicy(Hub.LockingPolicy.NONE); }
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(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.counter_activity); tvCounter = (TextView) findViewById(R.id.tvCounter); cflCounter = (CounterFrameLayout) findViewById(R.id.cflCounter); tvCounter.setText("" + mRepCounter); mHub = Hub.getInstance(); if (!mHub.init(this, getPackageName())) { // We can't do anything with the Myo device if the Hub can't be initialized, so exit. Toast.makeText(this, "Couldn't initialize Hub", Toast.LENGTH_SHORT).show(); finish(); return; } mTextToSpeech = new TextToSpeech( getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) {} }); mTextToSpeech.setLanguage(Locale.US); Intent intent = getIntent(); mWorkout = intent.getParcelableExtra(Workout.TAG); mHub.setLockingPolicy(Hub.LockingPolicy.NONE); mHub.addListener(mWorkout); float peak = intent.getFloatExtra(CalibrationActivity.KEY_PEAK, 0); float dip = intent.getFloatExtra(CalibrationActivity.KEY_DIP, 0); mWorkout.setRange(dip, peak); mWorkout.setCallback(this); Log.e(TAG, "PEAK: " + dip); Log.e(TAG, "DIP: " + peak); }
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 startAction() { collecting = true; jsonDataCollector.getListeners().stream().forEach(RecordListener::start); startButton.setDefaultButton(false); stopButton.setDefaultButton(true); toggleButtons(); // todo must be killed by stop action new Thread( () -> { while (collecting) { hub.run(100); } System.out.println("collecting = " + collecting); }) .start(); }
@Override protected void onDestroy() { super.onDestroy(); mHub.removeListener(mWorkout); }
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()); } }