BackgroundTaskDeferral _deferral;   // for a headless Windows 10 for IoT projects you need to hold a deferral to keep the app active in the background

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            _deferral = taskInstance.GetDeferral();  // get the deferral handle

            MAX7219 driver = new MAX7219(4, MAX7219.Rotate.None, MAX7219.Transform.HorizontalFlip, MAX7219.ChipSelect.CE0);  // 4 panels, rotate 90 degrees, SPI CE0
            LED8x8Matrix matrix = new LED8x8Matrix(driver);     // pass the driver to the LED8x8Matrix Graphics Library

            while (true) {
                matrix.ScrollStringInFromRight("Hello World 2015 ", 100);
            }
        }
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            _deferral = taskInstance.GetDeferral();
            LED8x8Matrix matrix = new LED8x8Matrix(driver);

            BMP180 tempAndPressure = new BMP180();

            while (true)
            {
                var message = $"{Math.Round(tempAndPressure.Temperature.DegreesCelsius, 1)}C, {Math.Round(tempAndPressure.Pressure.Hectopascals, 1)}hPa ";

                matrix.ScrollStringInFromRight(message, 70);
            }
        }
        private void ScrollText(string message, int repeatCount)
        {
            // update UI
            UpdateUi(message, repeatCount);

            //var driver = new Ht16K33(new byte[] { 0x70 }, Ht16K33.Rotate.None, LedDriver.Display.On, 2, LedDriver.BlinkRate.Off, "I2C1");
            var driver = new Ht16K33();

            LED8x8Matrix matrix = new LED8x8Matrix(driver);
            matrix.SetBrightness(1); // override default brightness

            // We'll repeat the message here
            for (int i = 0; i < repeatCount; i++)
            {
                matrix.FrameClear();
                matrix.ScrollStringInFromRight(message + "        ", 100);
                matrix.FrameClear();
            }
        }
        public async void Start() {

            var driver = new MAX7219(4, MAX7219.Rotate.None, MAX7219.Transform.HorizontalFlip, MAX7219.ChipSelect.CE1);

            LED8x8Matrix matrix = new LED8x8Matrix(driver);

            matrix.SetBrightness(2);

            while (true) {

                matrix.FrameClear();
                matrix.ScrollStringInFromRight("Hello World 2015", 100);

                matrix.FrameClear();
                matrix.ScrollStringInFromLeft("Hello World 2015", 100);

                //continue;

                for (ushort p = 0; p < matrix.PanelsPerFrame; p++) {
                    matrix.DrawSymbol(Grid8x8.Symbols.Block, Led.On, p);
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }


                for (int p = 0; p < matrix.Length; p++) {
                    matrix.FrameSet(Led.On, p);
                    matrix.FrameSet(Led.On, matrix.Length - 1 - p);

                    matrix.FrameDraw();
                    await Task.Delay(2);

                    matrix.FrameSet(Led.Off, p);
                    matrix.FrameSet(Led.Off, matrix.Length - 1 - p);

                    matrix.FrameDraw();
                    await Task.Delay(2);
                }


                for (int c = 0; c < matrix.ColumnsPerFrame; c = c + 2) {
                    matrix.ColumnDrawLine(c);
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }


                for (int r = 0; r < matrix.RowsPerPanel; r = r + 2) {
                    matrix.RowDrawLine(r);
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                await Task.Delay(1000);

                for (ushort i = 0; i < matrix.PanelsPerFrame; i++) {
                    matrix.DrawLetter(i.ToString()[0], Led.On, i);
                }

                matrix.FrameDraw();
                await Task.Delay(1000);

                for (int r = 0; r < matrix.RowsPerPanel * 2; r++) {
                    matrix.FrameRollDown();
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                for (int r = 0; r < matrix.RowsPerPanel * 2; r++) {
                    matrix.FrameRollUp();
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                for (int c = 0; c < matrix.ColumnsPerFrame * 1; c++) {
                    matrix.FrameRollRight();
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                for (int c = 0; c < matrix.ColumnsPerFrame * 1; c++) {
                    matrix.FrameRollLeft();
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }


                //await Task.Delay(1000);
                //continue;

                matrix.DrawString("ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890", 100, 0);
                matrix.FrameClear();

                for (int i = 0; i < matrix.RowsPerPanel; i++) {
                    matrix.DrawBox(i, i, matrix.ColumnsPerFrame - (i * 2), matrix.RowsPerPanel - (i * 2), Led.On);
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                for (byte l = 0; l < 2; l++) {
                    matrix.SetFrameState(LedDriver.Display.Off);
                    await Task.Delay(250);
                    matrix.SetFrameState(LedDriver.Display.On);
                    await Task.Delay(250);
                }



                matrix.FrameClear();

                for (int r = 0; r < 2; r++) {
                    for (int i = 0; i < matrix.RowsPerPanel; i++) {
                        matrix.RowDrawLine(i, i - 0, matrix.ColumnsPerFrame - i - 1);
                        matrix.FrameDraw();
                        await Task.Delay(50);
                    }

                    //for (byte l = 0; l < 6; l++) {
                    //    matrix.SetBrightness(l);
                    //    await Task.Delay(250);
                    //}

                    //matrix.SetBrightness(1);

                    for (int i = 0; i < matrix.RowsPerPanel; i++) {
                        matrix.RowDrawLine(i, i - 0, matrix.ColumnsPerFrame - i - 1, Led.Off);
                        matrix.FrameDraw();
                        await Task.Delay(50);
                    }
                }

                await Task.Delay(500);
                matrix.FrameClear();

                matrix.ScrollStringInFromRight("Hello World ", 100);
                matrix.FrameClear();
                matrix.ScrollStringInFromLeft("Happy Birthday ", 100);


                for (int i = 0; i < matrix.fontSimple.Length; i = i + matrix.PanelsPerFrame) {
                    for (int p = 0; p < matrix.PanelsPerFrame; p++) {
                        if (p + i >= matrix.fontSimple.Length) { break; }
                        matrix.DrawBitmap(matrix.fontSimple[p + i], Led.On, (p + i) % matrix.PanelsPerFrame);
                    }
                    matrix.FrameDraw();
                    await Task.Delay(100 * matrix.PanelsPerFrame);
                }

                foreach (Grid8x8.Symbols sym in Enum.GetValues(typeof(Grid8x8.Symbols))) {
                    for (int p = 0; p < matrix.PanelsPerFrame; p++) {
                        matrix.DrawSymbol(sym, Led.On, p);
                    }
                    matrix.FrameDraw();
                    await Task.Delay(100 * matrix.PanelsPerFrame);
                }
            }
        }
        public async void Start() {

            Pixel[] palette = new Pixel[] { BiColour.Red, BiColour.Green, BiColour.Yellow };


            var driver = new Ht16K33BiColor(new byte[] { 0x71 }, Ht16K33BiColor.Rotate.D90);
            LED8x8Matrix matrix = new LED8x8Matrix(driver);

            matrix.SetBrightness(6);

            while (true) {

                matrix.FrameClear();
                matrix.ScrollStringInFromRight("Hello World 2015", 60, new Pixel[] { BiColour.Red, BiColour.Green, BiColour.Yellow });

                matrix.FrameClear();
                matrix.ScrollStringInFromLeft("Hello World 2015", 100, palette);

                //continue;

                for (ushort p = 0; p < matrix.PanelsPerFrame; p++) {
                    matrix.DrawSymbol(Grid8x8.Symbols.Block, BiColour.Red, p);
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }


                for (int p = 0; p < matrix.Length; p++) {
                    matrix.FrameSet(BiColour.Green, p);
                    matrix.FrameSet(BiColour.Green, matrix.Length - 1 - p);

                    matrix.FrameDraw();
                    await Task.Delay(2);

                    matrix.FrameSet(Led.Off, p);
                    matrix.FrameSet(Led.Off, matrix.Length - 1 - p);

                    matrix.FrameDraw();
                    await Task.Delay(2);
                }


                for (int c = 0; c < matrix.ColumnsPerFrame; c = c + 2) {
                    matrix.ColumnDrawLine(c);
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }


                for (int r = 0; r < matrix.RowsPerPanel; r = r + 2) {
                    matrix.RowDrawLine(r, BiColour.Yellow);
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                await Task.Delay(1000);

                for (ushort i = 0; i < matrix.PanelsPerFrame; i++) {
                    matrix.DrawLetter(i.ToString()[0], BiColour.Green, i);
                }

                matrix.FrameDraw();
                await Task.Delay(1000);

                for (int r = 0; r < matrix.RowsPerPanel * 2; r++) {
                    matrix.FrameRollDown();
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                for (int r = 0; r < matrix.RowsPerPanel * 2; r++) {
                    matrix.FrameRollUp();
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                for (int c = 0; c < matrix.ColumnsPerFrame * 1; c++) {
                    matrix.FrameRollRight();
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                for (int c = 0; c < matrix.ColumnsPerFrame * 1; c++) {
                    matrix.FrameRollLeft();
                    matrix.FrameDraw();
                    await Task.Delay(50);
                }

                matrix.DrawString("Wow, such colour :)", palette, 200, 0);
                matrix.FrameClear();

                for (int i = 0; i < matrix.RowsPerPanel; i++) {
                    matrix.DrawBox(i, i, matrix.ColumnsPerFrame - (i * 2), matrix.RowsPerPanel - (i * 2), Led.On);
                    matrix.FrameDraw();
                    await Task.Delay(100);
                }

                for (byte l = 0; l < 2; l++) {
                    matrix.SetFrameState(LedDriver.Display.Off);
                    await Task.Delay(250);
                    matrix.SetFrameState(LedDriver.Display.On);
                    await Task.Delay(250);
                }



                matrix.FrameClear();

                for (int r = 0; r < 4; r++) {
                    for (int i = 0; i < matrix.RowsPerPanel; i++) {
                        matrix.RowDrawLine(i, i - 0, matrix.ColumnsPerFrame - i - 1, palette[i % palette.Length]);
                        matrix.FrameDraw();
                        await Task.Delay(50);
                    }


                    for (int i = 0; i < matrix.RowsPerPanel; i++) {
                        matrix.RowDrawLine(i, i - 0, matrix.ColumnsPerFrame - i - 1, Led.Off);
                        matrix.FrameDraw();
                        await Task.Delay(50);
                    }
                }

                await Task.Delay(250);
                matrix.FrameClear();


                for (int i = 0; i < matrix.fontSimple.Length; i = i + matrix.PanelsPerFrame) {
                    for (int p = 0; p < matrix.PanelsPerFrame; p++) {
                        if (p + i >= matrix.fontSimple.Length) { break; }
                        matrix.DrawBitmap(matrix.fontSimple[p + i], palette[i % palette.Length], (p + i) % matrix.PanelsPerFrame);
                    }
                    matrix.FrameDraw();
                    await Task.Delay(150 * matrix.PanelsPerFrame);
                }

                foreach (Grid8x8.Symbols sym in Enum.GetValues(typeof(Grid8x8.Symbols))) {
                    for (int p = 0; p < matrix.PanelsPerFrame; p++) {
                        matrix.DrawSymbol(sym, palette[p % palette.Length], p);
                    }
                    matrix.FrameDraw();
                    await Task.Delay(150 * matrix.PanelsPerFrame);
                }
            }
        }
        public MainPage()
        {
            this.InitializeComponent();



            var driver = new Ht16K33(new byte[] { 0x70 }, Ht16K33.Rotate.None, LedDriver.Display.On, 2, LedDriver.BlinkRate.Off, "I2C1");

            LED8x8Matrix matrix = new LED8x8Matrix(driver);

            matrix.SetBrightness(1);

            while (true)
            {

                matrix.FrameClear();

                for (int i = 0; i < matrix.fontSimple.Length; i = i + matrix.PanelsPerFrame)
                {
                    for (int p = 0; p < matrix.PanelsPerFrame; p++)
                    {
                        if (p + i >= matrix.fontSimple.Length) { break; }
                        matrix.DrawBitmap(matrix.fontSimple[p + i], Led.On, (p + i) % matrix.PanelsPerFrame);
                    }
                    matrix.FrameDraw();
                    Task.Delay(100 * matrix.PanelsPerFrame);
                }

                foreach (Grid8x8.Symbols sym in Enum.GetValues(typeof(Grid8x8.Symbols)))
                {
                    for (int p = 0; p < matrix.PanelsPerFrame; p++)
                    {
                        matrix.DrawSymbol(sym, p);
                    }
                    matrix.FrameDraw();
                    Task.Delay(100 * matrix.PanelsPerFrame);
                }

                matrix.FrameClear();
                matrix.ScrollStringInFromRight("Hello World 2016", 100);

                matrix.FrameClear();
                matrix.ScrollStringInFromLeft("Hello World 2016", 100);

                //continue;

                for (ushort p = 0; p < matrix.PanelsPerFrame; p++)
                {
                    matrix.DrawSymbol(Grid8x8.Symbols.Block, p);
                    matrix.FrameDraw();
                    Task.Delay(100);
                }


                for (int p = 0; p < matrix.Length; p++)
                {
                    matrix.FrameSet(Led.On, p);
                    matrix.FrameSet(Led.On, matrix.Length - 1 - p);

                    matrix.FrameDraw();
                    Task.Delay(2);

                    matrix.FrameSet(Led.Off, p);
                    matrix.FrameSet(Led.Off, matrix.Length - 1 - p);

                    matrix.FrameDraw();
                    Task.Delay(2);
                }


                for (int c = 0; c < matrix.ColumnsPerFrame; c = c + 2)
                {
                    matrix.ColumnDrawLine(c);
                    matrix.FrameDraw();
                    Task.Delay(100);
                }


                for (int r = 0; r < matrix.RowsPerPanel; r = r + 2)
                {
                    matrix.RowDrawLine(r);
                    matrix.FrameDraw();
                    Task.Delay(100);
                }

                Task.Delay(1000);

                for (ushort i = 0; i < matrix.PanelsPerFrame; i++)
                {
                    matrix.DrawLetter(i.ToString()[0], i);
                }

                matrix.FrameDraw();
                Task.Delay(1000);

                for (int r = 0; r < matrix.RowsPerPanel * 2; r++)
                {
                    matrix.FrameRollDown();
                    matrix.FrameDraw();
                    Task.Delay(100);
                }

                for (int r = 0; r < matrix.RowsPerPanel * 2; r++)
                {
                    matrix.FrameRollUp();
                    matrix.FrameDraw();
                    Task.Delay(100);
                }

                for (int c = 0; c < matrix.ColumnsPerFrame * 1; c++)
                {
                    matrix.FrameRollRight();
                    matrix.FrameDraw();
                    Task.Delay(100);
                }

                for (int c = 0; c < matrix.ColumnsPerFrame * 1; c++)
                {
                    matrix.FrameRollLeft();
                    matrix.FrameDraw();
                    Task.Delay(100);
                }


                //await Task.Delay(1000);
                //continue;

                matrix.DrawString("ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890", 100, 0);
                matrix.FrameClear();

                for (int i = 0; i < matrix.RowsPerPanel; i++)
                {
                    matrix.DrawBox(i, i, matrix.ColumnsPerFrame - (i * 2), matrix.RowsPerPanel - (i * 2));
                    matrix.FrameDraw();
                    Task.Delay(100);
                }

                for (byte l = 0; l < 2; l++)
                {
                    matrix.SetFrameState(LedDriver.Display.Off);
                    Task.Delay(250);
                    matrix.SetFrameState(LedDriver.Display.On);
                    Task.Delay(250);
                }



                matrix.FrameClear();

                for (int r = 0; r < 2; r++)
                {
                    for (int i = 0; i < matrix.RowsPerPanel; i++)
                    {
                        matrix.RowDrawLine(i, i - 0, matrix.ColumnsPerFrame - i - 1);
                        matrix.FrameDraw();
                        Task.Delay(50);
                    }

                    //for (byte l = 0; l < 6; l++) {
                    //    matrix.SetBrightness(l);
                    //    await Task.Delay(250);
                    //}

                    //matrix.SetBrightness(1);

                    for (int i = 0; i < matrix.RowsPerPanel; i++)
                    {
                        matrix.RowDrawLine(i, i - 0, matrix.ColumnsPerFrame - i - 1, Led.Off);
                        matrix.FrameDraw();
                        Task.Delay(50);
                    }
                }

                Task.Delay(500);
                matrix.FrameClear();

                matrix.ScrollStringInFromRight("Hello World ", 100);
                matrix.FrameClear();
                matrix.ScrollStringInFromLeft("Happy Birthday ", 100);


                }
            }