This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
MonitoredHBridge.h
191 lines (163 loc) · 5.82 KB
/
MonitoredHBridge.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**********************************************************************
ESP32 COMMAND STATION
COPYRIGHT (c) 2018-2020 Mike Dunston
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses
**********************************************************************/
#ifndef MONITORED_H_BRIDGE_
#define MONITORED_H_BRIDGE_
#include "TrackOutputDescriptor.h"
#include "sdkconfig.h"
#include <executor/StateFlow.hxx>
#include <openlcb/EventHandlerTemplates.hxx>
#include <openlcb/Node.hxx>
#include <openlcb/PolledProducer.hxx>
#include <os/Gpio.hxx>
#include <os/OS.hxx>
#include <utils/ConfigUpdateListener.hxx>
#include <utils/Debouncer.hxx>
#include <utils/format_utils.hxx>
#include <utils/logging.h>
#include <driver/adc.h>
#include <esp_adc_cal.h>
#include <esp_bit_defs.h>
namespace esp32cs
{
class HBridgeShortDetector : public DefaultConfigUpdateListener
, public openlcb::Polling
{
public:
HBridgeShortDetector(openlcb::Node *node
, const adc1_channel_t senseChannel
, const Gpio *enablePin
, const uint32_t limitMilliAmps
, const uint32_t maxMilliAmps
, const string &name
, const string &bridgeType
, const esp32cs::TrackOutputConfig &cfg);
HBridgeShortDetector(openlcb::Node *node
, const adc1_channel_t senseChannel
, const Gpio *enablePin
, const uint32_t
, const std::string &name
, const std::string &bridgeType
, const esp32cs::TrackOutputConfig &cfg);
enum STATE : uint8_t
{
STATE_OVERCURRENT = BIT(0)
, STATE_SHUTDOWN = BIT(1)
, STATE_ON = BIT(2)
, STATE_OFF = BIT(3)
};
std::string getName()
{
return name_;
}
uint32_t getMaxMilliAmps()
{
return maxMilliAmps_;
}
uint32_t getLastReading()
{
return lastReading_;
}
bool isProgrammingTrack()
{
return isProgTrack_;
}
bool isEnabled()
{
return state_ != STATE_OFF;
}
float getUsage()
{
if (state_ != STATE_OFF)
{
return ((lastReading_ * maxMilliAmps_) / 4096.0f);
}
return 0.0f;
}
std::string getState();
std::string getStateAsJson();
std::string getStatusData();
std::string get_state_for_dccpp();
UpdateAction apply_configuration(int fd, bool initial_load, BarrierNotifiable *done) override
{
AutoNotify n(done);
UpdateAction res = initial_load ? REINIT_NEEDED : UPDATED;
openlcb::EventId short_detected = cfg_.event_short().read(fd);
openlcb::EventId short_cleared = cfg_.event_short_cleared().read(fd);
openlcb::EventId shutdown = cfg_.event_shutdown().read(fd);
openlcb::EventId shutdown_cleared = cfg_.event_shutdown_cleared().read(fd);
auto saved_node = shortBit_.node();
if (short_detected != shortBit_.event_on() ||
short_cleared != shortBit_.event_off())
{
shortBit_.openlcb::MemoryBit<uint8_t>::~MemoryBit();
new (&shortBit_)openlcb::MemoryBit<uint8_t>(saved_node, short_detected, short_cleared, &state_, STATE_OVERCURRENT);
shortProducer_.openlcb::BitEventProducer::~BitEventProducer();
new (&shortProducer_)openlcb::BitEventProducer(&shortBit_);
res = REINIT_NEEDED;
}
if (shutdown != shortBit_.event_on() ||
shutdown_cleared != shortBit_.event_off())
{
saved_node = shutdownBit_.node();
shutdownBit_.openlcb::MemoryBit<uint8_t>::~MemoryBit();
new (&shutdownBit_)openlcb::MemoryBit<uint8_t>(saved_node, shutdown, shutdown_cleared, &state_, STATE_SHUTDOWN);
shutdownProducer_.openlcb::BitEventProducer::~BitEventProducer();
new (&shutdownProducer_)openlcb::BitEventProducer(&shutdownBit_);
res = REINIT_NEEDED;
}
return res;
}
void factory_reset(int fd) override
{
LOG(INFO
, "[LCC] MonitoredHBridge(%s) factory_reset(%d) invoked, defaulting "
"configuration", name_.c_str(), fd);
cfg_.description().write(fd, StringPrintf("%s Track", name_.c_str()));
}
void poll_33hz(openlcb::WriteHelper *helper, Notifiable *done) override;
void enable_prog_response(bool enable)
{
progEnable_ = enable;
}
private:
const adc1_channel_t channel_;
const Gpio *enablePin_;
const Gpio *thermalWarningPin_;
const uint32_t maxMilliAmps_;
const std::string name_;
const std::string bridgeType_;
const uint32_t overCurrentLimit_;
const uint32_t shutdownLimit_;
const bool isProgTrack_;
const uint32_t progAckLimit_;
const esp32cs::TrackOutputConfig cfg_;
const uint8_t targetLED_;
const uint8_t adcSampleCount_{CONFIG_ADC_AVERAGE_READING_COUNT};
const uint8_t overCurrentRetryCount_{CONFIG_DCC_HBRIDGE_OVERCURRENT_BEFORE_SHUTDOWN};
const uint64_t currentReportInterval_{SEC_TO_USEC(CONFIG_DCC_HBRIDGE_USAGE_REPORT_INTERVAL)};
uint32_t warnLimit_{0};
openlcb::MemoryBit<uint8_t> shortBit_;
openlcb::MemoryBit<uint8_t> shutdownBit_;
openlcb::BitEventProducer shortProducer_;
openlcb::BitEventProducer shutdownProducer_;
uint64_t lastReport_{0};
uint32_t lastReading_{0};
uint8_t state_{STATE_OFF};
uint8_t overCurrentCheckCount_{0};
bool progEnable_{false};
void configure();
};
} // namespace esp32cs
#endif // MONITORED_H_BRIDGE_