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);
                }
            };
        }
            public Motor(PCA9685 pwm, int pwmChannel, String direction1Pin, String direction2Pin)
            {
                PeripheralManager manager = PeripheralManager.Instance;

                this.direction1 = manager.OpenGpio(direction1Pin);
                this.direction2 = manager.OpenGpio(direction2Pin);
                this.speed      = 0.0;
                this.pwm        = pwm;
                this.disposed   = false;

                this.pwmChannel = pwmChannel;
                // Initialize the pin as a high output
                this.direction1.SetDirection(Gpio.DirectionOutInitiallyHigh);
                // Low voltage is considered active
                this.direction1.SetActiveType(Gpio.ActiveHigh);

                // Initialize the pin as a high output
                this.direction2.SetDirection(Gpio.DirectionOutInitiallyHigh);
                // Low voltage is considered active
                this.direction2.SetActiveType(Gpio.ActiveHigh);
            }
        public PCA9685(String I2CBus, String GPIO_NAME)
        {
            PeripheralManager pioService = PeripheralManager.Instance;
            II2cDevice        _device    = pioService.OpenI2cDevice(I2CBus, I2C_ADDRESS);

            this.write5   = new byte[5];
            this.write2   = new byte[2];
            this.write1   = new byte[1];
            this.read1    = new byte[1];
            this.disposed = false;

            this.device = _device;
            try
            {
                PeripheralManager manager = PeripheralManager.Instance;
                this.outputEnable = manager.OpenGpio(GPIO_NAME);
            }
            catch (Exception e)
            {
                Log.WriteLine(LogPriority.Info, TAG, "Unable to access GPIO", e);
            }


            if (this.outputEnable != null)
            {
                // Initialize the pin as a high output
                this.outputEnable.SetDirection(Gpio.DirectionOutInitiallyHigh);
                // Low voltage is considered active
                this.outputEnable.SetActiveType(Gpio.ActiveLow);
                // Toggle the value to be LOW
                this.outputEnable.Value = (false);
                //this.outputEnable.SetDriveMode(GpioPinDriveMode.Output);
                //this.outputEnable.Write(GpioPinValue.Low);
            }

            this.WriteRegister(Register.Mode1, (byte)0x20);
            this.WriteRegister(Register.Mode2, (byte)0x06);
        }
        /// <summary>
        /// Creates a new instance of the FEZ HAT.
        /// </summary>
        /// <returns>The new instance.</returns>
        public static FezHat Create()
        {
            PeripheralManager manager = PeripheralManager.Instance;

            //var gpioController = GpioController.GetDefault();
            //var i2cController = (await DeviceInformation.FindAllAsync(I2cDevice.GetDeviceSelector(FezHat.I2cDeviceName)))[0];
            FezHat hat = new FezHat();

            hat.accelerometer = new MMA8453(I2cDeviceName);
            hat.analog        = new ADS7830(I2cDeviceName);

            hat.pwm = new PCA9685(I2cDeviceName, "BCM13");//gpioController.OpenPin(13)
            hat.pwm.OutputEnabled = true;
            hat.pwm.Frequency     = 1500;

            hat.dio16 = manager.OpenGpio("BCM16"); //gpioController.OpenPin(16);
            hat.dio26 = manager.OpenGpio("BCM26"); //gpioController.OpenPin(26);
            hat.dio24 = manager.OpenGpio("BCM24"); //gpioController.OpenPin(24);
            hat.dio18 = manager.OpenGpio("BCM18"); //gpioController.OpenPin(18);xx
            hat.dio22 = manager.OpenGpio("BCM22"); //gpioController.OpenPin(22);

            //hat.dio16.SetDriveMode(GpioPinDriveMode.Input);
            // Initialize the pin as an input
            hat.dio16.SetDirection(Gpio.DirectionIn);
            // High voltage is considered active
            hat.dio16.SetActiveType(Gpio.ActiveHigh);

            //hat.dio26.SetDriveMode(GpioPinDriveMode.Input);
            // Initialize the pin as an input
            hat.dio26.SetDirection(Gpio.DirectionIn);
            // High voltage is considered active
            hat.dio26.SetActiveType(Gpio.ActiveHigh);

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

            //hat.dio18.SetDriveMode(GpioPinDriveMode.Input);
            // Initialize the pin as an input
            hat.dio18.SetDirection(Gpio.DirectionIn);
            // High voltage is considered active
            hat.dio18.SetActiveType(Gpio.ActiveHigh);

            //hat.dio22.SetDriveMode(GpioPinDriveMode.Input);
            // Initialize the pin as an input
            hat.dio22.SetDirection(Gpio.DirectionIn);
            // High voltage is considered active
            hat.dio22.SetActiveType(Gpio.ActiveHigh);

            hat.motorEnable = manager.OpenGpio("BCM12");//gpioController.OpenPin(12);
            //hat.motorEnable.SetDriveMode(GpioPinDriveMode.Output);
            // Initialize the pin as a high output
            hat.motorEnable.SetDirection(Gpio.DirectionOutInitiallyHigh);
            // Low voltage is considered active
            hat.motorEnable.SetActiveType(Gpio.ActiveHigh);
            hat.motorEnable.Value = true;

            //hat.MotorA = new Motor(hat.pwm, 14, 27, 23);
            hat.MotorA = new Motor(hat.pwm, 14, "BCM27", "BCM23");
            //hat.MotorB = new Motor(hat.pwm, 13, 6, 5);
            hat.MotorB = new Motor(hat.pwm, 13, "BCM6", "BCM5");

            hat.D2 = new RgbLed(hat.pwm, 1, 0, 2);
            hat.D3 = new RgbLed(hat.pwm, 4, 3, 15);

            hat.S1 = new Servo(hat.pwm, 9);
            hat.S2 = new Servo(hat.pwm, 10);

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