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() {
Serial.print(F("Board : "));
Serial.println(BOARD);
sensor_commands::initialize();
#ifdef AMPEL_WIFI
WiFiConnect(SENSOR_ID);
......
......@@ -17,7 +17,7 @@ namespace config {
namespace sensor {
SCD30 scd30;
int16_t co2 = 0;
uint16_t co2 = 0;
float temperature = 0;
float humidity = 0;
String timestamp = "";
......@@ -25,6 +25,13 @@ namespace sensor {
uint32_t waiting_color = color::blue;
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() {
#if defined(ESP8266)
Wire.begin(12, 14); // ESP8266 - D6, D5;
......@@ -68,6 +75,8 @@ namespace sensor {
Serial.print(F("Auto-calibration is "));
Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF.");
sensor_commands::defineCallback(printCO2, &co2);
}
//NOTE: should timer deviation be used to adjust measurement_timestep?
......
......@@ -7,6 +7,7 @@
#include "config.h"
#include "led_effects.h"
#include "util.h"
#include "sensor_commands.h"
#include <Wire.h>
namespace config {
......@@ -18,7 +19,7 @@ namespace config {
namespace sensor {
extern SCD30 scd30;
extern int16_t co2;
extern uint16_t co2;
extern float temperature;
extern float humidity;
extern String timestamp;
......
......@@ -70,10 +70,20 @@ namespace led_effects {
onBoardLEDOff();
}
void helloRing(void *c) {
Serial.print("HELLO FROM LED RINGS!");
Serial.print("I have ");
Serial.print(NUMPIXELS);
Serial.println("LEDs.");
}
void setupRing() {
pixels.begin();
pixels.setBrightness(config::max_brightness);
LEDsOff();
sensor_commands::defineCallback(helloRing, 0);
}
void toggleNightMode() {
......
......@@ -2,6 +2,7 @@
#define LED_EFFECTS_H_INCLUDED
#include <Arduino.h>
#include "config.h"
#include "sensor_commands.h"
// Adafruit NeoPixel (Arduino library for controlling single-wire-based LED pixels and strip)
// https://github.com/adafruit/Adafruit_NeoPixel
......
#include "sensor_commands.h"
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
// that will be passed as argument to the function.
struct Callback {
......@@ -11,32 +13,19 @@ namespace sensor_commands {
void *data;
};
Callback callbacks[3];
Callback callbacks[MAX_CALLBACKS];
// 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 defineCallback(void (*f)(void*), void *d) {
if (callbacks_count < MAX_CALLBACKS) {
callbacks[callbacks_count++] = Callback(f, d);
} else {
Serial.println("OH NOOEEEESSS!!!! TOO MANY CALLBACKS");
}
}
void run() {
// 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);
}
}
......
#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 {
void initialize();
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