-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatteryImpl.cpp
216 lines (182 loc) · 4.9 KB
/
BatteryImpl.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* BatteryImpl.cpp
*
* Created on: 30.04.2014
* Author: niklausd
*/
#include "SpinTimer.h"
#include "Battery.h"
#include "BatteryVoltageEvalFsm.h"
#include "BatteryImpl.h"
//-----------------------------------------------------------------------------
class BattStartupTimerAction : public SpinTimerAction
{
private:
BatteryImpl* m_battImpl;
public:
BattStartupTimerAction(BatteryImpl* battImpl)
: m_battImpl(battImpl)
{ }
void timeExpired()
{
if (0 != m_battImpl)
{
m_battImpl->startup();
}
}
};
//-----------------------------------------------------------------------------
class BattStatusEvalTimerAction : public SpinTimerAction
{
private:
BatteryImpl* m_battImpl;
public:
BattStatusEvalTimerAction(BatteryImpl* battImpl)
: m_battImpl(battImpl)
{ }
void timeExpired()
{
if (0 != m_battImpl)
{
m_battImpl->evaluateStatus();
}
}
};
//-----------------------------------------------------------------------------
const unsigned int BatteryImpl::s_DEFAULT_STARTUP_TIME = 500;
const unsigned int BatteryImpl::s_DEFAULT_POLL_TIME = 5000;
const unsigned int BatteryImpl::s_DEFAULT_ASYNC_STATUS_EVAL_TIME = 0;
BatteryImpl::BatteryImpl(BatteryAdapter* adapter, BatteryThresholdConfig batteryThresholdConfig)
: m_adapter(adapter)
, m_evalFsm(new BatteryVoltageEvalFsm(this))
, m_startupTimer(new SpinTimer(s_DEFAULT_STARTUP_TIME, new BattStartupTimerAction(this), SpinTimer::IS_NON_RECURRING, SpinTimer::IS_AUTOSTART))
, m_pollTimer(new SpinTimer(s_DEFAULT_POLL_TIME, new BattStatusEvalTimerAction(this), SpinTimer::IS_RECURRING, SpinTimer::IS_NON_AUTOSTART))
, m_evalStatusTimer(new SpinTimer(s_DEFAULT_ASYNC_STATUS_EVAL_TIME, m_pollTimer->action(), SpinTimer::IS_NON_RECURRING, SpinTimer::IS_NON_AUTOSTART)) // re-use the same BattStatusEvalTimerAdapter object
, m_batteryVoltage(0.0)
, m_battVoltageSenseFactor(2.0)
, m_battWarnThreshd(batteryThresholdConfig.battWarnThreshd)
, m_battStopThrshd(batteryThresholdConfig.battStopThrshd)
, m_battShutThrshd(batteryThresholdConfig.battShutThrshd)
, m_battHyst(batteryThresholdConfig.battHyst)
{ }
BatteryImpl::~BatteryImpl()
{
delete m_evalStatusTimer;
m_evalStatusTimer = 0;
delete m_pollTimer->action();
delete m_pollTimer; m_pollTimer = 0;
delete m_startupTimer->action();
delete m_startupTimer; m_startupTimer = 0;
delete m_evalFsm;
m_adapter = 0;
}
void BatteryImpl::attachAdapter(BatteryAdapter* adapter)
{
m_adapter = adapter;
m_evalFsm->attachAdapter(m_adapter);
battVoltageSensFactorChanged();
}
BatteryAdapter* BatteryImpl::adapter()
{
return m_adapter;
}
void BatteryImpl::startup()
{
if (0 != m_adapter)
{
m_battVoltageSenseFactor = m_adapter->readBattVoltageSenseFactor();
}
evaluateStatusAsync();
m_pollTimer->start(s_DEFAULT_POLL_TIME);
}
void BatteryImpl::evaluateStatus()
{
if ((0 != m_adapter) && (0 != m_evalFsm))
{
m_batteryVoltage = m_adapter->readRawBattSenseValue() * m_battVoltageSenseFactor * adapter()->getVAdcFullrange() / (adapter()->getNAdcFullrange() + 1);
m_evalFsm->evaluateStatus();
}
}
void BatteryImpl::evaluateStatusAsync()
{
m_evalStatusTimer->start(s_DEFAULT_ASYNC_STATUS_EVAL_TIME);
}
void BatteryImpl::battVoltageSensFactorChanged()
{
if (0 != m_adapter)
{
m_battVoltageSenseFactor = m_adapter->readBattVoltageSenseFactor();
}
}
float BatteryImpl::getBatteryVoltage()
{
return m_batteryVoltage;
}
bool BatteryImpl::isBattVoltageOk()
{
bool isVoltageOk = false;
if (0 != m_evalFsm)
{
isVoltageOk = m_evalFsm->isBattVoltageOk();
}
return isVoltageOk;
}
bool BatteryImpl::isBattVoltageBelowWarnThreshold()
{
bool isVoltageBelowWarnThreshold = true;
if (0 != m_evalFsm)
{
isVoltageBelowWarnThreshold = m_evalFsm->isBattVoltageBelowWarnThreshold();
}
return isVoltageBelowWarnThreshold;
}
bool BatteryImpl::isBattVoltageBelowStopThreshold()
{
bool isVoltageBelowStopThreshold = true;
if (0 != m_evalFsm)
{
isVoltageBelowStopThreshold = m_evalFsm->isBattVoltageBelowStopThreshold();
}
return isVoltageBelowStopThreshold;
}
bool BatteryImpl::isBattVoltageBelowShutdownThreshold()
{
bool isVoltageBelowShutdownThreshold = true;
if (0 != m_evalFsm)
{
isVoltageBelowShutdownThreshold = m_evalFsm->isBattVoltageBelowShutdownThreshold();
}
return isVoltageBelowShutdownThreshold;
}
const char* BatteryImpl::getCurrentStateName()
{
if (0 == m_evalFsm)
{
return "BatteryImpl::m_evalFsm, null pointer exception";
}
return m_evalFsm->state()->toString();
}
const char* BatteryImpl::getPreviousStateName()
{
if (0 == m_evalFsm)
{
return "BatteryImpl::m_evalFsm, null pointer exception";
}
return m_evalFsm->previousState()->toString();
}
float BatteryImpl::battWarnThreshd()
{
return m_battWarnThreshd;
}
float BatteryImpl::battStopThrshd()
{
return m_battStopThrshd;
}
float BatteryImpl::battShutThrshd()
{
return m_battShutThrshd;
}
float BatteryImpl::battHyst()
{
return m_battHyst;
}