Core/Task.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Task.h
8  *
9  * @author mikee47 mike@sillyhouse.net
10  *
11  * Sming provides a task queue for applications to use. It can be used to defer processing
12  * from an interrupt service routine, but can also be used to provide a form of multi-tasking.
13  *
14  * This class provides a simple way to create a task which runs continuously, or can be started
15  * and stopped as required.
16  *
17  * You can see how this class is used in the `Basic_Tasks` sample.
18  *
19  *
20  ****/
21 
22 #pragma once
23 
24 #include <SimpleTimer.h>
25 
33 class Task
34 {
35 public:
39  enum class State {
40  Suspended,
41  Sleeping,
42  Running,
43  };
44 
48  enum class Notify {
49  None,
50  Suspending,
51  Resuming,
52  Sleeping,
53  Waking,
54  };
55 
56  virtual ~Task()
57  {
58  }
59 
64  bool resume()
65  {
66  switch(state) {
67  case State::Sleeping:
68  sleepTimer.stop();
69  notification = Notify::Waking;
70  state = State::Running;
71  return schedule();
72  case State::Suspended:
73  notification = Notify::Resuming;
74  state = State::Running;
75  return schedule();
76  case State::Running:
77  return true;
78  default:
79  assert(false);
80  return false;
81  }
82  }
83 
87  void suspend()
88  {
89  switch(state) {
90  case State::Sleeping:
91  sleepTimer.stop();
92  notify(Notify::Suspending);
93  state = State::Suspended;
94  break;
95  case State::Running:
96  notify(Notify::Suspending);
97  state = State::Suspended;
98  break;
99  case State::Suspended:
100  break;
101  default:
102  assert(false);
103  }
104  }
105 
110  void sleep(unsigned interval)
111  {
112  switch(state) {
113  case State::Suspended:
114  case State::Running:
115  sleepTimer.initializeMs(
116  interval,
117  [](void* param) {
118  auto task = static_cast<Task*>(param);
119  task->notify(Notify::Waking);
120  task->state = State::Running;
121  task->service();
122  },
123  this);
124  sleepTimer.startOnce();
125  notify(Notify::Sleeping);
126  state = State::Sleeping;
127  break;
128  case State::Sleeping:
129  break;
130  }
131  }
132 
133 protected:
137  virtual void loop() = 0;
138 
142  virtual void onNotify(Notify code)
143  {
144  }
145 
146 private:
147  /*
148  *
149  */
150  void notify(Notify code)
151  {
152  notification = Notify::None;
153  onNotify(code);
154  }
155 
160  bool schedule()
161  {
162  if(scheduled) {
163  return true;
164  }
165 
166  scheduled = System.queueCallback(
167  [](uint32_t param) {
168  auto task = reinterpret_cast<Task*>(param);
169  task->scheduled = false;
170  task->service();
171  },
172  uint32_t(this));
173 
174  return scheduled;
175  }
176 
177  /*
178  * Executed via task queue
179  */
180  void service()
181  {
182  if(state == State::Running) {
183  if(notification != Notify::None) {
184  notify(notification);
185  }
186  loop();
187  schedule();
188  }
189  }
190 
191 private:
192  State state{State::Suspended};
193  Notify notification{Notify::None};
194  bool scheduled{false};
195  SimpleTimer sleepTimer;
196 };
CallbackTimer & initializeMs(TimerCallback callback, void *arg=nullptr)
Initialise hardware timer in milliseconds (static check) with Timer Callback and optional argument.
Definition: CallbackTimer.h:186
void stop()
Stops timer.
Definition: CallbackTimer.h:239
bool startOnce()
Start one-shot timer.
Definition: CallbackTimer.h:232
static bool queueCallback(TaskCallback32 callback, uint32_t param=0)
Queue a deferred callback.
Class to support running a background task.
Definition: Core/Task.h:34
virtual ~Task()
Definition: Core/Task.h:56
virtual void loop()=0
Inherited classes override this to perform actual work.
virtual void onNotify(Notify code)
Called immediately before calling to loop() to indicate a state change.
Definition: Core/Task.h:142
bool resume()
Call to set task running.
Definition: Core/Task.h:64
Notify
Notification of state change.
Definition: Core/Task.h:48
void sleep(unsigned interval)
Puts the task to sleep for a while.
Definition: Core/Task.h:110
void suspend()
Suspend a task.
Definition: Core/Task.h:87
State
State of a task.
Definition: Core/Task.h:39
SystemClass System
Global instance of system object.