Commit 81c4e10d authored by Eric Duminil's avatar Eric Duminil
Browse files

Very basic example, with callbacks from other scripts

parent 8dd0b6cd
...@@ -89,8 +89,6 @@ void setup() { ...@@ -89,8 +89,6 @@ void setup() {
Serial.print(F("Board : ")); Serial.print(F("Board : "));
Serial.println(BOARD); Serial.println(BOARD);
sensor_commands::initialize();
#ifdef AMPEL_WIFI #ifdef AMPEL_WIFI
WiFiConnect(SENSOR_ID); WiFiConnect(SENSOR_ID);
......
...@@ -17,7 +17,7 @@ namespace config { ...@@ -17,7 +17,7 @@ namespace config {
namespace sensor { namespace sensor {
SCD30 scd30; SCD30 scd30;
int16_t co2 = 0; uint16_t co2 = 0;
float temperature = 0; float temperature = 0;
float humidity = 0; float humidity = 0;
String timestamp = ""; String timestamp = "";
...@@ -25,6 +25,13 @@ namespace sensor { ...@@ -25,6 +25,13 @@ namespace sensor {
uint32_t waiting_color = color::blue; uint32_t waiting_color = color::blue;
bool should_calibrate = false; bool should_calibrate = false;
void printCO2(void *data) {
int parameter = *(uint16_t*) data;
Serial.print("NICE! HELLO FROM SENSOR.");
Serial.print("CO2 : ");
Serial.println(parameter);
}
void initialize() { void initialize() {
#if defined(ESP8266) #if defined(ESP8266)
Wire.begin(12, 14); // ESP8266 - D6, D5; Wire.begin(12, 14); // ESP8266 - D6, D5;
...@@ -68,6 +75,8 @@ namespace sensor { ...@@ -68,6 +75,8 @@ namespace sensor {
Serial.print(F("Auto-calibration is ")); Serial.print(F("Auto-calibration is "));
Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF."); Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF.");
sensor_commands::defineCallback(printCO2, &co2);
} }
//NOTE: should timer deviation be used to adjust measurement_timestep? //NOTE: should timer deviation be used to adjust measurement_timestep?
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "config.h" #include "config.h"
#include "led_effects.h" #include "led_effects.h"
#include "util.h" #include "util.h"
#include "sensor_commands.h"
#include <Wire.h> #include <Wire.h>
namespace config { namespace config {
...@@ -18,7 +19,7 @@ namespace config { ...@@ -18,7 +19,7 @@ namespace config {
namespace sensor { namespace sensor {
extern SCD30 scd30; extern SCD30 scd30;
extern int16_t co2; extern uint16_t co2;
extern float temperature; extern float temperature;
extern float humidity; extern float humidity;
extern String timestamp; extern String timestamp;
......
...@@ -70,10 +70,20 @@ namespace led_effects { ...@@ -70,10 +70,20 @@ namespace led_effects {
onBoardLEDOff(); onBoardLEDOff();
} }
void helloRing(void *c) {
Serial.print("HELLO FROM LED RINGS!");
Serial.print("I have ");
Serial.print(NUMPIXELS);
Serial.println("LEDs.");
}
void setupRing() { void setupRing() {
pixels.begin(); pixels.begin();
pixels.setBrightness(config::max_brightness); pixels.setBrightness(config::max_brightness);
LEDsOff(); LEDsOff();
sensor_commands::defineCallback(helloRing, 0);
} }
void toggleNightMode() { void toggleNightMode() {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define LED_EFFECTS_H_INCLUDED #define LED_EFFECTS_H_INCLUDED
#include <Arduino.h> #include <Arduino.h>
#include "config.h" #include "config.h"
#include "sensor_commands.h"
// Adafruit NeoPixel (Arduino library for controlling single-wire-based LED pixels and strip) // Adafruit NeoPixel (Arduino library for controlling single-wire-based LED pixels and strip)
// https://github.com/adafruit/Adafruit_NeoPixel // https://github.com/adafruit/Adafruit_NeoPixel
......
#include "sensor_commands.h" #include "sensor_commands.h"
namespace sensor_commands { namespace sensor_commands {
const uint8_t MAX_CALLBACKS = 20;
uint8_t callbacks_count = 0;
// A callback contains both a function and a pointer to arbitrary data // A callback contains both a function and a pointer to arbitrary data
// that will be passed as argument to the function. // that will be passed as argument to the function.
struct Callback { struct Callback {
...@@ -11,32 +13,19 @@ namespace sensor_commands { ...@@ -11,32 +13,19 @@ namespace sensor_commands {
void *data; void *data;
}; };
Callback callbacks[3]; Callback callbacks[MAX_CALLBACKS];
// This callback expects an int. void defineCallback(void (*f)(void*), void *d) {
void print_int(void *data) { if (callbacks_count < MAX_CALLBACKS) {
int parameter = *(int*) data; callbacks[callbacks_count++] = Callback(f, d);
Serial.println(parameter); } else {
} Serial.println("OH NOOEEEESSS!!!! TOO MANY CALLBACKS");
}
// 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() { void run() {
// Test all the callbacks. // Test all the callbacks.
for (size_t i = 0; i < 3; i++) { for (uint8_t i = 0; i < callbacks_count; i++) {
callbacks[i].function(callbacks[i].data); callbacks[i].function(callbacks[i].data);
} }
} }
......
#include <Arduino.h> #include <Arduino.h>
/** Other scripts can use this namespace, in order to define callbacks.
* Those callbacks can then be used to send commands to the sensor (reset, calibrate, night mode, ...)
*
*/
namespace sensor_commands { namespace sensor_commands {
void initialize();
void run(); void run();
void defineCallback(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