public void UpdatePins(I2CInterface i2c) //@TODO: We need to update VVAR directions on state change! { //Read inputs byte[] IN = this.ReadRegisters(i2c, Register.GPIOA, 2); //Update input pins for (int i = 0; i < 16; i++) { int bank = i / 8; int bit = i % 8; if (this.Pins[i].Input) { this.Pins[i].Value = (IN[bank] >> bit & 1) == 1; } } //Write outputs byte[] OUT = new byte[2]; for (int i = 0; i < 16; i++) { int bank = i / 8; int bit = i % 8; OUT[bank] |= (byte)((this.Pins[i].Value ? 1 : 0) << bit); } this.WriteRegisters(i2c, Register.GPIOA, OUT); }
/// <summary> /// Write to a register on the MCP23017 via I2C /// </summary> /// <param name="i2c">I2C Interface, probabally the Bus Pirate</param> /// <param name="register">Register Address</param> /// <param name="value">Value to write</param> private void WriteRegister(I2CInterface i2c, Register register, byte value) { byte opcode = (byte)(((this.Address % 8) << 1) | 0x40); byte[] data = { opcode, (byte)register, value }; i2c.WriteThenRead(data, 0); }
/// <summary> /// Read a register on the MCP23017 via I2C /// </summary> /// <param name="i2c">I2C Interface, probabally the Bus Pirate</param> /// <param name="register">Register Address</param> /// <returns>Value Read</returns> private byte ReadRegister(I2CInterface i2c, Register register) { throw new NotImplementedException(); /*byte opcode = (byte)(((this.Address % 8) << 1) | 0x41); * byte[] data = { opcode, (byte)register }; * return i2c.WriteThenRead(data, 1)[0];*/ }
/// <summary> /// Write to sequential registers on the MCP23017 via I2C /// </summary> /// <param name="i2c">I2C Interface, probabally the Bus Pirate</param> /// <param name="register">Base Register Address</param> /// <param name="value">Value to write</param> private void WriteRegisters(I2CInterface i2c, Register register, byte[] values) { byte opcode = (byte)(((this.Address % 8) << 1) | 0x40); List <byte> data = new List <byte> { opcode, (byte)register }; data.AddRange(values); i2c.WriteThenRead(data.ToArray(), 0); }
internal void UpdateDirections(I2CInterface i2c) { byte[] IODIR = new byte[2]; for (int i = 0; i < 16; i++) { int bank = i / 8; int bit = i % 8; IODIR[bank] |= (byte)((this.Pins[i].Input ? 1 : 0) << bit); } //Write values out this.WriteRegisters(i2c, Register.IODIRA, IODIR); }
/// <summary> /// Read from sequential registers on the MCP23017 via I2C /// </summary> /// <param name="i2c">I2C Interface, probabally the Bus Pirate</param> /// <param name="register">Base Register Address</param> /// <returns>Values Read</returns> private byte[] ReadRegisters(I2CInterface i2c, Register register, uint registerstoread) { //Set address pointer byte opcode = (byte)(((this.Address % 8) << 1) | 0x40); byte[] data = { opcode, (byte)register }; i2c.WriteThenRead(data, 0); //Read data opcode = (byte)(((this.Address % 8) << 1) | 0x41); return(i2c.WriteThenRead(new byte[] { opcode }, registerstoread)); }
public void Setup(I2CInterface i2c) { //Setup IOCON //Actually happy with default value. //Setup directions and pullups byte[] IODIR = new byte[2]; byte[] GPPU = new byte[2]; for (int i = 0; i < 16; i++) { int bank = i / 8; int bit = i % 8; IODIR[bank] |= (byte)((this.Pins[i].Input ? 1 : 0) << bit); GPPU[bank] |= (byte)((this.Pins[i].PullUp ? 1 : 0) << bit); } //Write values out this.WriteRegisters(i2c, Register.IODIRA, IODIR); this.WriteRegisters(i2c, Register.GPPUA, GPPU); }
//@TODO: We need to update VVAR directions on state change! public void UpdatePins(I2CInterface i2c) { //Read inputs byte[] IN = this.ReadRegisters(i2c, Register.GPIOA, 2); //Update input pins for (int i = 0; i < 16; i++) { int bank = i / 8; int bit = i % 8; if (this.Pins[i].Input) { this.Pins[i].Value = (IN[bank] >> bit & 1) == 1; } } //Write outputs byte[] OUT = new byte[2]; for (int i = 0; i < 16; i++) { int bank = i / 8; int bit = i % 8; OUT[bank] |= (byte)((this.Pins[i].Value ? 1 : 0) << bit); } this.WriteRegisters(i2c, Register.GPIOA, OUT); }
/// <summary> /// Write to sequential registers on the MCP23017 via I2C /// </summary> /// <param name="i2c">I2C Interface, probabally the Bus Pirate</param> /// <param name="register">Base Register Address</param> /// <param name="value">Value to write</param> private void WriteRegisters(I2CInterface i2c, Register register, byte[] values) { byte opcode = (byte)(((this.Address % 8) << 1) | 0x40); List<byte> data = new List<byte> { opcode, (byte)register }; data.AddRange(values); i2c.WriteThenRead(data.ToArray(), 0); }
/// <summary> /// Read from sequential registers on the MCP23017 via I2C /// </summary> /// <param name="i2c">I2C Interface, probabally the Bus Pirate</param> /// <param name="register">Base Register Address</param> /// <returns>Values Read</returns> private byte[] ReadRegisters(I2CInterface i2c, Register register, uint registerstoread) { //Set address pointer byte opcode = (byte)(((this.Address % 8) << 1) | 0x40); byte[] data = { opcode, (byte)register }; i2c.WriteThenRead(data, 0); //Read data opcode = (byte)(((this.Address % 8) << 1) | 0x41); return i2c.WriteThenRead(new byte[] {opcode}, registerstoread); }
/// <summary> /// Read a register on the MCP23017 via I2C /// </summary> /// <param name="i2c">I2C Interface, probabally the Bus Pirate</param> /// <param name="register">Register Address</param> /// <returns>Value Read</returns> private byte ReadRegister(I2CInterface i2c, Register register) { throw new NotImplementedException(); /*byte opcode = (byte)(((this.Address % 8) << 1) | 0x41); byte[] data = { opcode, (byte)register }; return i2c.WriteThenRead(data, 1)[0];*/ }
internal IOWarrior40(IntPtr handle) : base(handle) { I2C = new I2CInterfaceImplementation(this, Pipe.SPECIAL_MODE, 6); }
public MT2131(I2CInterface device) { I2cDevice = device; BusID = defaultBusID; }