-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSceneHelper.cpp
71 lines (58 loc) · 1.66 KB
/
SceneHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "SceneHelper.h"
SceneHelper::SceneHelper()
{
fauxmo.onSetState([&](unsigned char sceneId, const char * sceneName, bool state) {
_handleSceneSwitch(sceneId, state);
});
fauxmo.onGetState([&](unsigned char sceneId, const char * sceneName) {
return _activeScene == sceneId;
});
}
void SceneHelper::setDeviceName(const char* deviceName)
{
_deviceName = deviceName;
Serial.printf("[SceneHelper] _deviceName = %s\n\n", _deviceName);
}
/**
* Adds a new scene to our collection.
*
* @param scene
*/
void SceneHelper::add(Scene scene)
{
char sceneName[100];
sprintf_P(sceneName, "%s: %s", _deviceName, scene.name);
scene.id = fauxmo.addDevice(sceneName);
if (scene.isOffSwitch) {
Serial.printf("[SceneHelper] Adding off switch scene with name \"%s\".\n", sceneName);
} else {
Serial.printf(
"[SceneHelper] Adding scene with name \"%s\" and color rgb(%d,%d,%d).\n",
sceneName, scene.r, scene.g, scene.b
);
}
_scenes.push_back(scene);
}
void SceneHelper::handle()
{
fauxmo.handle();
}
void SceneHelper::_handleSceneSwitch(unsigned char sceneId, bool state)
{
for (unsigned int i = 0; i < _scenes.size(); i++) {
if (_scenes[i].id != sceneId) continue;
if (!state || _scenes[i].isOffSwitch) {
_activeScene = -1;
_fireChangeHandler(0, 0, 0);
return;
}
_activeScene = sceneId;
_fireChangeHandler(_scenes[i].r, _scenes[i].g, _scenes[i].b);
}
}
void SceneHelper::_fireChangeHandler(uint8_t r, uint8_t g, uint8_t b)
{
if (_onChangeHandler) {
_onChangeHandler(r, g, b);
}
}