From 49b3c85560c17e64e4d70125d9460608fa125922 Mon Sep 17 00:00:00 2001 From: FReenen Date: Tue, 14 May 2024 20:57:44 +0200 Subject: [PATCH] did some random stuff --- .gitmodules | 3 + rx_esp2866/lib/cli | 1 + rx_esp2866/src/boats.c | 233 ++++++++++++++++++++++++++++++++++++++ rx_esp2866/src/boats.h | 51 +++++++++ rx_esp2866/src/commands.c | 23 ++++ rx_esp2866/src/commands.h | 8 ++ 6 files changed, 319 insertions(+) create mode 100644 .gitmodules create mode 160000 rx_esp2866/lib/cli create mode 100644 rx_esp2866/src/boats.c create mode 100644 rx_esp2866/src/boats.h create mode 100644 rx_esp2866/src/commands.c create mode 100644 rx_esp2866/src/commands.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5ad83ac --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "rx_esp2866/lib/cli"] + path = rx_esp2866/lib/cli + url = git@gitea.finnvanreenen.nl:HR/EMS31_submodules.git diff --git a/rx_esp2866/lib/cli b/rx_esp2866/lib/cli new file mode 160000 index 0000000..34f830b --- /dev/null +++ b/rx_esp2866/lib/cli @@ -0,0 +1 @@ +Subproject commit 34f830bd1d23379f58b956f28cd6cd0171d46957 diff --git a/rx_esp2866/src/boats.c b/rx_esp2866/src/boats.c new file mode 100644 index 0000000..6801ad6 --- /dev/null +++ b/rx_esp2866/src/boats.c @@ -0,0 +1,233 @@ +#include "FIFOBuffChar.h" + +#include +#include +#include + +bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration +{ + bool ok = false; + if ((fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL)) + { + // buffer is empty. add first element + fifo->FirstEl_p = malloc(sizeof(FIFOBuffChar_element_t)); + if (fifo->FirstEl_p != NULL) + { + fifo->FirstEl_p->character = i; // "value" to "character" + fifo->FirstEl_p->nextElement = NULL; + + fifo->LastEl_p = fifo->FirstEl_p; + fifo->size = 1; + ok = true; + } + else + { + fifo->full = true; + ok = false; + } + } + else if ((fifo->LastEl_p != NULL) && (fifo->FirstEl_p != NULL)) + { + // add element to exsiting buffer + FIFOBuffChar_element_t* el = malloc(sizeof(FIFOBuffChar_element_t)); + if (el != NULL) + { + el->character = i; // "value" to "character" + el->nextElement = NULL; + fifo->LastEl_p->nextElement = el; + fifo->LastEl_p = el; + fifo->size++; + ok = true; + } + else + { + fifo->full = true; + ok = false; + } + } + else + { + // buffer is unhealthy. try to clear it and reinit + if (fifo->FirstEl_p == NULL) + { + printf("buffer_dyn - buffer_put(): ERROR: first pointer is NULL\n"); + } + //TODO: this while loop is unsave + while (fifo->FirstEl_p != NULL) + { + FIFOBuffChar_get(fifo, NULL); // Replace buffer_get with FIFOBuffChar_get + } + if (fifo->LastEl_p != NULL) + { + free(fifo->LastEl_p); + fifo->LastEl_p = NULL; + printf("buffer_dyn - buffer_put(): ERROR: last pointer is NULL\n"); + } + FIFOBuffChar_put(fifo, i); // Replace buffer_put with FIFOBuffChar_put + ok = false; + } + return ok; +} + +bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function parameters to match the declaration +{ + bool ok = false; + if (fifo->FirstEl_p != NULL) + { + *p = fifo->FirstEl_p->character; // "value" to "character" + FIFOBuffChar_element_t* next = fifo->FirstEl_p->nextElement; + free(fifo->FirstEl_p); + // fifo->FirstEl_p = NULL; + fifo->size--; + fifo->full = false; + if (next == NULL) + { + // this was the last element in the buffer + if (fifo->LastEl_p == fifo->FirstEl_p) + { + // clear last element becouse it's the same + fifo->LastEl_p = NULL; + fifo->FirstEl_p = NULL; + fifo->size = 0; + ok = true; + } + else if (fifo->LastEl_p != NULL) + { + // buffer chain is broken; skip to last element + fifo->FirstEl_p = fifo->LastEl_p; + printf("buffer_dyn - buffer_get(): ERROR: next element is NULL, but it's not the last element. skip to last element\n"); + ok = false; + } + else + { + // somehow the last element is NULL + fifo->size = 0; + fifo->FirstEl_p = NULL; + printf("buffer_dyn - buffer_get(): ERROR: last element pointer is NULL\n"); + ok = false; + } + } + else + { + // set first element pointer to next element + fifo->FirstEl_p = next; + ok = true; + } + } + else + { + ok = false; + } + return ok; +} + +bool FIFOBuffChar_pop(FIFOBuffChar_t *fifo) +{ + bool ok = true; + + switch (fifo->size){ + case 0: // buffer is empty + ok = false; + break; + + case 1: // singel item in buffer + // make buffer empty + free(fifo->FirstEl_p); + fifo->FirstEl_p = NULL; + fifo->LastEl_p = NULL; + fifo->size = 0; + ok = true; + break; + + default: // buffer is at least 2 element big + // find the second last element + { + FIFOBuffChar_element_t* secondLastEl = fifo->FirstEl_p; + while ( + secondLastEl->nextElement != fifo->LastEl_p + && ((FIFOBuffChar_element_t*)secondLastEl->nextElement)->nextElement != NULL + ){ + secondLastEl = secondLastEl->nextElement; + } + + // remove last element + free(fifo->LastEl_p); + + // update chain + secondLastEl->nextElement = NULL; + fifo->LastEl_p = secondLastEl; + fifo->size--; + ok = true; + break; + } + } + + return ok; +} + +bool FIFOBuffChar_is_full(FIFOBuffChar_t *fifo) +{ + return fifo->full; +} + +bool FIFOBuffChar_is_empty(FIFOBuffChar_t *fifo) +{ + // printf("empty %s: %s; fifo->LastEl_p = %i; fifo->FirstEl_p = %i\n", + // (fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL) ? "true" : "false", + // (fifo->LastEl_p == fifo->FirstEl_p) ? "same" : "diff", + // (int) fifo->LastEl_p, + // (int) fifo->FirstEl_p); + return ((fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL)); +} + + +FIFOBuffChar_t* FIFOBuffChar_create(void) +{ + + // Allocate memory for a new FIFOBuffChar_t + FIFOBuffChar_t* fifo = malloc(sizeof(FIFOBuffChar_t)); + + if (fifo == NULL) { + // Memory allocation failed + return NULL; + } + + // Initialize the FIFO buffer + // Point the last element to the first one + // Initialize other fields of the FIFOBuffChar_t struct + + fifo->FirstEl_p = NULL; + fifo->LastEl_p = NULL; + fifo->size = 0; + fifo->full = false; + + return fifo; + +} + + +bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo) +{ + + // Check if the buffer is already empty + if (fifo->FirstEl_p == NULL) + { + // Delete all elements in the buffer + FIFOBuffChar_element_t* currentElement = fifo->FirstEl_p; + while (currentElement != NULL) + { + FIFOBuffChar_element_t* nextElement = currentElement->nextElement; + free(currentElement); + currentElement = nextElement; + } + } + + free(fifo); + return true; +} + + +unsigned int FIFOBuffChar_getSize(FIFOBuffChar_t *fifo) +{ + return fifo->size; +} diff --git a/rx_esp2866/src/boats.h b/rx_esp2866/src/boats.h new file mode 100644 index 0000000..8008575 --- /dev/null +++ b/rx_esp2866/src/boats.h @@ -0,0 +1,51 @@ +#ifndef BOATS_h +#define BOATS_h + +#include +#include +#include + +typedef struct Boats_s { + uint16_t id; + char* name; + bool avaliable; + bool locked; +} Boats_t; + +typedef struct BoatsDB_el_s { + Boats_t boat; + void* nextEl; +} BoatsDB_el_t; + +// defines all vars for the buffer to operate +typedef struct BoatsDB_s { + BoatsDB_el_t* FirstEl_p; + BoatsDB_el_t* LastEl_p; + unsigned char size; + bool full; +} BoatsDB_t; + +// create a fifo buffer and initilizes it with zeros +extern BoatsDB_t* BoatsDB_init(); + +// destroy a fifo buffer (free up its space) +// returns true on success or false otherways +extern bool BoatsDB_deinit(BoatsDB_t* fifo); + +// put value i in buffer if there is still memory avaliable +// returns true on success or false otherways +extern bool BoatsDB_put(BoatsDB_t *fifo, char i); + +// get value from buffer and writes it to *p if buffer not empty +// returns true on success or false otherways +extern bool BoatsDB_getById(BoatsDB_t *fifo, uint16_t id, ); + +// returns false if last put fails to gain memory and no get is called afterwards or true otherwise +extern bool BoatsDB_is_full(BoatsDB_t *fifo); + +// returns true when buffer is empty or false otherways +extern bool BoatsDB_is_empty(BoatsDB_t *fifo); + +extern unsigned int BoatsDB_getSize(BoatsDB_t *fifo); + +#endif \ No newline at end of file diff --git a/rx_esp2866/src/commands.c b/rx_esp2866/src/commands.c new file mode 100644 index 0000000..c02f65b --- /dev/null +++ b/rx_esp2866/src/commands.c @@ -0,0 +1,23 @@ + +#include "../lib/cli/CMDList/CMDList.h" + +#include "commands.h" + +int boads(char* line) +{ + // do stuff +} + +const CMD_t Commands[] = { + { "ctrl", &test } +}; + +CMDList_t* getCMDList() +{ + CMDList_t* list = CMDList_init(); + for (int i = sizeof(Commands) / sizeof(CMD_t); i >= 0; i--) + { + CMDList_add(list, (CMD_t*)&Commands[i], Commands[i].cmd); + } + return list; +} diff --git a/rx_esp2866/src/commands.h b/rx_esp2866/src/commands.h new file mode 100644 index 0000000..f0fabdd --- /dev/null +++ b/rx_esp2866/src/commands.h @@ -0,0 +1,8 @@ +#ifndef COMMAND_H +#define COMMAND_H + +#include "../lib/cli/CMDList/CMDList.h" + +extern CMDList_t* getCMDList(); + +#endif