Пример #1
0
        public static void WaitForAllButtons()
        {
            const ConnectorPin sdaPin = ConnectorPin.P1Pin03;
            const ConnectorPin sclPin = ConnectorPin.P1Pin05;

            using (var driver = new I2cDriver(sdaPin.ToProcessor(), sclPin.ToProcessor()))
            {
                var deviceConnection = new Mcp23017I2cConnection(driver.Connect(0x20));
                Console.WriteLine("Connected");
                Console.WriteLine("press all 5 of the buttons");

                deviceConnection.SetDirection(Mcp23017Pin.B1, Mcp23017PinDirection.Input);
                deviceConnection.SetDirection(Mcp23017Pin.B3, Mcp23017PinDirection.Input);
                deviceConnection.SetDirection(Mcp23017Pin.B5, Mcp23017PinDirection.Input);
                deviceConnection.SetDirection(Mcp23017Pin.B6, Mcp23017PinDirection.Input);
                deviceConnection.SetDirection(Mcp23017Pin.B7, Mcp23017PinDirection.Input);

                deviceConnection.SetResistor(Mcp23017Pin.B1, Mcp23017PinResistor.PullUp);
                deviceConnection.SetResistor(Mcp23017Pin.B3, Mcp23017PinResistor.PullUp);
                deviceConnection.SetResistor(Mcp23017Pin.B5, Mcp23017PinResistor.PullUp);
                deviceConnection.SetResistor(Mcp23017Pin.B6, Mcp23017PinResistor.PullUp);
                deviceConnection.SetResistor(Mcp23017Pin.B7, Mcp23017PinResistor.PullUp);


                bool[] btns = { false, false, false, false, false };

                bool btnsPushed = false;
                while (!btnsPushed && !Console.KeyAvailable)
                {
                    Thread.Sleep(100);
                    if (!deviceConnection.GetPinStatus(Mcp23017Pin.B1))
                    {
                        btns[0] = true; Console.Write("UP ");
                    }
                    if (!deviceConnection.GetPinStatus(Mcp23017Pin.B3))
                    {
                        btns[1] = true; Console.Write("ENTER ");
                    }
                    if (!deviceConnection.GetPinStatus(Mcp23017Pin.B5))
                    {
                        btns[2] = true; Console.Write("DOWN ");
                    }
                    if (!deviceConnection.GetPinStatus(Mcp23017Pin.B6))
                    {
                        btns[3] = true; Console.Write("TOPSEL ");
                    }
                    if (!deviceConnection.GetPinStatus(Mcp23017Pin.B7))
                    {
                        btns[4] = true; Console.Write("BOTSEL ");
                    }

                    btnsPushed = btns[0] && btns[1] && btns[2] && btns[3] && btns[4];
                }
            }

            Console.WriteLine();
            Console.WriteLine("All buttons pressed");
        }
Пример #2
0
        public void Start()
        {
            _isRunning = true;

            while (ButtonEvent != null && _isRunning)
            {
                foreach (ButtonState btn in _states)
                {
                    if (!deviceConnection.GetPinStatus(btn.Pin))
                    {
                        if (!btn.IsPressed)
                        {
                            // Console.WriteLine("raise down");
                            ButtonEvent(this, new ButtonArgs()
                            {
                                Button = btn.Button, ButtonEvent = ButtonEventType.Down
                            });
                            btn.IsPressed = true;
                        }
                    }
                    else if (btn.IsPressed)
                    {
                        btn.IsPressed = false;
                        ButtonEvent(this, new ButtonArgs()
                        {
                            Button = btn.Button, ButtonEvent = ButtonEventType.Up
                        });
                    }
                }
            }
        }