Pages: [1]   Go Down
Author Topic: Timing library  (Read 1458 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am going to program a robot, so I need to do many things that include lots of delays at once. That's why I made a library that lets you use non-blocking delays that call a function on timeout. It's not as good as threading, but it's very useful in combination with finite state machine classes.

I'm a C++ beginner, but I'm very good at python. I first tried to make the event array change size, but it was hopeless(and would have used up too much cpu.). I'm still trying to figure out how to set the size of the array in the constructor. All improvements are welcome.

Heres the code: (put it in libraries - Timing)
Code:
/*
  Timing.cpp - Non-blocking delay library.
  Created by Joonatan Saarhelo, February 27, 2011.

  Example:

#include <Timing.h>
Timer events;

boolean last = 0;

void setup()
{
  pinMode(13, OUTPUT);
  events.add(1000,set);
}

void loop()
{
  events.update();
}

void set()
{
  last = last == false;
  digitalWrite(13, last);
  events.add(1000,set);
}
*/

#include "WProgram.h"
#include "Timing.h"
#include "functionpointer.h"

Timer::Timer()
{
  length = 0;
  lastcheck = millis();
}

void Timer::add(int in, fpointer func)
{
  events[length] = Event();
  events[length].function = func;
  events[length].timeout = millis()+in;
  length++;
}

void Timer::update()
{
  long time = millis();
  boolean overflow = time < lastcheck;
  for(int se=0; se < length; se++)
  {
    if(events[se].timeout < time or overflow and events[se].timeout > lastcheck)
    {
      events[se].function();
      // siirrä taaksepäin
      for(int i=se+1; i < length; i++) {events[i-1] = events[i];}
      length -= 1;
    }
  }
}
Code:
/*
  Timing.h - Non-blocking delay library.
  Created by Joonatan Saarhelo, February 27, 2011.
 
  Example:

#include <Timing.h>
Timer events;

boolean last = 0;

void setup()
{
  pinMode(13, OUTPUT);
  events.add(1000,set);
}

void loop()
{
  events.update();
}

void set()
{
  last = last == false;
  digitalWrite(13, last);
  events.add(1000,set);
}
*/

#ifndef AHolemassa
#define AHolemassa

#include "WProgram.h"
#include "functionpointer.h"

class Timer
{
struct Event
{
fpointer function;
long timeout;
};
Event events[10];
  int length;
  long lastcheck;
  public:
    Timer();
    //Timer(int maxevents);
    void add(int in, fpointer func);
    void update();
};

#endif
Last and least: functionpointer.h
Code:
typedef void (*fpointer)();

If my english is strange it's because I'm from Finland.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Looks useful, you may write a playground article about it how it works and how it can be used.
Rob
PS, your English is far better than my Finnish smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: