Commit 130d5dd4 authored by Eric Duminil's avatar Eric Duminil
Browse files

Callback names

parent 81c4e10d
......@@ -149,10 +149,7 @@ void loop() {
if (Serial.available() > 0) {
commandString = Serial.readStringUntil('\n');
Serial.print(F("OHHHH. It would be cool to do something with : '"));
Serial.print(commandString);
Serial.println("'");
sensor_commands::run();
sensor_commands::run(commandString.c_str());
}
if (sensor::processData()) {
......
......@@ -76,7 +76,7 @@ namespace sensor {
Serial.print(F("Auto-calibration is "));
Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF.");
sensor_commands::defineCallback(printCO2, &co2);
sensor_commands::defineCallback("co2", printCO2, &co2);
}
//NOTE: should timer deviation be used to adjust measurement_timestep?
......
......@@ -83,7 +83,7 @@ namespace led_effects {
pixels.setBrightness(config::max_brightness);
LEDsOff();
sensor_commands::defineCallback(helloRing, 0);
sensor_commands::defineCallback("led", helloRing, 0);
}
void toggleNightMode() {
......
......@@ -141,6 +141,8 @@ namespace mqtt {
messageString += (char) message[i];
}
Serial.println("'.");
sensor_commands::run(messageString.c_str());
return;
//TODO: Move this logic to a separate class, which could be used by Serial/MQTT/WebServer
......
......@@ -4,6 +4,7 @@
#include <Arduino.h>
#include "config.h"
#include "led_effects.h"
#include "sensor_commands.h"
#ifdef AMPEL_CSV
# include "csv_writer.h"
#endif
......
......@@ -2,6 +2,7 @@
namespace sensor_commands {
const uint8_t MAX_CALLBACKS = 20;
const uint8_t MAX_COMMAND_SIZE = 20;
uint8_t callbacks_count = 0;
// A callback contains both a function and a pointer to arbitrary data
// that will be passed as argument to the function.
......@@ -14,19 +15,29 @@ namespace sensor_commands {
};
Callback callbacks[MAX_CALLBACKS];
char callback_names[MAX_CALLBACKS][MAX_COMMAND_SIZE];
void defineCallback(void (*f)(void*), void *d) {
void defineCallback(const char *command, void (*f)(void*), void *d) {
if (callbacks_count < MAX_CALLBACKS) {
callbacks[callbacks_count++] = Callback(f, d);
callbacks[callbacks_count] = Callback(f, d);
strlcpy(callback_names[callbacks_count], command, MAX_COMMAND_SIZE);
callbacks_count++;
} else {
Serial.println("OH NOOEEEESSS!!!! TOO MANY CALLBACKS");
Serial.println(F("Too many callbacks have been defined."));
}
}
void run() {
void run(const char *command) {
Serial.print(F("Received command : '"));
Serial.print(command);
Serial.println("'");
// Test all the callbacks.
for (uint8_t i = 0; i < callbacks_count; i++) {
Serial.print("Trying '");
Serial.print(callback_names[i]);
Serial.println("'");
callbacks[i].function(callbacks[i].data);
}
Serial.println("Done.");
}
}
......@@ -6,6 +6,6 @@
*/
namespace sensor_commands {
void run();
void defineCallback(void (*f)(void*), void *d);
void run(const char *command);
void defineCallback(const char *command, void (*f)(void*), void *d);
}
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