Also moves the tables into FLASH out of RAM.
#define DEBUG_PRINTF printf
struct ptentry {
- uint16_t command_cs;
+ const char *command;
void (* pfunc)(char *str, Shell *sh);
};
#define SHELL_PROMPT "> "
/*---------------------------------------------------------------------------*/
-bool Shell::parse(register char *str, struct ptentry *t)
+bool Shell::parse(register char *str, const struct ptentry *t)
{
- struct ptentry *p;
- for (p = t; p->command_cs != 0; ++p) {
- if (get_checksum(str) == p->command_cs) {
+ const struct ptentry *p;
+ for (p = t; p->command != 0; ++p) {
+ if (strncasecmp(str, p->command, strlen(p->command)) == 0) {
break;
}
}
p->pfunc(str, this);
- return p->command_cs != 0;
+ return p->command != 0;
}
/*---------------------------------------------------------------------------*/
static void help(char *str, Shell *sh)
}
}
/*---------------------------------------------------------------------------*/
-static struct ptentry parsetab[] = {
- {CHECKSUM("netstat"), connections},
- {CHECKSUM("exit"), quit},
- {CHECKSUM("quit"), quit},
- {CHECKSUM("test"), test},
- {CHECKSUM("?"), help},
+static const struct ptentry parsetab[] = {
+ {"netstat", connections},
+ {"exit", quit},
+ {"quit", quit},
+ {"test", test},
+ {"?", help},
/* Default action */
{0, unknown}
void setConsole();
private:
- bool parse(register char *str, struct ptentry *t);
+ bool parse(register char *str, const struct ptentry *t);
Telnetd *telnet; // telnet instance we are connected to
StreamOutput *pstream;
bool isConsole;
#define CONF_SD 2
#define CONF_EEPROM 3
-#define config_get_command_checksum CHECKSUM("config-get")
-#define config_set_command_checksum CHECKSUM("config-set")
-#define config_load_command_checksum CHECKSUM("config-load")
-
void Configurator::on_module_loaded()
{
this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
{
SerialMessage new_message = *static_cast<SerialMessage *>(argument);
- // ignore comments
- if(new_message.message[0] == ';') return;
+ // ignore comments and blank lines and if this is a G code then also ignore it
+ char first_char = new_message.message[0];
+ if(strchr(";( \n\rGMTN", first_char) != NULL) return;
string possible_command = new_message.message;
-
- // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
- uint16_t check_sum = get_checksum( possible_command.substr(0, possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
+ string cmd = shift_parameter(possible_command);
// Act depending on command
- if (check_sum == config_get_command_checksum)
- this->config_get_command( get_arguments(possible_command), new_message.stream );
- else if (check_sum == config_set_command_checksum)
- this->config_set_command( get_arguments(possible_command), new_message.stream );
- else if (check_sum == config_load_command_checksum)
- this->config_load_command( get_arguments(possible_command), new_message.stream );
+ if (cmd == "config_get"){
+ this->config_get_command( possible_command, new_message.stream );
+ } else if (cmd == "config_set"){
+ this->config_set_command( possible_command, new_message.stream );
+ } else if (cmd == "config_load"){
+ this->config_load_command( possible_command, new_message.stream );
+ }
}
// Process and respond to eeprom gcodes (M50x)
#include "PublicDataRequest.h"
#include "PlayerPublicAccess.h"
-#define play_command_checksum CHECKSUM("play")
-#define progress_command_checksum CHECKSUM("progress")
-#define abort_command_checksum CHECKSUM("abort")
#define on_boot_gcode_checksum CHECKSUM("on_boot_gcode")
#define on_boot_gcode_enable_checksum CHECKSUM("on_boot_gcode_enable")
}
}
-
// When a new line is received, check if it is a command, and if it is, act upon it
void Player::on_console_line_received( void *argument )
{
SerialMessage new_message = *static_cast<SerialMessage *>(argument);
- // ignore comments
- if(new_message.message[0] == ';') return;
+ // ignore comments and blank lines and if this is a G code then also ignore it
+ char first_char = new_message.message[0];
+ if(strchr(";( \n\rGMTN", first_char) != NULL) return;
string possible_command = new_message.message;
+ string cmd = shift_parameter(possible_command);
//new_message.stream->printf("Received %s\r\n", possible_command.c_str());
- // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
- unsigned short check_sum = get_checksum( possible_command.substr(0, possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
-
// Act depending on command
- if (check_sum == play_command_checksum)
- this->play_command( get_arguments(possible_command), new_message.stream );
- else if (check_sum == progress_command_checksum)
- this->progress_command(get_arguments(possible_command), new_message.stream );
- else if (check_sum == abort_command_checksum)
- this->abort_command(get_arguments(possible_command), new_message.stream );
+ if (cmd == "play"){
+ this->play_command( possible_command, new_message.stream );
+ }else if (cmd == "progress"){
+ this->progress_command( possible_command, new_message.stream );
+ }else if (cmd == "abort")
+ this->abort_command( possible_command, new_message.stream );
}
// Play a gcode file by considering each line as if it was received on the serial console
extern "C" uint32_t __malloc_free_list;
extern "C" uint32_t _sbrk(int size);
-#define get_temp_command_checksum CHECKSUM("temp")
-#define get_pos_command_checksum CHECKSUM("pos")
-
// command lookup table
-SimpleShell::ptentry_t SimpleShell::commands_table[] = {
- {CHECKSUM("ls"), &SimpleShell::ls_command},
- {CHECKSUM("cd"), &SimpleShell::cd_command},
- {CHECKSUM("pwd"), &SimpleShell::pwd_command},
- {CHECKSUM("cat"), &SimpleShell::cat_command},
- {CHECKSUM("rm"), &SimpleShell::rm_command},
- {CHECKSUM("reset"), &SimpleShell::reset_command},
- {CHECKSUM("dfu"), &SimpleShell::dfu_command},
- {CHECKSUM("break"), &SimpleShell::break_command},
- {CHECKSUM("help"), &SimpleShell::help_command},
- {CHECKSUM("?"), &SimpleShell::help_command},
- {CHECKSUM("version"), &SimpleShell::version_command},
- {CHECKSUM("mem"), &SimpleShell::mem_command},
- {CHECKSUM("get"), &SimpleShell::get_command},
- {CHECKSUM("set_temp"), &SimpleShell::set_temp_command},
- {CHECKSUM("net"), &SimpleShell::net_command},
- {CHECKSUM("load"), &SimpleShell::load_command},
- {CHECKSUM("save"), &SimpleShell::save_command},
+const SimpleShell::ptentry_t SimpleShell::commands_table[] = {
+ {"ls", SimpleShell::ls_command},
+ {"cd", SimpleShell::cd_command},
+ {"pwd", SimpleShell::pwd_command},
+ {"cat", SimpleShell::cat_command},
+ {"rm", SimpleShell::rm_command},
+ {"reset", SimpleShell::reset_command},
+ {"dfu", SimpleShell::dfu_command},
+ {"break", SimpleShell::break_command},
+ {"help", SimpleShell::help_command},
+ {"?", SimpleShell::help_command},
+ {"version", SimpleShell::version_command},
+ {"mem", SimpleShell::mem_command},
+ {"get", SimpleShell::get_command},
+ {"set_temp", SimpleShell::set_temp_command},
+ {"net", SimpleShell::net_command},
+ {"load", SimpleShell::load_command},
+ {"save", SimpleShell::save_command},
// unknown command
- {0, NULL}
+ {NULL, NULL}
};
+int SimpleShell::reset_delay_secs= 0;
+
// Adam Greens heap walk from http://mbed.org/forum/mbed/topic/2701/?page=4#comment-22556
static uint32_t heapWalk(StreamOutput *stream, bool verbose)
{
this->register_for_event(ON_GCODE_RECEIVED);
this->register_for_event(ON_SECOND_TICK);
- this->reset_delay_secs = 0;
+ reset_delay_secs = 0;
}
void SimpleShell::on_second_tick(void *)
{
// we are timing out for the reset
- if (this->reset_delay_secs > 0) {
- if (--this->reset_delay_secs == 0) {
+ if (reset_delay_secs > 0) {
+ if (--reset_delay_secs == 0) {
system_reset(false);
}
}
}
}
-bool SimpleShell::parse_command(unsigned short cs, string args, StreamOutput *stream)
+bool SimpleShell::parse_command(const char *cmd, string args, StreamOutput *stream)
{
- for (ptentry_t *p = commands_table; p->pfunc != NULL; ++p) {
- if (cs == p->command_cs) {
- PFUNC fnc= p->pfunc;
- (this->*fnc)(args, stream);
+ for (const ptentry_t *p = commands_table; p->command != NULL; ++p) {
+ if (strncasecmp(cmd, p->command, strlen(p->command)) == 0) {
+ p->func(args, stream);
return true;
}
}
{
SerialMessage new_message = *static_cast<SerialMessage *>(argument);
- // ignore comments
- if (new_message.message[0] == ';') return;
+ // ignore comments and blank lines and if this is a G code then also ignore it
+ char first_char = new_message.message[0];
+ if(strchr(";( \n\rGMTN", first_char) != NULL) return;
string possible_command = new_message.message;
//new_message.stream->printf("Received %s\r\n", possible_command.c_str());
-
- unsigned short check_sum = get_checksum( possible_command.substr(0, possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
+ string cmd = shift_parameter(possible_command);
// find command and execute it
- parse_command(check_sum, get_arguments(possible_command), new_message.stream);
+ parse_command(cmd.c_str(), possible_command, new_message.stream);
}
// Act upon an ls command
void SimpleShell::reset_command( string parameters, StreamOutput *stream)
{
stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
- this->reset_delay_secs = 5; // reboot in 5 seconds
+ reset_delay_secs = 5; // reboot in 5 seconds
}
// go into dfu boot mode
// used to test out the get public data events
void SimpleShell::get_command( string parameters, StreamOutput *stream)
{
- int what = get_checksum(shift_parameter( parameters ));
+ string what = shift_parameter( parameters );
void *returned_data;
- if (what == get_temp_command_checksum) {
+ if (what == "temp") {
string type = shift_parameter( parameters );
bool ok = THEKERNEL->public_data->get_value( temperature_control_checksum, get_checksum(type), current_temperature_checksum, &returned_data );
stream->printf("%s is not a known temperature device\r\n", type.c_str());
}
- } else if (what == get_pos_command_checksum) {
+ } else if (what == "pos") {
bool ok = THEKERNEL->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
if (ok) {
#include "Module.h"
+#include <functional>
#include <string>
using std::string;
void on_second_tick(void *);
private:
- void ls_command(string parameters, StreamOutput *stream );
- void cd_command(string parameters, StreamOutput *stream );
- void delete_file_command(string parameters, StreamOutput *stream );
- void pwd_command(string parameters, StreamOutput *stream );
- void cat_command(string parameters, StreamOutput *stream );
- void rm_command(string parameters, StreamOutput *stream );
- void break_command(string parameters, StreamOutput *stream );
- void reset_command(string parameters, StreamOutput *stream );
- void dfu_command(string parameters, StreamOutput *stream );
- void help_command(string parameters, StreamOutput *stream );
- void version_command(string parameters, StreamOutput *stream );
- void get_command(string parameters, StreamOutput *stream );
- void set_temp_command(string parameters, StreamOutput *stream );
- void mem_command(string parameters, StreamOutput *stream );
-
- void net_command( string parameters, StreamOutput *stream);
-
- void load_command( string parameters, StreamOutput *stream);
- void save_command( string parameters, StreamOutput *stream);
-
- bool parse_command(unsigned short cs, string args, StreamOutput *stream);
-
- typedef void (SimpleShell::*PFUNC)(string parameters, StreamOutput *stream);
+ static void ls_command(string parameters, StreamOutput *stream );
+ static void cd_command(string parameters, StreamOutput *stream );
+ static void delete_file_command(string parameters, StreamOutput *stream );
+ static void pwd_command(string parameters, StreamOutput *stream );
+ static void cat_command(string parameters, StreamOutput *stream );
+ static void rm_command(string parameters, StreamOutput *stream );
+ static void break_command(string parameters, StreamOutput *stream );
+ static void reset_command(string parameters, StreamOutput *stream );
+ static void dfu_command(string parameters, StreamOutput *stream );
+ static void help_command(string parameters, StreamOutput *stream );
+ static void version_command(string parameters, StreamOutput *stream );
+ static void get_command(string parameters, StreamOutput *stream );
+ static void set_temp_command(string parameters, StreamOutput *stream );
+ static void mem_command(string parameters, StreamOutput *stream );
+
+ static void net_command( string parameters, StreamOutput *stream);
+
+ static void load_command( string parameters, StreamOutput *stream);
+ static void save_command( string parameters, StreamOutput *stream);
+
+ bool parse_command(const char *cmd, string args, StreamOutput *stream);
+
+ typedef void (*PFUNC)(string parameters, StreamOutput *stream);
typedef struct {
- unsigned short command_cs;
- PFUNC pfunc;
- } ptentry_t;
+ const char *command;
+ const PFUNC func;
+ } const ptentry_t;
- static ptentry_t commands_table[];
- int reset_delay_secs;
+ static const ptentry_t commands_table[];
+ static int reset_delay_secs;
};