query_store.cpp
6.3 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2015-2016, Arkadiusz Materek (arekmat@poczta.fm)
*
* Licensed under GNU General Public License 3.0 or later.
*
* 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.
*/
#include "query_store.hpp"
namespace dali {
namespace controller {
QueryStore::QueryStore(Memory* memory, Lamp* lamp) :
mMemoryController(memory), mLampController(lamp) {
}
Status QueryStore::reset() {
Status status = mMemoryController->reset();
mLampController->onReset();
return status;
}
Status QueryStore::storeActualLevelInDtr() {
mMemoryController->setDTR(mLampController->getLevel());
return Status::OK;
}
Status QueryStore::storeDtrAsMaxLevel() {
uint8_t minLevel = mMemoryController->getMinLevel();
uint8_t maxLevel = mMemoryController->getDTR();
if (maxLevel < minLevel) {
maxLevel = minLevel;
}
if (maxLevel > DALI_LEVEL_MAX) {
maxLevel = DALI_LEVEL_MAX;
}
Status status = mMemoryController->setMaxLevel(maxLevel);
uint8_t level = mMemoryController->getActualLevel();
if ((level != 0) && (level > maxLevel)) {
mLampController->setLevel(maxLevel, 0);
}
return status;
}
Status QueryStore::storeDtrAsMinLevel() {
uint8_t minLevel = mMemoryController->getDTR();
uint8_t maxLevel = mMemoryController->getMaxLevel();
if (minLevel > maxLevel) {
minLevel = maxLevel;
}
uint8_t phiscalMinLevel = mMemoryController->getPhisicalMinLevel();
if (minLevel < phiscalMinLevel) {
minLevel = phiscalMinLevel;
}
Status status = mMemoryController->setMinLevel(minLevel);
uint8_t level = mMemoryController->getActualLevel();
if ((level != 0) && (level < minLevel)) {
mLampController->setLevel(minLevel, 0);
}
return status;
}
Status QueryStore::storeDtrAsFailureLevel() {
return mMemoryController->setFaliureLevel(mMemoryController->getDTR());
}
Status QueryStore::storePowerOnLevel() {
return mMemoryController->setPowerOnLevel(mMemoryController->getDTR());
}
Status QueryStore::storeDtrAsFadeTime() {
uint8_t fadeTime = mMemoryController->getDTR();
if (fadeTime < DALI_FADE_TIME_MIN) {
fadeTime = DALI_FADE_TIME_MIN;
}
if (fadeTime > DALI_FADE_TIME_MAX) {
fadeTime = DALI_FADE_TIME_MAX;
}
return mMemoryController->setFadeTime(fadeTime);
}
Status QueryStore::storeDtrAsFadeRate() {
uint8_t fadeRate = mMemoryController->getDTR();
if (fadeRate < DALI_FADE_RATE_MIN) {
fadeRate = DALI_FADE_RATE_MIN;
}
if (fadeRate > DALI_FADE_RATE_MAX) {
fadeRate = DALI_FADE_RATE_MAX;
}
return mMemoryController->setFadeRate(fadeRate);
}
Status QueryStore::storeDtrAsScene(uint8_t scene) {
return mMemoryController->setLevelForScene(scene, mMemoryController->getDTR());
}
Status QueryStore::removeFromScene(uint8_t scene) {
return mMemoryController->setLevelForScene(scene, DALI_MASK);
}
Status QueryStore::addToGroup(uint8_t group) {
if (group > DALI_GROUP_MAX) {
return Status::ERROR;
}
uint16_t groups = mMemoryController->getGroups();
groups |= 1 << group;
return mMemoryController->setGroups(groups);
}
Status QueryStore::removeFromGroup(uint8_t group) {
if (group > DALI_GROUP_MAX) {
return Status::ERROR;
}
uint16_t groups = mMemoryController->getGroups();
groups &= ~(1 << group);
return mMemoryController->setGroups(groups);
}
Status QueryStore::storeDtrAsShortAddr() {
uint8_t addr = mMemoryController->getDTR();
if ((addr != DALI_MASK) && ((addr >> 1) > DALI_ADDR_MAX)) {
return Status::ERROR;
}
return mMemoryController->setShortAddr(addr);
}
uint8_t QueryStore::queryStatus() {
uint8_t status = 0;
if (!isMemoryValid()) { // FIXME this should be false but for debug purpose is checked
status |= (1 << 0);
}
if (queryLampFailure()) {
status |= (1 << 1);
}
if (queryLampPowerOn()) {
status |= (1 << 2);
}
if (queryLampLimitError()) {
status |= (1 << 3);
}
if (queryIsFading()) {
status |= (1 << 4);
}
if (queryResetState()) {
status |= (1 << 5);
}
if (queryMissingShortAddr()) {
status |= (1 << 6);
}
if (!queryLampPowerSet()) {
status |= (1 << 7);
}
return status;
}
bool QueryStore::queryLampFailure() {
return mLampController->isFailure();
}
bool QueryStore::queryLampPowerOn() {
return mLampController->isPowerOn();
}
bool QueryStore::queryLampLimitError() {
return mLampController->isLimitError();
}
bool QueryStore::queryIsFading() {
return mLampController->isFading();
}
bool QueryStore::queryResetState() {
return mMemoryController->isReset();
}
bool QueryStore::queryMissingShortAddr() {
return mMemoryController->getShortAddr() == DALI_MASK;
}
bool QueryStore::queryLampPowerSet() {
return mLampController->isPowerSet();
}
uint8_t QueryStore::queryActualLevel() {
return mLampController->getLevel();
}
uint8_t QueryStore::queryMaxLevel() {
return mMemoryController->getMaxLevel();
}
uint8_t QueryStore::queryMinLevel() {
return mMemoryController->getMinLevel();
}
uint8_t QueryStore::queryPowerOnLevel() {
return mMemoryController->getPowerOnLevel();
}
uint8_t QueryStore::queryFaliureLevel() {
return mMemoryController->getFaliureLevel();
}
uint8_t QueryStore::queryFadeRateOrTime() {
uint8_t fadeRate = mMemoryController->getFadeRate();
uint8_t fadeTime = mMemoryController->getFadeTime();
return (fadeTime << 4) | fadeRate;
}
uint8_t QueryStore::queryLevelForScene(uint8_t scene) {
return mMemoryController->getLevelForScene(scene);
}
uint8_t QueryStore::queryGroupsL() {
return mMemoryController->getGroupsL();
}
uint8_t QueryStore::queryGroupsH() {
return mMemoryController->getGroupsH();
}
uint8_t QueryStore::queryRandomAddrH() {
return (uint8_t) ((mMemoryController->getRandomAddr() >> 16) & 0xff);
}
uint8_t QueryStore::queryRandomAddrM() {
return (uint8_t) ((mMemoryController->getRandomAddr() >> 8) & 0xff);
}
uint8_t QueryStore::queryRandomAddrL() {
return (uint8_t) ((mMemoryController->getRandomAddr() >> 0) & 0xff);
}
bool QueryStore::isMemoryValid() {
return mMemoryController->isValid();
}
} // namespace controller
} // namespace dali