Created
January 3, 2022 14:42
-
-
Save emilekm/5c5cf1bf2873f601d07d3eca158135ef to your computer and use it in GitHub Desktop.
Queue buffer for STM32
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
#define BUF_SIZE 16 | |
enum { | |
BUF_INSERT_SUCCESS = 0 | |
BUF_ERR_FULL | |
} | |
int8_t buf[BUF_SIZE]; | |
uint8_t front = 0; | |
uint8_t rear = 0; | |
uint8_t len = 0 | |
uint8_t queue(int8_t data) { | |
if (len == BUF_SIZE) return BUF_ERR_FULL; | |
buf[rear] = data; | |
len++; | |
rear = rear + 1 == BUF_SIZE ? 0 : rear + 1 | |
return BUF_INSERT_SUCCESS; | |
} | |
int8_t dequeue() { | |
if (len == 0) return 0; | |
uint8_t temp = front; | |
len--; | |
front = front + 1 == BUF_SIZE ? 0 : front + 1 | |
return buf[temp] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment