Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new Lcm1602a1 display object.
        /// This object will use the board's GPIO pins, or the McpController pins if one is provided.
        /// </summary>
        /// <param name="mcpController">McpController that is used to provide the pins for the display. Null if we should use board's GPIO pins instead.</param>
        /// <param name="registerSelect">The pin that controls the regsiter select.</param>
        /// <param name="readWrite">The pin that controls the read and write switch.</param>
        /// <param name="enable">The pin that controls the enable switch.</param>
        /// <param name="backlight">The pin that controls the backlight of the display.</param>
        /// <param name="data">Collection of pins holding the data that will be printed on the screen.</param>
        public Lcm1602a1(Mcp23008 mcpController, int registerSelect, int readWrite, int enable, int backlight, int[] data)
        {
            _rwPin     = readWrite;
            _rsPin     = registerSelect;
            _enablePin = enable;
            _dataPins  = data;
            _backlight = backlight;

            _rowOffsets = new byte[4];

            _displayFunction = DisplayFlags.LCD_1LINE | DisplayFlags.LCD_5x8DOTS;

            if (data.Length == 4)
            {
                _displayFunction |= DisplayFlags.LCD_4BITMODE;
            }
            else if (data.Length == 8)
            {
                _displayFunction |= DisplayFlags.LCD_8BITMODE;
            }
            else
            {
                throw new ArgumentException($"The length of the array given to parameter {nameof(data)} must be 4 or 8");
            }

            if (mcpController == null)
            {
                _controller = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) ?
                              new GpioController(PinNumberingScheme.Logical, new UnixDriver()) :
                              new GpioController(PinNumberingScheme.Logical, new Windows10Driver());
                _usingMcp = false;
            }
            else
            {
                _mcpController = mcpController;
                _usingMcp      = true;
            }

            OpenPin(_rsPin, PinMode.Input);
            if (_rwPin != -1)
            {
                OpenPin(_rwPin, PinMode.Input);
            }
            if (_backlight != -1)
            {
                OpenPin(_backlight, PinMode.Input);
            }
            OpenPin(_enablePin, PinMode.Input);
            foreach (int i in _dataPins)
            {
                OpenPin(i, PinMode.Input);
            }
            // By default, initialize the display with one row and 16 characters.
            Begin(16, 1);
        }
