-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_CG_RadSens.h
136 lines (113 loc) · 3.43 KB
/
_CG_RadSens.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "esphome.h"
#include "CG_RadSens.h"
#define SECONDS_PER_INTERVAL 5
#ifdef RS_DEFAULT_I2C_ADDRESS
#undef RS_DEFAULT_I2C_ADDRESS
#define RS_DEFAULT_I2C_ADDRESS 0x66
#endif
using namespace esphome;
class CountsPerMinute {
int m_currentCpm;
int m_maximumCpm;
int m_intervalsPerMinute;
int * m_intervalCounts;
int m_currentInterval;
int m_initialInterval;
public:
CountsPerMinute() {}
void init(int intervalsPerMinute) {
this -> m_currentCpm = 0;
this -> m_maximumCpm = 0;
this -> m_intervalsPerMinute = intervalsPerMinute;
this -> m_intervalCounts = new int[this -> m_intervalsPerMinute];
for (int i = 0; i < this -> m_intervalsPerMinute; i++) {
this -> m_intervalCounts[i] = 0;
}
this -> m_currentInterval = 0;
this -> m_initialInterval = 0;
}
bool isReady() {
return this -> m_initialInterval >= this -> m_intervalsPerMinute;
}
int getCurrentCpm() {
return this -> m_currentCpm;
}
int getMaximumCpm() {
return this -> m_maximumCpm;
}
void resetCpm() {
this -> m_maximumCpm = 0;
}
void add(int count) {
if (this -> isReady()) {
this -> m_currentCpm = this -> m_currentCpm - this -> m_intervalCounts[m_currentInterval] + count;
} else {
this -> m_currentCpm += count;
this -> m_initialInterval++;
}
if (this -> m_maximumCpm < this -> m_currentCpm) {
this -> m_maximumCpm = this -> m_currentCpm;
}
this -> m_intervalCounts[m_currentInterval] = count;
m_currentInterval++;
if (m_currentInterval >= m_intervalsPerMinute) {
m_currentInterval = 0;
}
}
};
class MyRadSens: public PollingComponent, public CustomAPIDevice {
public: Sensor * IntensityDynamic_Sensor = new Sensor();
Sensor * IntensityStatic_Sensor = new Sensor();
Sensor * CurrentCPM_Sensor = new Sensor();
Sensor * MaxCPM_Sensor = new Sensor();
Sensor * Sensivity_Sensor = new Sensor();
CG_RadSens myself {
RS_DEFAULT_I2C_ADDRESS
};
CountsPerMinute cpm;
MyRadSens(): PollingComponent(SECONDS_PER_INTERVAL * 1000) {}
uint32_t pulsesPrev = 0;
int current_sensivity = 0;
void on_set_sensivity(int sensivity) {
myself.setSensitivity(sensivity);
ESP_LOGD("Sensivity", "Set to %d", sensivity);
}
void on_reset_cpm() {
cpm.resetCpm();
ESP_LOGD("CPM", "Reset successful");
}
void setup() override {
myself.init();
myself.setLedState(true);
myself.setSensitivity(105);
cpm.init(60 / SECONDS_PER_INTERVAL);
register_service( & MyRadSens::on_reset_cpm, "reset_cpm" );
register_service( & MyRadSens::on_set_sensivity, "set_sensivity", {
"sensivity"
});
}
void update() override {
float IntensityDynamic = myself.getRadIntensyDynamic();
float IntensityStatic = myself.getRadIntensyStatic();
int Sensivity = myself.getSensitivity();
int Pulses = myself.getNumberOfPulses();
if (Pulses > pulsesPrev) {
cpm.add(Pulses - pulsesPrev);
}
if (cpm.isReady()) {
CurrentCPM_Sensor -> publish_state(cpm.getCurrentCpm());
MaxCPM_Sensor -> publish_state(cpm.getMaximumCpm());
}
if (IntensityDynamic != 0) {
IntensityDynamic_Sensor -> publish_state(IntensityDynamic);
}
if (IntensityStatic != 0) {
IntensityStatic_Sensor -> publish_state(IntensityStatic);
}
if (current_sensivity != Sensivity) {
Sensivity_Sensor -> publish_state(Sensivity);
}
pulsesPrev = Pulses;
current_sensivity = Sensivity;
}
};