Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
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];*/
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        //@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);
        }
Ejemplo n.º 10
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);
 }
Ejemplo n.º 11
0
 /// <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);
 }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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];*/
 }
Ejemplo n.º 14
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);
 }
Ejemplo n.º 15
0
 internal IOWarrior40(IntPtr handle) : base(handle)
 {
     I2C = new I2CInterfaceImplementation(this, Pipe.SPECIAL_MODE, 6);
 }
Ejemplo n.º 16
0
 public MT2131(I2CInterface device)
 {
     I2cDevice = device;
     BusID     = defaultBusID;
 }