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 | 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 | 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 | 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 | 26 | if(sunny && !wasSunny) { | |
| 28 | 12 | this->sunStart = now; | |
| 29 | 12 | this->sunDone = false; | |
| 30 | } | ||
| 31 | 14 | else if(!sunny && wasSunny) { | |
| 32 | 3 | this->noSunStart = now; | |
| 33 | 3 | this->noSunDone = false; | |
| 34 | } | ||
| 35 | 26 | if(windy && !wasWindy) { | |
| 36 | 8 | this->windStart = now; | |
| 37 | 8 | this->windDone = false; | |
| 38 | } | ||
| 39 | 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 | 13174 | if(sunFlag) { | |
| 47 | 19 | if(sunny && !windy) { | |
| 48 | // Sunny and calm, so the shade should be extended. | ||
| 49 | 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 | 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 | 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 | 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 | 8 | if(windy) return SensorMove::none; | |
| 71 | 6 | if(sunny && this->sunDone) return SensorMove::extend; | |
| 72 | 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 | 13160 | if(move == SensorMove::none) return t; | |
| 78 | 9 | const bool extend = move == SensorMove::extend; | |
| 79 | 9 | if(tilt == tilt_types::tiltonly) { | |
| 80 | 3 | t.setTilt = true; | |
| 81 | 3 | t.tilt = extend ? (myTiltPos >= 0 ? myTiltPos : 100.0f) : 0.0f; | |
| 82 | } | ||
| 83 | else { | ||
| 84 | 6 | t.setLift = true; | |
| 85 | 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 | 40 | return this->windLast != 0 && now - this->windLast < SOMFY_NO_WIND_REMOTE_TIMEOUT; | |
| 91 | } | ||
| 92 |