/// <summary>
        /// Write the given value to the given pin.
        /// </summary>
        /// <param name="pin">The pin to set.</param>
        /// <param name="state">The new state of the pin.</param>
        public void WriteDigital(DigitalPin pin, bool state)
        {
            IGpio gpioPin = pin == DigitalPin.DIO16 ? this.dio16 : this.dio26;

            //if (gpioPin. != GpioPinDriveMode.Output)
            //gpioPin.SetDriveMode(GpioPinDriveMode.Output);
            // Initialize the pin as a high output
            gpioPin.SetDirection(Gpio.DirectionOutInitiallyHigh);
            // Low voltage is considered active
            gpioPin.SetActiveType(Gpio.ActiveHigh);

            gpioPin.Value = (state ? true : false);
        }
        /// <summary>
        /// Reads the current state of the given pin.
        /// </summary>
        /// <param name="pin">The pin to read.</param>
        /// <returns>True if high, false is low.</returns>
        public bool ReadDigital(DigitalPin pin)
        {
            //if (!Enum.IsDefined(typeof(DigitalPin), pin)) throw new ArgumentException(nameof(pin));

            IGpio gpioPin = pin == DigitalPin.DIO16 ? this.dio16 : this.dio26;

            //if (gpioPin.GetDriveMode() != GpioPinDriveMode.Input)
            //  gpioPin.SetDriveMode(GpioPinDriveMode.Input);

            // Initialize the pin as an input
            gpioPin.SetDirection(Gpio.DirectionIn);
            // High voltage is considered active
            gpioPin.SetActiveType(Gpio.ActiveHigh);

            return(gpioPin.Value == true);
        }
        private void SetupDemo1()
        {
            try
            {
                var LED_PIN_NAME = "GPIO2_IO02";

                // Red LED
                //Raspberry Pi 3 - BCM6
                //i.MX7D - GPIO2_IO02

                _redLED = _manager.OpenGpio(LED_PIN_NAME);
                //redLED = RainbowHat.OpenLedRed();
                // Configure as an output.
                _redLED.SetDirection(Gpio.DirectionOutInitiallyLow);
            }
            catch (IOException ex)
            {
                Log.Error(TAG, "Error during onCreate!", ex);
            }

            _ledToggleView = FindViewById <ToggleButton>(Resource.Id.ledToggle);

            try
            {
                _ledToggleView.Checked = _redLED.Value;
            }
            catch (IOException ex)
            {
                Log.Error(TAG, "Error during setChecked!", ex);
            }


            _ledToggleView.CheckedChange += (sender, e) => {
                try
                {
                    _redLED.Value = e.IsChecked;
                }
                catch (IOException ex)
                {
                    Log.Error(TAG, "Error during onCheckedChanged!", ex);
                }
            };
        }
        private void SetupDemo2()
        {
            try
            {
                var pinName = "GPIO6_IO14"; //A button for i.MX7D, BCM21 for Rpi3
                _buttonA = _manager.OpenGpio(pinName);
                // Configure as an input, trigger events on every change.
                _buttonA.SetDirection(Gpio.DirectionIn);
                // Value is true when the pin is HIGH
                _buttonA.SetActiveType(Gpio.ActiveHigh);
                _buttonA.SetEdgeTriggerType(Gpio.EdgeFalling);
                _buttonA.RegisterGpioCallback(new Handler(), this);


                var buttonB        = RainbowHat.OpenButtonB();
                var ledToggleViewB = FindViewById <ToggleButton>(Resource.Id.ledToggleB);
                buttonB.ButtonEvent += (sender, e) => {
                    ledToggleViewB.Checked = !ledToggleViewB.Checked;
                };
            } catch (IOException ex) {
                Log.Error(TAG, "Error during onCreate!", ex);
            }
        }
        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;
            }
        }
Пример #6
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;
            }
        }