【玩转.Net MF – 06】为Cortex-M3打造轻量级TinyGUI(上)

简介: Microsoft .Net Micro Framework 官方UI库为WPF,针对320*240的LCD而言,至少额外需要150K以上RAM才能基本运行。

Microsoft .Net Micro Framework 官方UI库为WPF,针对320*240的LCD而言,至少额外需要150K以上RAM才能基本运行。而市面上常见Cortex-M3开发板的RAM大多为128K,少数开发板即使具备512k的RAM,运行官方自带的示例SimpleWPFApplication,也会出现内存溢出问题。此外由于Cortex-M3内核CPU主频大都在72M左右,官方图形库运行速度较慢。

一、参数指标

名称

代码大小

内存需求

运行性能

WPF

120k(含Microsoft.SPOT.TinyCore.pe)

150k

运行基准测试程序,TinyGUI运行速度大概是WPF的3~5倍

TinyGUI

<2k

无需求

注:TinyGUI采用类似DirectDraw技术,直接操作显存,所以无内存需求,且运行速度快
image.png

二、位图显示技术比较

WPF支持标准BMP,JPG,GIF图片显示,从使用角度来看非常方便,但是由于嵌入式LCD大都为16bit显示(RGB565格式),无论是BMP还是JPG和GIF都需要进行颜色转换,此外后者还需要进行格式转换处理。以上操作,不仅导致运行速度慢,还需要一定的内存进行图形缓存。

TinyGUI的位图显示采用转换后的tinyBMP位图格式,其格式和LCD显存格式保持一致,由于图形转换工作通过程序(如下)预先完成,所以在嵌入式系统上直接向显存拷贝即可完成位图显示,所以运行速度极快。

image.png

(注:其实Net Micro Framework的字体就是采用类似技术,官方提供转换程序和tinyFont字体库)

核心代码其实很简单,就是把32位位图转换为指定RGB(或BGR)格式的16位位图。

byte[] bytBuff = new byte[picBar.Height * picBar.Width * 2 + 12];

    BinaryWriter bw = new BinaryWriter(new MemoryStream(bytBuff));

    bw.Write(new byte[] { 84, 105, 110, 121, 66, 77, 80, 0 }); //TinyBMP/0;

    bw.Write((UInt16)picBar.Width);

    bw.Write((UInt16)picBar.Height);

    Bitmap bmp = new Bitmap(picBar.Image, picBar.Width, picBar.Height);

    for (int y = 0; y < bmp.Height; y++)

    {

        tspBar.Value = y;

        for (int x = 0; x < bmp.Width; x++)

        {

            bw.Write(Color_32_16(bmp.GetPixel(x, y)));

        }

    }

三、TinyGUI图库接口

namespace System.TinyGUI

{

    public sealed class Graphics

    {

        public Graphics();

 

        public static void Clear(uint color);

        public static void DrawEllipse(int x, int y, int width, int height, uint color);

        public static void DrawImage(int x, int y, byte[] bytData);

        public static void DrawImageEx(int x, int y, byte[] bytData, uint MaskColor);

        public static void DrawLine(int x1, int y1, int x2, int y2, uint color);

        public static void DrawRectangle(int x, int y, int width, int height, uint color);

        public static void DrawString(int x, int y, string s, uint color);

        public static void FillEllipse(int x, int y, int width, int height, uint color);

        public static void FillRectangle(int x, int y, int width, int height, uint color);

        public static uint GetPixel(int x, int y);

        public static void Print(string str);

        public static void SetPixel(int x, int y, uint color);

    }

}

  四、TinyGUI测试程序

运行效果图如下:

 image.png
image.png

   

 

