protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); if (_useHubs) { InitializeHubs(); } SetContentView(Resource.Layout.Main); SensorManager = (SensorManager)GetSystemService(SensorService); _dynamicSensorCallback = new WeatherDynamicSensorCallback(this); _imageView = FindViewById <ImageView>(Resource.Id.imageView); PubSubHandler.GetInstance().Subscribe <TemperatureMessage>(OnTemperatureMessage); PubSubHandler.GetInstance().Subscribe <PressureMessage>(OnPressureMessage); PubSubHandler.GetInstance().Subscribe <HumidityMessage>(OnHumidityMessage); try { _ledRainbowStrip = new Apa102Contrib(BoardDefaults.GetSpiBus(), Apa102Contrib.Mode.Bgr); _ledRainbowStrip.Brightness = LedstripBrightness; for (var i = 0; i < _rainbow.Length; i++) { float[] hsv = { i * 360f / _rainbow.Length, 1.0f, 1.0f }; _rainbow[i] = Color.HSVToColor(255, hsv); } } catch (Exception e) { System.Console.WriteLine(e); _ledRainbowStrip = null; } try { var pioService = new PeripheralManagerService(); _led = pioService.OpenGpio(BoardDefaults.GetLedGpioPin()); _led.SetEdgeTriggerType(Gpio.EdgeNone); _led.SetDirection(Gpio.DirectionOutInitiallyLow); _led.SetActiveType(Gpio.ActiveHigh); } catch (Exception e) { System.Console.WriteLine(e); } try { _buttonInputDriver = new ButtonInputDriver(BoardDefaults.GetButtonGpioPin(), ButtonContrib.LogicState.PressedWhenLow, (int)KeyEvent.KeyCodeFromString("KEYCODE_A")); _buttonInputDriver.Register(); Log.Debug(Tag, "Initialized GPIO Button that generates a keypress with KEYCODE_A"); } catch (Exception e) { throw new Exception("Error initializing GPIO button", e); } try { _bmx280SensorDriver = new Bmx280SensorDriver(BoardDefaults.GetI2cBus()); SensorManager.RegisterDynamicSensorCallback(_dynamicSensorCallback); _bmx280SensorDriver.RegisterTemperatureSensor(); _bmx280SensorDriver.RegisterPressureSensor(); _bmx280SensorDriver.RegisterHumiditySensor(); Log.Debug(Tag, "Initialized I2C BMP280"); } catch (Exception e) { throw new Exception("Error initializing BMP280", e); } try { _display = new AlphanumericDisplay(BoardDefaults.GetI2cBus()); _display.SetEnabled(true); _display.Clear(); Log.Debug(Tag, "Initialized I2C Display"); } catch (Exception e) { Log.Error(Tag, "Error initializing display", e); Log.Debug(Tag, "Display disabled"); _display = null; } try { Speaker = new Speaker(BoardDefaults.GetSpeakerPwmPin()); var slide = ValueAnimator.OfFloat(440, 440 * 4); slide.SetDuration(50); slide.RepeatCount = 5; slide.SetInterpolator(new LinearInterpolator()); slide.AddUpdateListener(new SlideUpdateListener(this)); // slide.Start(); } catch (Exception e) { System.Console.WriteLine(e); throw; } }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.activity_main); _tempValueTxtiew = FindViewById <TextView>(Resource.Id.tempValue); _pressureValueTxtView = FindViewById <TextView>(Resource.Id.pressureValue); _sensorManager = (SensorManager)GetSystemService(SensorService); _dynamicSensorCallback = new DynamicSensorCallback(this); try { _buttonInputDriver = new ButtonInputDriver(BoardDefaults.GetButtonGpioPin(), Google.Android.Things.Contrib.Driver.Button.Button.LogicState.PressedWhenLow, (int)KeyEvent.KeyCodeFromString("KEYCODE_A")); _buttonInputDriver.Register(); Log.Debug(TAG, "Initialized GPIO Button that generates a keypress with KEYCODE_A"); } catch (Exception e) { throw new Exception("Error initializing GPIO button", e); } try { _environmentalSensorDriver = new Bmx280SensorDriver(BoardDefaults.GetI2cBus()); _sensorManager.RegisterDynamicSensorCallback(_dynamicSensorCallback); _environmentalSensorDriver.RegisterTemperatureSensor(); _environmentalSensorDriver.RegisterPressureSensor(); Log.Debug(TAG, "Initialized I2C BMP280"); } catch (Exception e) { throw new Exception("Error initializing BMP280", e); } try { _display = new AlphanumericDisplay(BoardDefaults.GetI2cBus()); _display.SetEnabled(true); _display.Clear(); Log.Debug(TAG, "Initialized I2C Display"); } catch (Exception e) { Log.Error(TAG, "Error initializing display", e); Log.Debug(TAG, "Display disabled"); _display = null; } try { _ledStrip = new Apa102(BoardDefaults.GetSpiBus(), Apa102.Mode.Bgr); _ledStrip.Brightness = LEDSTRIP_BRIGHTNESS; for (int i = 0; i < _rainbow.Length; i++) { float[] hsv = { i * 360f / _rainbow.Length, 1.0f, 1.0f }; _rainbow[i] = Color.HSVToColor(255, hsv); } } catch (Exception e) { Console.WriteLine(e); _ledStrip = null; } try { PeripheralManager pioService = PeripheralManager.Instance; _led = pioService.OpenGpio(BoardDefaults.GetLedGpioPin()); _led.SetEdgeTriggerType(Gpio.EdgeNone); _led.SetDirection(Gpio.DirectionOutInitiallyLow); _led.SetActiveType(Gpio.ActiveHigh); } catch (Exception e) { Console.WriteLine(e); } try { _speaker = new Speaker(BoardDefaults.GetSpeakerPwmPin()); ValueAnimator slide = ValueAnimator.OfFloat(440, 440 * 4); slide.SetDuration(50); slide.RepeatCount = 5; slide.SetInterpolator(new LinearInterpolator()); slide.AddUpdateListener(this); } catch (Exception e) { Console.WriteLine(e); throw; } }
public void Initialize() { if (_useHubs) { InitializeHubs(); } SensorManager = (SensorManager)GetSystemService(SensorService); _dynamicSensorCallback = new WeatherDynamicSensorCallback(this); PubSubHandler.GetInstance().Subscribe <TemperatureMessage>(OnTemperatureMessage); PubSubHandler.GetInstance().Subscribe <PressureMessage>(OnPressureMessage); PubSubHandler.GetInstance().Subscribe <HumidityMessage>(OnHumidityMessage); try { _ledRainbowStrip = new Apa102(BoardDefaults.GetSpiBus(), Apa102.Mode.Bgr); _ledRainbowStrip.Brightness = LedstripBrightness; for (var i = 0; i < _rainbow.Length; i++) { float[] hsv = { i * 360f / _rainbow.Length, 1.0f, 1.0f }; _rainbow[i] = Color.HSVToColor(255, hsv); } } catch (Exception e) { System.Console.WriteLine(e); _ledRainbowStrip = null; } try { var pioService = PeripheralManager.Instance; _led = pioService.OpenGpio(BoardDefaults.GetLedGpioPin()); _led.SetEdgeTriggerType(Gpio.EdgeNone); _led.SetDirection(Gpio.DirectionOutInitiallyLow); _led.SetActiveType(Gpio.ActiveHigh); } catch (Exception e) { System.Console.WriteLine(e); } try { _buttonInputDriver = new ButtonInputDriver(BoardDefaults.GetButtonGpioPin(), Button.LogicState.PressedWhenLow, (int)KeyEvent.KeyCodeFromString("KEYCODE_A")); _buttonInputDriver.Register(); Log.Debug(Tag, "Initialized GPIO Button that generates a keypress with KEYCODE_A"); } catch (Exception e) { throw new Exception("Error initializing GPIO button", e); } try { _bmx280SensorDriver = RainbowHat.CreateSensorDriver(); // Register the drivers with the framework SensorManager.RegisterDynamicSensorCallback(_dynamicSensorCallback); _bmx280SensorDriver.RegisterTemperatureSensor(); _bmx280SensorDriver.RegisterPressureSensor(); _bmx280SensorDriver.RegisterHumiditySensor(); Log.Debug(Tag, "Initialized I2C BMP280"); } catch (IOException e) { throw new RuntimeException("Error initializing BMP280", e); } try { _display = new AlphanumericDisplay(BoardDefaults.GetI2cBus()); _display.SetEnabled(true); _display.Clear(); Log.Debug(Tag, "Initialized I2C Display"); } catch (Exception e) { Log.Error(Tag, "Error initializing display", e); Log.Debug(Tag, "Display disabled"); _display = null; } }