天天看点

让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输出显示。