private static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var connectionSettingsx20 = new I2cConnectionSettings(1, 0x20);
            var i2cDevicex20          = I2cDevice.Create(connectionSettingsx20);
            var mcp23017x20           = new Mcp23017(i2cDevicex20);

            mcp23017x20.WriteByte(Register.IODIR, 0b0000_0000, Port.PortA);
            mcp23017x20.WriteByte(Register.IODIR, 0b0000_0000, Port.PortB);

            var connectionSettingsx24 = new I2cConnectionSettings(1, 0x24);
            var i2cDevicex24          = I2cDevice.Create(connectionSettingsx24);
            var mcp23017x24           = new Mcp23017(i2cDevicex24);

            mcp23017x24.WriteByte(Register.IODIR, 0b0000_0000, Port.PortA);
            mcp23017x24.WriteByte(Register.IODIR, 0b0000_0000, Port.PortB);


            while (true)
            {
                byte dataPortAswitch20 = mcp23017x20.ReadByte(Register.GPIO, Port.PortA);
                byte dataPortBswitch20 = mcp23017x20.ReadByte(Register.GPIO, Port.PortB);

                Console.WriteLine($"Port A = {dataPortAswitch20:D3} - Port B = {dataPortBswitch20:D3}");

                // echo the x20 switches on x24 LEDs
                mcp23017x24.WriteByte(Register.GPIO, dataPortAswitch20, Port.PortA);
                mcp23017x24.WriteByte(Register.GPIO, dataPortBswitch20, Port.PortB);

                Task.Delay(5000).Wait();
            }
        }
        public void GivenToBigBitArray_ShouldThrowArgumentException()
        {
            var           ar   = new BitArray(new byte[] { 0xFF, 0xFF, 0x1 });
            Func <ushort> func = () => Mcp23017.BitArrayToUshort(ar);

            func.Should().Throw <ArgumentException>();
        }
        public void GivenValidBitArray_ConvertsToUShort()
        {
            var ar     = new BitArray(new byte[] { 0xFF, 0x05 });
            var actual = Mcp23017.BitArrayToUshort(ar);

            actual.Should().Be(0x05FF);
        }
Exemple #4
0
        public static void Main()
        {
            // The Adafruit LCD Shield uses a MCP23017 IC as multiplex chip
            Mcp23017 Mux = new Mcp23017();

            // Pins 0 to 4 on the Mux-chip are connected to the buttons
            IGPIPort ButtonSelect = Mux.Pins[0];
            IGPIPort ButtonRight  = Mux.Pins[1];
            IGPIPort ButtonDown   = Mux.Pins[2];
            IGPIPort ButtonUp     = Mux.Pins[3];
            IGPIPort ButtonLeft   = Mux.Pins[4];

            // Enables pull-ups for all the buttons
            for (int i = 0; i < 5; ++i)
            {
                Mux.EnablePullup(i, true);
                Mux.Pins[i].InvertReadings = true;
            }

            // Pins 6 to 8 on the Mux-chip are for the backlight
            IGPOPort Red   = Mux.Pins[6];  // Red backlight
            IGPOPort Green = Mux.Pins[7];  // Green backlight
            IGPOPort Blue  = Mux.Pins[8];  // Blue backlight

            // Pins 9 to 15 are connected to the HD44780 LCD
            Hd44780Lcd Display = new Hd44780Lcd(
                Data: Mux.CreateParallelOut(9, 4),
                ClockEnablePin: Mux.Pins[13],
                ReadWritePin: Mux.Pins[14],
                RegisterSelectPin: Mux.Pins[15]
                );

            // Initializes the game
            Games.HD44780_Snake.Init(Display, ButtonSelect, ButtonLeft, ButtonRight, ButtonUp, ButtonDown);

            // Turn on blue backlight
            Blue.Write(false); Red.Write(true); Green.Write(true);

            // Display splash
            Games.HD44780_Snake.Splash();

            // Wait 5 sec.
            Thread.Sleep(5000);

            // Turn on green backlight
            Blue.Write(true); Red.Write(true); Green.Write(false);

            // Starts the game
            try
            {
                Games.HD44780_Snake.Start();
            } catch (Exception e) {
                Display.ClearDisplay();
                Display.Write(e.Message);
            }

            // Turn on red backlight
            Blue.Write(true); Red.Write(false); Green.Write(true);
        }
