Added cmd.c and cmd.h

Functions in main copied to cmd.c
This commit is contained in:
Roelandts_Gert
2023-11-13 12:55:23 +01:00
committed by Sander Speetjens
parent e5273c7012
commit 9f25995c82
3 changed files with 182 additions and 0 deletions

15
project/Core/Inc/cmd.h Normal file
View File

@@ -0,0 +1,15 @@
/*
* cmd.h
*
* Created on: 13 Nov 2023
* Author: gertr
*/
#ifndef INC_CMD_H_
#define INC_CMD_H_
void echo_init( void );
#endif /* INC_CMD_H_ */

163
project/Core/Src/cmd.c Normal file
View File

@@ -0,0 +1,163 @@
/*
* cmd.c
*
* Created on: 13 Nov 2023
* Author: gertr
*/
#include "cmd.h"
#include "log.h"
#include <tcp.h>
#include <stdio.h>
#include <string.h>
static void echo_close (struct tcp_pcb *pcb )
{
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_close(pcb);
}
static err_t echo_recv( void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err )
{
int i;
int len;
u16_t len_text;
char *pc;
int check = 0;
char tcp_buffer[1024];
char text[256];
char color[11];
char textColor[11];
if ( err == ERR_OK && p != NULL )
{
tcp_recved( pcb, p->tot_len );
pc = (char *)p->payload;
len =p->tot_len;
len_text = len - 3;
for( i=0; i<len; i++ )
{
tcp_buffer[i] = pc[i];
}
if (strncmp(tcp_buffer, "help", 4) == 0) {
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, 0 );
tcp_output(pcb);
}
else if( strncmp(tcp_buffer, "text ", 5) == 0 ) {
for (i = 0; i < len - 4; i++){
text[i] = tcp_buffer[i + 5];
}
text[i-1]='\r';
text[i]='\n';
text[i+1]='\0';
tcp_write( pcb, text, len_text, 0);
tcp_output(pcb);
check = 1;
}
else if( strncmp(tcp_buffer, "color", 5) == 0 ) {
for (i = 0; i < 3; i++){
color[i] = tcp_buffer[i + 6];
color[i+3] = tcp_buffer[i + 10];
color[i+6] = tcp_buffer[i + 14];
}
color[9]='\r';
color[10]='\n';
tcp_write( pcb, color, 11, 0);
tcp_output(pcb);
check = 1;
}
else if( strncmp(tcp_buffer, "textColor", 9) == 0 ) {
for (i = 0; i < 3; i++){
textColor[i] = tcp_buffer[i + 10];
textColor[i+3] = tcp_buffer[i + 14];
textColor[i+6] = tcp_buffer[i + 18];
}
textColor[9]='\r';
textColor[10]='\n';
tcp_write( pcb, textColor, 11, 0);
tcp_output(pcb);
check = 1;
}
else if( strcmp(tcp_buffer, "listImages") == 0 ) {
check = 1;
}
else if( strcmp(tcp_buffer, "setImages") == 0 ) {
check = 1;
}
else if( strcmp(tcp_buffer, "exit") == 0 ) {
echo_close( pcb );
check = 1;
}
if(check == 0 && (strncmp(tcp_buffer, "\r\n", 2) != 0 )) {
tcp_write( pcb, "Onbestaand commando: help voor lijst van commando's\r\n", 53, 0 );
}
pbuf_free( p );
if( len > tcp_sndbuf( pcb ) ) {
len= tcp_sndbuf( pcb );
}
//tcp_write( pcb, tcp_buffer, len, 0 );
tcp_sent( pcb, NULL );
}
else
{
pbuf_free( p );
}
if( err == ERR_OK && p == NULL )
{
echo_close( pcb );
}
if(strncmp(tcp_buffer, "\r\n", 2) != 0 ) {
tcp_write( pcb, "User: ", 6, 0 );
}
return ERR_OK;
}
static err_t echo_accept(void *arg, struct tcp_pcb *pcb, err_t err )
{
LWIP_UNUSED_ARG( arg );
LWIP_UNUSED_ARG( err );
tcp_setprio( pcb, TCP_PRIO_MIN );
tcp_recv( pcb, echo_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, 0 );
tcp_sent( pcb, NULL );
return ERR_OK;
}
void echo_init( void )
{
struct tcp_pcb *tcp_pcb;
tcp_pcb = tcp_new();
tcp_bind(tcp_pcb, IP_ADDR_ANY, 23);
tcp_pcb = tcp_listen( tcp_pcb );
tcp_accept( tcp_pcb, echo_accept );
}

View File

@@ -35,6 +35,10 @@
#include "tcp_cmd.h"
#include "website_backend.h"
#include "cmd.h"
#include <tcp.h>
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/