COSA
An Object-Oriented Platform for Arduino Programming
Event.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_EVENT_HH
22 #define COSA_EVENT_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/Queue.hh"
26 
27 // Default event queue size
28 #ifndef COSA_EVENT_QUEUE_MAX
29 # if defined(BOARD_ATTINY)
30 # define COSA_EVENT_QUEUE_MAX 8
31 # else
32 # define COSA_EVENT_QUEUE_MAX 16
33 # endif
34 #endif
35 
39 class Event {
40 public:
45  static const uint8_t QUEUE_MAX = COSA_EVENT_QUEUE_MAX;
46 
53  enum {
54  NULL_TYPE = 0,
55 
56  FALLING_TYPE, // Digital Pins
59 
60  SAMPLE_REQUEST_TYPE, // Analog Pins
62 
63  WATCHDOG_TYPE, // Watchdog and timers
65 
66  BEGIN_TYPE, // Finite State Machines
68 
69  RUN_TYPE, // Thread
70 
71  CONNECT_TYPE, // Device drivers and protocol stacks
77 
78  OPEN_TYPE, // Device drivers and storage
86 
89 
90  USER_TYPE = 64, // User defined events/messages, 64-254
91 
92  ERROR_TYPE = 255 // Error event
93  } __attribute__((packed));
94 
98  class Handler {
99  public:
107  virtual void on_event(uint8_t type, uint16_t value)
108  {
109  UNUSED(type);
110  UNUSED(value);
111  }
112  };
113 
114 public:
121  Event(int8_t type = NULL_TYPE, Handler* target = NULL, uint16_t value = 0) :
122  m_type(type),
123  m_target(target),
124  m_value(value)
125  {}
126 
131  uint8_t type() const
132  {
133  return (m_type);
134  }
135 
140  Handler* target() const
141  {
142  return (m_target);
143  }
144 
149  uint16_t value() const
150  {
151  return (m_value);
152  }
153 
158  void* env() const
159  {
160  return ((void*) m_value);
161  }
162 
166  void dispatch()
167  __attribute__((always_inline))
168  {
169  if (m_target != NULL) m_target->on_event(m_type, m_value);
170  }
171 
180  static bool push(uint8_t type, Handler* target, uint16_t value = 0)
181  __attribute__((always_inline))
182  {
183  Event event(type, target, value);
184  return (queue.enqueue(&event));
185  }
186 
195  static bool push(uint8_t type, Handler* target, void* env)
196  __attribute__((always_inline))
197  {
198  return (push(type, target, (uint16_t) env));
199  }
200 
205 
212  static bool service(uint32_t ms = 0L);
213 
214 private:
215  uint8_t m_type;
216  Handler* m_target;
217  uint16_t m_value;
218 };
219 
220 #endif
221 
static const uint8_t QUEUE_MAX
Definition: Event.hh:45
static Queue< Event, QUEUE_MAX > queue
Definition: Event.hh:204
#define NULL
Definition: Types.h:101
bool enqueue(T *data)
Definition: Queue.hh:119
Handler * target() const
Definition: Event.hh:140
#define COSA_EVENT_QUEUE_MAX
Definition: Event.hh:32
void dispatch()
Definition: Event.hh:166
uint16_t value() const
Definition: Event.hh:149
#define UNUSED(x)
Definition: ATmega328P.hh:31
Definition: Event.hh:39
uint8_t type() const
Definition: Event.hh:131
virtual void on_event(uint8_t type, uint16_t value)
Definition: Event.hh:107
Event(int8_t type=NULL_TYPE, Handler *target=NULL, uint16_t value=0)
Definition: Event.hh:121
static bool service(uint32_t ms=0L)
Definition: Event.cpp:27
static bool push(uint8_t type, Handler *target, void *env)
Definition: Event.hh:195
void * env() const
Definition: Event.hh:158
static bool push(uint8_t type, Handler *target, uint16_t value=0)
Definition: Event.hh:180