Exemple #5
0
        private async Task Init()
        {
            expander = new Mcp23017();
            await expander.InitMCP23017Async();

            expander.EnableInterrupts(7);
            expander.EnableInterruptsMirroring();
            expander.pinMode(7, Mcp23017.Direction.INPUT);
            expander.pinMode(8, Mcp23017.Direction.OUTPUT);
            expander.pinMode(9, Mcp23017.Direction.OUTPUT);
            expander.pinMode(10, Mcp23017.Direction.OUTPUT);
            MoveLed();
        }
Exemple #6
0
        internal async Task InitChips()
        {
            p  = new Mcp23017(0x20);
            p1 = new Mcp23017(0x21);
            p2 = new Mcp23017(0x22);

            await p.InitMCP23017Async();

            await p1.InitMCP23017Async();

            await p2.InitMCP23017Async();

            for (int i = 0; i < 16; i++)
            {
                p.pinMode(i, Mcp23017.Direction.OUTPUT);
                p1.pinMode(i, Mcp23017.Direction.OUTPUT);
                p2.pinMode(i, Mcp23017.Direction.OUTPUT);
            }
        }
Exemple #7
0
        public static void Main()
        {
            // The Adafruit LCD Shield uses a MCP23017 IC as multiplex chip
            Mcp23017 Mux = new Mcp23017();

            // Pins 0 to 4 on the Mux-chip are connected to the buttons
            IGPIPort ButtonSelect = Mux.Pins[0];
            IGPIPort ButtonRight  = Mux.Pins[1];
            IGPIPort ButtonDown   = Mux.Pins[2];
            IGPIPort ButtonUp     = Mux.Pins[3];
            IGPIPort ButtonLeft   = Mux.Pins[4];

            // Enables pull-ups for all the buttons
            for (int i = 0; i < 5; ++i)
            {
                Mux.EnablePullup(i, true);
                Mux.Pins[i].InvertReadings = true;
            }

            // Pins 6 to 8 on the Mux-chip are for the backlight
            Mux.Pins[6].Write(false); // Red backlight
            Mux.Pins[7].Write(true);  // Green backlight
            Mux.Pins[8].Write(true);  // Blue backlight

            // Pins 9 to 15 are connected to the HD44780 LCD
            Hd44780Lcd Display = new Hd44780Lcd(
                Data: Mux.CreateParallelOut(9, 4),
                ClockEnablePin: Mux.Pins[13],
                ReadWritePin: Mux.Pins[14],
                RegisterSelectPin: Mux.Pins[15]
                );

            // Pressing the Select-button will shift through these colors
            bool[][] Colors = new bool[][] {
                new bool[3] {
                    false, true, true
                },
                new bool[3] {
                    true, false, true
                },
                new bool[3] {
                    true, true, false
                },
                new bool[3] {
                    false, false, true
                },
                new bool[3] {
                    false, true, false
                },
                new bool[3] {
                    true, false, false
                },
            };
            int ColorIndex = 0;

            // Fills up the display
            Display.ClearDisplay();
            Display.Write("Left:  ? Down: ?");
            Display.ChangePosition(1, 0);
            Display.Write("Right: ? Up:   ?");

            // Loops infinitely
            bool SelectPressed = false;

            while (true)
            {
                Display.ChangePosition(0, 7); Display.Write(ButtonLeft.Read() ? "1" : "0");
                Display.ChangePosition(1, 7); Display.Write(ButtonRight.Read() ? "1" : "0");
                Display.ChangePosition(0, 15); Display.Write(ButtonDown.Read() ? "1" : "0");
                Display.ChangePosition(1, 15); Display.Write(ButtonUp.Read() ? "1" : "0");

                // Handles the Select button
                if (ButtonSelect.Read())
                {
                    if (!SelectPressed)
                    {
                        SelectPressed = true;
                        ++ColorIndex;
                        if (ColorIndex == Colors.Length)
                        {
                            ColorIndex = 0;
                        }
                        Mux.Pins[6].Write(Colors[ColorIndex][0]);
                        Mux.Pins[7].Write(Colors[ColorIndex][1]);
                        Mux.Pins[8].Write(Colors[ColorIndex][2]);
                    }
                }
                else
                {
                    SelectPressed = false;
                }
            }
        }
