Home > Electronics, Links > Cool lightweight statemachine/thread library in pure C

Cool lightweight statemachine/thread library in pure C

June 25th, 2007

While searching for informtion on statemachines to be used in my “Safe 12vdc to 220vac inverter control box” i sumbled upon an extremely lightweight stackless threads library called ProtoThreads. Although I don’t think I can’t use it because of the stack limitation on the PIC12F508/9 or PIC16F84 that i plan to use, it is worth mentioning.

Here is an abstract of descripton found on the webpage:

Protothreads are extremely lightweight stackless threads designed for severely memory constrained systems, such as small embedded systems or wireless sensor network nodes. Protothreads provide linear code execution for event-driven systems implemented in C. Protothreads can be used with or without an underlying operating system to provide blocking event-handlers. Protothreads provide sequential flow of control without complex state machines or full multi-threading.

A simple task is defined like this:

#include "pt.h"

struct pt pt;
struct timer timer;

PT_THREAD(example(struct pt *pt))
{
  PT_BEGIN(pt);
  while(1) {
    if(initiate_io()) {
      timer_start(&timer);
      PT_WAIT_UNTIL(pt, io_completed() || timer_expired(&timer));
      read_data();
    }
  }
  PT_END(pt);
}

Benjamin Sølberg Electronics, Links

  1. No comments yet.
  1. No trackbacks yet.