コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Hd44780LcdConnection" /> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="pins">The pins.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// dataPins;There must be either 4 or 8 data pins
        /// or
        /// settings;ScreenHeight must be between 1 and 4 rows
        /// or
        /// settings;PatternWidth must be 5 pixels
        /// or
        /// settings;PatternWidth must be either 7 or 10 pixels height
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// At most 80 characters are allowed
        /// or
        /// 10 pixels height pattern cannot be used with 2 rows
        /// </exception>
        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;
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Hd44780LcdConnection" /> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="pins">The pins.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// dataPins;There must be either 4 or 8 data pins
        /// or
        /// settings;ScreenHeight must be between 1 and 4 rows
        /// or
        /// settings;PatternWidth must be 5 pixels
        /// or
        /// settings;PatternWidth must be either 7 or 10 pixels height
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// At most 80 characters are allowed
        /// or
        /// 10 pixels height pattern cannot be used with 2 rows
        /// </exception>
        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;
        }
コード例 #3
0
ファイル: Hd44780LcdDevice.cs プロジェクト: hugener/Pi
        /// <summary>
        /// Initializes a new instance of the <see cref="Hd44780LcdDevice"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="gpioConnectionDriverFactory">The gpio connection driver factory.</param>
        /// <param name="registerSelectPin">The register select pin.</param>
        /// <param name="clockPin">The clock pin.</param>
        /// <param name="hd44780DataPins">The HD44780 data pins.</param>
        /// <param name="backlight">The backlight.</param>
        /// <param name="readWrite">The read write.</param>
        /// <param name="threadFactory">The thread factory.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// pins - There must be either 4 or 8 data pins
        /// or
        /// settings - ScreenHeight must be between 1 and 4 rows
        /// or
        /// settings - PatternWidth must be 5 pixels
        /// or
        /// settings - PatternWidth must be either 7 or 10 pixels height.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// At most 80 characters are allowed
        /// or
        /// 10 pixels height pattern cannot be used with an even number of rows.
        /// </exception>
        public Hd44780LcdDevice(
            Hd44780LcdDeviceSettings settings,
            IGpioConnectionDriverFactory gpioConnectionDriverFactory,
            ConnectorPin registerSelectPin,
            ConnectorPin clockPin,
            Hd44780DataPins hd44780DataPins,
            ConnectorPin?backlight       = null,
            ConnectorPin?readWrite       = null,
            IThreadFactory threadFactory = null)
        {
            this.gpioConnectionDriverFactory = GpioConnectionDriverFactory.EnsureGpioConnectionDriverFactory(gpioConnectionDriverFactory);
            this.gpioConnectionDriver        = gpioConnectionDriverFactory.Get();
            this.pins      = new Hd44780Pins(this.gpioConnectionDriver, registerSelectPin, clockPin, backlight, readWrite, hd44780DataPins.ConnectorPins);
            this.thread    = ThreadFactory.EnsureThreadFactory(threadFactory).Create();
            this.syncDelay = settings.SyncDelay;

            if (this.pins.Data.Length != 4 && this.pins.Data.Length != 8)
            {
                throw new ArgumentOutOfRangeException(nameof(hd44780DataPins), this.pins.Data.Length, "There must be either 4 or 8 data pins");
            }

            this.width  = settings.ScreenWidth;
            this.height = settings.ScreenHeight;
            if (this.height < 1 || this.height > MaxHeight)
            {
                throw new ArgumentOutOfRangeException(nameof(settings.ScreenHeight), this.height, "ScreenHeight must be between 1 and 4 rows");
            }

            if (this.width * this.height > MaxChar)
            {
                throw new ArgumentException("At most 80 characters are allowed");
            }

            if (settings.PatternWidth != 5)
            {
                throw new ArgumentOutOfRangeException(nameof(settings.PatternWidth), settings.PatternWidth, "PatternWidth must be 5 pixels");
            }

            if (settings.PatternHeight != 8 && settings.PatternHeight != 10)
            {
                throw new ArgumentOutOfRangeException(nameof(settings.PatternHeight), settings.PatternHeight, "PatternHeight must be either 7 or 10 pixels height");
            }

            if (settings.PatternHeight == 10 && this.height % 2 == 0)
            {
                throw new ArgumentException("10 pixels height pattern cannot be used with an even number of rows");
            }

            this.functions = (settings.PatternHeight == 8 ? Functions.Matrix5X8 : Functions.Matrix5X10)
                             | (this.height == 1 ? Functions.OneLine : Functions.TwoLines)
                             | (this.pins.Data.Length == 4 ? Functions.Data4Bits : Functions.Data8Bits);

            this.entryModeFlags = /*settings.RightToLeft
                                   * ? EntryModeFlags.EntryRight | EntryModeFlags.EntryShiftDecrement
                                   * :*/EntryModeFlags.EntryLeft | EntryModeFlags.EntryShiftDecrement;

            this.encoding = settings.Encoding;

            this.BacklightEnabled = false;

            this.pins.ReadWrite?.Write(false);

            this.pins.RegisterSelect.Write(false);
            this.pins.Clock.Write(false);
            foreach (var dataPin in this.pins.Data)
            {
                dataPin.Write(false);
            }

            this.WriteByte(0x33, false); // Initialize
            this.WriteByte(0x32, false);

            this.thread.Sleep(TimeSpanUtility.FromMicroseconds(50));

            this.WriteCommand(Command.SetFunctions, (int)this.functions);
            this.WriteCommand(Command.SetDisplayFlags, (int)this.displayFlags);
            this.WriteCommand(Command.SetEntryModeFlags, (int)this.entryModeFlags);

            this.Clear();
            this.BacklightEnabled = true;
        }
