diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c1dcd23 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "g++", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/bin/test", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "all", + "miDebuggerPath": "/usr/bin/gdb" + } + ] +} \ No newline at end of file diff --git a/Makefile b/Makefile index 3a4ccb1..8ee189c 100644 --- a/Makefile +++ b/Makefile @@ -2,23 +2,14 @@ CROSS_COMPILE ?= TARGET_FILE ?= test -C_FLAGS += -Wall -Werror -DGNU -D__GNU__ -D__GCC__ -C_FLAGS += -Os -C_FLAGS += -ffunction-sections -C_FLAGS += -fdata-sections -C_FLAGS += -std=c18 -C_FLAGS += -pedantic -C_FLAGS += -pedantic-errors -C_FLAGS += -ggdb3 -C_FLAGS += -Iinclude - -CXXFLAGS += -Os +CXXFLAGS += -O0 CXXFLAGS += -ggdb3 CXXFLAGS += -std=c++17 CXXFLAGS += -pedantic CXXFLAGS += -pedantic-errors CXXFLAGS += -ffunction-sections -fdata-sections CXXFLAGS += -fno-implicit-inline-templates +CXXFLAGS += -Iinc LD_LIBS := -lc -lgcc -lpthread @@ -62,5 +53,5 @@ define makedep endef ifneq ($(MAKECMDGOALS),clean) --include $($(subst .o,.d,$(OBJS)) +-include $(subst .o,.d,$(OBJS)) endif diff --git a/inc/ringbuffer.h b/inc/ringbuffer.h new file mode 100644 index 0000000..19639cb --- /dev/null +++ b/inc/ringbuffer.h @@ -0,0 +1,24 @@ +#ifndef __INC_RINGBUFFER_H__ +#define __INC_RINGBUFFER_H__ + +#include + +class ringbuffer +{ +public: + ringbuffer(); + + int write(char); + int read(char&); + +private: + enum { + MAX_BUFFER_SIZE = 2 + }; + + std::array buffer; + std::array::iterator head; + std::array::iterator tail; +}; + +#endif diff --git a/src/main.cc b/src/main.cc index 3546f57..d2645ee 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,28 +1,23 @@ #include -#include +#include "ringbuffer.h" -void *fake_it(void *param) -{ - std::cout << "Hello " << __FUNCTION__ << "\r\n"; - return NULL; -} - -void *poller(void *param) -{ - std::cout << "Hello " << __FUNCTION__ << "\r\n"; - return NULL; -} +ringbuffer buf; int main(void) { - pthread_t it_thread, poll_thread; - - pthread_create(&it_thread, NULL, fake_it, NULL); - pthread_create(&poll_thread, NULL, poller, NULL); - std::cout << "Hello World!!!" << "\r\n"; - - pthread_join(it_thread, NULL); - pthread_join(poll_thread, NULL); + buf.write('a'); + buf.write('b'); + buf.write('c'); + char c; + if(buf.read(c)) { + std::cout << c; + } + if(buf.read(c)) { + std::cout << c; + } + if(buf.read(c)) { + std::cout << c; + } return 0; } diff --git a/src/ringbuffer.cc b/src/ringbuffer.cc new file mode 100644 index 0000000..cc74f64 --- /dev/null +++ b/src/ringbuffer.cc @@ -0,0 +1,39 @@ +#include "ringbuffer.h" + +ringbuffer::ringbuffer() +{ + head = buffer.begin(); + tail = buffer.begin(); +} + +int ringbuffer::write(char c) +{ + if(head - tail == MAX_BUFFER_SIZE) { + return 0; + } + + if(head == buffer.end()) { + head = buffer.begin(); + } + + *head = c; + head++; + + return 1; +} + +int ringbuffer::read(char& c) +{ + if(head - tail == 0) { + return 0; + } + + if(tail == buffer.end()) { + tail = buffer.begin(); + } + + c = *tail; + tail++; + + return 1; +}