/// <summary> /// Initializes a new instance of the <see cref="Mcp23008InputBinaryPin"/> class. /// </summary> /// <param name="connection">The connection.</param> /// <param name="pin">The pin.</param> /// <param name="resistor">The resistor.</param> /// <param name="polarity">The polarity.</param> public Mcp23008InputBinaryPin(Mcp23008I2cConnection connection, Mcp23008Pin pin, Mcp23008PinResistor resistor = Mcp23008PinResistor.None, Mcp23008PinPolarity polarity = Mcp23008PinPolarity.Normal) { this.connection = connection; this.pin = pin; connection.SetDirection(pin, Mcp23008PinDirection.Input); connection.SetResistor(pin, resistor); connection.SetPolarity(pin, polarity); }
/// <summary> /// Gets the pin status. /// </summary> /// <param name="pin">The pin.</param> /// <returns>The pin status.</returns> public bool GetPinStatus(Mcp23008Pin pin) { var register = Register.GPIO; connection.WriteByte((byte)register); var status = connection.ReadByte(); var bit = (byte)((int)pin & 0xFF); return((status & bit) != 0x00); }
/// <summary> /// Initializes a new instance of the <see cref="Mcp23008OutputBinaryPin"/> class. /// </summary> /// <param name="connection">The connection.</param> /// <param name="pin">The pin.</param> /// <param name="resistor">The resistor.</param> /// <param name="polarity">The polarity.</param> public Mcp23008OutputBinaryPin(Mcp23008I2cConnection connection, Mcp23008Pin pin, Mcp23008PinResistor resistor = Mcp23008PinResistor.None, Mcp23008PinPolarity polarity = Mcp23008PinPolarity.Normal) { this.connection = connection; this.pin = pin; connection.SetDirection(pin, Mcp23008PinDirection.Output); connection.SetResistor(pin, resistor); connection.SetPolarity(pin, polarity); }
private static Hd44780Configuration LoadMcp23008Configuration(IEnumerable <string> args) { var addressText = args.SkipWhile(s => !String.Equals(s, "mcp23008", StringComparison.InvariantCultureIgnoreCase)).Skip(1).DefaultIfEmpty("0x20").First(); var address = addressText.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase) ? Int32.Parse(addressText.Substring(2), NumberStyles.HexNumber) : Int32.Parse(addressText); const Mcp23008Pin registerSelectPin = Mcp23008Pin.Pin1; const Mcp23008Pin clockPin = Mcp23008Pin.Pin2; const Mcp23008Pin backlightPin = Mcp23008Pin.Pin7; var dataPins = new[] { Mcp23008Pin.Pin3, Mcp23008Pin.Pin4, Mcp23008Pin.Pin5, Mcp23008Pin.Pin6 }; Console.WriteLine(); Console.WriteLine("Using I2C connection over MCP23008 Expander"); Console.WriteLine("\tRegister Select: {0}", registerSelectPin); Console.WriteLine("\tClock: {0}", clockPin); Console.WriteLine("\tData 1: {0}", dataPins[0]); Console.WriteLine("\tData 2: {0}", dataPins[1]); Console.WriteLine("\tData 3: {0}", dataPins[2]); Console.WriteLine("\tData 4: {0}", dataPins[3]); Console.WriteLine("\tBacklight: {0}", backlightPin); Console.WriteLine("\tRead/write: GND"); Console.WriteLine(); const ConnectorPin sdaPin = ConnectorPin.P1Pin03; const ConnectorPin sclPin = ConnectorPin.P1Pin05; var driver = new I2cDriver(sdaPin.ToProcessor(), sclPin.ToProcessor()) { ClockDivider = 512 }; var connection = new Mcp23008I2cConnection(driver.Connect(address)); var retVal = new Hd44780Configuration(driver) { Pins = new Hd44780Pins( connection.Out(registerSelectPin), connection.Out(clockPin), dataPins.Select(pin => (IOutputBinaryPin)connection.Out(pin))) }; retVal.Pins.Backlight = connection.Out(backlightPin); return(retVal); }
/// <summary> /// Sets the resistor. /// </summary> /// <param name="pin">The pin.</param> /// <param name="resistor">The resistor.</param> public void SetResistor(Mcp23008Pin pin, Mcp23008PinResistor resistor) { var register = Register.GPPU; connection.WriteByte((byte)register); var resistors = connection.ReadByte(); var bit = (byte)((int)pin & 0xFF); var newResistors = (resistor == Mcp23008PinResistor.PullUp) ? resistors | bit : resistors & ~bit; connection.Write(new[] { (byte)register, (byte)newResistors }); }
/// <summary> /// Sets the polarity. /// </summary> /// <param name="pin">The pin.</param> /// <param name="polarity">The polarity.</param> public void SetPolarity(Mcp23008Pin pin, Mcp23008PinPolarity polarity) { var register = Register.IPOL; connection.WriteByte((byte)register); var polarities = connection.ReadByte(); var bit = (byte)((int)pin & 0xFF); var newPolarities = (polarity == Mcp23008PinPolarity.Inverted) ? polarities | bit : polarities & ~bit; connection.Write(new[] { (byte)register, (byte)newPolarities }); }
/// <summary> /// Sets the direction. /// </summary> /// <param name="pin">The pin.</param> /// <param name="direction">The direction.</param> public void SetDirection(Mcp23008Pin pin, Mcp23008PinDirection direction) { var register = Register.IODIR; connection.WriteByte((byte)register); var directions = connection.ReadByte(); var bit = (byte)((int)pin & 0xFF); var newDirections = (direction == Mcp23008PinDirection.Input) ? directions | bit : directions & ~bit; connection.Write(new[] { (byte)register, (byte)newDirections }); }
/// <summary> /// Sets the pin status. /// </summary> /// <param name="pin">The pin.</param> /// <param name="enabled">if set to <c>true</c>, pin is enabled.</param> public void SetPinStatus(Mcp23008Pin pin, bool enabled) { var register = Register.GPIO; connection.WriteByte((byte)register); var status = connection.ReadByte(); var bit = (byte)((int)pin & 0xFF); var newStatus = enabled ? status | bit : status & ~bit; connection.Write((byte)register, (byte)newStatus); }
/// <summary> /// Sets the resistor. /// </summary> /// <param name="pin">The pin.</param> /// <param name="resistor">The resistor.</param> public void SetResistor(Mcp23008Pin pin, Mcp23008PinResistor resistor) { var register = Register.Gppu; this.connection.WriteByte((byte)register); var resistors = this.connection.ReadByte(); var bit = (byte)((int)pin & 0xFF); var newResistors = resistor == Mcp23008PinResistor.PullUp ? resistors | bit : resistors & ~bit; this.connection.Write((byte)register, (byte)newResistors); }
/// <summary> /// Sets the polarity. /// </summary> /// <param name="pin">The pin.</param> /// <param name="polarity">The polarity.</param> public void SetPolarity(Mcp23008Pin pin, Mcp23008PinPolarity polarity) { var register = Register.Ipol; this.connection.WriteByte((byte)register); var polarities = this.connection.ReadByte(); var bit = (byte)((int)pin & 0xFF); var newPolarities = polarity == Mcp23008PinPolarity.Inverted ? polarities | bit : polarities & ~bit; this.connection.Write((byte)register, (byte)newPolarities); }
/// <summary> /// Sets the direction. /// </summary> /// <param name="pin">The pin.</param> /// <param name="direction">The direction.</param> public void SetDirection(Mcp23008Pin pin, Mcp23008PinDirection direction) { var register = Register.Iodir; this.connection.WriteByte((byte)register); var directions = this.connection.ReadByte(); var bit = (byte)((int)pin & 0xFF); var newDirections = direction == Mcp23008PinDirection.Input ? directions | bit : directions & ~bit; this.connection.Write((byte)register, (byte)newDirections); }
/// <summary> /// Toogles the specified pin. /// </summary> /// <param name="pin">The pin.</param> public void Toogle(Mcp23008Pin pin) { var register = Register.GPIO; connection.WriteByte((byte)register); var status = connection.ReadByte(); var bit = (byte)((int)pin & 0xFF); var bitEnabled = (status & bit) != 0x00; var newBitEnabled = !bitEnabled; var newStatus = newBitEnabled ? status | bit : status & ~bit; connection.Write((byte)register, (byte)newStatus); }
/// <summary> /// Creates an input binary pin. /// </summary> /// <param name="connection">The connection.</param> /// <param name="pin">The pin.</param> /// <param name="resistor">The resistor.</param> /// <param name="polarity">The polarity.</param> /// <returns>The pin.</returns> public static Mcp23008InputBinaryPin In(this Mcp23008I2cConnection connection, Mcp23008Pin pin, Mcp23008PinResistor resistor = Mcp23008PinResistor.None, Mcp23008PinPolarity polarity = Mcp23008PinPolarity.Normal) { return(new Mcp23008InputBinaryPin(connection, pin, resistor, polarity)); }
/// <summary> /// Creates an output binary pin. /// </summary> /// <param name="connection">The connection.</param> /// <param name="pin">The pin.</param> /// <param name="resistor">The resistor.</param> /// <param name="polarity">The polarity.</param> /// <returns>The pin.</returns> public static Mcp23008OutputBinaryPin Out(this Mcp23008I2cConnection connection, Mcp23008Pin pin, Mcp23008PinResistor resistor = Mcp23008PinResistor.None, Mcp23008PinPolarity polarity = Mcp23008PinPolarity.Normal) { return new Mcp23008OutputBinaryPin(connection, pin, resistor, polarity); }
/// <summary> /// Creates an output binary pin. /// </summary> /// <param name="connection">The connection.</param> /// <param name="pin">The pin.</param> /// <param name="resistor">The resistor.</param> /// <param name="polarity">The polarity.</param> /// <returns>The output pin.</returns> public static Mcp23008OutputBinaryPin Out(this Mcp23008Device connection, Mcp23008Pin pin, Mcp23008PinResistor resistor = Mcp23008PinResistor.None, Mcp23008PinPolarity polarity = Mcp23008PinPolarity.Normal) { return(new Mcp23008OutputBinaryPin(connection, pin, resistor, polarity)); }