protected override void OnDestroy()
        {
            base.OnDestroy();

            if (_display != null)
            {
                try
                {
                    _display.Clear();
                    _display.SetEnabled(false);
                    _display.Close();
                }
                catch (IOException e)
                {
                    Log.Error(Tag, "Error disabling display", e);
                }
                finally
                {
                    _display = null;
                }
            }

            if (_ledRainbowStrip != null)
            {
                try
                {
                    _ledRainbowStrip.Write(new int[7]);
                    _ledRainbowStrip.Brightness = 0;
                    _ledRainbowStrip.Close();
                }
                catch (IOException e)
                {
                    Log.Error(Tag, "Error disabling ledstrip", e);
                }
                finally
                {
                    _ledRainbowStrip = null;
                }
            }
        }
        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;
            }
        }
Exemple #3
0
        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 OnDestroy()
        {
            base.OnDestroy();
            // Clean up sensor registrations
            _sensorManager.UnregisterListener(new TemperatureListener(this));
            _sensorManager.UnregisterListener(new PressureListener(this));

            // Clean up peripheral.
            if (_environmentalSensorDriver != null)
            {
                try
                {
                    _environmentalSensorDriver.Close();
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                }

                _environmentalSensorDriver = null;
            }

            if (_buttonInputDriver != null)
            {
                try
                {
                    _buttonInputDriver.Close();
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                }

                _buttonInputDriver = null;
            }

            if (_display != null)
            {
                try
                {
                    _display.Clear();
                    _display.SetEnabled(false);
                    _display.Close();
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                }
                finally
                {
                    _display = null;
                }
            }

            if (_ledStrip != null)
            {
                try
                {
                    _ledStrip.Brightness = 0;
                    _ledStrip.Write(new int[7]);
                    _ledStrip.Close();
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                }
                finally
                {
                    _ledStrip = null;
                }
            }

            if (_led != null)
            {
                try
                {
                    _led.Value = false;
                    _led.Close();
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                }
                finally
                {
                    _led = null;
                }
            }

            //TODO https://github.com/androidthings/weatherstation/blob/master/app/src/main/java/com/example/androidthings/weatherstation/WeatherStationActivity.java#L304
        }
Exemple #5
0
        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;
            }
        }