Last active
December 26, 2021 12:58
-
-
Save OsoianMarcel/23017af3adc6d5b8cc512b49c1a11edd to your computer and use it in GitHub Desktop.
A basic example of queue written in C (Arduino)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A basic example of queue written in C | |
// Useful for small microcontrollers | |
// Tags: Arduino | Atmega | AVR | STM32 | |
#include <stdio.h> | |
#include <stdint.h> | |
// Maximum number of items in queue | |
#define QUEUE_MAX 5 | |
// Add your custom properies (the prop "set" is required) | |
typedef struct QueueItem_s | |
{ | |
uint8_t value; // Custom property | |
uint8_t set; | |
} QueueItem_t; | |
typedef struct Queue_s | |
{ | |
uint8_t last; | |
uint8_t cons; | |
QueueItem_t items[QUEUE_MAX]; | |
} Queue_t; | |
// Returns 1 on success, otherwise if the queue is full returns 0 | |
uint8_t queue_push(Queue_t *q, QueueItem_t i) | |
{ | |
if (q->last >= QUEUE_MAX) | |
{ | |
q->last = 0; | |
} | |
if (q->items[q->last].set == 1) | |
{ | |
return 0; | |
} | |
q->items[q->last++] = i; | |
return 1; | |
} | |
// Returns next queue item, otherwire if the queue is empty then returns an (QueueItem_t){0, 0} | |
QueueItem_t queue_next(Queue_t *q) | |
{ | |
if (q->cons >= QUEUE_MAX) | |
{ | |
q->cons = 0; | |
} | |
if (q->items[q->cons].set == 0) | |
{ | |
return (QueueItem_t){0, 0}; | |
} | |
QueueItem_t i = q->items[q->cons]; | |
q->items[q->cons].set = 0; | |
q->cons++; | |
return i; | |
} | |
void queue_clear(Queue_t *q) | |
{ | |
for (uint8_t i = 0; i < QUEUE_MAX; i++) | |
{ | |
q->items[i].set = 0; | |
} | |
q->last = 0; | |
q->cons = 0; | |
} | |
int main() | |
{ | |
// Declare queue instance | |
Queue_t queue = { | |
.last = 0, | |
.cons = 0, | |
.items = {} | |
}; | |
// Queue testing | |
uint8_t i; | |
// Push 4 elements | |
for (i = 0; i < 4; i++) | |
{ | |
printf("push%d: %d\n", i, queue_push(&queue, (QueueItem_t){i * 10, 1})); | |
} | |
// Consume 4 elements | |
for (i = 0; i < 4; i++) | |
{ | |
printf("next%d: %d\n", i, queue_next(&queue).value); | |
} | |
// Push 6 elements (last element does not fit in queue) | |
for (i = 0; i < 6; i++) | |
{ | |
printf("push%d: %d\n", i, queue_push(&queue, (QueueItem_t){i * 10, 1})); | |
} | |
// Consume 6 elements (last element does not exists, on last element the queue will return empty element) | |
for (i = 0; i < 6; i++) | |
{ | |
printf("next%d: %d\n", i, queue_next(&queue).value); | |
} | |
// Output queue stats | |
printf("\nstats\n"); | |
for (int si = 0; si < QUEUE_MAX; si++) | |
{ | |
printf("%d: %d / %d\n", si, queue.items[si].value, queue.items[si].set); | |
} | |
// /Queue testing | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
push0: 1 | |
push1: 1 | |
push2: 1 | |
push3: 1 | |
next0: 0 | |
next1: 10 | |
next2: 20 | |
next3: 30 | |
push0: 1 | |
push1: 1 | |
push2: 1 | |
push3: 1 | |
push4: 1 | |
push5: 0 | |
next0: 0 | |
next1: 10 | |
next2: 20 | |
next3: 30 | |
next4: 40 | |
next5: 0 | |
stats | |
0: 10 / 0 | |
1: 20 / 0 | |
2: 30 / 0 | |
3: 40 / 0 | |
4: 0 / 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment