-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathdivert.h
125 lines (98 loc) · 2.38 KB
/
divert.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
// Solar PV power diversion
// Modulate charge rate based on solar PV output
// Glyn Hudson | OpenEnergyMonitor.org
#ifndef _EMONESP_DIVERT_H
#define _EMONESP_DIVERT_H
#ifndef EVSE_DIVERT_HYSTERESIS
#define EVSE_DIVERT_HYSTERESIS 0.5 // A
#endif
#include <Arduino.h>
#include <MicroTasks.h>
#include "evse_man.h"
#include "input_filter.h"
enum divert_type {
DIVERT_TYPE_UNSET = -1,
DIVERT_TYPE_SOLAR = 0,
DIVERT_TYPE_GRID = 1
};
extern int solar;
extern int grid_ie;
class DivertMode
{
public:
enum Value : uint8_t {
Normal = 1,
Eco = 2
};
DivertMode() = default;
constexpr DivertMode(Value value) : _value(value) { }
constexpr DivertMode(long value) : _value((Value)value) { }
operator Value() const { return _value; }
explicit operator bool() = delete; // Prevent usage: if(state)
DivertMode operator= (const Value val) {
_value = val;
return *this;
}
DivertMode operator= (const long val) {
_value = (Value)val;
return *this;
}
private:
Value _value;
};
class DivertTask : public MicroTasks::Task
{
private:
// global variable
EvseManager *_evse;
DivertMode _mode;
EvseState _state;
uint32_t _last_update;
int _charge_rate;
MicroTasks::EventListener _evseState;
double _available_current;
double _smoothed_available_current;
time_t _min_charge_end;
uint8_t _evse_last_state;
InputFilter _inputFilter;
protected:
void setup();
unsigned long loop(MicroTasks::WakeReason reason);
public:
DivertTask(EvseManager &evse);
~DivertTask();
void begin();
// Change mode
void setMode(DivertMode mode);
DivertMode getMode() {
return _mode;
}
uint32_t lastUpdate() {
return _last_update;
}
uint32_t chargeRate() {
return _charge_rate;
}
double availableCurrent() {
return _available_current;
}
double smoothedAvailableCurrent() {
return _smoothed_available_current;
}
// Set charge rate depending on charge mode and solarPV output
void update_state();
EvseState getState() {
return _state;
}
uint32_t getLastUpdate() {
return _last_update;
}
int getChargeRate() {
return _charge_rate;
}
time_t getMinChargeTimeRemaining();
bool isActive();
void initDivertType();
};
extern class DivertTask divert;
#endif // _EMONESP_DIVERT_H