private static void WriteSequentialBytes(Mcp23xxx mcp23xxx) { // This assumes the device is in default Sequential Operation mode. Console.WriteLine("Write Sequential Bytes"); using (mcp23xxx) { void SequentialRead(Mcp23xxx mcp) { byte[] dataRead = mcp23xxx.Read(0, 2, Port.PortA, Bank.Bank0); Console.WriteLine($"\tIODIRA: 0x{dataRead[0]:X2}"); Console.WriteLine($"\tIODIRB: 0x{dataRead[1]:X2}"); } Console.WriteLine("Before Write"); SequentialRead(mcp23xxx); byte[] dataWrite = new byte[] { 0x12, 0x34 }; mcp23xxx.Write(0, dataWrite, Port.PortA, Bank.Bank0); Console.WriteLine("After Write"); SequentialRead(mcp23xxx); dataWrite = new byte[] { 0xFF, 0xFF }; mcp23xxx.Write(0, dataWrite, Port.PortA, Bank.Bank0); Console.WriteLine("After Writing Again"); SequentialRead(mcp23xxx); } }
private static void WriteIndividualByte(Mcp23xxx mcp23xxx) { // This assumes the device is in default Sequential Operation mode. Console.WriteLine("Write Individual Byte"); using (mcp23xxx) { Register.Address address = Register.Address.IODIR; void IndividualRead(Mcp23xxx mcp, Register.Address addressToRead) { byte[] dataRead = mcp23xxx.Read(addressToRead, 1, Port.PortB, Bank.Bank0); Console.WriteLine($"\tIODIRB: 0x{dataRead[0]:X2}"); } Console.WriteLine("Before Write"); IndividualRead(mcp23xxx, address); byte[] dataWrite = new byte[] { 0x12 }; mcp23xxx.Write(address, dataWrite, Port.PortB, Bank.Bank0); Console.WriteLine("After Write"); IndividualRead(mcp23xxx, address); dataWrite = new byte[] { 0xFF }; mcp23xxx.Write(address, dataWrite, Port.PortB, Bank.Bank0); Console.WriteLine("After Writing Again"); IndividualRead(mcp23xxx, address); } }
// This is now Read(pinNumber) private static void ReadBits(Mcp23xxx mcp23xxx) { Console.WriteLine("Read Bits"); for (int bitNumber = 0; bitNumber < 8; bitNumber++) { PinValue bit = mcp23xxx.Read(bitNumber); Console.WriteLine($"{bitNumber}: {bit}"); } }
public void Read_GoodPin(TestDevice testDevice) { Mcp23xxx device = testDevice.Device; for (int pin = 0; pin < testDevice.Device.PinCount; pin++) { bool first = pin < 8; int register = testDevice.Device.PinCount == 16 ? (first ? 0x12 : 0x13) : 0x09; // Flip the bit on (set the backing buffer directly to simulate incoming data) testDevice.ChipMock.Registers[register] = (byte)(1 << (first ? pin : pin - 8)); Assert.Equal(PinValue.High, device.Read(pin)); // Clear the register testDevice.ChipMock.Registers[register] = 0x00; Assert.Equal(PinValue.Low, device.Read(pin)); } }
private static void ReadAllRegisters(Mcp23xxx mcp23xxx) { // This assumes the device is in default Sequential Operation mode. Console.WriteLine("Read All Registers"); using (mcp23xxx) { // Start at first register. Total of 22 registers for MCP23x17. byte[] data = mcp23xxx.Read(0, 22, Port.PortA, Bank.Bank0); for (int index = 0; index < data.Length; index++) { Console.WriteLine($"0x{index:X2}: 0x{data[index]:X2}"); } } }
private static void ReadSwitchesWriteLeds(Mcp23xxx mcp23xxx) { Console.WriteLine("Read Switches & Write LEDs"); using (mcp23xxx) { // Input direction for switches. mcp23xxx.Write(Register.Address.IODIR, 0b1111_1111, Port.PortA, Bank.Bank0); // Output direction for LEDs. mcp23xxx.Write(Register.Address.IODIR, 0b0000_0000, Port.PortB, Bank.Bank0); while (true) { // Read switches. byte data = mcp23xxx.Read(Register.Address.GPIO, Port.PortA, Bank.Bank0); // Write data to LEDs. mcp23xxx.Write(Register.Address.GPIO, data, Port.PortB, Bank.Bank0); Console.WriteLine(data); Thread.Sleep(500); } } }