コード例 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Hd44780LcdConnection"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="registerSelectPin">The register select pin.</param>
        /// <param name="clockPin">The clock pin.</param>
        /// <param name="dataPins">The data pins.</param>
        public Hd44780LcdConnection(Hd44780LcdConnectionSettings settings, ProcessorPin registerSelectPin, ProcessorPin clockPin, IEnumerable <ProcessorPin> dataPins)
        {
            settings = settings ?? new Hd44780LcdConnectionSettings();

            this.registerSelectPin = registerSelectPin;
            this.clockPin          = clockPin;
            this.dataPins          = dataPins.ToArray();

            if (this.dataPins.Length != 4 && this.dataPins.Length != 8)
            {
                throw new ArgumentOutOfRangeException("dataPins", this.dataPins.Length, "There must be either 4 or 8 data pins");
            }

            width  = settings.ScreenWidth;
            height = settings.ScreenHeight;
            if (height < 1 || height > 2)
            {
                throw new ArgumentOutOfRangeException("ScreenHeight", height, "Screen must have either 1 or 2 rows");
            }
            if (width * height > 80)
            {
                throw new ArgumentException("At most 80 characters are allowed");
            }

            if (settings.PatternWidth != 5)
            {
                throw new ArgumentOutOfRangeException("PatternWidth", settings.PatternWidth, "Pattern must be 5 pixels width");
            }
            if (settings.PatternHeight != 8 && settings.PatternHeight != 10)
            {
                throw new ArgumentOutOfRangeException("PatternHeight", settings.PatternWidth, "Pattern must be either 7 or 10 pixels height");
            }
            if (settings.PatternHeight == 10 && height == 2)
            {
                throw new ArgumentException("10 pixels height pattern cannot be used with 2 rows");
            }

            functions = (settings.PatternHeight == 8 ? Functions.Matrix5x8 : Functions.Matrix5x10)
                        | (height == 1 ? Functions.OneLine : Functions.TwoLines)
                        | (this.dataPins.Length == 4 ? Functions.Data4bits : Functions.Data8bits);

            entryModeFlags = /*settings.RightToLeft
                              * ? EntryModeFlags.EntryRight | EntryModeFlags.EntryShiftDecrement
                              * :*/EntryModeFlags.EntryLeft | EntryModeFlags.EntryShiftDecrement;

            encoding = settings.Encoding;

            connectionDriver = new MemoryGpioConnectionDriver();

            connectionDriver.Allocate(registerSelectPin, PinDirection.Output);
            connectionDriver.Write(registerSelectPin, false);

            connectionDriver.Allocate(clockPin, PinDirection.Output);
            connectionDriver.Write(clockPin, false);

            foreach (var dataPin in this.dataPins)
            {
                connectionDriver.Allocate(dataPin, PinDirection.Output);
                connectionDriver.Write(dataPin, false);
            }

            WriteByte(0x33, false); // Initialize
            WriteByte(0x32, false);

            WriteCommand(Command.SetFunctions, (int)functions);
            WriteCommand(Command.SetDisplayFlags, (int)displayFlags);
            WriteCommand(Command.SetEntryModeFlags, (int)entryModeFlags);

            Clear();
        }
コード例 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Hd44780LcdConnection"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="registerSelectPin">The register select pin.</param>
        /// <param name="clockPin">The clock pin.</param>
        /// <param name="dataPins">The data pins.</param>
        public Hd44780LcdConnection(Hd44780LcdConnectionSettings settings, IOutputPin registerSelectPin, IOutputPin clockPin, IEnumerable<IOutputPin> dataPins)
        {
            settings = settings ?? new Hd44780LcdConnectionSettings();

            this.registerSelectPin = registerSelectPin;
            this.clockPin = clockPin;
            this.dataPins = dataPins.ToArray();

            if (this.dataPins.Length != 4 && this.dataPins.Length != 8)
                throw new ArgumentOutOfRangeException("dataPins", this.dataPins.Length, "There must be either 4 or 8 data pins");

            width = settings.ScreenWidth;
            height = settings.ScreenHeight;
            if (height < 1 || height > 2)
                throw new ArgumentOutOfRangeException("ScreenHeight", height, "Screen must have either 1 or 2 rows");
            if (width * height > 80)
                throw new ArgumentException("At most 80 characters are allowed");

            if (settings.PatternWidth != 5)
                throw new ArgumentOutOfRangeException("PatternWidth", settings.PatternWidth, "Pattern must be 5 pixels width");
            if (settings.PatternHeight != 8 && settings.PatternHeight != 10)
                throw new ArgumentOutOfRangeException("PatternHeight", settings.PatternWidth, "Pattern must be either 7 or 10 pixels height");
            if (settings.PatternHeight == 10 && height == 2)
                throw new ArgumentException("10 pixels height pattern cannot be used with 2 rows");

            functions = (settings.PatternHeight == 8 ? Functions.Matrix5x8 : Functions.Matrix5x10)
                | (height == 1 ? Functions.OneLine : Functions.TwoLines)
                | (this.dataPins.Length == 4 ? Functions.Data4bits : Functions.Data8bits);

            entryModeFlags = /*settings.RightToLeft
                ? EntryModeFlags.EntryRight | EntryModeFlags.EntryShiftDecrement
                :*/ EntryModeFlags.EntryLeft | EntryModeFlags.EntryShiftDecrement;

            encoding = settings.Encoding;

            registerSelectPin.Write(false);
            clockPin.Write(false);
            foreach (var dataPin in this.dataPins)
                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();
        }