Ejemplo n.º 2
0
        static async Task App()
        {
            var board = await ConnectionService.Instance.GetFirstDeviceAsync();

            await board.ConnectAsync();

            var gpio = new Mcp23008(board.I2c);

            gpio.Pins[0].DigitalValue  = true;
            gpio.Pins[7].PullUpEnabled = true;
            while (!Console.KeyAvailable)
            {
                Console.WriteLine(await gpio.Pins[7].AwaitDigitalValueChangeAsync());
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            // int[] dataPins = { 12, 11, 10, 9 };
            // int registerSelectPin = 15;
            // int enablePin = 13;
            // int readAndWritePin = 14;
            // DateTime xmas = new DateTime(2019, 12, 25);
            // CancellationTokenSource cts = new CancellationTokenSource();
            // using (var lcd = new Lcm1602c(registerSelectPin, enablePin, dataPins))
            // {
            //     lcd.Clear(); //Clear in case there was a previous program that left some text on the screen
            //     lcd.Begin(16, 2); //Initialize the lcd to use 2 rows, each with 16 characters.

            //     lcd.Print("X-Mas Countdown"); //Print string on first row.

            //     Console.CancelKeyPress += (o, e) => // Add handler for when the program should be terminated.
            //     {
            //         cts.Cancel();
            //     };

            //     while (!cts.Token.IsCancellationRequested) // Loop until Ctr-C is pressed.
            //     {
            //         lcd.SetCursor(0, 1);
            //         TimeSpan countdown = xmas - DateTime.Now;
            //         lcd.Print($"");
            //     }
            // }

            Mcp23008 mcpDevice = new Mcp23008(new UnixI2cDevice(new I2cConnectionSettings(1, 0x21)));

            int[] dataPins          = { 3, 4, 5, 6 };
            int   registerSelectPin = 1;
            int   enablePin         = 2;
            int   backlightPin      = 7;

            using (Lcm1602a1 lcd = new Lcm1602a1(mcpDevice, registerSelectPin, -1, enablePin, backlightPin, dataPins))
            {
                lcd.Clear();
                lcd.Begin(16, 2);

                lcd.Print("Hello World!");
                lcd.SetCursor(0, 1);
                lcd.Print(".NET Core");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This method will use an mcp gpio extender to connect to the LCM display.
        /// This has been tested on the CrowPi lcd display.
        /// </summary>
        static void UsingMcp()
        {
            UnixI2cDevice i2CDevice = new UnixI2cDevice(new I2cConnectionSettings(1, 0x21));
            Mcp23008      mcpDevice = new Mcp23008(i2CDevice);

            int[] dataPins          = { 3, 4, 5, 6 };
            int   registerSelectPin = 1;
            int   enablePin         = 2;
            int   backlight         = 7;

            using (mcpDevice)
                using (Lcd1602 lcd = new Lcd1602(registerSelectPin, enablePin, dataPins, backlight, controller: mcpDevice))
                {
                    lcd.Clear();

                    lcd.Write("Hello World");
                    lcd.SetCursorPosition(0, 1);
                    lcd.Write(".NET Core");
                }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// This method will use an mcp gpio extender to connect to the LCM display.
        /// This has been tested on the CrowPi lcd display.
        /// </summary>
        private static void UsingMcp()
        {
            I2cDevice i2CDevice = I2cDevice.Create(new I2cConnectionSettings(1, 0x21));
            Mcp23008  driver    = new Mcp23008(i2CDevice);

            int[] dataPins          = { 3, 4, 5, 6 };
            int   registerSelectPin = 1;
            int   enablePin         = 2;
            int   backlight         = 7;

            using (driver)
                using (Lcd1602 lcd = new Lcd1602(registerSelectPin, enablePin, dataPins, backlight, controller: new GpioController(PinNumberingScheme.Logical, driver)))
                {
                    lcd.Clear();

                    lcd.Write("Hello World");
                    lcd.SetCursorPosition(0, 1);
                    lcd.Write(".NET Core");
                }
        }
Ejemplo n.º 6
0
        public static void SampleEntryPoint()
        {
            Console.WriteLine("Starting...");

            var i2cDevice  = new UnixI2cDevice(new I2cConnectionSettings(busId: 1, deviceAddress: 0x27));
            var controller = new Mcp23008(i2cDevice);
            var lcd        = new Lcd1602(registerSelectPin: 0, enablePin: 2, dataPins: new int[] { 4, 5, 6, 7 }, backlightPin: 3, readWritePin: 1, controller: controller);

            using (lcd)
            {
                Console.WriteLine("Initialized");
                Console.ReadLine();

                TestPrompt("SetCursor", lcd, SetCursorTest);
                TestPrompt("Underline", lcd, l => l.UnderlineCursorVisible = true);
                lcd.UnderlineCursorVisible = false;
                TestPrompt("Walker", lcd, WalkerTest);
                CreateTensCharacters(lcd);
                TestPrompt("CharacterSet", lcd, CharacterSet);

                // Shifting
                TestPrompt("Autoshift", lcd, AutoShift);
                TestPrompt("DisplayLeft", lcd, l => ShiftDisplayTest(l, a => a.ShiftDisplayLeft()));
                TestPrompt("DisplayRight", lcd, l => ShiftDisplayTest(l, a => a.ShiftDisplayRight()));
                TestPrompt("CursorLeft", lcd, l => ShiftCursorTest(l, a => a.ShiftCursorLeft()));
                TestPrompt("CursorRight", lcd, l => ShiftCursorTest(l, a => a.ShiftCursorRight()));

                // Long string
                TestPrompt("Twenty", lcd, l => l.Write(Twenty));
                TestPrompt("Fourty", lcd, l => l.Write(Fourty));
                TestPrompt("Eighty", lcd, l => l.Write(Eighty));

                TestPrompt("Twenty-", lcd, l => WriteFromEnd(l, Twenty));
                TestPrompt("Fourty-", lcd, l => WriteFromEnd(l, Fourty));
                TestPrompt("Eighty-", lcd, l => WriteFromEnd(l, Eighty));

                TestPrompt("Wrap", lcd, l => l.Write(new string('*', 80) + ">>>>>"));
                TestPrompt("Perf", lcd, PerfTests);


                // Shift display right
                lcd.Write("Hello .NET!");
                try
                {
                    int   state = 0;
                    Timer timer = new Timer(1000);
                    timer.Elapsed += (o, e) =>
                    {
                        lcd.SetCursorPosition(0, 1);
                        lcd.Write(DateTime.Now.ToLongTimeString() + " ");
                        if (state == 0)
                        {
                            state = 1;
                        }
                        else
                        {
                            lcd.ShiftDisplayRight();
                            state = 0;
                        }
                    };
                    timer.AutoReset = true;
                    timer.Enabled   = true;
                    Console.ReadLine();
                }
                finally
                {
                    lcd.DisplayOn   = false;
                    lcd.BacklightOn = false;
                    Console.WriteLine("Done...");
                }
            }
        }
Ejemplo n.º 7
0
 public Display(I2cDevice i2c)
 {
     _i2c = i2c;
     _mcp = new Mcp23008(_i2c);
     _lcd = new Lcd2004(registerSelectPin: 0, enablePin: 2, dataPins: new int[] { 4, 5, 6, 7 }, backlightPin: 3, backlightBrightness: 0.1f, readWritePin: 1, controller: _mcp);
 }
Ejemplo n.º 8
0
        public static void Test()
        {
            Console.WriteLine("Starting...");

#if USEI2C
            var i2cDevice  = I2cDevice.Create(new I2cConnectionSettings(busId: 1, deviceAddress: 0x21));
            var controller = new Mcp23008(i2cDevice);
            var lcd        = new Lcd1602(registerSelectPin: 1, enablePin: 2, dataPins: new int[] { 3, 4, 5, 6 }, backlightPin: 7, controller: controller);
#elif USERGB
            var i2cLcdDevice = I2cDevice.Create(new I2cConnectionSettings(busId: 1, deviceAddress: 0x3E));
            var i2cRgbDevice = I2cDevice.Create(new I2cConnectionSettings(busId: 1, deviceAddress: 0x62));
            var lcd          = new LcdRgb1602(i2cLcdDevice, i2cRgbDevice);
#else
            Hd44780 lcd = new Hd44780(new Size(20, 4), LcdInterface.CreateGpio(12, 26, new int[] { 16, 17, 18, 19, 20, 21, 22, 23 }, readWritePin: 13));
#endif
            using (lcd)
            {
                Console.WriteLine("Initialized");
                Console.ReadLine();

                TestPrompt("SetCursor", lcd, SetCursorTest);
                TestPrompt("Underline", lcd, l => l.UnderlineCursorVisible = true);
                lcd.UnderlineCursorVisible = false;
                TestPrompt("Walker", lcd, WalkerTest);
                CreateTensCharacters(lcd);
                TestPrompt("CharacterSet", lcd, CharacterSet);

                // Shifting
                TestPrompt("Autoshift", lcd, AutoShift);
                TestPrompt("DisplayLeft", lcd, l => ShiftDisplayTest(l, a => a.ShiftDisplayLeft()));
                TestPrompt("DisplayRight", lcd, l => ShiftDisplayTest(l, a => a.ShiftDisplayRight()));
                TestPrompt("CursorLeft", lcd, l => ShiftCursorTest(l, a => a.ShiftCursorLeft()));
                TestPrompt("CursorRight", lcd, l => ShiftCursorTest(l, a => a.ShiftCursorRight()));

                // Long string
                TestPrompt("Twenty", lcd, l => l.Write(Twenty));
                TestPrompt("Fourty", lcd, l => l.Write(Fourty));
                TestPrompt("Eighty", lcd, l => l.Write(Eighty));

                TestPrompt("Twenty-", lcd, l => WriteFromEnd(l, Twenty));
                TestPrompt("Fourty-", lcd, l => WriteFromEnd(l, Fourty));
                TestPrompt("Eighty-", lcd, l => WriteFromEnd(l, Eighty));

                TestPrompt("Wrap", lcd, l => l.Write(new string('*', 80) + ">>>>>"));
                TestPrompt("Perf", lcd, PerfTests);

#if USERGB
                TestPrompt("Colors", lcd, SetBacklightColorTest);
#endif

                // Shift display right
                lcd.Write("Hello .NET!");
                try
                {
                    int   state = 0;
                    Timer timer = new Timer(1000);
                    timer.Elapsed += (o, e) =>
                    {
                        lcd.SetCursorPosition(0, 1);
                        lcd.Write(DateTime.Now.ToLongTimeString() + " ");
                        if (state == 0)
                        {
                            state = 1;
                        }
                        else
                        {
                            lcd.ShiftDisplayRight();
                            state = 0;
                        }
                    };
                    timer.AutoReset = true;
                    timer.Enabled   = true;
                    Console.ReadLine();
                }
                finally
                {
                    lcd.DisplayOn   = false;
                    lcd.BacklightOn = false;
                    Console.WriteLine("Done...");
                }
            }
        }
Ejemplo n.º 9
0
 public void Init()
 {
     _i2c = I2cDevice.Create(new I2cConnectionSettings(1, 0x27));
     _mcp = new Mcp23008(_i2c);
     _lcd = new Lcd2004(registerSelectPin: 0, enablePin: 2, dataPins: new int[] { 4, 5, 6, 7 }, backlightPin: 3, backlightBrightness: 0.1f, readWritePin: 1, controller: _mcp);
 }