Search in sources :

Example 1 with MotorHat

use of com.google.android.things.contrib.driver.motorhat.MotorHat in project robocar by androidthings.

the class RobocarActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // init AdvertisingInfo
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    mAdvertisingInfo = PreferenceUtils.loadAdvertisingInfo(prefs);
    if (mAdvertisingInfo == null) {
        mAdvertisingInfo = AdvertisingInfo.generateAdvertisingInfo();
        PreferenceUtils.saveAdvertisingInfo(prefs, mAdvertisingInfo);
    }
    try {
        mMotorHat = new MotorHat(BoardDefaults.getI2cBus());
    } catch (IOException e) {
        throw new RuntimeException("Failed to create MotorHat", e);
    }
    try {
        mDisplay = new AlphanumericDisplay(BoardDefaults.getI2cBus());
        mDisplay.setEnabled(true);
        mDisplay.setBrightness(0.5f);
        mDisplay.clear();
    } catch (IOException e) {
        // We may not have a display, which is OK. CarController only uses it if it's not null.
        Log.e(TAG, "Failed to open display.", e);
        mDisplay = null;
    }
    try {
        mButtonInputDriver = new ButtonInputDriver(BoardDefaults.getButtonGpioPin(), LogicState.PRESSED_WHEN_HIGH, KeyEvent.KEYCODE_A);
        mButtonInputDriver.register();
    } catch (IOException e) {
        Log.e(TAG, "Failed to open button driver.", e);
        mButtonInputDriver = null;
    }
    String[] ledPins = BoardDefaults.getLedGpioPins();
    mLed = new TricolorLed(ledPins[0], ledPins[1], ledPins[2]);
    mCarController = new CarController(mMotorHat, mLed, mDisplay);
    mResetHandler = new Handler();
    mViewModel = ViewModelProviders.of(this).get(RobocarViewModel.class);
    mNearbyAdvertiser = mViewModel.getRobocarAdvertiser();
    mNearbyAdvertiser.setAdvertisingInfo(mAdvertisingInfo);
    mNearbyAdvertiser.setPairedDiscovererInfo(PreferenceUtils.loadDiscovererInfo(prefs));
    mNearbyAdvertiser.getAdvertisingLiveData().observe(this, new Observer<Boolean>() {

        @Override
        public void onChanged(@Nullable Boolean value) {
            mIsAdvertising = value == null ? false : value;
            updateUi();
        }
    });
    mNearbyAdvertiser.getCompanionConnectionLiveData().observe(this, new Observer<CompanionConnection>() {

        @Override
        public void onChanged(@Nullable CompanionConnection connection) {
            setConnection(connection);
        }
    });
    if (savedInstanceState == null) {
        // First launch. Attach the connector fragment and give it our client to connect.
        ConnectorFragment.attachTo(this, mViewModel.getGoogleApiClient());
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) Handler(android.os.Handler) IOException(java.io.IOException) MotorHat(com.google.android.things.contrib.driver.motorhat.MotorHat) AlphanumericDisplay(com.google.android.things.contrib.driver.ht16k33.AlphanumericDisplay) ButtonInputDriver(com.google.android.things.contrib.driver.button.ButtonInputDriver)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 Handler (android.os.Handler)1 ButtonInputDriver (com.google.android.things.contrib.driver.button.ButtonInputDriver)1 AlphanumericDisplay (com.google.android.things.contrib.driver.ht16k33.AlphanumericDisplay)1 MotorHat (com.google.android.things.contrib.driver.motorhat.MotorHat)1 IOException (java.io.IOException)1