-
Notifications
You must be signed in to change notification settings - Fork 0
#1 circbuffer vava #2
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
Open
Vava-2549
wants to merge
8
commits into
main
Choose a base branch
from
#1-circbuffer-vava
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−0
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a8f46d4
Header files for circular buffer and gps
Vava-2549 e34e649
Source code for circular buffer and gps
Vava-2549 946df18
Added comments for circ_buff.h
Vava-2549 4412ff7
Added comments to m8n.h
Vava-2549 776a76e
Added comments to circ_buffer.c
Vava-2549 41a92d6
Added comments to m8n.c
Vava-2549 e5557d9
Made changes to receive and send data as 8-bit arrays instead of sing…
Vava-2549 44141a5
Fixed errors due to incompatible pointers
Vava-2549 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * circ_buffer.h | ||
| * | ||
| * Created on: 16 มี.ค. 2569 | ||
| * Author: pkuna | ||
| */ | ||
|
|
||
| #ifndef INC_CIRC_BUFFER_H_ | ||
| #define INC_CIRC_BUFFER_H_ | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdint.h> | ||
| #include <stdbool.h> | ||
|
|
||
| typedef struct { | ||
| uint8_t * const buffer; | ||
| int head; | ||
| int tail; | ||
| const int maxLen; | ||
| } circularBuffer; | ||
|
|
||
| #define circBuffDef(buff, size) \ | ||
| uint8_t buff##_data_space[size]; \ | ||
| circularBuffer buff = { \ | ||
| .buffer = buff##_data_space, \ | ||
| .head = 0, \ | ||
| .tail = 0, \ | ||
| .maxLen = size \ | ||
| } | ||
|
|
||
| extern circularBuffer gpsBuffer; | ||
|
|
||
| bool checkCircBuff(circularBuffer *cb); | ||
| bool circBuffPush(circularBuffer *cb, uint8_t data); | ||
| bool circBuffPop(circularBuffer *cb, uint8_t *data); | ||
|
|
||
| #endif /* INC_CIRC_BUFFER_H_ */ |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * m8n.h | ||
| * | ||
| * Created on: 16 มี.ค. 2569 | ||
| * Author: pkuna | ||
| */ | ||
|
|
||
| #ifndef SRC_M8N_H_ | ||
| #define SRC_M8N_H_ | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdint.h> | ||
| #include <stdbool.h> | ||
| #include "circ_buffer.h" | ||
|
|
||
| uint8_t gpsData = 0x55; | ||
| volatile uint64_t CAN_TX; | ||
| int gpsDataSize = 128; | ||
|
|
||
| circBuffDef(gpsBuffer, 1000); | ||
|
|
||
| #endif /* SRC_M8N_H_ */ |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * circ_buffer.c | ||
| * | ||
| * Created on: 16 มี.ค. 2569 | ||
| * Author: pkuna | ||
| */ | ||
|
|
||
| #include "circ_buffer.h" | ||
|
|
||
| bool checkCircBuff(circularBuffer *cb) { | ||
| if (cb->head == cb->tail) { | ||
| return true; | ||
| } else if ((cb->head + 1) == cb->tail) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| bool circBuffPush(circularBuffer *cb, uint8_t data) { | ||
| if (checkCircBuff(cb)) { | ||
| int next = cb->head + 1; | ||
| if (next >= cb->maxLen) { | ||
| next = 0; | ||
| } | ||
| cb->buffer[cb->head] = data; | ||
| cb->head = next; | ||
|
|
||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| bool circBuffPop(circularBuffer *cb, uint8_t *data) { | ||
| if (!checkCircBuff(cb)) { | ||
| int next = cb->tail + 1; | ||
| if (next >= cb->maxLen) { | ||
| next = 0; | ||
| } | ||
| *data = cb->buffer[cb->tail]; | ||
| cb->tail = next; | ||
|
|
||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * m8n.c | ||
| * | ||
| * Created on: 16 มี.ค. 2569 | ||
| * Author: pkuna | ||
| */ | ||
|
|
||
| #include "main.h" | ||
| #include "m8n.h" | ||
|
|
||
| int readData(void) { | ||
| while (1) { | ||
| /* UART data receiver */ | ||
| HAL_UART_Receive(&huart2, &gpsData, gpsDataSize, 1000); | ||
| HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_2); | ||
| HAL_Delay(1000); | ||
|
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. This HAL_Delay has potential of causing massive errors cause you would be processing data at just about every 2 seconds which is definitely too slow for NMEA data coming in at 9600 baud, aka we might lose data cause of the HAL_Delay. |
||
|
|
||
| if (circBuffPush(&gpsBuffer, gpsData)) { | ||
| printf("yes"); | ||
| } | ||
|
|
||
| if (circBuffPop(&gpsBuffer, &CAN_TX)) { | ||
| printf("yes"); | ||
| } | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@Vava-2549 there seems to be a wrap around bug here. The full detection check is missing a modulo, so it breaks when the head is at the last index. Assume a 5 bit buffer, in your code if the head is at the last index (indicating full), 4 + 1 doesnot equal 0 (aka your tail is at 0). I think you might need (head + 1) % 5 here, so that (4 + 1 % 5) == 0 indicating the buffer is full. But if you referenced this code from somewhere do confirm and let me know