chipKIT® Development Platform

Inspired by Arduino™

Protothreads on MAX32

Created Sat, 24 Dec 2011 23:19:30 +0000 by fiddler


fiddler

Sat, 24 Dec 2011 23:19:30 +0000

I'm trying to get the Protothread example up and running on the Max32. It compiles and works fine on a Duemilanove board using the same enviroment (mpide).

When I set the mpide board selection to Max32, I get a compiler error saying fatal error: pt.h: No such file.

However I have put the pt directory (including the pt.h file) in the mpide directory

Is there another place I need to put the header filer ?

I'm running Linux Ubuntu 11

Cheers K


fiddler

Sun, 25 Dec 2011 02:42:51 +0000

The code sample is here:

/* Copyright (c) 2011, Jan Clement
 * licenced under the GPL
 *
 * Author: Jan Clement <jan.clement@audiokits.de>
 * 
 * Example code to demonstrate the use of protothreads 
 * in an arduino sketch. It toggles an LED  using two 
 * independent protothreads. One pt toggles every 
 * 1000ms, the other one every 900ms. The result is an 
 * erratic blinking pattern.
 */

#include <pt.h>   // include protothread library

#define LEDPIN 13  // LEDPIN is a constant 

static struct pt pt1, pt2; // each protothread needs one of these

void setup() {
  pinMode(LEDPIN, OUTPUT); // LED init
  PT_INIT(&pt1);  // initialise the two
  PT_INIT(&pt2);  // protothread variables
}

void toggleLED() {
  boolean ledstate = digitalRead(LEDPIN); // get LED state
  ledstate ^= 1;   // toggle LED state using xor
  digitalWrite(LEDPIN, ledstate); // write inversed state back
}

/* This function toggles the LED after 'interval' ms passed */
static int protothread1(struct pt *pt, int interval) {
  static unsigned long timestamp = 0;
  PT_BEGIN(pt);
  while(1) { // never stop 
    /* each time the function is called the second boolean
    *  argument "millis() - timestamp > interval" is re-evaluated
    *  and if false the function exits after that. */
    PT_WAIT_UNTIL(pt, millis() - timestamp > interval );
    timestamp = millis(); // take a new timestamp
    toggleLED();
  }
  PT_END(pt);
}
/* exactly the same as the protothread1 function */
static int protothread2(struct pt *pt, int interval) {
  static unsigned long timestamp = 0;
  PT_BEGIN(pt);
  while(1) {
    PT_WAIT_UNTIL(pt, millis() - timestamp > interval );
    timestamp = millis();
    toggleLED();
  }
  PT_END(pt);
}

void loop() {
  protothread1(&pt1, 900); // schedule the two protothreads
  protothread2(&pt2, 1000); // by calling them infinitely
}

fiddler

Sun, 25 Dec 2011 08:15:02 +0000

Dooh.

All I needed to do was to import the library.

Nawell

:-) K