Try this, I'm working it up into a module:
// change as needed for your setup
//Pi pins conencting to MCP23017 - unlikely to change if yiu areusing hardware i2c
const ConnectorPin sdaPin = ConnectorPin.P1Pin03;
const ConnectorPin sclPin = ConnectorPin.P1Pin05;
// MCP23017 to LCD pins, change these to match your actual connection
const Mcp23017Pin registerSelectPin = Mcp23017Pin.B7;
const Mcp23017Pin clockPin = Mcp23017Pin.B5;
const Mcp23017Pin backlightPin = Mcp23017Pin.B0; // note this is assumed to be connected to the LCD led+
const Mcp23017Pin readWritePin = Mcp23017Pin.B6;
var dataPins = new[]
{
Mcp23017Pin.B4,
Mcp23017Pin.B3,
Mcp23017Pin.B2,
Mcp23017Pin.B1
};
// i2C address of the MCP23017
const int address = 0x20;
// LCD setting characters wide and lines deep
var settings = new Hd44780LcdConnectionSettings
{
ScreenWidth = 16,
ScreenHeight = 2,
};
// connect the system
var driver = new I2cDriver(sdaPin.ToProcessor(), sclPin.ToProcessor()) { ClockDivider = 512 };
var i2cconnection = new Mcp23017I2cConnection(driver.Connect(address));
var Pins = new Hd44780Pins(
i2cconnection.Out(registerSelectPin),
i2cconnection.Out(clockPin),
dataPins.Select(pin => (IOutputBinaryPin)i2cconnection.Out(pin)))
{
Backlight = i2cconnection.Out( backlightPin ),
ReadWrite = i2cconnection.Out( readWritePin )
};
var connection = new Hd44780LcdConnection(settings, Pins);
connection.Clear();
connection.BacklightEnabled = true;
connection.WriteLine("OS Version");
connection.WriteLine(Environment.OSVersion);
When.ProgramStopping(()=>{
((IDisposable)connection).Dispose();
((IDisposable)driver).Dispose();
return true;
});
while (Program.IsEnabled)
{
// do whatever