How do I write applications that run with TinyOS?

Matlab is inherently a single-threaded interpreter and was designed for applications with a single point of execution.  When working with TinyOS and sensor networks, however, it is more natural to use an event-driven programming model.  Usually, we have two types of events that drive our application: clock events and/or message received events.

Designing event-driven applications in Matlab are actually quite simple if you use the following guidelines:

defineMyAppEnvironment script

Use this file to setup your matlab path and global variables for this app.  It needs to be called only once per matlab session.

Init, Reinit, Start, Stop and EventHandler functions

It is easiest to use the following 5 types of functions when writing event-driven applications

  1. initializeMyApp.m -- to initialize your variables
  2. reinitializeMyApp.m  -- to set clear data but keep all other state
  3. startMyApp.m -- to setup all the event handlers (which will be active immediately)
  4. stopMyApp.m -- to stop all event handlers
  5. XXEventHandler.m -- to handle either message events or clock events.

Global variables vs Java objects.

While this is bad programming style, Matlab functions are unfortunately pass-by-value, which creates some serious problems for event-driven applications and Matlab objects.  If/when you encounter these problems, you have two options:  store your data in a Java object, which is passed by reference within matlab or store your data in a matlab global variable which can be imported into any function that also declares it global.

What if I want to write a clock driven application that receives packets?

As an excercise, write a MessegeListener class that buffers messages.  Then, use the clock event handler in your application to read the buffers.