Beispiel #1
0
        private static void CharacterSet(Hd44780 lcd)
        {
            StringBuilder sb = new StringBuilder(256);

            for (int i = 0; i < 256; i++)
            {
                sb.Append((char)i);
            }

            int  character = 0;
            int  line      = 0;
            Size size      = lcd.Size;

            while (character < 256)
            {
                lcd.SetCursorPosition(0, line);
                lcd.Write(sb.ToString(character, Math.Min(size.Width, 256 - character)));
                line++;
                character += size.Width;
                if (line >= size.Height)
                {
                    line = 0;
                    System.Threading.Thread.Sleep(1000);
                }
            }
        }
Beispiel #2
0
        static async Task App()
        {
            var board = await ConnectionService.Instance.GetFirstDeviceAsync();

            await board.ConnectAsync();

            // configure the native parallel interface with the pins we're using
            board.ParallelInterface.RegisterSelectPin = board.Pins[0];
            board.ParallelInterface.ReadWritePin      = board.Pins[1];
            board.ParallelInterface.EnablePin         = board.Pins[2];
            board.ParallelInterface.DataBus.Add(board.Pins[3]);
            board.ParallelInterface.DataBus.Add(board.Pins[4]);
            board.ParallelInterface.DataBus.Add(board.Pins[5]);
            board.ParallelInterface.DataBus.Add(board.Pins[6]);
            board.ParallelInterface.DataBus.Add(board.Pins[7]);
            board.ParallelInterface.DataBus.Add(board.Pins[8]);
            board.ParallelInterface.DataBus.Add(board.Pins[9]);
            board.ParallelInterface.DataBus.Add(board.Pins[10]);

            var display = new Hd44780(board.ParallelInterface, 16, 2);
            await display.WriteLine("The time is:").ConfigureAwait(false);

            while (true)
            {
                await display.Write(DateTime.Now.ToLongTimeString()).ConfigureAwait(false);

                await display.SetCursorPosition(0, 1).ConfigureAwait(false);

                await Task.Delay(1000).ConfigureAwait(false);
            }
        }
Beispiel #3
0
        /// <summary>
        /// This demonstrates the use of a large font on a 20x4 display. This is useful especially when showing values that should be readable from farther away,
        /// such as the time, or a temperature.
        /// </summary>
        /// <param name="lcd">The display</param>
        public static void LargeValueDemo(Hd44780 lcd)
        {
            LcdValueUnitDisplay value = new LcdValueUnitDisplay(lcd, CultureInfo.CurrentCulture);

            value.InitForRom("A00");
            value.Clear();
            Console.WriteLine("Big clock test");
            while (!Console.KeyAvailable)
            {
                value.DisplayTime(DateTime.Now, "T");
                Thread.Sleep(200);
            }

            Console.ReadKey(true);
            Console.WriteLine("Showing fake temperature");
            value.DisplayValue("24.2 °C", "Temperature");
            Console.ReadKey(true);

            Console.WriteLine("Now showing a text");
            CancellationTokenSource src = new CancellationTokenSource();

            value.DisplayBigTextAsync("The quick brown fox jumps over the lazy dog at 10.45PM on May, 3rd", TimeSpan.FromSeconds(0.5), src.Token);
            Console.ReadKey(true);
            src.Cancel();
        }
Beispiel #4
0
        private static void SetBacklightColorTest(Hd44780 lcd)
        {
            var colorLcd = lcd as LcdRgb;

            if (colorLcd == null)
            {
                Console.WriteLine("Color backlight not supported");
                return;
            }

            Color[] colors =
            {
                Color.Red,   Color.Green,     Color.Blue,         Color.Aqua, Color.Azure,
                Color.Brown, Color.Chocolate, Color.LemonChiffon, Color.Lime, Color.Tomato, Color.Yellow
            };

            foreach (var color in colors)
            {
                lcd.Clear();
                lcd.Write(color.Name);

                colorLcd.SetBacklightColor(color);
                System.Threading.Thread.Sleep(1000);
            }

            lcd.Clear();
            colorLcd.SetBacklightColor(Color.White);
        }
Beispiel #5
0
 private static void WriteFromEnd(Hd44780 lcd, string value)
 {
     lcd.Increment = false;
     lcd.SetCursorPosition(lcd.Size.Width - 1, lcd.Size.Height - 1);
     lcd.Write(value);
     lcd.Increment = true;
 }
Beispiel #6
0
 /// <summary>
 /// A small test that shows something useful. It may not work optimally due to wrong character mappings if the month names contain non-ascii characters.
 /// </summary>
 static void TestClock(Hd44780 lcd)
 {
     using (System.Timers.Timer timer = new System.Timers.Timer(100))
     {
         object myLock = new object();
         timer.Elapsed += (o, e) =>
         {
             // The callback may be executed in parallel several times, but the display component is not reentrant!
             if (Monitor.TryEnter(myLock))
             {
                 var now = DateTime.Now;
                 lcd.SetCursorPosition(0, 0);
                 lcd.Write(String.Format(CultureInfo.CurrentCulture, "{0:dddd}", now));
                 lcd.SetCursorPosition(0, 1);
                 lcd.Write(String.Format(CultureInfo.CurrentCulture, "{0:M} {0:yyyy}", now, now));
                 lcd.SetCursorPosition(0, 2);
                 lcd.Write("It is now ");
                 lcd.SetCursorPosition(0, 3);
                 lcd.Write(String.Format(CultureInfo.CurrentCulture, "{0}", now.ToLongTimeString()));
                 Monitor.Exit(myLock);
             }
         };
         timer.AutoReset = true;
         timer.Enabled   = true;
         Console.ReadLine();
     }
 }
Beispiel #7
0
        private static void ShiftDisplayTest(Hd44780 lcd, Action <Hd44780> action)
        {
            Size size = lcd.Size;

            lcd.Write(Eighty.Substring(0, size.Height * size.Width));
            ShiftTest(lcd, action);
        }
Beispiel #8
0
        private static void AutoShift(Hd44780 lcd)
        {
            lcd.AutoShift = true;
            Size size = lcd.Size;

            lcd.Write(Eighty.Substring(0, size.Width + size.Width / 2));
            lcd.AutoShift = false;
        }
Beispiel #9
0
        private static void ShiftTest(Hd44780 lcd, Action <Hd44780> action)
        {
            Size size = lcd.Size;

            for (int i = 0; i <= size.Width; i++)
            {
                action(lcd);
                System.Threading.Thread.Sleep(250);
            }
        }
Beispiel #10
0
 static void DisplayAndBackLightOnOff(Hd44780 lcd)
 {
     lcd.Clear();
     lcd.Write("This is some text");
     lcd.DisplayOn = false;
     Thread.Sleep(1000);
     lcd.DisplayOn   = true;
     lcd.BacklightOn = false;
     Thread.Sleep(1000);
     lcd.BacklightOn = true;
 }
Beispiel #11
0
        private static void SetCursorTest(Hd44780 lcd)
        {
            Size size   = lcd.Size;
            int  number = 0;

            for (int i = 0; i < size.Height; i++)
            {
                lcd.SetCursorPosition(0, i);
                lcd.Write($"{number++}");
                lcd.SetCursorPosition(size.Width - 1, i);
                lcd.Write($"{number++}");
            }
        }
Beispiel #12
0
        static void TestPrompt(string test, Hd44780 lcd, Action <Hd44780> action)
        {
            string prompt = $"Test {test}:";

            lcd.Clear();
            lcd.Write(prompt);
            lcd.BlinkingCursorVisible = true;
            Console.Write(prompt);
            Console.ReadLine();
            lcd.BlinkingCursorVisible = false;
            lcd.Clear();
            action(lcd);
            Console.Write("Test Complete:");
            Console.ReadLine();
            lcd.Clear();
        }
Beispiel #13
0
        private static void WalkerTest(Hd44780 lcd)
        {
            CreateWalkCharacters(lcd);
            string walkOne = new string('\x8', lcd.Size.Width);
            string walkTwo = new string('\x9', lcd.Size.Width);

            for (int i = 0; i < 5; i++)
            {
                lcd.SetCursorPosition(0, 0);
                lcd.Write(walkOne);
                System.Threading.Thread.Sleep(500);
                lcd.SetCursorPosition(0, 0);
                lcd.Write(walkTwo);
                System.Threading.Thread.Sleep(500);
            }
        }
Beispiel #14
0
        private static void PerfTests(Hd44780 lcd)
        {
            string    stars     = new string('*', 80);
            Stopwatch stopwatch = Stopwatch.StartNew();

            lcd.Clear();
            for (int i = 0; i < 25; i++)
            {
                lcd.Write(Eighty);
                lcd.Write(stars);
            }

            lcd.Clear();
            stopwatch.Stop();
            string result = $"Elapsed ms: {stopwatch.ElapsedMilliseconds}";

            lcd.Write(result);
            Console.WriteLine(result);
        }
Beispiel #15
0
        public static void Test(Hd44780 lcd)
        {
            Console.WriteLine("Starting...");
            lcd.Clear();
            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);

            TestPrompt("DisplayEnable", lcd, DisplayAndBackLightOnOff);

            // 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);

            TestPrompt("Colors", lcd, SetBacklightColorTest);

            TestPrompt("Time", lcd, TestClock);
            lcd.DisplayOn   = false;
            lcd.BacklightOn = false;
            Console.WriteLine("Done...");
        }
Beispiel #16
0
 private static void CreateWalkCharacters(Hd44780 lcd)
 {
     // Walk 1
     lcd.CreateCustomCharacter(0,
                               0b_00110,
                               0b_00110,
                               0b_01100,
                               0b_10111,
                               0b_00100,
                               0b_01110,
                               0b_01010,
                               0b_10001);
     // Walk 2
     lcd.CreateCustomCharacter(1,
                               0b_00110,
                               0b_00110,
                               0b_01100,
                               0b_01100,
                               0b_00110,
                               0b_00110,
                               0b_01010,
                               0b_01010);
 }
Beispiel #17
0
 private static void CreateTensCharacters(Hd44780 lcd)
 {
     // 10
     lcd.CreateCustomCharacter(0,
                               0b_10000,
                               0b_10000,
                               0b_10000,
                               0b_10000,
                               0b_10111,
                               0b_00101,
                               0b_00101,
                               0b_00111);
     // 20
     lcd.CreateCustomCharacter(1,
                               0b_11100,
                               0b_00100,
                               0b_11100,
                               0b_10000,
                               0b_11111,
                               0b_00101,
                               0b_00101,
                               0b_00111);
     // 30
     lcd.CreateCustomCharacter(2,
                               0b_11100,
                               0b_00100,
                               0b_11100,
                               0b_00100,
                               0b_11111,
                               0b_00101,
                               0b_00101,
                               0b_00111);
     // 40
     lcd.CreateCustomCharacter(3,
                               0b_10100,
                               0b_10100,
                               0b_11100,
                               0b_00100,
                               0b_00111,
                               0b_00101,
                               0b_00101,
                               0b_00111);
     // 50
     lcd.CreateCustomCharacter(4,
                               0b_11100,
                               0b_10000,
                               0b_11100,
                               0b_00100,
                               0b_11111,
                               0b_00101,
                               0b_00101,
                               0b_00111);
     // 60
     lcd.CreateCustomCharacter(5,
                               0b_11100,
                               0b_10000,
                               0b_11100,
                               0b_10100,
                               0b_11111,
                               0b_00101,
                               0b_00101,
                               0b_00111);
     // 70
     lcd.CreateCustomCharacter(6,
                               0b_11100,
                               0b_00100,
                               0b_01000,
                               0b_01000,
                               0b_01111,
                               0b_00101,
                               0b_00101,
                               0b_00111);
     // 80
     lcd.CreateCustomCharacter(7,
                               0b_11100,
                               0b_10100,
                               0b_11100,
                               0b_10100,
                               0b_11111,
                               0b_00101,
                               0b_00101,
                               0b_00111);
 }
Beispiel #18
0
 private static void ShiftCursorTest(Hd44780 lcd, Action <Hd44780> action)
 {
     lcd.BlinkingCursorVisible = true;
     ShiftTest(lcd, action);
     lcd.BlinkingCursorVisible = false;
 }
Beispiel #19
0
        public static void Test()
        {
            Console.WriteLine("Starting...");

#if USEI2C
            var i2cDevice  = new UnixI2cDevice(new I2cConnectionSettings(busId: 1, deviceAddress: 0x21));
            var controller = new Mcp23008Adapter(new Mcp23008(i2cDevice));
            var lcd        = new Hd44780(registerSelect: 1, enable: 2, data: new int[] { 3, 4, 5, 6 }, size: new Size(16, 2), backlight: 7, controller: controller);
#else
            Hd44780 lcd = new Hd44780(12, 26, new int[] { 16, 17, 18, 19, 20, 21, 22, 23 }, new Size(20, 4), readWrite: 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);

                // 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...");
                }
            }
        }