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());
}
}
Aggregations