reflow-oven-control-sw/stm-firmware/shell.c

140 lines
3.6 KiB
C
Raw Normal View History

#include <shell.h>
#include <uart/uart.h>
#include <string.h>
#include <adc-meas.h>
#define xstr(x) str(x)
#define str(x) #x
#ifndef GIT_VER
#define GIT_VER "VERSION NOT SET"
#endif
static shellmatta_instance_t shell;
static char shell_buffer[512];
static char history_buffer[1024];
static shellmatta_retCode_t write_shell_callback(const char *data, uint32_t len)
{
uart_send_array(data, len);
return SHELLMATTA_OK;
}
static shellmatta_retCode_t shell_cmd_ver(const shellmatta_handle_t handle,
const char *arguments,
uint32_t length)
{
(void)arguments;
(void)length;
shellmatta_printf(handle, "Reflow Oven Controller Firmware %s", xstr(GIT_VER));
return SHELLMATTA_OK;
}
static shellmatta_retCode_t shell_cmd_pt1000_res(const shellmatta_handle_t handle,
const char *arguments,
uint32_t length)
{
(void)arguments;
(void)length;
float resistance;
int pt1000_status;
const char *display_status;
enum adc_pt1000_error pt1000_flags;
const char *status_text[] = {"VALID", "WATCHDOG", "DATA-OVERFLOW", "UNSTABLE"};
display_status = status_text[0];
pt1000_status = adc_pt1000_get_current_resistance(&resistance);
if (pt1000_status == 2) {
display_status = status_text[3];
} else if (pt1000_status) {
pt1000_flags = adc_pt1000_check_error();
if (pt1000_flags & ADC_PT1000_WATCHDOG_ERROR)
display_status = status_text[1];
else if (pt1000_flags & ADC_PT1000_OVERFLOW)
display_status = status_text[2];
}
shellmatta_printf(handle, "PT1000 resistance: %.2f Ohm [%s]\r\n", resistance, display_status);
return SHELLMATTA_OK;
}
static shellmatta_retCode_t shell_cmd_clear_error_status(const shellmatta_handle_t handle,
const char *arguments,
uint32_t length)
{
adc_pt1000_clear_error();
return SHELLMATTA_OK;
}
//typedef struct shellmatta_cmd
//{
// char *cmd; /**< command name */
// char *cmdAlias; /**< command alias */
// char *helpText; /**< help text to print in "help" command */
// char *usageText; /**< usage text to print on parameter error */
// shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callack function */
// struct shellmatta_cmd *next; /**< pointer to next command or NULL */
//} shellmatta_cmd_t;
static shellmatta_cmd_t cmd[3] = {
{
.cmd = "version",
.cmdAlias = "ver",
.helpText = "Print firmware version",
.usageText = NULL,
.cmdFct = shell_cmd_ver,
.next = &cmd[1],
},
{
.cmd = "pt1000",
.cmdAlias = "pt",
.helpText = "Get current filtered and calibrated PT1000 resistance",
.usageText = NULL,
.cmdFct = shell_cmd_pt1000_res,
.next = &cmd[2],
},
{
.cmd = "pt1000-clear-error",
.cmdAlias = "pt-clear",
.helpText = "Clear error status of PT1000 reading",
.usageText = NULL,
.cmdFct = shell_cmd_clear_error_status,
.next = NULL,
},
};
shellmatta_handle_t shell_init(void)
{
shellmatta_handle_t handle;
shellmatta_retCode_t ret;
ret = shellmatta_doInit(&shell, &handle, shell_buffer, sizeof(shell_buffer), history_buffer, sizeof(history_buffer),
"Enter command:\r\n", cmd, write_shell_callback);
if (ret != SHELLMATTA_OK)
handle = NULL;
return handle;
}
void shell_handle_input(shellmatta_handle_t shell, const char *data, size_t len)
{
if (!shell)
return;
shellmatta_processData(shell, (char *)data, (uint32_t)len);
}
void shell_print_string(shellmatta_handle_t shell, const char *string)
{
if (!shell)
return;
shellmatta_write(shell, (char *)string, strlen(string));
}