May 27, 2011

Getting sharper while falling in love with .Net Micro Framework

After many years of programming in AVR-GCC I decided to make the leap to .Net Micro Framework.

Why?

  1. I develop in C# everyday, I’m really really good at it.
  2. I’m tired of digging through data sheets setting registers!
  3. I want to use GOOD tools like Visual Studio!
  4. One word… BREAKPOINTS.image
  5. I’m tired of penny pinching RAM. Getting fast write time to FAT is really difficult on Arduino and kills half your RAM at least.
  6. Events rock.

Now, if someone wants something in C on AVR I am more than happy to oblige. I am developing a few things using LUFA as of late, but I’m going to start concentrating on learning .Net Microframework.

Projects I’m considering

  1. Porting all RockSat-X code to C#.
  2. ADB / ADK bridge for .Net Micro Framework.
  3. .Net MicroFramework Oscilloscope

Things I ‘m hoping to see fixed in future .Net MF.

  1. .Net support for analog ports. Everyone has their own implementation making writing emulators for analog REALLY HARD.
  2. Compiled mode and Interpreted mode. The JIT compilation engine is amazing. Understandably it wont fit on MF but why can’t my comp compile and spit down pure machine code rather than interpreting line by line.

A final note. I really think these could be a good option for education. Many schools teach programming and teach C#

May 18, 2011

Matlab,C# and DynamicObject a perfect marriage!

I am using an old version of matlab (7.5). I have been porting some code to C# but I wanted to make sure my plots were correct. After writing _m.Execute about 10 times I decided to write a wrapper for plot. I quickly started needing many commands and thought, there has to be an easier way. What if we can write code like this in C#?

static void Main(string[] args)
{
dynamic m = new Matlab();
m.cd(Environment.CurrentDirectory);
m.close();
m.clear();
m.figure(1);
m.plot(new double[] { 1, 2, 3 }, new double[] { 3, 1, 3 });
m.title("plot title");
var z=m.zeros(2,100);
var o = m.ones(2, 100);

}

Well in fact you can if we do some dancing! First, get familiar with the wonderful class that Microsoft built called DynamicObject. This class allows us to dynamically dispatch calls anywhere we want, in our case, MATLAB. We get the call to TryInvokeMember. From there we use the binder to tell us what we are doing and then we execute it. There is still along way for this project to go, but for now, I’ve got C# friendly code calling matlab!


using System.Text;
usingSystem.Dynamic;

namespaceMatlabExtensions
{
    public classMatlab : DynamicObject
  
{
        MLApp.MLApp _a;
        publicMatlab()
        {
            _a = newMLApp.MLApp();
        }

        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out objectresult)
        {
            StringBuilder command = newStringBuilder();
            command.Append(binder.Name);
            command.Append("(");
            int idx = 0;
            foreach (var arg inargs)
            {
                stringvar = string.Format("arg{0}", idx++);
                _a.PutWorkspaceData(var, "base", arg);
                command.Append(var);
                command.Append(",");
            }

            stringexec = command.ToString();
            exec = exec.TrimEnd(',') + ")";
            _a.Execute(exec);
            try
          
{
                result = _a.GetVariable("ans", "base");
            }
            catch{ result = null; }
           
            return true;
        }
    }
}

A small note, you will need to reference the matlab com library.


image

Followers