Trying to make a Queue module for multiple UARTs
Hi gang. My application uses from 0 to 3 UARTs, or maybe USB CDC instead ... LOTS of end-user settable options. Even before I get to that, I'm not happy with my queue implementation that's to be used with UART (or CDC) Tx and Rx. I would love for the enqueue function below to be part of queue structure itself, but really any ideas or pointers would help.
// Queue type (lost about using functions in it):
typedef struct
{
uint8_t *buffer;
uint32_t size;
uint32_t head; // Enqueue data using the head index.
uint32_t tail; // Dequeue data using the tail index.
uint32_t count;
//bool (*enqueue)(uint8_t *buffer, const uint8_t entry); // Fail
//bool (*enqueue)(Queue_T *queue, const uint8_t entry); // Fail
}
Queue_T;
// Test data
uint8_t TestBuffer[16];
Queue_T MyQueue;
// IN USE: Initialize a queue...
MyQueue.buffer = TestBuffer;
KeyQueue.size = 16;
QueueInit(&MyQueue);
// IN USE: enqueue a byte:
QueueEnqueue(&TestBuffer, 47);
// Init function
void QueueInit(Queue_T *queue)
{
queue->head = queue->size - 1;
queue->tail = queue->size - 1;
queue->count = 0;
}
// Enqueue Function (also dequeue, peek, etc needed)
bool QueueEnqueue(Queue_T *queue, const uint8_t entry)
{
uint32_t index;
index = queue->head; // Queue points to last entry, so incr
index++; // pointer for new entry.
if(index >= queue->size) // If past end of queue area, wrap to
index = 0; // start.
if(index == queue->tail) // If head==tail, then the queue is full.
return 1; // Exit failure.
queue->buffer[index] = entry; // Append the byte or word value.
queue->head = index; // Save the new head pointer.
queue->count++; // Update the count.
return 0; // Exit success.
}Any pointers on any of this would be great. It seems like such a waste to initialize the queue data type then have to spell out each function. It would be nice to be able to do something like:
MyQueue.enqueue(47);instead of
QueueEnqueue(&TestBuffer, 47);since MyQueue already knows about TestBuffer.
This is the closest thing I could find and I could not beat it into submission: