Files
rcrf-wifi/rx_esp32/src/commands.c
2025-07-03 20:56:43 +02:00

215 lines
3.9 KiB
C

#include <CLI/CLI.h>
#include <CMDList/CMDList.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#include <string.h>
#include "commands.h"
#include "logger.h"
#include "utils.h"
#include "led.h"
#include "servos.h"
typedef enum {
BOAT_AVAILABLE,
BOAT_INCTRL,
BOAT_LOCKED
} boatStatus_t;
boatStatus_t BoatStatus = BOAT_AVAILABLE;
char* readInt(char* str, uint32_t* out)
{
*out = 0;
while ((*str >= '0') && (*str <= '9'))
{
*out *= 10;
*out += *str - '0';
str++;
}
return str;
}
int cmd_contrl(char* line, void* cli)
{
uint32_t ch_d[2];
int out = 0;
char msg[40];
char* arg = getNextArg(line, ':');
if (arg == NULL)
{
snprintf(&msg[0], 40, "no arguments given, expect 2\n");
CLI_stringOut((CLI_t*)cli, &msg[0]);
out = -1;
}
else
{
readInt(arg, &ch_d[0]);
arg = getNextArg(arg, ',');
if (arg == NULL)
{
snprintf(&msg[0], 40, "one argument given, expect 2\n");
CLI_stringOut((CLI_t*)cli, &msg[0]);
out = -2;
}
else
{
readInt(arg, &ch_d[1]);
servo_set(0, (uint8_t)ch_d[0]);
servo_set(1, (uint8_t)ch_d[1]);
snprintf(&msg[0], 40, "channel data: 0:%lu, 1:%lu\n", ch_d[0], ch_d[1]);
CLI_stringOut((CLI_t*)cli, &msg[0]);
}
}
return out;
}
// servotrim:0 410;614;819
int cmd_servotrim(char* line, void* cli)
{
uint8_t ch;
int out = 0;
char msg[40];
char* arg = getNextArg(line, ':');
if (arg == NULL)
{
snprintf(&msg[0], 40, "no arguments given, expect at least one\n");
CLI_stringOut((CLI_t*)cli, &msg[0]);
out = -1;
}
else
{
readInt(arg, &ch);
arg = getNextArg(arg, ' ');
if (arg != NULL)
{
readInt(arg, &Servos[ch].pulse_min);
arg = getNextArg(arg, ':');
readInt(arg, &Servos[ch].pulse_mid);
arg = getNextArg(arg, ':');
readInt(arg, &Servos[ch].pulse_max);
}
snprintf(&msg[0], 40, "servo ch %d: %d:%d:%d\n", Servos[ch].pulse_min, Servos[ch].pulse_mid, Servos[ch].pulse_max);
CLI_stringOut((CLI_t*)cli, &msg[0]);
}
return out;
}
extern volatile bool running;
int cmd_shutdown(char* line, void* cli)
{
CLI_stringOut((CLI_t*)cli, "goodby.\n");
running = false;
return INT_MIN;
}
int cmd_status(char* line, void* cli)
{
char* arg = getNextArg(line, ':');
int ret = 0;
if (arg != NULL)
{
if (strcmp(arg, "available") == 0)
{
BoatStatus = BOAT_AVAILABLE;
}
else if (strcmp(arg, "inctrl") == 0)
{
BoatStatus = BOAT_INCTRL;
}
else if (strcmp(arg, "locked") == 0)
{
BoatStatus = BOAT_LOCKED;
}
else
{
ret = -1;
}
}
char str[20];
switch (BoatStatus)
{
case BOAT_AVAILABLE:
snprintf(&str[0], 20, "status:available\n");
CLI_stringOut((CLI_t*)cli, &str[0]);
break;
case BOAT_INCTRL:
snprintf(&str[0], 20, "status:inctrl\n");
CLI_stringOut((CLI_t*)cli, &str[0]);
break;
case BOAT_LOCKED:
snprintf(&str[0], 20, "status:locked\n");
CLI_stringOut((CLI_t*)cli, &str[0]);
break;
}
return ret;
}
int setLed(char* line, void* cli)
{
uint32_t r, g, b;
char* arg = getNextArg(line, ':');
readInt(arg, &r);
arg = getNextArg(arg, ',');
readInt(arg, &g);
arg = getNextArg(arg, ',');
readInt(arg, &b);
led_setRGB(r, g, b);
char msg[30];
snprintf(&msg[0], 30, "led: r%lu g%lu n%lu\n", r, g, b);
CLI_stringOut((CLI_t*)cli, &msg[0]);
return 0;
}
int cmd_history(char* line, void* cli)
{
CLI_PrintHistory((CLI_t*)cli);
return 0;
}
int cmd_showlog(char* line, void* cli)
{
logger_printFullLog(cli);
return 0;
}
int cmd_clearlog(char* line, void* cli)
{
logger_clearBuffer();
return 0;
}
const CMD_t Commands[] = {
#if TARGET == TARGET_RX
{ "d", &cmd_contrl },
{ "status", &cmd_status },
{ "servotrim", &cmd_servotrim},
#elif TARGET == TARGET_TX
{ "led", &setLed },
#endif
{ "history", &cmd_history },
{ "shutdown", &cmd_shutdown },
{ "log", &cmd_showlog },
{ "logclear", &cmd_clearlog }
};
CMDList_t* getCMDList()
{
CMDList_t* list = CMDList_init();
for (int i = sizeof(Commands) / sizeof(CMD_t) - 1; i >= 0; i--)
{
CMDList_add(list, (CMD_t*)&Commands[i], Commands[i].cmd);
}
return list;
}