diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index f5ec7d291..65a3cab9f 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -612,18 +612,104 @@ int http_fn_flash_read_tool(http_request_t *request) { poststr(request, NULL); return 0; } +#if PLATFORM_BK7231T | PLATFORM_BK7231N +void test_ty_read_uart_data_to_buffer(int port, void* param) +{ + int rc = 0; + + while((rc = uart_read_byte(port)) != -1) + { + // if(__ty_uart_read_data_size(port) < (ty_uart[port].buf_len-1)) + // { + // ty_uart[port].buf[ty_uart[port].in++] = rc; + /// if(ty_uart[port].in >= ty_uart[port].buf_len){ + /// ty_uart[port].in = 0; + /// } + // } + } + +} +#endif +#if PLATFORM_BK7231T | PLATFORM_BK7231N +#include "../../beken378/func/user_driver/BkDriverUart.h" +#endif + +void TuyaMCU_Bridge_InitUART(int baud) { +#if PLATFORM_BK7231T | PLATFORM_BK7231N + bk_uart_config_t config; + + config.baud_rate = 9600; + config.data_width = 0x03; + config.parity = 0; //0:no parity,1:odd,2:even + config.stop_bits = 0; //0:1bit,1:2bit + config.flow_control = 0; //FLOW_CTRL_DISABLED + config.flags = 0; + + bk_uart_initialize(0, &config, NULL); + bk_uart_set_rx_callback(0, test_ty_read_uart_data_to_buffer, NULL); +#else + + +#endif +} +void TuyaMCU_Bridge_SendUARTByte(byte b) { +#if PLATFORM_BK7231T | PLATFORM_BK7231N + bk_send_byte(0, b); +#elif WINDOWS + // STUB - for testing + printf("%02X", b); +#else + + +#endif +} + + +void TuyaMCU_Send(byte *data, int size) { + int i; + unsigned char check_sum; + + TuyaMCU_Bridge_InitUART(9600); + + check_sum = 0; + for(i = 0; i < size; i++) { + byte b = data[i]; + check_sum += b; + TuyaMCU_Bridge_SendUARTByte(b); + } + TuyaMCU_Bridge_SendUARTByte(check_sum); + + printf("\nWe sent %i bytes to Tuya MCU\n",size+1); +} int http_fn_uart_tool(http_request_t *request) { char tmpA[256]; - + byte results[128]; + int resultLen = 0; http_setup(request, httpMimeTypeHTML); poststr(request,htmlHeader); poststr(request,g_header); poststr(request,"

UART Tool

"); - + + if(http_getArg(request->url,"data",tmpA,sizeof(tmpA))) { - hprintf128(request,"

Sent %s!

",tmpA); + if(0){ + TuyaMCU_Send(tmpA, strlen(tmpA)); + // bk_send_string(0,tmpA); + } else { + byte b; + const char *p; + + p = tmpA; + while(*p) { + b = hexbyte(p); + results[resultLen] = b; + resultLen++; + p+=2; + } + TuyaMCU_Send(results, resultLen); + } } else { strcpy(tmpA,"Hello UART world"); } diff --git a/src/new_cmd.c b/src/new_cmd.c new file mode 100644 index 000000000..5e26d864c --- /dev/null +++ b/src/new_cmd.c @@ -0,0 +1,119 @@ +#include "new_cmd.h" +#include "new_pins.h" +#include "new_cfg.h" +#include + +#define HASH_SIZE 128 + +static int generateHashValue(const char *fname) { + int i; + int hash; + char letter; + + hash = 0; + i = 0; + while (fname[i] != '\0') { + letter = tolower(fname[i]); + if (letter =='.') break; // don't include extension + if (letter =='\\') letter = '/'; // damn path names + hash+=(int)(letter)*(i+119); + i++; + } + hash = (hash ^ (hash >> 10) ^ (hash >> 20)); + hash &= (HASH_SIZE-1); + return hash; +} + +command_t *g_commands[HASH_SIZE]; + +void CMD_RegisterCommand(const char *name, const char *args, commandHandler_t handler) { + int hash; + command_t *newCmd; + + // check + newCmd = CMD_Find(name); + if(newCmd != 0) { + printf("ERROR: command with name %s already exists!\n",name); + return; + } + + hash = generateHashValue(name); + newCmd = (command_t*)malloc(sizeof(command_t)); + newCmd->argsFormat = args; + newCmd->handler = handler; + newCmd->name = name; + newCmd->next = g_commands[hash]; + g_commands[hash] = newCmd; +} + +command_t *CMD_Find(const char *name) { + int hash; + command_t *newCmd; + + hash = generateHashValue(name); + + newCmd = g_commands[hash]; + while(newCmd != 0) { + if(!stricmp(newCmd->name,name)) { + return newCmd; + } + } + return 0; +} + +#define MAX_CMD_LEN 512 +#define MAX_ARGS 32 + +static char g_buffer[MAX_CMD_LEN]; +static char *g_args[MAX_ARGS]; +static int g_numArgs = 0; + +bool isWhiteSpace(char ch) { + if(ch == ' ') + return true; + if(ch == '\t') + return true; + if(ch == '\n') + return true; + if(ch == '\r') + return true; + return false; +} +void CMD_ExecuteCommand(const char *s) { + char *p; + int i; + + while(isWhiteSpace(*s)) { + s++; + } + + strcpy(g_buffer,s); + p = g_buffer; + g_numArgs = 0; + g_args[g_numArgs] = p; + g_numArgs++; + while(*p != 0) { + if(isWhiteSpace(*p)) { + *p = 0; + if((p[1] != 0)) { + g_args[g_numArgs] = p+1; + g_numArgs++; + } + } + if(*p == ',') { + *p = 0; + g_args[g_numArgs] = p+1; + g_numArgs++; + } + p++; + } + + if(1){ + printf("Command parsed debug out! %i args\n",g_numArgs); + for(i = 0; i < g_numArgs; i++) { + printf("Arg %i is %s\n",i,g_args[i]); + } + } +} + + diff --git a/src/new_cmd.h b/src/new_cmd.h new file mode 100644 index 000000000..b80b157cc --- /dev/null +++ b/src/new_cmd.h @@ -0,0 +1,18 @@ + + +typedef void (*commandHandler_t)(); + +typedef struct command_s { + const char *name; + const char *argsFormat; + commandHandler_t handler; + struct command_s *next; +} command_t; + +command_t *CMD_Find(const char *name); +void CMD_RegisterCommand(const char *name, const char *args, commandHandler_t handler); +void CMD_ExecuteCommand(const char *s); + + + + diff --git a/src/win_main.c b/src/win_main.c index be1256da9..35d971fbd 100644 --- a/src/win_main.c +++ b/src/win_main.c @@ -61,6 +61,20 @@ void strcat_safe_test(){ misc_formatUpTimeString(24*60*60, timeStrF); misc_formatUpTimeString(24*60*60+60*60+50, timeStrG); misc_formatUpTimeString(100*24*60*60+60*60+15*60+50, timeStrH); + + // some command examples, compatible with Tasmota syntax + CMD_ExecuteCommand("TuyaSend3 108,ff0000646464ff"); + CMD_ExecuteCommand("TuyaSend4 103,2"); + CMD_ExecuteCommand("TuyaSend5 108, ABCD"); + CMD_ExecuteCommand("TuyaSend8"); + // TODO + //CMD_ExecuteCommand("Backlog Dimmer 10; Dimmer 100 "); + //CMD_ExecuteCommand("Backlog Status 1; Power2 on; Delay 20; Power2 off; Status 4"); + CMD_ExecuteCommand("Tuyqqqqqqqqqq arg1 arg2 arg3 arg4"); + CMD_ExecuteCommand("Tuyqqqqqqqqqq"); + CMD_ExecuteCommand("Tuyqqqqqqqqqq"); + CMD_ExecuteCommand("Tuyqqqqqqqqqq"); + CMD_ExecuteCommand("Tuyqqqqqqqqqq"); } int Time_getUpTimeSeconds() { return rand()% 100000;