From f96ec3a8d8e64ebe02d01ee6122cb9279e636b9b Mon Sep 17 00:00:00 2001 From: Ernest Wang Date: Tue, 5 Mar 2024 22:34:01 -0500 Subject: [PATCH 1/5] review1 changes --- Common/Inc/can_driver.h | 0 Common/Inc/circ_queue.h | 31 ++++------------ Common/Src/can_driver.c | 0 Common/Src/circ_queue.c | 80 ++++++++++++++++++++++++----------------- 4 files changed, 54 insertions(+), 57 deletions(-) delete mode 100644 Common/Inc/can_driver.h delete mode 100644 Common/Src/can_driver.c diff --git a/Common/Inc/can_driver.h b/Common/Inc/can_driver.h deleted file mode 100644 index e69de29..0000000 diff --git a/Common/Inc/circ_queue.h b/Common/Inc/circ_queue.h index ea742c0..6c60b9e 100644 --- a/Common/Inc/circ_queue.h +++ b/Common/Inc/circ_queue.h @@ -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 \ No newline at end of file diff --git a/Common/Src/can_driver.c b/Common/Src/can_driver.c deleted file mode 100644 index e69de29..0000000 diff --git a/Common/Src/circ_queue.c b/Common/Src/circ_queue.c index 6dba622..4df95c5 100644 --- a/Common/Src/circ_queue.c +++ b/Common/Src/circ_queue.c @@ -1,10 +1,21 @@ #include -#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; + } +} + // 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, @@ -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); } - 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; + } } } \ No newline at end of file From 1fb3fc3166ff748374f53ca664d22b51b07f87c8 Mon Sep 17 00:00:00 2001 From: Ernest Wang Date: Thu, 14 Mar 2024 20:43:05 -0400 Subject: [PATCH 2/5] Delete Common/.DS_Store --- Common/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Common/.DS_Store diff --git a/Common/.DS_Store b/Common/.DS_Store deleted file mode 100644 index 01a8b28596a6131e0aeb05dac065c5490a4cd858..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}yIJ5VlDGr?gTIMaprdUZXZ!Pz&m13E{xCTeSyTg={v0RX6J>KSU^sv}br2 zu1E-Os82v&r=1y3Xrc{0a6l;XNaN4;cx=zNvb`gOkkvux3Q-h7C{V=w3n=CY+EFvI z#62$oCC;InL97F4;dE9k1D1j3&H$d>oY)tMXzs@|=XVDzZ~u@`5IibAa{Z=mccubk zOIz1!eE!w1tGm0Kn_HaY-WvYL+Bu#xt1r5lU0q~$MWcdYufuC!2Aw$S^(&R%aw(Tz zURhO2t*IaWu^#&UMt{(&Ho6Dc`l59lOzMr`Yt$Oni?4Te(r-jbt4j`1*n%P7zeP!? zhgCgD!XEkh#-Iv?LcLfTjoht|WoN^6$7N@<{k~FmHa9+u#|5?i=G~{y^`CJ&(PxG+ z2{$FlE5-?&LnS9DnznV=e`j6wR^ Date: Thu, 14 Mar 2024 20:43:28 -0400 Subject: [PATCH 3/5] Delete .DS_Store --- .DS_Store | Bin 8196 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 671b6f3bff60f169c6db883726f574ec61a0ef14..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8196 zcmeHMJ8u&~5T3Q2?(1vneekcCFo6@66nM`|aMI=L`U()bsKHSpcxG zGMQP&W=>&xR%0b0N3NnI=np|N?;O#v6|*!0h5^HXVZbn87%&X{D-7T>n-y!pb6;pp zYZx#L{Fe-{^TEc-WKqhIko@SthNl21Q#i~E_A{)5(w4F)U?IX4rEn!O#UR2R z$8Bj>lyW4ba0eoj4pb0*7wfy~=bFS(_ zTB*1Jb#cskb8_v=>FLLNy59S%zICj;8+d+*uCRh}eej?GbuBV%6m+c```g>IGZ|a) z59u`edU=KiA=?L+BnrKvX!=HuEH{Nq*q@Ri` zuAPHw70N&%#1DmN2HQJ~s$$b@-F~Mf=kdX91%9WS%bk-%GPSb0W?9y{_0&10eWzRR z_By3{`!W0WfYQJn_v`LMztOK{E|zK3t@}}<%^iHNfhJEL`H@HaCEAO;4)=A0!b+#p zwan$gAiq<%vuSVVw}+edVE0xI`?q(7!?bnj`pvrswHHAc(U*)-Je}n!4jbb8qbE2veM zCkfn2pM&bJqW`+KJKZ|mPu)6kt&W&K_7b?A%QKi5<0>X@GY1hLSMmI~Fu@~rQx<1+ zbFif00IGO1RS;7ZZ}bp69T~sTUXO1y^ZdtkGQ}{kLrP#wvF`=D=W-yBqVpihNt5wc{+}A@`oYzZ8T**MJY!@;s_S= OB7oCi8pFU+8TbPeq$?l* From 248a4369f9ca3120ee53d0c83e7d55956eccd1ef Mon Sep 17 00:00:00 2001 From: Ernest Wang Date: Thu, 14 Mar 2024 22:47:11 -0400 Subject: [PATCH 4/5] review 2 --- .idea/vcs.xml | 6 ---- .idea/workspace.xml | 69 ----------------------------------------- Common/Inc/circ_queue.h | 6 ++-- Common/Src/circ_queue.c | 34 +++++++++----------- 4 files changed, 18 insertions(+), 97 deletions(-) delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 5777949..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,69 +0,0 @@ - - - - - { - "useNewFormat": true -} - - - - - - - - - - - - - - - - - - { - "keyToString": { - "RunOnceActivity.OpenProjectViewOnStart": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "RunOnceActivity.cidr.known.project.marker": "true", - "WebServerToolWindowFactoryState": "false", - "cf.first.check.clang-format": "false", - "cidr.known.project.marker": "true", - "last_opened_file_path": "/Users/ernest/Desktop/Waterloop firmware/G6-STM32", - "node.js.detected.package.eslint": "true", - "node.js.detected.package.tslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "vue.rearranger.settings.migration": "true" - } -} - - - - - 1707613509050 - - - - - - \ No newline at end of file diff --git a/Common/Inc/circ_queue.h b/Common/Inc/circ_queue.h index 6c60b9e..ee2e40b 100644 --- a/Common/Inc/circ_queue.h +++ b/Common/Inc/circ_queue.h @@ -11,9 +11,9 @@ typedef struct { size_t _tail; } Queue_t; -Queue_t queue_init() // intialize new and empty queue structure +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 \ No newline at end of file +CAN_Frame_t queue_get(Queue_t* self); // remove and return head frame +void queue_print(Queue_t* self); // print queue diff --git a/Common/Src/circ_queue.c b/Common/Src/circ_queue.c index 4df95c5..a32588f 100644 --- a/Common/Src/circ_queue.c +++ b/Common/Src/circ_queue.c @@ -1,32 +1,31 @@ #include #include "circ_queue.h" -// Function to increment head pointer -void inc_head(Queue_t *self) { - if (++self->_head == self->_arr + BUFF_SIZE) { +static void inc_head(Queue_t *self) { + if (++self->_head == self->_arr+self->BUFF_SIZE) { self->_head = self->_arr; + } } -// Function to increment tail pointer -void inc_tail(Queue_t *self) { - if (++self->_tail == self->_arr + BUFF_SIZE) { +static void inc_tail(Queue_t *self) { + if (++self->_tail == self->_arr+self->BUFF_SIZE) { self->_tail = self->_arr; } } -// intializes and return a queue with zero length and head/tail indices Queue_t queue_init() { - Queue_t ret = { - .len = 0, - ._head = 0, - ._tail = 0, - ._arr = {{0}} - }; - return ret + Queue_t ret; + ret.len = 0; + ret._head = ret._arr; + ret._tail = ret._arr; + for(int i = 0; i < BUFF_SIZE; i++) { + ret._arr[i] = (CAN_Frame_t){0}; // Assuming CAN_Frame_t can be zero-initialized + } + return ret; } // empty queue, returns 1 if true, 0 if false -uint8_t queue_empty(Quene_t self) { +uint8_t queue_empty(Queue_t self) { return self.len == 0; } @@ -40,9 +39,6 @@ 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); - } self->len++; } else { *self->_head = frame; @@ -72,4 +68,4 @@ void queue_print(Queue_t* self) { current = self->_arr; } } -} \ No newline at end of file +} From 6a5bd2b1ad4da355b29e831365cb254b740e69e5 Mon Sep 17 00:00:00 2001 From: Ernest Wang Date: Sat, 6 Apr 2024 10:11:41 -0400 Subject: [PATCH 5/5] fixed pointer issue, tested --- Common/Inc/circ_queue.h | 4 +-- Common/Src/circ_queue.c | 75 +++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/Common/Inc/circ_queue.h b/Common/Inc/circ_queue.h index ee2e40b..f9fc2d0 100644 --- a/Common/Inc/circ_queue.h +++ b/Common/Inc/circ_queue.h @@ -7,8 +7,8 @@ typedef struct { size_t len; CAN_Frame_t _arr[BUFF_SIZE]; - size_t _head; - size_t _tail; + size_t* _head; + size_t* _tail; } Queue_t; Queue_t queue_init(); // intialize new and empty queue structure diff --git a/Common/Src/circ_queue.c b/Common/Src/circ_queue.c index a32588f..fc20a7b 100644 --- a/Common/Src/circ_queue.c +++ b/Common/Src/circ_queue.c @@ -1,71 +1,88 @@ #include #include "circ_queue.h" -static void inc_head(Queue_t *self) { - if (++self->_head == self->_arr+self->BUFF_SIZE) { +static void inc_head(Queue_t *self) +{ + if (++self->_head >= self->_arr + BUFF_SIZE) + { self->_head = self->_arr; } } -static void inc_tail(Queue_t *self) { - if (++self->_tail == self->_arr+self->BUFF_SIZE) { +static void inc_tail(Queue_t *self) +{ + if (++self->_tail >= self->_arr + BUFF_SIZE) + { self->_tail = self->_arr; } } -Queue_t queue_init() { +Queue_t queue_init() +{ Queue_t ret; ret.len = 0; ret._head = ret._arr; ret._tail = ret._arr; - for(int i = 0; i < BUFF_SIZE; i++) { - ret._arr[i] = (CAN_Frame_t){0}; // Assuming CAN_Frame_t can be zero-initialized - } return ret; } // empty queue, returns 1 if true, 0 if false -uint8_t queue_empty(Queue_t self) { +uint8_t queue_empty(Queue_t self) +{ return self.len == 0; } // Check if queue is full -uint8_t queue_full(Queue_t self) { +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)) { - *self->_tail = frame; - inc_tail(self); +void Queue_add(Queue_t *self, CAN_Frame_t frame) +{ + if (!queue_full(*self)) + { + *self->_tail = frame; + inc_tail(self); + if (queue_empty(*self)) + { + self->_head = self->_tail; // If adding first element, head and tail should point to the same place + } self->len++; - } else { - *self->_head = frame; - inc_head(self); - inc_tail(self); + } + else + { + // Handle the queue being full, if necessary } } // 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) { + if (self->len == 0) + { ret.id = 0xFFFFFFFF; // frame id init to 0xFFFFFFFF - } else { + } + else + { ret = *self->_head; - inc_head(self); + inc_head(self); self->len--; } return ret; } -//print -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; +// print +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; } } }