Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed Common/Inc/can_driver.h
Empty file.
31 changes: 7 additions & 24 deletions Common/Inc/circ_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,14 @@
// define Queue structure
typedef struct {
size_t len;
CANFrames_t _arr[BUFF_SIZE];
CAN_Frame_t _arr[BUFF_SIZE];
size_t _head;
size_t _tail;
} Queue_t;

// macro _INC_HEAD for _head increment
#define _INC_HEAD(self) { \
if (self->_head == (BUFF_SIZE - 1)){ \
self->_head = 0 \
} else { \
self->_head += 1; \
} \
}

// macro _INC_TAIL for _tail indexing
#define _INC_TAIL(self) { \
if (self->_tail == (BUFF_SIZE - 1)) { \
self->_tail = 0; \
} else { \
self->_tail += 1; \
} \
}

Queue_t Queue_init() // intialize new and empty queue structure
uint8_t Queue_empty (Queue_t* self); // check if queue is empty
void Queue_add(Queue_t* self, CAN_Frame_t val); // add a frame to queue
CAN_Frame_t Queue_get(Queue* self); // remove and return head frame
void Queue_print(Queue_t* self); // print queue
Queue_t queue_init() // intialize new and empty queue structure
uint8_t queue_empty (Queue_t self); // check if queue is empty
uint8_t queue_full(Queue_t self);
void queue_add(Queue_t* self, CAN_Frame_t val); // add a frame to queue
CAN_Frame_t queue_get(Queue* self); // remove and return head frame
void queue_print(Queue_t* self); // print queue
Empty file removed Common/Src/can_driver.c
Empty file.
80 changes: 47 additions & 33 deletions Common/Src/circ_queue.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
#include <stdio.h>
#include "config.h"
#include "can_driver.h"
#include "circ_queue.h"

// Function to increment head pointer
void inc_head(Queue_t *self) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make these two increment functions static if they only need to be accessed in this .c file

if (++self->_head == self->_arr + BUFF_SIZE) {
self->_head = self->_arr;
}

// Function to increment tail pointer
void inc_tail(Queue_t *self) {
if (++self->_tail == self->_arr + BUFF_SIZE) {
self->_tail = self->_arr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_head and _tail aren't defined as pointers

}
}

// intializes and return a queue with zero length and head/tail indices
Queue_t Queue_init() {
Queue_t queue_init() {
Queue_t ret = {
.len = 0,
._head = 0,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set _head and _tail to _arr to start

Expand All @@ -15,47 +26,50 @@ Queue_t Queue_init() {
}

// empty queue, returns 1 if true, 0 if false
uint8_t Queue_empty(Quene_t* self) {
return self->len == 0;
uint8_t queue_empty(Quene_t self) {
return self.len == 0;
}

// Check if queue is full
uint8_t queue_full(Queue_t self) {
return self.len == BUFF_SIZE;
}

// adds new CANFrame to queue
void Queue_add(Queue_t* self, CAN_Frame_t frame) {
if ( !(self->len == BUFF_SIZE) ) {
if (Queue_empty(self)) {
self->_arr[self->_head] = frame;
void queue_add(Queue_t* self, CAN_Frame_t frame) {
if (!(self->len == BUFF_SIZE)) {
*self->_tail = frame;
inc_tail(self);
if (self->len > 0) {
inc_tail(self);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does it increment twice if (len == 0)? I think you had the right logic before.

}
else {
_INC_TAIL(self);
self->_arr[self->_tail] = frame;
}
self->len += 1;
}
else {
self->_arr[self->_head] = val;
_INC_HEAD(self);
_INC_TAIL(self);
self->len++;
} else {
*self->_head = frame;
inc_head(self);
inc_tail(self);
}
}

// removes frame from queue
CAN_Frame_t Queue_get(Queue_t* self) {
CAN_Frame_t queue_get(Queue_t* self) {
CAN_Frame_t ret;
if (self->len == 0) {
CAN_Frame_t ret = {

}
}
else {
CAN_Frame_t ret = self->_arr[self->_head];
_INC_HEAD(self);
self->len -= 1;
return ret
ret.id = 0xFFFFFFFF; // frame id init to 0xFFFFFFFF
} else {
ret = *self->_head;
inc_head(self);
self->len--;
}
return ret;
}

//print
void Queue_print(Queue_t* self) {
for (size_t i = 0; i < (BUFF_SIZE - 1); i++) {
printf(self->_arr[i])
void queue_print(Queue_t* self) {
CAN_Frame_t* current = self->_head;
for (size_t i = 0; i < self->len; i++) {
printf("Frame ID: %u\n", current->id);
if (++current == self->_arr + BUFF_SIZE) {
current = self->_arr;
}
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a newline at the end of both files