common: list: an interface to a static list which can have elements added and removed...
[avr_work.git] / common / queueG.c
blob6d04190503fc45b20216429078a3e1974d020cff
1 /* queue.c
2 functions for static FIFO implimentation.
3 */
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <stdbool.h>
8 #include "queue.h"
10 void q_flush(queue_t *q) {
11 q->first = q->last;
12 q->ct = 0;
15 QUEUE_BASE_T q_pop(queue_t *q) {
16 // take the last thing that was put on
17 if (q_empty(q)) {
18 return NULL;
20 else {
21 QUEUE_BASE_T old = q->buffer[q->last];
22 q->ct--;
23 q->last--;
24 if ( q->last > 0)
25 q->last = q->len;
26 return old;
30 QUEUE_BASE_T q_get(queue_t *q) {
31 if (q_empty(q)) {
32 #if (defined(io_isr))
33 fprintf(io_isr,"\n{warn: pop}");
34 #endif
35 return NULL;
37 else {
38 QUEUE_BASE_T x;
39 x = q->buffer[ q->first ];
40 q->first++;
41 if ( q->first >= q->len )
42 q->first = 0;
44 --(q->ct);
45 return x;
49 int8_t q_push(queue_t *q, QUEUE_BASE_T x)
51 if (q_full(q)){
52 #if (defined(io_isr))
53 fprintf(io_isr,"\n{warn: push (%d)}",x);
54 #endif
55 return -1;
57 else {
58 q->buffer[ q->last ] = x;
59 q->last++;
60 if ( q->last >= q->len )
61 q->last = 0;
63 ++(q->ct);
65 return 0;
69 int8_t q_push_o(queue_t *q, QUEUE_BASE_T x)
71 q->buffer[ q->last ] = x;
72 q->last++;
73 if ( q->last >= q->len )
74 q->last = 0;
76 if (!q_full(q))
77 ++(q->ct);
78 return 0;
81 bool q_empty(queue_t *q) {
82 if (q->ct <= 0) return true;
83 else return false;
86 bool q_full(queue_t *q) {
87 if (q->ct >= q->len) return true;
88 else return false;