Commit 149a1b18 authored by Eric Duminil's avatar Eric Duminil
Browse files

Sorted commands in log

parent 6b33430b
Pipeline #2833 passed with stage
in 1 minute and 36 seconds
#include "sensor_console.h"
namespace sensor_console {
const uint8_t MAX_CALLBACKS = 20;
const uint8_t MAX_COMMANDS = 20;
const uint8_t MAX_COMMAND_SIZE = 30;
uint8_t callbacks_count = 0;
uint8_t commands_count = 0;
struct Callback {
struct Command {
const char *name;
union {
void (*intFunction)(int32_t);
......@@ -16,30 +16,30 @@ namespace sensor_console {
bool has_parameter;
};
Callback callbacks[MAX_CALLBACKS];
Command commands[MAX_COMMANDS];
//NOTE: Probably possible to DRY (with templates?)
void defineCommand(const char *name, void (*function)(void), const char *doc) {
if (callbacks_count < MAX_CALLBACKS) {
callbacks[callbacks_count].name = name;
callbacks[callbacks_count].voidFunction = function;
callbacks[callbacks_count].doc = doc;
callbacks[callbacks_count].has_parameter = false;
callbacks_count++;
if (commands_count < MAX_COMMANDS) {
commands[commands_count].name = name;
commands[commands_count].voidFunction = function;
commands[commands_count].doc = doc;
commands[commands_count].has_parameter = false;
commands_count++;
} else {
Serial.println(F("Too many callbacks have been defined."));
Serial.println(F("Too many commands have been defined."));
}
}
void defineIntCommand(const char *name, void (*function)(int32_t), const char *doc) {
if (callbacks_count < MAX_CALLBACKS) {
callbacks[callbacks_count].name = name;
callbacks[callbacks_count].intFunction = function;
callbacks[callbacks_count].doc = doc;
callbacks[callbacks_count].has_parameter = true;
callbacks_count++;
if (commands_count < MAX_COMMANDS) {
commands[commands_count].name = name;
commands[commands_count].intFunction = function;
commands[commands_count].doc = doc;
commands[commands_count].has_parameter = true;
commands_count++;
} else {
Serial.println(F("Too many callbacks have been defined."));
Serial.println(F("Too many commands have been defined."));
}
}
......@@ -106,11 +106,18 @@ namespace sensor_console {
}
}
void listAvailableCallbacks() {
for (uint8_t i = 0; i < callbacks_count; i++) {
int compareName(const void *s1, const void *s2) {
struct Command *c1 = (struct Command*) s1;
struct Command *c2 = (struct Command*) s2;
return strcmp(c1->name, c2->name);
}
void listAvailableCommands() {
qsort(commands, commands_count, sizeof(commands[0]), compareName);
for (uint8_t i = 0; i < commands_count; i++) {
Serial.print(" ");
Serial.print(callbacks[i].name);
Serial.print(callbacks[i].doc);
Serial.print(commands[i].name);
Serial.print(commands[i].doc);
Serial.println(".");
}
led_effects::showKITTWheel(color::red, 1);
......@@ -125,23 +132,23 @@ namespace sensor_console {
bool has_argument;
has_argument = (parseCommand(command, function_name, argument) == 0);
for (uint8_t i = 0; i < callbacks_count; i++) {
if (!strcmp(function_name, callbacks[i].name) && has_argument == callbacks[i].has_parameter) {
Serial.print("Calling : ");
for (uint8_t i = 0; i < commands_count; i++) {
if (!strcmp(function_name, commands[i].name) && has_argument == commands[i].has_parameter) {
Serial.print(F("Calling : "));
Serial.print(function_name);
if (has_argument) {
Serial.print("(");
Serial.print(argument);
Serial.println(")");
callbacks[i].intFunction(argument);
commands[i].intFunction(argument);
} else {
Serial.println("()");
callbacks[i].voidFunction();
commands[i].voidFunction();
}
return;
}
}
Serial.println(F("Message not supported. Available commands :"));
listAvailableCallbacks();
listAvailableCommands();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment