public Hd44780LcdConnection(Hd44780LcdConnectionSettings settings, Hd44780Pins pins)
{
settings = settings ?? new Hd44780LcdConnectionSettings();
this.pins = pins;
if (pins.Data.Length != 4 && pins.Data.Length != 8)
throw new ArgumentOutOfRangeException("pins", pins.Data.Length, "There must be either 4 or 8 data pins");
width = settings.ScreenWidth;
height = settings.ScreenHeight;
if (height < 1 || height > MAX_HEIGHT)
throw new ArgumentOutOfRangeException("settings", height, "ScreenHeight must be between 1 and 4 rows");
if (width * height > MAX_CHAR)
throw new ArgumentException("At most 80 characters are allowed");
if (settings.PatternWidth != 5)
throw new ArgumentOutOfRangeException("settings", settings.PatternWidth, "PatternWidth must be 5 pixels");
if (settings.PatternHeight != 8 && settings.PatternHeight != 10)
throw new ArgumentOutOfRangeException("settings", settings.PatternWidth, "PatternWidth must be either 7 or 10 pixels height");
if (settings.PatternHeight == 10 && (height % 2) == 0)
throw new ArgumentException("10 pixels height pattern cannot be used with an even number of rows");
functions = (settings.PatternHeight == 8 ? Functions.Matrix5x8 : Functions.Matrix5x10)
| (height == 1 ? Functions.OneLine : Functions.TwoLines)
| (pins.Data.Length == 4 ? Functions.Data4bits : Functions.Data8bits);
entryModeFlags = /*settings.RightToLeft
? EntryModeFlags.EntryRight | EntryModeFlags.EntryShiftDecrement
:*/ EntryModeFlags.EntryLeft | EntryModeFlags.EntryShiftDecrement;
encoding = settings.Encoding;
BacklightEnabled = false;
if (pins.ReadWrite != null)
pins.ReadWrite.Write(false);
pins.RegisterSelect.Write(false);
pins.Clock.Write(false);
foreach (var dataPin in pins.Data)
dataPin.Write(false);
WriteByte(0x33, false); // Initialize
WriteByte(0x32, false);
WriteCommand(Command.SetFunctions, (int) functions);
WriteCommand(Command.SetDisplayFlags, (int) displayFlags);
WriteCommand(Command.SetEntryModeFlags, (int) entryModeFlags);
Clear();
BacklightEnabled = true;
}