From a784f3c33f2df55b587019e03828f7dc88221b1f Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Wed, 29 Nov 2023 00:31:28 +0100 Subject: [PATCH] tcp_cmd create wrapper for tcp_write function create print header and help function --- project/Core/Src/tcp_cmd.c | 58 +++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/project/Core/Src/tcp_cmd.c b/project/Core/Src/tcp_cmd.c index 527b23a..a37219c 100644 --- a/project/Core/Src/tcp_cmd.c +++ b/project/Core/Src/tcp_cmd.c @@ -16,6 +16,28 @@ static void tcp_cmd_close(struct tcp_pcb* pcb) { tcp_close(pcb); } +void tcp_cmd_write(struct tcp_pcb* pcb, const char* str) { + tcp_write(pcb, str, strlen(str), TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + tcp_output(pcb); +} + +void tcp_cmd_print_header(struct tcp_pcb* pcb) { + tcp_cmd_write(pcb, " Welcome bij de TCP CMD Interface\r\n" + "(Typ help voor een lijst van de commando's! X om te sluiten)\r\n" + "============================================================\r\n" + "User: "); +} + +void tcp_cmd_print_help(struct tcp_pcb* pcb) { + tcp_cmd_write(pcb, "help : laat lijst zien met alle commando's\r\n" + "text : geeft tekst mee die op LCD komt (uw_text)\r\n" + "color : kleur achtergrond van scherm (255 255 255)\r\n" + "textColor : kleur van tekst (255 255 255)\r\n" + "listImages: laat een lijst zien van de mogelijke afbeeldingen\r\n" + "setImage : veranderd te afbeelding (naam_afbeelding)\r\n" + "exit : sluit de verbinding\r\n"); +} + static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) { size_t i; size_t len; @@ -37,6 +59,7 @@ static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t char* endptr; + LWIP_UNUSED_ARG(arg); if (err == ERR_OK && p != NULL) { tcp_recved(pcb, p->tot_len); @@ -46,14 +69,7 @@ static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t if (!strncmp(pc, "help", 4)) { check = 1; - tcp_write(pcb, "help : laat lijst zien met alle commando's\r\n" - "text : geeft tekst mee die op LCD komt (uw_text)\r\n" - "color : kleur achtergrond van scherm (255 255 255)\r\n" - "textColor : kleur van tekst (255 255 255)\r\n" - "listImages: laat een lijst zien van de mogelijke afbeeldingen\r\n" - "setImage : veranderd te afbeelding (naam_afbeelding)\r\n" - "exit : sluit de verbinding\r\n", 354, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); - tcp_output(pcb); + tcp_cmd_print_help(pcb); } else if (!strncmp(pc, "text ", 5)) { for (i = 0; i < len - 4; i++) { text[i] = pc[i + 5]; @@ -91,12 +107,11 @@ static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t llfs_file_t file_list[number_of_files]; - for (size_t i = 0; i < number_of_files; i++) { - tcp_write(pcb, file_list[i].name, strlen(file_list[i].name), TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); - tcp_write(pcb, "\r\n", 2, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); - } - } else { - tcp_write(pcb, "NO files in filesystem\r\n", 24, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + number_of_files = llfs_file_list(file_list, number_of_files, NULL); + + for (size_t i = 0; i < number_of_files; i++) { + tcp_cmd_write(pcb, file_list[i].name); + tcp_cmd_write(pcb, "\r\n"); } check = 1; @@ -133,9 +148,9 @@ static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t lcd_clear_images(); lcd_draw_gif_from_fs(filename, 10, 10); } else if (!file_in_fs) { - tcp_write(pcb, "File NOT in filesystem\n\r", 24, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + tcp_cmd_write(pcb, "File NOT in filesystem\n\r"); } else { - tcp_write(pcb, "Extension NOT supported\n\r", 25, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + tcp_cmd_write(pcb, "Extension NOT supported\n\r"); } check = 1; @@ -147,7 +162,7 @@ static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t } if (!check && (strncmp(pc, "\r\n", 2) != 0)) { - tcp_write(pcb, "Onbestaand commando: help voor lijst van commando's\r\n", 53, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + tcp_cmd_write(pcb, "Onbestaand commando: help voor lijst van commando's\r\n"); } pbuf_free(p); @@ -164,7 +179,7 @@ static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t tcp_cmd_close(pcb); } if (strncmp(pc, "\r\n", 2) != 0) { - tcp_write(pcb, "User: ", 6, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + tcp_cmd_write(pcb, "User: "); } return ERR_OK; @@ -177,11 +192,8 @@ static err_t tcp_cmd_accept(void* arg, struct tcp_pcb* pcb, err_t err) { tcp_recv(pcb, tcp_cmd_recv); tcp_err(pcb, NULL); tcp_poll(pcb, NULL, 4); - tcp_write(pcb, " Welcom bij de TCP CMD Interface\r\n" - "(Typ help voor een lijst van de commando's! X om te sluiten)\r\n" - "============================================================\r\n" - "User: ", 168, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); - tcp_sent(pcb, NULL); + + tcp_cmd_print_header(pcb); return ERR_OK; }