Exemple #8
0
        internal async Task CalcDelay(IBackgroundTaskInstance taskInstance)
        {
            // Swarm s = new Swarm(taskInstance, settings.interval);


            await InitChips();

            p.writeGPIOAB(0xffff);
            p1.writeGPIOAB(0xffff);
            p2.writeGPIOAB(0xffff);

            int      delay = 0;
            TimeSpan timeSpan;
            int      totalMilliseconds = 0;

            int      TestTime  = 500;
            DateTime startTime = DateTime.Now;
            DateTime endTime   = DateTime.Now;
            int      stepCount;
            int      spincount = settings.spinCount;


            bool calculating = true;

            while (calculating)
            {
                stepCount = TestTime / settings.interval;



                ushort allOn  = 0xffff;
                ushort allOff = 0x0000;

                ushort value = allOn;


                startTime = DateTime.Now;
                for (int i = 0; i < stepCount; i++)
                {
                    p.writeGPIOAB(value);
                    Spin(spincount);
                    p1.writeGPIOAB(value);
                    Spin(spincount);
                    p2.writeGPIOAB(value);
                    Spin(spincount);

                    value = (value == allOn) ? allOff : allOn;
                }
                endTime = DateTime.Now;


                timeSpan = endTime - startTime;

                totalMilliseconds = timeSpan.Seconds * 1000 + timeSpan.Milliseconds;

                if (Math.Abs(TestTime - totalMilliseconds) < 30)
                {
                    calculating = false;
                }
                else
                {
                    if (totalMilliseconds > TestTime)
                    {
                        spincount -= 1;
                    }
                    else
                    {
                        spincount += 1;
                    }
                }


                p.writeGPIOAB(0);
                p1.writeGPIOAB(0);
                p2.writeGPIOAB(0);
            }

            p  = null;
            p1 = null;
            p2 = null;
            GC.Collect();
            System.Threading.Tasks.Task.Delay(500).Wait();
            settings.spinCount = spincount;
        }
Exemple #9
0
 public Relay(Mcp23017 _mcp, Port _port, byte _value)
 {
     mcp   = _mcp;
     port  = _port;
     value = _value;
 }
