Simple task manager which uses a Ticker

Committer:
Phlaphead
Date:
Fri Jan 14 20:24:54 2011 +0000
Revision:
2:3cb7f5770feb
Parent:
1:e95b703c6ad7
Added documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Phlaphead 0:e381c3adaa04 1 #ifndef Task_h
Phlaphead 0:e381c3adaa04 2 #define Task_h
Phlaphead 0:e381c3adaa04 3
Phlaphead 0:e381c3adaa04 4 #include "mbed.h"
Phlaphead 0:e381c3adaa04 5
Phlaphead 0:e381c3adaa04 6 #define DEFAULT_INTERVAL 1000 //1ms
Phlaphead 0:e381c3adaa04 7
Phlaphead 2:3cb7f5770feb 8 /**
Phlaphead 2:3cb7f5770feb 9 * Override this class to create a Task that can be managed with the TaskManager.
Phlaphead 2:3cb7f5770feb 10 */
Phlaphead 0:e381c3adaa04 11 class Task
Phlaphead 0:e381c3adaa04 12 {
Phlaphead 0:e381c3adaa04 13
Phlaphead 0:e381c3adaa04 14 public:
Phlaphead 0:e381c3adaa04 15
Phlaphead 2:3cb7f5770feb 16 /**
Phlaphead 2:3cb7f5770feb 17 * Contructor. Sets interval to default of 1ms.
Phlaphead 2:3cb7f5770feb 18 */
Phlaphead 0:e381c3adaa04 19 Task();
Phlaphead 2:3cb7f5770feb 20
Phlaphead 2:3cb7f5770feb 21 /**
Phlaphead 2:3cb7f5770feb 22 * Constructor.
Phlaphead 2:3cb7f5770feb 23 * @param _interval The running interval of the task in us.
Phlaphead 2:3cb7f5770feb 24 */
Phlaphead 0:e381c3adaa04 25 Task(int _interval);
Phlaphead 1:e95b703c6ad7 26
Phlaphead 2:3cb7f5770feb 27 /**
Phlaphead 2:3cb7f5770feb 28 * Starts the task. Should be automatically called from the TaskManager when it is added.
Phlaphead 2:3cb7f5770feb 29 */
Phlaphead 0:e381c3adaa04 30 void start();
Phlaphead 2:3cb7f5770feb 31
Phlaphead 2:3cb7f5770feb 32 /**
Phlaphead 2:3cb7f5770feb 33 * Virtual method which gets called at the specified interval.
Phlaphead 2:3cb7f5770feb 34 */
Phlaphead 0:e381c3adaa04 35 virtual void tick() = 0;
Phlaphead 2:3cb7f5770feb 36
Phlaphead 2:3cb7f5770feb 37 /**
Phlaphead 2:3cb7f5770feb 38 * Stops the task from running
Phlaphead 2:3cb7f5770feb 39 */
Phlaphead 1:e95b703c6ad7 40 void stop();
Phlaphead 1:e95b703c6ad7 41
Phlaphead 2:3cb7f5770feb 42 /**
Phlaphead 2:3cb7f5770feb 43 * Returns true if the task is running.
Phlaphead 2:3cb7f5770feb 44 */
Phlaphead 1:e95b703c6ad7 45 bool isRunning() { return running; }
Phlaphead 0:e381c3adaa04 46
Phlaphead 0:e381c3adaa04 47
Phlaphead 0:e381c3adaa04 48 protected:
Phlaphead 0:e381c3adaa04 49
Phlaphead 2:3cb7f5770feb 50 /**
Phlaphead 2:3cb7f5770feb 51 * Set the interval that the task tick method runs.
Phlaphead 2:3cb7f5770feb 52 * @_interval Interval in us.
Phlaphead 2:3cb7f5770feb 53 */
Phlaphead 0:e381c3adaa04 54 void setInterval(int _interval) { interval = _interval; }
Phlaphead 0:e381c3adaa04 55
Phlaphead 0:e381c3adaa04 56
Phlaphead 0:e381c3adaa04 57 private:
Phlaphead 0:e381c3adaa04 58
Phlaphead 0:e381c3adaa04 59 Ticker ticker;
Phlaphead 0:e381c3adaa04 60 int interval;
Phlaphead 1:e95b703c6ad7 61 bool running;
Phlaphead 0:e381c3adaa04 62
Phlaphead 0:e381c3adaa04 63 void preTick();
Phlaphead 0:e381c3adaa04 64
Phlaphead 0:e381c3adaa04 65 };
Phlaphead 0:e381c3adaa04 66
Phlaphead 0:e381c3adaa04 67 #endif