Arduino-Scheduler
Portable Collaborative Multi-Tasking Scheduler for Arduino
Queue.h
Go to the documentation of this file.
1 
19 #ifndef SCHEDULER_QUEUE_H
20 #define SCHEDULER_QUEUE_H
21 
27 template <class T, uint8_t NMEMB>
28 class Queue {
29  static_assert(NMEMB && !(NMEMB & (NMEMB - 1)), "Queue::NMEMB should be power of 2");
30 public:
35  Queue() :
36  m_put(0),
37  m_get(0)
38  {}
39 
44  unsigned int available() const
45  __attribute__((always_inline))
46  {
47  return ((NMEMB + m_put - m_get) & MASK);
48  }
49 
54  unsigned int room() const
55  __attribute__((always_inline))
56  {
57  return ((NMEMB - m_put + m_get - 1) & MASK);
58  }
59 
64  void push(const T* data)
65  {
66  await(room());
67  unsigned int next = (m_put + 1) & MASK;
68  m_buffer[next] = *data;
69  m_put = next;
70  }
71 
77  void push_P(const T* data)
78  {
79  await(room());
80  unsigned int next = (m_put + 1) & MASK;
81  memcpy_P(&m_buffer[next], data, sizeof(T));
82  m_put = next;
83  }
84 
90  void pull(T* data)
91  {
92  await(available());
93  unsigned int next = (m_get + 1) & MASK;
94  m_get = next;
95  *data = m_buffer[next];
96  }
97 
98 private:
99  static const unsigned int MASK = (NMEMB - 1);
100  volatile unsigned int m_put;
101  volatile unsigned int m_get;
102  T m_buffer[NMEMB];
103 };
104 
105 #endif
void pull(T *data)
Definition: Queue.h:90
void push(const T *data)
Definition: Queue.h:64
#define await(cond)
Definition: Scheduler.h:162
Definition: Queue.h:28
unsigned int available() const
Definition: Queue.h:44
void push_P(const T *data)
Definition: Queue.h:77
unsigned int room() const
Definition: Queue.h:54
Queue()
Definition: Queue.h:35