COSA
An Object-Oriented Platform for Arduino Programming
FSM.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_FSM_HH
22 #define COSA_FSM_HH
23 
24 #include "Cosa/Job.hh"
25 #include "Cosa/Event.hh"
26 #include "Cosa/Watchdog.hh"
27 
37 class FSM : protected Job {
38 public:
47  typedef bool (*StateHandler)(FSM* fsm, uint8_t type);
48 
55  FSM(StateHandler init, Job::Scheduler* scheduler = NULL, uint16_t period = 0) :
56  Job(scheduler),
57  m_state(init),
58  m_period(period),
59  m_param(0)
60  {}
61 
68  __attribute__((always_inline))
69  {
70  if (UNLIKELY(fn == NULL)) return;
71  m_state = fn;
72  }
73 
78  __attribute__((always_inline))
79  {
80  return (m_state);
81  }
82 
87  void period(uint8_t ms)
88  {
89  m_period = ms;
90  }
91 
96  void get(uint16_t& param) const
97  {
98  param = m_param;
99  }
100 
105  void get(void*& param) const
106  {
107  param = (void*) m_param;
108  }
109 
115  void send(uint8_t type, uint16_t value = 0)
116  __attribute__((always_inline))
117  {
118  Event::push(type, this, value);
119  }
120 
126  void send(uint8_t type, void* value)
127  __attribute__((always_inline))
128  {
129  Event::push(type, this, value);
130  }
131 
135  bool begin()
136  {
137  if ((m_period != 0) && (m_period != TIMEOUT_REQUEST)) {
138  expire_after(m_period);
139  start();
140  }
142  return (true);
143  }
144 
148  void end()
149  {
150  cancel_timer();
152  }
153 
158  void set_timer(uint16_t ms)
159  __attribute__((always_inline))
160  {
161  m_period = TIMEOUT_REQUEST;
162  expire_after(ms);
163  start();
164  }
165 
171  __attribute__((always_inline))
172  {
173  if (UNLIKELY(m_period == 0)) return;
174  stop();
175  m_period = 0;
176  }
177 
178 private:
179  static const uint16_t TIMEOUT_REQUEST = 0xffff;
180  StateHandler m_state;
181  uint16_t m_period;
182  uint16_t m_param;
183 
191  virtual void on_event(uint8_t type, uint16_t value)
192  {
193  m_param = value;
194  m_state(this, type);
195  if (type == Event::TIMEOUT_TYPE) {
196  if ((m_period != 0) && (m_period != TIMEOUT_REQUEST)) {
197  expire_after(m_period);
198  start();
199  }
200  }
201  }
202 };
203 
204 #endif
205 
StateHandler state() const
Definition: FSM.hh:77
Definition: FSM.hh:37
void end()
Definition: FSM.hh:148
bool(* StateHandler)(FSM *fsm, uint8_t type)
Definition: FSM.hh:47
int32_t expire_after() const
Definition: Job.hh:129
#define NULL
Definition: Types.h:101
void state(StateHandler fn)
Definition: FSM.hh:67
Definition: Job.hh:33
void cancel_timer()
Definition: FSM.hh:170
void send(uint8_t type, void *value)
Definition: FSM.hh:126
bool stop()
Definition: Job.hh:169
void set_timer(uint16_t ms)
Definition: FSM.hh:158
void period(uint8_t ms)
Definition: FSM.hh:87
bool begin()
Definition: FSM.hh:135
bool start()
Definition: Job.hh:159
#define UNLIKELY(x)
Definition: Types.h:153
static bool push(uint8_t type, Handler *target, uint16_t value=0)
Definition: Event.hh:180
FSM(StateHandler init, Job::Scheduler *scheduler=NULL, uint16_t period=0)
Definition: FSM.hh:55
void send(uint8_t type, uint16_t value=0)
Definition: FSM.hh:115