Exemple #10
0
        private static void RunFiringSequence(FiringSequence firingSequence)
        {
            var i2cConnectionSettings_a = new I2cConnectionSettings(1, s_deviceAddress_a);
            var i2cConnectionSettings_b = new I2cConnectionSettings(1, s_deviceAddress_b);
            var i2cDevice_a             = I2cDevice.Create(i2cConnectionSettings_a);
            var i2cDevice_b             = I2cDevice.Create(i2cConnectionSettings_b);

            // Staticly configure each relay on board A (negative)
            Mcp23017 mcp23017_a = new Mcp23017(i2cDevice_a);

            Relay[] relayBoard_a = new Relay[16];
            relayBoard_a[0]  = new Relay(mcp23017_a, Port.PortA, 0xfe);
            relayBoard_a[1]  = new Relay(mcp23017_a, Port.PortA, 0xfd);
            relayBoard_a[2]  = new Relay(mcp23017_a, Port.PortA, 0xfb);
            relayBoard_a[3]  = new Relay(mcp23017_a, Port.PortA, 0xf7);
            relayBoard_a[4]  = new Relay(mcp23017_a, Port.PortA, 0xef);
            relayBoard_a[5]  = new Relay(mcp23017_a, Port.PortA, 0xdf);
            relayBoard_a[6]  = new Relay(mcp23017_a, Port.PortA, 0xbf);
            relayBoard_a[7]  = new Relay(mcp23017_a, Port.PortA, 0x7f);
            relayBoard_a[8]  = new Relay(mcp23017_a, Port.PortB, 0xfe);
            relayBoard_a[9]  = new Relay(mcp23017_a, Port.PortB, 0xfd);
            relayBoard_a[10] = new Relay(mcp23017_a, Port.PortB, 0xfb);
            relayBoard_a[11] = new Relay(mcp23017_a, Port.PortB, 0xf7);
            relayBoard_a[12] = new Relay(mcp23017_a, Port.PortB, 0xef);
            relayBoard_a[13] = new Relay(mcp23017_a, Port.PortB, 0xdf);
            relayBoard_a[14] = new Relay(mcp23017_a, Port.PortB, 0xbf);
            relayBoard_a[15] = new Relay(mcp23017_a, Port.PortB, 0x7f);

            // Staticly configure each relay on board B (positive)
            Mcp23017 mcp23017_b = new Mcp23017(i2cDevice_b);

            Relay[] relayBoard_b = new Relay[16];
            relayBoard_b[0]  = new Relay(mcp23017_b, Port.PortA, 0xfe);
            relayBoard_b[1]  = new Relay(mcp23017_b, Port.PortA, 0xfd);
            relayBoard_b[2]  = new Relay(mcp23017_b, Port.PortA, 0xfb);
            relayBoard_b[3]  = new Relay(mcp23017_b, Port.PortA, 0xf7);
            relayBoard_b[4]  = new Relay(mcp23017_b, Port.PortA, 0xef);
            relayBoard_b[5]  = new Relay(mcp23017_b, Port.PortA, 0xdf);
            relayBoard_b[6]  = new Relay(mcp23017_b, Port.PortA, 0xbf);
            relayBoard_b[7]  = new Relay(mcp23017_b, Port.PortA, 0x7f);
            relayBoard_b[8]  = new Relay(mcp23017_b, Port.PortB, 0xfe);
            relayBoard_b[9]  = new Relay(mcp23017_b, Port.PortB, 0xfd);
            relayBoard_b[10] = new Relay(mcp23017_b, Port.PortB, 0xfb);
            relayBoard_b[11] = new Relay(mcp23017_b, Port.PortB, 0xf7);
            relayBoard_b[12] = new Relay(mcp23017_b, Port.PortB, 0xef);
            relayBoard_b[13] = new Relay(mcp23017_b, Port.PortB, 0xdf);
            relayBoard_b[14] = new Relay(mcp23017_b, Port.PortB, 0xbf);
            relayBoard_b[15] = new Relay(mcp23017_b, Port.PortB, 0x7f);

            // Populate cues
            int cueCount = relayBoard_a.Length * relayBoard_b.Length;

            Cue[] cues       = new Cue[cueCount];
            int   currentCue = 0;

            for (int i = 0; i < relayBoard_b.Length; i++)
            {
                for (int j = 0; j < relayBoard_a.Length; j++)
                {
                    cues[currentCue++] = new Cue(relayBoard_b[i], relayBoard_a[j]);
                }
            }

            // Fire instructions
            for (int i = 0; i < firingSequence.instructions.Length; i++)
            {
                FireCues(cues, firingSequence.instructions[i]);
            }
        }
        public void GivenNullInput_ShouldThrowArgumentNullException()
        {
            Func <ushort> func = () => Mcp23017.BitArrayToUshort(null);

            func.Should().Throw <ArgumentNullException>();
        }
        public void GivenValidInput_PinIsSetToCorrectValue(ushort pinValues, byte pin, bool newState, ushort expected)
        {
            ushort actual = Mcp23017.SetBit(pinValues, pin, newState);

            actual.Should().Be(expected);
        }