-
Notifications
You must be signed in to change notification settings - Fork 5
/
amg8833_person_detected.h
72 lines (66 loc) · 1.75 KB
/
amg8833_person_detected.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "esphome.h"
#include <SparkFun_GridEYE_Arduino_Library.h>
class AMG8833PersonDetectedComponent : public PollingComponent, public BinarySensor
{
public:
float get_setup_priority() const override { return esphome::setup_priority::BUS; }
AMG8833PersonDetectedComponent() : PollingComponent(100) {}
void setup() override
{
Wire.begin();
grideye.begin();
}
void loop() override
{
}
void update() override
{
float pixel_temperature;
float min = 0;
float max = 0;
unsigned char local_min_index = 0;
unsigned char local_max_index = 0;
for (unsigned char i = 0; i < total_pixels; i++)
{
pixel_temperature = grideye.getPixelTemperature(i);
if (i == 0 || pixel_temperature > max)
{
max = pixel_temperature;
local_max_index = i;
}
if (i == 0 || pixel_temperature < min)
{
min = pixel_temperature;
local_min_index = i;
}
}
if (max - min > 4)
{
if (!last_state)
{
publish_state(true);
delay_count = delay;
}
last_state = true;
}
else
{
if (last_state)
{
delay_count--;
if (delay_count <= 0)
{
publish_state(false);
last_state = false;
}
}
}
}
protected:
GridEYE grideye;
static const int size = 8;
static const int total_pixels = size * size;
static const int delay = 50;
int delay_count = delay;
bool last_state = false;
};