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