Pico-Arduino
PicoQueue.h
1 #pragma once
2 
3 #include "pico/stdlib.h"
4 extern "C" {
5 #include "pico/util/queue.h"
6 }
13 template <class T>
14 class Queue {
15  public:
16  Queue( int maxCount = 100,bool blocking=true, bool withLock=true ){
17  is_blocking = blocking;
18  if (withLock){
19  queue_init_with_spinlock(&q,sizeof(T), maxCount, nextSpinlockNumber());
20  } else {
21  queue_init(&q,sizeof(T),maxCount);
22  }
23  }
24 
25  ~Queue() {
26  queue_free(&q);
27  }
28 
29  bool isEmpty() {
30  return queue_is_empty(&q);
31  }
32 
33  bool isFull() {
34  return queue_is_full(&q);
35  }
36 
37  bool peek(T &data){
38  bool result = false;
39  if (is_blocking){
40  queue_peek_blocking(&q, (void*) &data);
41  result = true;
42  } else {
43  result = queue_try_peek(&q, (void*) &data);
44  }
45  return result;
46  }
47 
48  bool push(T &data){
49  bool result = false;
50  if (is_blocking){
51  queue_add_blocking(&q, (void*) &data);
52  result = true;
53  } else {
54  result = queue_try_add(&q, (void*) &data);
55  }
56  return result;
57  }
58 
59  bool pop(T &data){
60  bool result = false;
61  if (is_blocking){
62  queue_remove_blocking(&q, (void*) &data);
63  result = true;
64  } else {
65  result = queue_try_remove(&q, (void*) &data);
66  }
67  return result;
68  }
69 
70  uint size() {
71  return queue_get_level(&q);
72  }
73 
74  void clear() {
75  T data;
76  while(remove((void*) &data));
77  }
78 
79  protected:
80  queue_t q;
81  bool is_blocking;
82 
83  uint nextSpinlockNumber(){
84  static uint spinlock_num;
85  return spinlock_num++;
86  }
87 };
Construct a new Pico Queue object.
Definition: PicoQueue.h:14