Mobile Software Engineering

2009-02-23 by tamberg

Running Processing Code on the .NET Micro Framework

While most of our current projects focus on .NET we also love Arduino - an electronics prototyping platform based on open-source hardware, an easy-to-learn programming language called Processing and a simple software development environment. Evolved from ideas explored in the Aesthetics and Computation Group at the MIT Media Lab, Processing defines a procedural subset of Java together with a lean but effective high-level library. Primarily designed to simplify visual computing on desktop machines, Processing and its philosophy could be successfully ported to the embedded world.

Sites like Instructables, MAKEzine, Tinker.it! and Liquidware show the enormous momentum of the rapidly growing Arduino community. The technical reason that Processing is quite easy to support by hardware vendors like Arduino is the fact that it can be translated to C almost one-to-one. So, after thinking about this whole story, Marc came up today with an elegant way to run Processing on even more devices, namely all those supporting the .NET Micro Framework. As a proof of concept, here is a (partial) port of the Processing library and the original Hello World in Processing - or is it C#?

// Processing.cs

using System.Threading;
using Microsoft.SPOT.Hardware;

partial class Processing {
  const bool HIGH = true, LOW = false;
  const int OUTPUT = 0;

  object[] pins = new object[128];

  void delay (int ms) {
    Thread.Sleep(ms);
  }

  void digitalWrite (int pin, bool value) {
    (pins[pin] as OutputPort).Write(value);
  }

  void pinMode (int pin, int mode) {
    pins[pin] = new OutputPort((Cpu.Pin) pin, false);
  }

  static void Main () {
    Processing p = new Processing();
    p.setup();
    while (true) {
      p.loop();
    }
  }
}
// Program.cs

partial class Processing {
  int ledPin = 13;

  void setup () {
    pinMode(ledPin, OUTPUT);
  }

  void loop () {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
  }
}