Skip to content

Commit 7509ba1

Browse files
committedMar 15, 2018
Fixed time handling in damper
1 parent 6b79225 commit 7509ba1

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed
 

‎devices/sonoff/Impl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SonoffDevice : public Device {
3333

3434
Socket<SOCKET_NAME, SOCKET_GPIO> socket;
3535
Light<LIGHT_NAME, LIGHT_GPIO> light;
36-
ToggleButton<BUTTON_NAME, BUTTON_GPIO, true, 1> button;
36+
ToggleButton<BUTTON_NAME, BUTTON_GPIO, true> button;
3737
};
3838

3939

‎framework/features/Button.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
#include "Feature.h"
55
#include "../util/Observed.h"
6+
#include "../util/Damper.h"
67

78

8-
template<const char* const name, uint16_t gpio, bool inverted, uint16_t damper_time = 0>
9+
template<const char* const name, uint16_t gpio, bool inverted, uint16_t damper_time = 100>
910
class Button : public Feature<name> {
1011
constexpr static const char* const ON = "1";
1112
constexpr static const char* const OFF = "0";

‎framework/features/Socket.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66

77
template <const char* const name, uint16_t gpio, bool invert=false>
8-
class Socket : public OnOffFeature<name, gpio, invert, 1> {
9-
using OnOffFeature<name, gpio, invert, 1>::OnOffFeature;
8+
class Socket : public OnOffFeature<name, gpio, invert, 1000> {
9+
using OnOffFeature<name, gpio, invert, 1000>::OnOffFeature;
1010
};
1111

1212
#endif

‎framework/features/ToggleButton.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include "Button.h"
55

66

7-
template<const char* const name, uint16_t gpio, bool inverted = true, bool activeEdge = true>
8-
class ToggleButton : public Button<name, gpio, inverted> {
7+
template<const char* const name, uint16_t gpio, bool inverted = true, bool activeEdge = true, uint16_t damper_time = 100>
8+
class ToggleButton : public Button<name, gpio, inverted, damper_time> {
99
protected:
1010
using Button<name, gpio, inverted>::LOG;
1111

‎framework/features/TriggerButton.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include "Button.h"
55

66

7-
template<const char* const name, uint16_t gpio, bool inverted=true>
8-
class TriggerButton : public Button<name, gpio, inverted> {
7+
template<const char* const name, uint16_t gpio, bool inverted=true, uint16_t damper_time = 100>
8+
class TriggerButton : public Button<name, gpio, inverted, damper_time> {
99

1010
public:
1111
using Callback = typename Button<name, gpio, inverted>::Callback;

‎framework/util/Damper.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
template<uint16_t damper_time = 0>
66
class Damper {
77
public:
8-
Damper() : lastChange(RTC.getRtcNanoseconds()) { }
8+
Damper() :
9+
lastChange(RTC.getRtcNanoseconds() / 1000000) {
10+
}
911

1012
bool isDamped() {
11-
if(damper_time <= 0)
12-
return false;
13+
if (damper_time == 0) {
14+
return false;
15+
}
1316

14-
const uint64_t now = RTC.getRtcNanoseconds();
17+
const uint32_t now = RTC.getRtcNanoseconds() / 1000000;
1518

16-
if (this->lastChange + ((uint64_t) damper_time * NS_PER_SECOND) > now) {
19+
if (this->lastChange + ((uint32_t) damper_time) > now) {
1720
return true;
1821
}
1922

@@ -23,7 +26,7 @@ class Damper {
2326
}
2427

2528
private:
26-
uint64_t lastChange;
29+
uint32_t lastChange;
2730
};
2831

2932
#endif

0 commit comments

Comments
 (0)