Compare commits

...

14 Commits

Author SHA1 Message Date
Dennis Boekholtz
58d08e2951 Merge branch 'master' into Dennis_buffer 2024-04-12 17:48:47 +02:00
c9fdbac547 Merge branch 'finley' 2024-04-12 17:17:49 +02:00
cbe0bcbdd7 add CMakeList for librarys 2024-04-12 15:01:52 +02:00
8433dbf42a rename lineout function to char out 2024-04-11 17:53:13 +02:00
Dennis Boekholtz
36a442b42b Merge branch 'master' into Dennis_buffer 2024-04-10 16:17:36 +02:00
21cc50e4a7 update backspace 2024-04-08 15:45:40 +02:00
c82862201b Merge branch 'finley' 2024-04-08 15:28:29 +02:00
24738ebeea fix pop from FIFOBuff and fix warnings for CLI 2024-04-08 15:08:10 +02:00
e6d9bb1da6 update CLI 2024-04-08 14:37:21 +02:00
b08f1afe7f add pop function in FIFOBuff 2024-04-08 14:36:53 +02:00
feec2a0a91 update CLI 2024-04-08 13:39:24 +02:00
87ca5d0e72 Merge branch 'master' into finley 2024-04-08 11:03:44 +02:00
3dbe121389 first draft of CLI 2024-04-06 22:14:53 +02:00
Dennis Boekholtz
2e66ebe1f7 Merge branch 'Dennis_buffer' 2024-04-06 21:14:03 +02:00
7 changed files with 305 additions and 6 deletions

161
CLI/CLI.c Normal file
View File

@ -0,0 +1,161 @@
#include "CLI.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include "../FIFOBuff/FIFOBuffChar.h"
CLI_charOutFn CLI_charOut;
CMDList_t* CMDList;
FIFOBuffChar_t* FIFO;
// initilize and register the lineout print function
bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList)
{
CLI_charOut = lineOut;
CMDList = cmdList;
FIFO = FIFOBuffChar_create();
if (CLI_charOut != NULL)
{
(*CLI_charOut)(">");
(*CLI_charOut)(" ");
}
return true;
}
bool CLI_deinit()
{
CLI_charOut = NULL;
CMDList = NULL;
FIFOBuffChar_delete(FIFO);
return true;
}
char* fifoToString(FIFOBuffChar_t* fifo)
{
char* out = malloc(fifo->size + 1);
char* write_p = out;
for (int i = fifo->size; i > 0; i--)
{
FIFOBuffChar_get(fifo, write_p);
write_p++;
}
*write_p = 0;
return out;
}
int tryExecute(FIFOBuffChar_t* fifo)
{
char* line = fifoToString(fifo);
CMD_t* cmd = CMDList_get(CMDList, line);
if (cmd != NULL)
{
(*(cmd->fn))(line);
if (CLI_charOut != NULL)
{
(*CLI_charOut)(">");
(*CLI_charOut)(" ");
}
return true;
}
else
{
if (CLI_charOut != NULL)
{
char err[100];
sprintf(&err[0], "command not found: %s\n> ", line);
for (int i=0; err[i] != 0; i++)
{
char c[2] = {err[i], 0};
(*CLI_charOut)(&c[0]);
}
}
return false;
}
}
// to recive a single caracter
bool CLI_charIn(char c)
{
bool ok = true;
if ((c >= 'a') && (c <= 'z'))
{
c &= (~0x20); // convert to uppercase
}
if ( //TODO: update list of accepted characters
((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))
|| (c == ' ')
|| (c == '-')
|| (c == '_')
)
{
FIFOBuffChar_put(FIFO, c); // save char in buffer
if (CLI_charOut != NULL)
{ // echo to terminal
(*CLI_charOut)(&c);
}
}
else
{
char str[100];
switch (c)
{
case '\n':
case '\r':
if (CLI_charOut != NULL)
{ // echo to terminal
(*CLI_charOut)(&c);
}
FIFOBuffChar_t* fifo = FIFO;
FIFO = FIFOBuffChar_create();
ok = tryExecute(fifo);
FIFOBuffChar_delete(fifo);
break;
case 127: // backspace
if (FIFOBuffChar_pop(FIFO))
{
(*CLI_charOut)("\x1b");
(*CLI_charOut)("[");
(*CLI_charOut)("D");
(*CLI_charOut)(" ");
(*CLI_charOut)("\x1b");
(*CLI_charOut)("[");
(*CLI_charOut)("D");
}
break;
case 27: // escape (start for arrow keys)
sprintf(&str[0], "\ninvlid char: ESC - (%i)\n", c);
for (int i=0; str[i] != 0; i++)
{
char ch[2] = {str[i], 0};
(*CLI_charOut)(&ch[0]);
}
break;
default:
sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c);
for (int i=0; str[i] != 0; i++)
{
char ch[2] = {str[i], 0};
(*CLI_charOut)(&ch[0]);
}
break;
}
}
return ok;
}

