Go Down

Topic: VariableTimedAction - new scheduling library (Read 248 times) previous topic - next topic

pizzapie

Jun 29, 2018, 03:40 am Last Edit: Jul 03, 2018, 02:23 am by pizzapie
I've added a new library to the LibraryManager that, in technical terms, is an interface that allows you to define a run method, similar to Java's Runnable interface, which will be executed at the specified interval.

In simpler terms, this library allows you to "multi-thread" or have multiple separately executing timed actions.

Here is the documentation.

How to Use

First and foremost, your loop method must call VariableTimedAction::updateActions constantly. For more timing-intensive applications it is recommended that the call to updateActions is the only thing in the loop method. It will look like this:

Code: [Select]
void loop() {
  //this static method should really be the only thing called in your loop method to make sure events are updated on time
  VariableTimedAction::updateActions();
}


Next, you can create a timer class like this:

Code: [Select]
class Counter : public VariableTimedAction {
private:
  int count = 0;

  //this method will be called at your specified interval
  unsigned long run() {
    //increase the timer
    count++;

    //return code of 0 indicates no change to the interval; if the interval must be changed, then return the new interval
    return 0;
  }

public:
  int getCount() {
    return count;
  }
};


To start this counter you can do this:

Code: [Select]
Counter secondCounter;

void setup() {
  //call the run method every 1000ms (or 1 sec)
  secondCounter.start(1000);
}


To pause/unpause, you can use the toggleRunning method. To stop the timer, you can use the stop method. If you use the stop method, you must use the start method to begin the counter again. To find out if the timer is running or it is currently paused, use the isRunning method. You can have as many timers as you like; each of them can be controlled individually.

I've attached the library along with two examples to this post.

Here is the Github page: https://github.com/amirchev/VariableTimedAction

Any feedback is welcome!

pizzapie

Minor update: example sketches placed into own folder so they will be compatible with Arduino IDE.

Robin2

Where is the documentation that explains how to use all the functions in your library?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pizzapie

Where is the documentation that explains how to use all the functions in your library?

...R
I've added some documentation and uploaded the library with documentation here. It will be available in the next release.

Robin2

If people could read the documentation without downloading and installing the library it might increase the number who are interested.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pizzapie

If people could read the documentation without downloading and installing the library it might increase the number who are interested.

...R
Do you think you can provide me with an example? Because I have the readme on the Github page, or are you referring more like a JavaDoc type html file?

Robin2

There is very little information in the ReadMe - but probably quite enough for a ReadMe file.

Have a look at the documentation for the AccelStepper and RF24 libraries. And, IMHO, while it is one of the best examples the AccelStepper documentation is not as clear as it could be for some of the functions.

I am NOT suggesting that your documentation needs to be presented in the same technical manner - it is the content I want to draw your attention to.  For example a PDF file would be fine.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pizzapie

#7
Jul 03, 2018, 02:19 am Last Edit: Jul 03, 2018, 02:20 am by pizzapie
I've just extracted this with Doxygen and uploaded it to GH Pages here. I'll also place it into the first post.

Go Up