Example #1
0
        private static void WriteByte(Mcp23x1x mcp23x1x)
        {
            // This assumes the device is in default Sequential Operation mode.
            Console.WriteLine("Write Individual Byte");

            Register register = Register.IODIR;

            void IndividualRead(Mcp23xxx mcp, Register registerToRead)
            {
                byte dataRead = mcp23x1x.ReadByte(registerToRead, Port.PortB);

                Console.WriteLine($"\tIODIRB: 0x{dataRead:X2}");
            }

            Console.WriteLine("Before Write");
            IndividualRead(mcp23x1x, register);

            mcp23x1x.WriteByte(register, 0x12, Port.PortB);

            Console.WriteLine("After Write");
            IndividualRead(mcp23x1x, register);

            mcp23x1x.WriteByte(register, 0xFF, Port.PortB);

            Console.WriteLine("After Writing Again");
            IndividualRead(mcp23x1x, register);
        }
        public static async Task LitFlooded(Mcp23xxx mcp23xxxWrite, int interval, bool silentFlooding)
        {
            if (silentFlooding)
            {
                return;
            }

            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("LitFlooded: Unable to cast Mcp23017 Write GPIO.");
                }
                else
                {
                    var sleep = 250;

                    var steps = interval / sleep;

                    for (var i = 0; i < steps; i++)
                    {
                        mcp23x1x.WriteByte(Register.GPIO, 255, Port.PortA);
                        mcp23x1x.WriteByte(Register.GPIO, 255, Port.PortB);

                        mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortA);
                        mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortB);

                        await Task.Delay(sleep);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error when LitFlooded: {ex.Message}");
            }
            finally
            {
                _ledsPlaying = false;
            }
        }
        public static async Task PlayDownScene(Mcp23xxx mcp23xxxWrite, int UpDownInterval)
        {
            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("PlayDownScene: Unable to cast Mcp23017 Write GPIO.");
                }
                else
                {
                    // Use UpDownInterval to predict how long the scen must play

                    var sleepInterval = 100;

                    var j = UpDownInterval / sleepInterval;

                    byte a = 0b_1000_0000;

                    for (var i = 0; i < j; i++)
                    {
                        var shifter = ((i + 8) % 8);

                        int b = a >> shifter;

                        mcp23x1x.WriteByte(Register.GPIO, (byte)b, Port.PortA);
                        mcp23x1x.WriteByte(Register.GPIO, (byte)b, Port.PortB);

                        await Task.Delay(sleepInterval);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error when PlayUpScene: {ex.Message}");
            }
            finally
            {
                _ledsPlaying = false;
            }
        }
        public static async Task <bool> DirectCircus(Mcp23xxx mcp23xxxWrite)
        {
            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("DirectCircus: Unable to cast Mcp23017 Write GPIO.");

                    return(false);
                }
                else
                {
                    Console.WriteLine("Ra da da da da da da da Circus");
                    Console.WriteLine("Da da da da da da da da");
                    Console.WriteLine("Afro Circus, Afro Circus, Afro");
                    Console.WriteLine("Polka dot, polka dot, polka dot, Afro!");

                    for (var i = 0; i < 256; i++)
                    {
                        mcp23x1x.WriteByte(Register.GPIO, (byte)i, Port.PortA);
                        mcp23x1x.WriteByte(Register.GPIO, (byte)i, Port.PortB);

                        await Task.Delay(20);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _ledsPlaying = false;
            }

            return(true);
        }
        public static async Task <bool> DirectAdvertise(Mcp23xxx mcp23xxxWrite)
        {
            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("DirectAdvertise: Unable to cast Mcp23017 Write GPIO.");

                    return(false);
                }
                else
                {
                    mcp23x1x.WriteByte(Register.GPIO, (byte)255, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, (byte)255, Port.PortB);

                    await Task.Delay(10000);

                    mcp23x1x.WriteByte(Register.GPIO, (byte)0, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, (byte)0, Port.PortB);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _ledsPlaying = false;
            }

            return(true);
        }
Example #6
0
        private static void ReadSwitchesWriteLeds(Mcp23x1x mcp23x1x)
        {
            Console.WriteLine("Read Switches & Write LEDs");

            // Input direction for switches.
            mcp23x1x.WriteByte(Register.IODIR, 0b1111_1111, Port.PortA);
            // Output direction for LEDs.
            mcp23x1x.WriteByte(Register.IODIR, 0b0000_0000, Port.PortB);

            while (true)
            {
                // Read switches.
                byte data = mcp23x1x.ReadByte(Register.GPIO, Port.PortA);
                // Write data to LEDs.
                mcp23x1x.WriteByte(Register.GPIO, data, Port.PortB);
                Console.WriteLine(data);
                Thread.Sleep(500);
            }
        }
Example #7
0
        // This is now Write(pinNumber)
        private static void WriteBits(Mcp23x1x mcp23x1x, GpioController controller)
        {
            Console.WriteLine("Write Bits");

            // Make port output and set all pins.
            // (SetPinMode will also set the direction for each GPIO pin)
            mcp23x1x.WriteByte(Register.IODIR, 0x00, Port.PortB);
            mcp23x1x.WriteByte(Register.GPIO, 0xFF, Port.PortB);

            for (int bitNumber = 9; bitNumber < 16; bitNumber++)
            {
                controller.Write(bitNumber, PinValue.Low);
                Console.WriteLine($"Bit {bitNumber} low");
                Thread.Sleep(500);
                controller.Write(bitNumber, PinValue.High);
                Console.WriteLine($"Bit {bitNumber} high");
                Thread.Sleep(500);
            }
        }
        public static async Task SwitchOffAllSlots(Mcp23xxx mcp23xxxWrite, byte lastDataPortA, byte lastDataPortB)
        {
            // all LEDs are dimmed.

            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("SwtichoffAllSlots: Unable to cast Mcp23017 Write GPIO.");
                }
                else
                {
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortB);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error when SwtichoffAllSlots: {ex.Message}");
            }
            finally
            {
                _ledsPlaying = false;
            }
        }
        public static async Task <bool> DirectMarkPosition(Mcp23xxx mcp23xxxWrite, int position)
        {
            // Position is a value between 1 and 16 (or 0 is all occupied)

            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("DirectMarkPosition: Unable to cast Mcp23017 Write GPIO.");

                    return(false);
                }
                else
                {
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortB);

                    var port = position <= 8 ? Port.PortA : Port.PortB;

                    byte bPos = position <= 8
                                        ? (byte)Math.Pow(2, position - 1)
                                        : (byte)Math.Pow(2, position - 9);

                    for (var i = 0; i < 50; i++)
                    {
                        if (position == 0)
                        {
                            Console.Write("Skip blink. ");
                            continue;
                        }

                        // blink led on i % 2 on else off
                        var j = (i % 2) == 0 ? bPos : 0;

                        mcp23x1x.WriteByte(Register.GPIO, (byte)j, port);

                        await Task.Delay(100);
                    }

                    // TODO : blinking for 0 => alle positions occupied
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _ledsPlaying = false;
            }

            return(true);
        }
        public static async Task <bool> DirectFirstEmptySlot(Mcp23xxx mcp23xxxWrite, int firstEmptySlot)
        {
            if (firstEmptySlot == 0)
            {
                Console.WriteLine("Skip blink.");
                return(true);
            }

            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("DirectFirstEmptySlot: Unable to cast Mcp23017 Write GPIO.");

                    return(false);
                }
                else
                {
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortB);

                    var port = firstEmptySlot <= 8 ? Port.PortA : Port.PortB;

                    byte bPos = firstEmptySlot <= 8
                                        ? (byte)Math.Pow(2, firstEmptySlot - 1)
                                        : (byte)Math.Pow(2, firstEmptySlot - 9);

                    for (var i = 0; i < 25; i++)
                    {
                        // blink led on i % 2 on else off
                        var j = (i % 2) == 0 ? bPos : 0;

                        Console.Write($"Lit {j}. ");

                        mcp23x1x.WriteByte(Register.GPIO, (byte)j, port);

                        await Task.Delay(100);
                    }

                    Console.WriteLine();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _ledsPlaying = false;
            }

            return(true);
        }