FastLED 3.9.15
Loading...
Searching...
No Matches
pir.cpp
Go to the documentation of this file.
1#ifndef FASTLED_INTERNAL
2#define FASTLED_INTERNAL
3#endif
4
5#include "FastLED.h"
6
7#include "fastpin.h"
8#include "fl/strstream.h"
9#include "fl/warn.h"
10#include "fl/assert.h"
11#include "sensors/pir.h"
12
13namespace fl {
14
15namespace {
16int g_counter = 0;
17Str getButtonName(const char *button_name) {
18 if (button_name) {
19 return Str(button_name);
20 }
21 int count = g_counter++;
22 if (count == 0) {
23 return Str("PIR");
24 }
25 StrStream 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, uint32_t latchMs, uint32_t risingTime,
41 uint32_t 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(millis());
45 });
46}
47
48bool Pir::detect(uint32_t now) {
49 bool currentState = mPir.detect();
50 if (currentState && !mLastState) {
51 mRamp.trigger(now);
52 }
53 mLastState = currentState;
54 return mRamp.isActive(now);
55}
56
57uint8_t Pir::transition(uint32_t now) {
58 // ensure detect() logic runs so we trigger on edges
59 detect(now);
60 return mRamp.update8(now);
61}
62
63} // namespace fl
central include file for FastLED, defines the CFastLED class/object
uint8_t transition(uint32_t now)
Returns a 0–255 ramp value: • ramps 0→255 over risingTime • holds 255 until latchMs–fallingTime • ram...
Definition pir.cpp:57
PirLowLevel mPir
Definition pir.h:68
UIButton mButton
Definition pir.h:71
TimeRamp mRamp
Definition pir.h:69
bool detect(uint32_t now)
Returns true if the PIR is “latched on” (within latchMs of last trigger).
Definition pir.cpp:48
bool mLastState
Definition pir.h:70
Pir(int pin, uint32_t latchMs=5000, uint32_t risingTime=1000, uint32_t fallingTime=1000, const char *button_name=nullptr)
Definition pir.cpp:40
bool detect()
Definition pir.cpp:35
PirLowLevel(int pin)
Definition pir.cpp:31
DigitalPin mPin
Definition pir.h:27
const string & str() const
Definition strstream.h:51
Class base definitions for defining fast pin access.
Str getButtonName(const char *button_name)
Definition pir.cpp:17
fl::string Str
Definition str.h:36
IMPORTANT!
Definition crgb.h:20