Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
co2ampel
ampel-firmware
Commits
81c4e10d
Commit
81c4e10d
authored
Apr 16, 2021
by
Eric Duminil
Browse files
Very basic example, with callbacks from other scripts
parent
8dd0b6cd
Changes
7
Hide whitespace changes
Inline
Side-by-side
ampel-firmware/ampel-firmware.ino
View file @
81c4e10d
...
...
@@ -89,8 +89,6 @@ void setup() {
Serial
.
print
(
F
(
"Board : "
));
Serial
.
println
(
BOARD
);
sensor_commands
::
initialize
();
#ifdef AMPEL_WIFI
WiFiConnect
(
SENSOR_ID
);
...
...
ampel-firmware/co2_sensor.cpp
View file @
81c4e10d
...
...
@@ -17,7 +17,7 @@ namespace config {
namespace
sensor
{
SCD30
scd30
;
int16_t
co2
=
0
;
u
int16_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?
...
...
ampel-firmware/co2_sensor.h
View file @
81c4e10d
...
...
@@ -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
u
int16_t
co2
;
extern
float
temperature
;
extern
float
humidity
;
extern
String
timestamp
;
...
...
ampel-firmware/led_effects.cpp
View file @
81c4e10d
...
...
@@ -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
()
{
...
...
ampel-firmware/led_effects.h
View file @
81c4e10d
...
...
@@ -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
...
...
ampel-firmware/sensor_commands.cpp
View file @
81c4e10d
#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
);
}
}
...
...
ampel-firmware/sensor_commands.h
View file @
81c4e10d
#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
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment