-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sleep-Light-Floor.cpp
86 lines (66 loc) · 2.04 KB
/
Sleep-Light-Floor.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <algorithm>
#include <NeoPixelBus.h>
#include "IoT/Device.hpp"
#include "IoT/Gpio.hpp"
#include "IoT/Input.hpp"
#include "IoT/IoT.hpp"
#include "IoT/Logger.hpp"
#include "IoT/Motion.hpp"
#include "IoT/SceneManager.hpp"
#include "IoT/Timer.hpp"
using namespace std;
void update();
uint8_t constexpr levelDelta = 255 * IoTClass::tick / 1000;
uint8_t constexpr offLevel = 0x00;
uint8_t constexpr dimLevel = 0x28;
uint8_t constexpr onLevel = 0xff;
uint16_t constexpr countPixels = 36;
uint8_t currentLevels[countPixels];
uint8_t targetLevels[countPixels];
NeoPixelBus< NeoGrbFeature, NeoEsp8266Uart1800KbpsMethod > neoPixelBus( countPixels );
IoTClass IoT( "Sleep/Light/Floor", "akvsoft", "sacomoco02047781", "192.168.178.28", 1883 );
Input input0( motion( 5000, gpioInput( 12, false )));
Input input1( motion( 5000, gpioInput( 14, false )));
Input input2( motion( 5000, gpioInput( 16, false )));
Device unterBett;
SceneManager sceneManager( "Sleep" );
void update()
{
if ( unterBett.get()) {
fill_n( targetLevels, countPixels, onLevel );
} else {
fill_n( targetLevels, countPixels, offLevel );
if ( input0.get()) {
fill_n( targetLevels + 0, 12, dimLevel );
}
if ( input1.get()) {
fill_n( targetLevels + 11, 14, dimLevel );
}
if ( input2.get()) {
fill_n( targetLevels + 24, 12, dimLevel );
}
}
for ( uint16_t i = 0 ; i < countPixels ; ++i ) {
int16_t diff = targetLevels[i] - currentLevels[i];
if ( diff == 0 ) {
// nothing to do
} else if ( abs( diff ) < levelDelta ) {
currentLevels[i] = targetLevels[i];
} else {
currentLevels[i] += levelDelta * ( diff > 0 ? 1 : -1 );
}
neoPixelBus.SetPixelColor( i, RgbColor( currentLevels[i] ));
}
neoPixelBus.Show();
}
void setup()
{
neoPixelBus.Begin();
IoT.loopEvent += [] { update(); };
sceneManager.addSceneDevice( unterBett, { Scene::SCENE2 } );
IoT.begin();
}
void loop()
{
IoT.loop();
}