-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/86dregyz4/circular queue #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set _head and _tail to _arr to start |
||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a newline at the end of both files |
||
There was a problem hiding this comment.
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