部分测试代码如下:

    static void DrawGraphics()

    {

        x = rnd.Next(239);

        width = rnd.Next(240 - x);

        y = rnd.Next(319);

        height = rnd.Next(320 - 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:

                if (rnd.Next(10) > 5)

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

                else

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

                break;

            case 2:

                Graphics.DrawLine(x, y, rnd.Next(239), rnd.Next(319), colors[c]);

                break;

        }

 

        Graphics.FillRectangle(0, 300, 240, 20, Color.White);

#if    STM3210E_EVAL

        Graphics.DrawString(2, 303, "Key - Back", Color.Black);

#else

        Graphics.DrawString(2, 303, "Select - Back", Color.Black);

#endif

    }

 

    static void DrawPicture()

    {

        if (++picIndex > 12) picIndex = 0;

        AccessFlash.Read((uint)(0x002A0000 + picIndex * 0xEA6C), 0xEA6C, picData);

        if(StateIndex!= SystemState.Main)  Graphics.DrawImage(20, 70, picData);

}

其中图片从Flash中进行读取,图片的下载方法可以参考我以前的博客文章《Flash远程读写》,为了在C#代码中读取Flash上的内容,我重新封装了一个AccessFlash类,可以直接读写Flash任意区域的数据,这部分内容我在后续文章中再进行介绍。

这篇文章仅仅介绍了TinyGUI应用层面的内容,下篇文章《为Cortex-M3打造轻量级TinyGUI(下)》将介绍TinyGUI是如何开发的,敬请关注。

相关文章
|
Windows
.Net Micro Framework研究—模拟器直接运行MF程序
既然VS2005能启动运行,我想我们也可以,唯一不同的是模拟器加载的参数不同而已
718 0
.Net Micro Framework研究—用MF编写俄罗斯方块
现在对MF充满了激情,所以从零做起(没有参考任何现成代码,只不过还是依照我早期的思路编写而成),花费我大半天的时间才编写完毕(在编写过程中,还发现MF对二维数组支持不大好,我后面会提到)
388 0
.Net Micro Framework研究—让MF支持鼠标
MF的标准模块仅支持按键,并不支持鼠标功能。但是对一些常见应用来说,如果没有鼠标(或触摸屏)用起来就太不习惯了
482 0
|
机器人
.Net Micro Framework研究—用MF控制机器人
机器人研究一直是我很早以前的梦想,没有想到在深入研究.Net Micro Framework同时能和机器人搭上了联系。
552 0
.Net Micro Framework研究—让MF支持英文输入法
目前字符串不仅无法转换为数字,并且没有字符插入功能,所以这个函数也必须自己来实现,此外输入焦点光标也需要自己绘制,好了,先不说难处了,先看看最终成果。
699 0
.Net Micro Framework研究—MF驱动继电器
年前张欣有一个比较好的想法,想用Digi的MF板驱动一个小型继电器,这样就可以用MF直接控制家中的小功率220V的电器了
583 0
|
安全
.Net MF V4.0开源前的代码整理
已经有好长一段时间没有更新博客了,一是去美国总部和台湾出差用了不少时间,二是做.Net MF代码整理又花了近一个月的时间。不过令人欣慰的是,目前.Net MF V4.0的相关代码整理已经告一段落,就等着下一步的开源了
657 0
|
Linux C++ C#
【.Net Micro Framework PortingKit – 15】移植总结兼MF未来发展
我坚信.Net Micro Framework在未来嵌入式发展中,一定会占有一席之地(更何况现在已经开源了,并且采用更为彻底的Apache 2.0 license)。希望更多的人参与其中,把.Net Micro Framework移植到更多嵌入式平台上去,使它成为真正名副其实的“框架”。
907 0
|
内存技术
【玩转.Net MF – 01】Flash远程读写
目前在PC远程访问设备Flash,也就是部署TinyCLR和下载应用程序
552 0
|
内存技术
【玩转.Net MF – 02】让PC成为MF的鼠标键盘
通过扩展我以前为.Net MF开发的WinForm库(参见我以前的文章《开源System.Windows.Forms库,让.Net Micro Framework界面开发和上位机一样简单》),增加一个输入代理层,就可以实现虚拟鼠标和键盘输入。
603 0
下一篇
无影云桌面