src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
62 / 62
100.0%
Branches:
85 / 86
98.8%
Functions:
7 / 7
100.0%

ShadeSensors.cpp
Line Branch Exec Source
1 #include "ShadeSensors.h"
2
3 // Whether a timer started at start has run timeoutMs by now. The unsigned
4 // subtraction keeps working across the millis() rollover.
5 217 static bool due(uint32_t start, uint32_t now, uint32_t timeoutMs) {
6
3/4
✓ Branch 0 taken 217 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 205 times.
217 return start != 0 && now - start >= timeoutMs;
7 }
8 1 void ShadeSensors::clear() { *this = ShadeSensors(); }
9 26 void ShadeSensors::onSensorFrame(bool wasSunny, bool wasWindy, bool sunny, bool windy, uint32_t now) {
10
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 11 times.
26 if(sunny) {
11 15 this->noSunStart = 0;
12 15 this->noSunDone = true;
13 }
14 else {
15 11 this->sunStart = 0;
16 11 this->sunDone = true;
17 }
18
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 17 times.
26 if(windy) {
19 9 this->noWindStart = 0;
20 9 this->noWindDone = true;
21 9 this->windLast = now;
22 }
23 else {
24 17 this->windStart = 0;
25 17 this->windDone = true;
26 }
27
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 3 times.
26 if(sunny && !wasSunny) {
28 12 this->sunStart = now;
29 12 this->sunDone = false;
30 }
31
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 8 times.
14 else if(!sunny && wasSunny) {
32 3 this->noSunStart = now;
33 3 this->noSunDone = false;
34 }
35
4/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 1 time.
26 if(windy && !wasWindy) {
36 8 this->windStart = now;
37 8 this->windDone = false;
38 }
39
4/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 16 times.
18 else if(!windy && wasWindy) {
40 1 this->noWindStart = now;
41 1 this->noWindDone = false;
42 }
43 26 }
44 13174 SensorMove ShadeSensors::check(bool sunFlag, bool sunny, bool windy, uint32_t now) {
45 13174 SensorMove move = SensorMove::none;
46
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 13155 times.
13174 if(sunFlag) {
47
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 2 times.
19 if(sunny && !windy) {
48 // Sunny and calm, so the shade should be extended.
49
8/8
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 2 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 7 times.
14 if(this->noWindDone && !this->sunDone && due(this->sunStart, now, SOMFY_SUN_TIMEOUT)) {
50 7 this->sunDone = true;
51 7 move = SensorMove::extend;
52 }
53
6/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 13 times.
14 if(!this->noWindDone && due(this->noWindStart, now, SOMFY_NO_WIND_TIMEOUT)) {
54 1 this->noWindDone = true;
55 1 move = SensorMove::extend;
56 }
57 }
58
8/8
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 18 times.
19 if(!sunny && !this->noSunDone && due(this->noSunStart, now, SOMFY_NO_SUN_TIMEOUT)) {
59 1 this->noSunDone = true;
60 1 move = SensorMove::retract;
61 }
62 }
63
8/8
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 12959 times.
✓ Branch 2 taken 203 times.
✓ Branch 3 taken 12 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 200 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 13171 times.
13174 if(windy && !this->windDone && due(this->windStart, now, SOMFY_WIND_TIMEOUT)) {
64 3 this->windDone = true;
65 3 move = SensorMove::retract;
66 }
67 13174 return move;
68 }
69 8 SensorMove ShadeSensors::onSunFlag(bool sunny, bool windy) const {
70
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if(windy) return SensorMove::none;
71
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
6 if(sunny && this->sunDone) return SensorMove::extend;
72
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 time.
5 if(!sunny && this->noSunDone) return SensorMove::retract;
73 2 return SensorMove::none;
74 }
75 13160 SensorTargets sensorTargets(SensorMove move, tilt_types tilt, float myPos, float myTiltPos) {
76 13160 SensorTargets t;
77
2/2
✓ Branch 0 taken 13151 times.
✓ Branch 1 taken 9 times.
13160 if(move == SensorMove::none) return t;
78 9 const bool extend = move == SensorMove::extend;
79
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if(tilt == tilt_types::tiltonly) {
80 3 t.setTilt = true;
81
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
3 t.tilt = extend ? (myTiltPos >= 0 ? myTiltPos : 100.0f) : 0.0f;
82 }
83 else {
84 6 t.setLift = true;
85
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
6 t.lift = extend ? (myPos >= 0 ? myPos : 100.0f) : 0.0f;
86 }
87 9 return t;
88 }
89 40 bool ShadeSensors::windBlocksDown(uint32_t now) const {
90
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 time.
40 return this->windLast != 0 && now - this->windLast < SOMFY_NO_WIND_REMOTE_TIMEOUT;
91 }
92