Commit 8dd0b6cd authored by Eric Duminil's avatar Eric Duminil
Browse files

Very basic example, with callbacks

parent 77831ea4
......@@ -33,10 +33,10 @@
#endif
#include "util.h"
#include "sensor_commands.h"
#include "co2_sensor.h"
#include "led_effects.h"
void keepServicesAlive();
void checkFlashButton();
......
......@@ -56,7 +56,6 @@
* and define your credentials and parameters in 'config.h'.
*/
/*****************************************************************
* PreInit *
*****************************************************************/
......@@ -68,6 +67,7 @@ void preinit() {
#endif
}
String commandString;
/*****************************************************************
* Setup *
......@@ -89,6 +89,8 @@ void setup() {
Serial.print(F("Board : "));
Serial.println(BOARD);
sensor_commands::initialize();
#ifdef AMPEL_WIFI
WiFiConnect(SENSOR_ID);
......@@ -147,6 +149,14 @@ void loop() {
// Short press for night mode, Long press for calibration.
checkFlashButton();
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();
}
if (sensor::processData()) {
#ifdef AMPEL_CSV
csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
......
#include "sensor_commands.h"
namespace sensor_commands {
// A callback contains both a function and a pointer to arbitrary data
// that will be passed as argument to the function.
struct Callback {
Callback(void (*f)(void*) = 0, void *d = 0) :
function(f), data(d) {
}
void (*function)(void*);
void *data;
};
Callback callbacks[3];
// This callback expects an int.
void print_int(void *data) {
int parameter = *(int*) data;
Serial.println(parameter);
}
// This one expects a C string.
void print_string(void *data) {
char *parameter = (char*) data;
Serial.println(parameter);
}
void initialize() {
// Register callbacks.
static int seventeen = 17;
static int forty_two = 42;
static char hello[] = "Hello, World!";
callbacks[0] = Callback(print_int, &seventeen);
callbacks[1] = Callback(print_int, &forty_two);
callbacks[2] = Callback(print_string, hello);
}
void run() {
// Test all the callbacks.
for (size_t i = 0; i < 3; i++) {
callbacks[i].function(callbacks[i].data);
}
}
}
#include <Arduino.h>
namespace sensor_commands {
void initialize();
void run();
}
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