天天看點

讓TinyGUI庫支援模拟器

Microsoft .Net Micro Framework 官方UI庫為WPF,針對320*240的LCD而言,至少額外需要150K以上RAM才能基本運作,是以推出了輕量級的圖形庫TinyGUI。

WPF支援标準BMP,JPG,GIF圖檔顯示,從使用角度來看非常友善,但是由于嵌入式LCD大都為16bit顯示(RGB565格式),無論是BMP還是JPG和GIF都需要進行顔色轉換,此外後者還需要進行格式轉換處理。以上操作,不僅導緻運作速度慢,還需要一定的記憶體進行圖形緩存。

第一個版本的庫,不支援模拟器顯示,這樣使用者在測試新應用時,必須借助實際的開發闆才能看到實際的運作效果,這大大影響了使用者的體驗,是以重新設計了代碼,讓模拟器也能支援TinyGUI庫的運作。

public static void Main()

{

    uint[] colors = new uint[]{Color.Black, Color.Red,Color.Green, Color.Orange,Color.Yellow, Color.Brown,Color.Purple, Color.Gray,

                            Color.DarkGray, Color.LightGray,Color.Blue, Color.Magenta,Color.Cyan, Color.White,Color.LightGreen};

    Graphics.Clear(Color.Blue);

    int x, y, width, height, c;

    long index = 0;

    HardwareProvider hwp = new HardwareProvider();

    int lcd_width,lcd_height,lcd_bitsPerPixel,lcd_orientationDeg;

    hwp.GetLCDMetrics(out lcd_width, out lcd_height, out lcd_bitsPerPixel, out lcd_orientationDeg);           

    int Graphics_Width = lcd_width - 1;

    int Graphics_Height = lcd_height - 1;

    Random rnd = new Random();

    while (true)

    {

        x = rnd.Next(Graphics_Width);

        width = rnd.Next(Graphics_Width - x);

        y = rnd.Next(Graphics_Height);

        height = rnd.Next(Graphics_Height - y);

        c = rnd.Next(colors.Length - 1);

        switch (index % 3)

        {

            case 0:

                if (rnd.Next(10) > 5)

                    Graphics.DrawRectangle(x, y, width, height, colors[c]);

                else

                    Graphics.FillRectangle(x, y, width, height, colors[c]);

                break;

            case 1:

                    Graphics.DrawEllipse(x, y, width, height, colors[c]);

                    Graphics.FillEllipse(x, y, width, height, colors[c]);

            case 2:

                Graphics.DrawLine(x, y, rnd.Next(Graphics_Width), rnd.Next(Graphics_Height), colors[c]);

        }

        Graphics.FillRectangle(0, Graphics_Height-19, Graphics_Width, 19, Color.White);

        Graphics.DrawString(2, Graphics_Height-17, (index++).ToString(), Color.Blue);

        Thread.Sleep(50);

    }

}

  在模拟器中運作的效果圖如下(其實在官方模拟器中也可以運作)。

讓TinyGUI庫支援模拟器

System.TinyGUI庫的下載下傳位址如下(包含文檔和示例代碼):

聲明:void Clear(uint color)

參數:color– 清除後的背景色 (24bit RGB)

傳回:無

說明: 以用指定顔色清除LCD顯示。

聲明:void SetPixel(int x,int y,uint color)

參數:x,y – 螢幕坐标

      color– 顔色

說明: 畫點。

聲明:uint GetPixel (int x,int y)

傳回:指定坐标的顔色

說明:傳回指定坐标的顔色,有些硬體不支援該函數。

聲明:void DrawLine(int x1, int y1, int x2, int y2, uint color)

參數:x1,y1,x2,y2 – 螢幕坐标

color– 顔色

說明:畫線。

聲明:void DrawRectangle(int x, int y, int width, int height, uint color)

參數:x,y – 螢幕左上角坐标

      width,height – 寬,高

說明:畫空心矩形。

聲明:void DrawEllipse(int x, int y, int width,int height, uint color)

說明:畫空心橢圓。

聲明:void DrawImage(int x, int y, byte[] bytData)

      bytData - TinyBMP格式的圖像資料

說明:位圖繪制(模拟器暫不支援)。

聲明:void DrawImageEx (int x, int y, byte[] bytData,uint MaskColor)

MaskColor– 屏蔽色

聲明:void DrawString (int x, int y,string s, uint color)

      s – 字元串

color– 字型顔色

說明:繪制字型(暫時僅支援符号和西文字元)

聲明:void FillRectangle (int x, int y, int width, int height, uint color)

color– 填充色

說明:畫填充矩形。

聲明:void FillEllipse (int x, int y, int width, int height, uint color)

說明:畫填充橢圓。

聲明:void Print(string str)

參數:str – LCD顯示的字元串

說明:LCD資訊輸出(底層LCD_Printf函數的封裝)。

聲明:void SuspendLayout ()

參數:無

說明:挂起LCD的輸出顯示。

說明:恢複挂起的LCD輸出顯示。