View File

@ -5,10 +5,10 @@
#include "../CMDList/CMDList.h"
typedef CLI_lineOutFn(char* line) CLI_lineoutFn_t;
typedef int (*CLI_charOutFn)(const char* line);
// initilize and register the lineout print function
bool CLI_init(CLI_lineoutFn_t* lineOut, CMDList_t* cmdList);
bool CLI_init(CLI_charOutFn lineOut, CMDList_t* cmdList);
bool CLI_deinit();
// to recive a single caracter

75
CMDList/printList.c Normal file
View File

@ -0,0 +1,75 @@
#ifndef PRINTCMDLIST_H
#define PRINTCMDLIST_H
#include "CMDList.h"
#include "printList.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
void printCMD(CMDList_t* list, char c, char* indent)
{
char* cmd;
if (list->cmd == NULL)
{
cmd = "(no cmd)";
}
else
{
cmd = list->cmd->cmd;
}
printf("%s%c -> %s\n", indent, c, cmd);
char* indent_p = indent;
while (*indent_p != 0)
{
indent_p++;
}
*indent_p = ' ';
*(indent_p+1) = ' ';
*(indent_p+2) = 0;
printCMDList(list, indent);
*indent_p = 0;
}
void printCMDList(CMDList_t* list, char* indent)
{
if (indent == NULL)
{
indent = (char*)malloc(100 * sizeof(char));
*indent = 0;
}
if (list->a != NULL) printCMD((CMDList_t*)list->a, 'a', indent);
if (list->b != NULL) printCMD((CMDList_t*)list->b, 'b', indent);
if (list->c != NULL) printCMD((CMDList_t*)list->c, 'c', indent);
if (list->d != NULL) printCMD((CMDList_t*)list->d, 'd', indent);
if (list->e != NULL) printCMD((CMDList_t*)list->e, 'e', indent);
if (list->f != NULL) printCMD((CMDList_t*)list->f, 'f', indent);
if (list->g != NULL) printCMD((CMDList_t*)list->g, 'g', indent);
if (list->h != NULL) printCMD((CMDList_t*)list->h, 'h', indent);
if (list->i != NULL) printCMD((CMDList_t*)list->i, 'i', indent);
if (list->j != NULL) printCMD((CMDList_t*)list->j, 'j', indent);
if (list->k != NULL) printCMD((CMDList_t*)list->k, 'k', indent);
if (list->l != NULL) printCMD((CMDList_t*)list->l, 'l', indent);
if (list->m != NULL) printCMD((CMDList_t*)list->m, 'm', indent);
if (list->n != NULL) printCMD((CMDList_t*)list->n, 'n', indent);
if (list->o != NULL) printCMD((CMDList_t*)list->o, 'o', indent);
if (list->p != NULL) printCMD((CMDList_t*)list->p, 'p', indent);
if (list->q != NULL) printCMD((CMDList_t*)list->q, 'q', indent);
if (list->r != NULL) printCMD((CMDList_t*)list->r, 'r', indent);
if (list->s != NULL) printCMD((CMDList_t*)list->s, 's', indent);
if (list->t != NULL) printCMD((CMDList_t*)list->t, 't', indent);
if (list->u != NULL) printCMD((CMDList_t*)list->u, 'u', indent);
if (list->v != NULL) printCMD((CMDList_t*)list->v, 'v', indent);
if (list->w != NULL) printCMD((CMDList_t*)list->w, 'w', indent);
if (list->x != NULL) printCMD((CMDList_t*)list->x, 'x', indent);
if (list->y != NULL) printCMD((CMDList_t*)list->y, 'y', indent);
if (list->z != NULL) printCMD((CMDList_t*)list->z, 'z', indent);
}
#endif

8
CMDList/printList.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef PRINTCMDLIST_H
#define PRINTCMDLIST_H
#include "CMDList.h"
extern void printCMDList(CMDList_t* list, char* indent);
#endif

11
CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
# FIFO Buffer
add_library(FIFOBuffChar FIFOBuff/FIFOBuffChar.c)
# CMD List
add_library(CMDList CMDList/CMDList.c)
add_library(CMDListPrint CMDList/printList.c)
# CLI
add_library(CLI CLI/CLI.c)
target_link_libraries(CLI CMDList FIFOBuffChar)

View File

@ -1,9 +1,8 @@
#include "FIFOBuffChar.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include "FIFOBuffChar.h"
bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration
{
@ -122,6 +121,48 @@ bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function para
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;

View File

@ -32,7 +32,10 @@ extern bool FIFOBuffChar_put(FIFOBuffChar_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 FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p); //moet dit "char" zijn of "uint8_t"
extern bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p);
// pop last added value from the buffer
extern bool FIFOBuffChar_pop(FIFOBuffChar_t *fifo);
// returns false if last put fails to gain memory and no get is called afterwards or true otherwise
extern bool FIFOBuffChar_is_full(FIFOBuffChar_t *fifo);