Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print list of commands #14

Open
PreciousRoy0 opened this issue Sep 18, 2021 · 5 comments
Open

Print list of commands #14

PreciousRoy0 opened this issue Sep 18, 2021 · 5 comments

Comments

@PreciousRoy0
Copy link

Hi i added a feature to your lib but I don't really know enough of git to ask to merge via it so here is what I did. I was in need to print out a list of all the commands available but I did not want to create a separate list with description for them so i added a function to your lib,

void SerialCommands::PrintCommandList()
{
	if (commands_head_ == nullptr)
	{
		serial_->println("No commands set!");
		return;
	}

	SerialCommand* current = commands_head_;
	while (current)
	{
		serial_->print("cmd: ");
		serial_->print(current->command);
		serial_->print(" - ");
		serial_->println(current->description);

		current = current->next;
	}
}

Added (const char* cdescription = "") to the SerialCommand container

typedef class SerialCommand SerialCommand;
class SerialCommand
{
public:
	SerialCommand(const char* cmd, void(*func)(SerialCommands*), bool one_k=false, const char* cdescription = "")
		: command(cmd),
		function(func),
		next(NULL),
		one_key(one_k),
		description(cdescription)
	{
	}

	const char* command;
	void(*function)(SerialCommands*);
	SerialCommand* next;
	bool one_key;
	const char* description;
};

Example:

const int kLedPin = LED_BUILTIN;

char serial_command_buffer_[32];
SerialCommands serial_commands_(&Serial, serial_command_buffer_, sizeof(serial_command_buffer_), "\r\n", " ");

//This is the default handler, and gets called when no other command matches. 
void cmd_unrecognized(SerialCommands* sender, const char* cmd)
{
	sender->GetSerial()->print("Unrecognized command [");
	sender->GetSerial()->print(cmd);
	sender->GetSerial()->println("]");
}

//called for ON command
void cmd_led_on(SerialCommands* sender)
{
	digitalWrite(kLedPin, HIGH);
	sender->GetSerial()->println("Led is on");
}

//called for OFF command
void cmd_led_off(SerialCommands* sender)
{
	digitalWrite(kLedPin, LOW);
	sender->GetSerial()->println("Led is off");
}

//Request list of commands
void cmd_GetCommandList(SerialCommands* sender)
{
	//Request a new feature or make it your self to print out a list of commands
	sender->GetSerial()->println("----------------------------------------------");
	sender->GetSerial()->println("-----------------Command List-----------------");
	sender->GetSerial()->println("----------------------------------------------");

	sender->PrintCommandList();
}


//Note: Commands are case sensitive
SerialCommand cmd_led_on_("ON", cmd_led_on, false, "Turn led on");
SerialCommand cmd_led_off_("OFF", cmd_led_off, false, "Turn led on");
SerialCommand cmd_question("?", cmd_GetCommandList, false, "Shows a list of available commands");

void setup()
{
	Serial.begin(57600);

	//Configure the LED for output and sets the intial state to off
	pinMode(kLedPin, OUTPUT);
	digitalWrite(kLedPin, LOW);

	serial_commands_.SetDefaultHandler(cmd_unrecognized);
	serial_commands_.AddCommand(&cmd_led_on_);
	serial_commands_.AddCommand(&cmd_led_off_);
	serial_commands_.AddCommand(&cmd_question);

	Serial.println("Ready!");
}

void loop()
{
	serial_commands_.ReadSerial();
}

and the output, it does not do much but it makes things a little bit easier.

----------------------------------------------
-----------------Command List-----------------
----------------------------------------------
cmd: ON - Turn led on
cmd: OFF - Turn led on
cmd: ? - Shows a list of available commands
@greenveg
Copy link

greenveg commented Mar 3, 2022

Is this merged into main branch @ppedro74 ?

Thanks!

@PreciousRoy0
Copy link
Author

Nope, have no clue how to use GitHub I should follow some tutorials explaining things. But I think it would be a good idea it saves a lot of work

@greenveg
Copy link

Friendly bump! :)

@PreciousRoy0
Copy link
Author

i will try to figure out how to make a branch, but its not up to me to join it I think

@PreciousRoy0
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants