FastLED 3.9.15
Loading...
Searching...
No Matches
pir.cpp.hpp
Go to the documentation of this file.
1#ifndef FASTLED_INTERNAL
2#define FASTLED_INTERNAL
3#endif
4
5#include "fl/system/fastled.h"
6
7#include "fl/system/fastpin.h"
8#include "fl/stl/strstream.h"
9#include "fl/log/log.h"
10#include "fl/stl/assert.h"
11#include "fl/sensors/pir.h"
12
13namespace fl {
14
15namespace {
16int g_counter = 0;
17string getButtonName(const char *button_name) {
18 if (button_name) {
19 return string(button_name);
20 }
21 int count = g_counter++;
22 if (count == 0) {
23 return string("PIR");
24 }
25 sstream s;
26 s << "PirLowLevel " << g_counter++;
27 return s.str();
28}
29} // namespace
30
32 mPin.setPinMode(DigitalPin::kInput);
33}
34
36 return mPin.high();
37}
38
39
40Pir::Pir(int pin, u32 latchMs, u32 risingTime,
41 u32 fallingTime, const char* button_name)
42 : mPir(pin), mRamp(risingTime, latchMs, fallingTime), mButton(getButtonName(button_name).c_str()) {
43 mButton.onChanged([this](UIButton&) {
44 this->mRamp.trigger(fl::millis());
45 });
46}
47
48bool Pir::detect(u32 now) {
49 bool currentState = mPir.detect();
50 if (currentState && !mLastState) {
51 // Use smart retrigger to avoid resetting brightness when already active
52 mRamp.trigger(now);
53 }
54 mLastState = currentState;
55 return mRamp.isActive(now);
56}
57
59 // ensure detect() logic runs so we trigger on edges
60 detect(now);
61 return mRamp.update8(now);
62}
63
64} // namespace fl
PirLowLevel mPir
Definition pir.h:64
UIButton mButton
Definition pir.h:67
bool detect(u32 now)
Returns true if the PIR is “latched on” (within latchMs of last trigger).
Definition pir.cpp.hpp:48
TimeRamp mRamp
Definition pir.h:65
bool mLastState
Definition pir.h:66
u8 transition(u32 now)
Returns a 0–255 ramp value: • ramps 0→255 over risingTime • holds 255 until latchMs–fallingTime • ram...
Definition pir.cpp.hpp:58
Pir(int pin, u32 latchMs=5000, u32 risingTime=1000, u32 fallingTime=1000, const char *button_name=nullptr)
Definition pir.cpp.hpp:40
PirLowLevel(int pin)
Definition pir.cpp.hpp:31
DigitalPin mPin
Definition pir.h:23
string str() const FL_NOEXCEPT
Definition strstream.h:43
Internal FastLED header for implementation files.
Centralized logging categories for FastLED hardware interfaces and subsystems.
string getButtonName(const char *button_name)
Definition pir.cpp.hpp:17
unsigned char u8
Definition stdint.h:131
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
Base definition for an LED controller.
Definition crgb.hpp:179