Pico-Arduino
PicoQueue.h
1 #pragma once
2 
3 #include "pico/stdlib.h"
4 extern "C" {
5 #include "pico/util/queue.h"
6 }
7 
8 namespace pico_arduino {
9 
16 template <class T>
17 class Queue {
18  public:
26  Queue( int maxCount = 100,bool blocking=true, bool withLock=true ){
27  is_blocking = blocking;
28  if (withLock){
29  queue_init_with_spinlock(&q,sizeof(T), maxCount, nextSpinlockNumber());
30  } else {
31  queue_init(&q,sizeof(T),maxCount);
32  }
33  }
34 
36  ~Queue() {
37  queue_free(&q);
38  }
39 
41  bool isEmpty() {
42  return queue_is_empty(&q);
43  }
44 
46  bool isFull() {
47  return queue_is_full(&q);
48  }
49 
51  bool peek(T &data){
52  bool result = false;
53  if (is_blocking){
54  queue_peek_blocking(&q, (void*) &data);
55  result = true;
56  } else {
57  result = queue_try_peek(&q, (void*) &data);
58  }
59  return result;
60  }
61 
63  bool push(T &data){
64  bool result = false;
65  if (is_blocking){
66  queue_add_blocking(&q, (void*) &data);
67  result = true;
68  } else {
69  result = queue_try_add(&q, (void*) &data);
70  }
71  return result;
72  }
73 
75  bool pop(T &data){
76  bool result = false;
77  if (is_blocking){
78  queue_remove_blocking(&q, (void*) &data);
79  result = true;
80  } else {
81  result = queue_try_remove(&q, (void*) &data);
82  }
83  return result;
84  }
85 
87  uint size() {
88  return queue_get_level(&q);
89  }
90 
92  void clear() {
93  T data;
94  while(remove((void*) &data));
95  }
96 
97  protected:
98  queue_t q;
99  bool is_blocking;
100 
101  uint nextSpinlockNumber(){
102  static uint spinlock_num;
103  return spinlock_num++;
104  }
105 };
106 
107 }
Construct a new Pico Queue object.
Definition: PicoQueue.h:17
~Queue()
Destructor - calls queue_free.
Definition: PicoQueue.h:36
bool pop(T &data)
Gets the next element (from the head) and removes it from the queue.
Definition: PicoQueue.h:75
uint size()
Provides the number of entries in the queue.
Definition: PicoQueue.h:87
bool peek(T &data)
Reads the next element w/o removing it from the queue.
Definition: PicoQueue.h:51
bool isFull()
checks if the queue is full
Definition: PicoQueue.h:46
void clear()
clears the queue by removing all elements
Definition: PicoQueue.h:92
Queue(int maxCount=100, bool blocking=true, bool withLock=true)
Construct a new Queue object.
Definition: PicoQueue.h:26
bool push(T &data)
Adds an element (at the end) of the queue.
Definition: PicoQueue.h:63
bool isEmpty()
checks if the queue is eimpty
Definition: PicoQueue.h:41
Pico Arduino Framework.
Definition: Arduino.cpp:26