diff --git a/ampel-firmware/sensor_console.cpp b/ampel-firmware/sensor_console.cpp index 0be09187371aa6bdb3238df471c8b2bfb08aa60f..d2246bc791e1ae13b0b8815ffa3b29dbbc3584e1 100644 --- a/ampel-firmware/sensor_console.cpp +++ b/ampel-firmware/sensor_console.cpp @@ -1,12 +1,12 @@ #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(); } }