forked from DCC-EX/CommandStation-EX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenuTrainCvWrite.cpp
326 lines (279 loc) · 7.09 KB
/
menuTrainCvWrite.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* LaBox Project
* menuTrainCvWrite Classes
*
* @Author : Thierry Paris
* @Organization : Locoduino.org
*/
#include "defines.h"
#include "DCC.h"
#ifdef USE_HMI
#include "menuobject.h"
#include "menuTrainCvWrite.h"
#include "hmi.h"
#include "LaboxModes.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int writeCVValue = -1;
int writeCVAddress = 0;
bool writeDisplayInProgress;
char messageWrite[21];
enum StateWriting
{
// Select Up Down
Ready, // Ready to escape from the dialog. Back to menu -- --
FixingAddress, // Select CV Address Fix Value +1 -1
FixingValue, // Select CV Value Start Writing +1 -1
Writing, // Waiting for end of writing ... -- -- --
Written_OK, // CV written without problem ... Quit OtherCV --
Written_FAIL // CV written WITH problem ... Quit Redo --
};
StateWriting writeState;
void cvWriteValueCallback(int16_t inValue)
{
if (inValue > 0)
writeState = Written_OK;
else
writeState = Written_FAIL;
writeDisplayInProgress = false;
}
#define DIAGWRITE(FCT) //DIAG(F("%s : %s CV:%d Val:%d"), FCT, writeState==Ready?"Ready":writeState == FixingAddress?"Fixing Address": \
//writeState==FixingValue?"Fixing value":"Writing", writeCVAddress, writeCVValue);
void menuTrainCvWrite::start()
{
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::start.. Begin");
if (!LaboxModes::progMode) {
LaboxModes::Restart(CVWRITE);
}
if (writeCVAddress > 0)
writeState = FixingAddress;
else
writeState = Ready;
writeDisplayInProgress = false;
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::start.. End");
}
/*!
@brief menuTrainCvWrite Constructor
@param Screen, an Adafruit_SSD1306 object to permit to display our menu
@param p, a parent object. All menu are chained between parent and sons
@param title, a menu has a title, a char[HMI_MenuMessageSize].
@param val, a integer which define the kind of menu or the returned value after selection
@return None (void).
@note
*/
menuTrainCvWrite::menuTrainCvWrite(Adafruit_SSD1306* screen, menuObject* p, const char* title, int value): menuObject(screen, p, title, value)
{
resetMenu();
}
/*!
@brief eventUp, Notification of a button event
@param None
@return None (void).
@note
*/
void menuTrainCvWrite::eventUp()
{
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::eventUp.. Begin");
DIAGWRITE("up begin");
menuObject::eventUp();
if (writeState == Ready || writeState == FixingAddress)
{
writeState = FixingAddress;
writeCVValue = -1;
writeCVAddress++;
if (writeCVAddress > 255)
writeCVAddress = 255;
else
writeDisplayInProgress = false;
}
if (writeState == FixingValue)
{
writeCVValue++;
if (writeCVValue > 255)
writeCVValue = 255;
else
writeDisplayInProgress = false;
}
if (writeState == Written_FAIL)
{
DIAGWRITE("up end value");
void (*ptr)(int16_t) = &cvWriteValueCallback;
DCC::writeCVByte(writeCVAddress, writeCVValue, ptr);
writeState = Writing;
writeDisplayInProgress = false;
}
if (writeState == Written_OK)
{
writeState = FixingAddress;
writeDisplayInProgress = false;
}
DIAGWRITE("up end");
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::eventUp.. End");
}
/*!
@brief eventDown, Notification of a button event
@param None
@return None (void).
@note
*/
void menuTrainCvWrite::eventDown()
{
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::eventDown.. Begin");
DIAGWRITE("down begin");
menuObject::eventDown();
if (writeState == Ready || writeState == FixingAddress)
{
writeState = FixingAddress;
writeCVValue = -1;
if (writeCVAddress > 0) {
writeCVAddress--;
writeDisplayInProgress = false;
}
if (writeCVAddress == 0) {
writeState = Ready;
writeDisplayInProgress = false;
}
}
if (writeState == FixingValue)
{
if (writeCVValue > 0) {
writeCVValue--;
writeDisplayInProgress = false;
}
}
if (writeState == Written_FAIL)
{
writeState = FixingAddress;
writeDisplayInProgress = false;
}
DIAGWRITE("down end");
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::eventDown.. End");
}
/*!
@brief eventSelect, Notification of a button event
@param None
@return None (void).
@note
*/
int menuTrainCvWrite::eventSelect()
{
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::eventSelect.. Begin");
DIAGWRITE("select begin");
if (writeState == FixingAddress) {
DIAGWRITE("select end address");
writeState = FixingValue;
writeDisplayInProgress = false;
return 0;
}
if (writeState == FixingValue) {
DIAGWRITE("select end value");
void (*ptr)(int16_t) = &cvWriteValueCallback;
DCC::writeCVByte(writeCVAddress, writeCVValue, ptr);
writeState = Writing;
writeDisplayInProgress = false;
return 0;
}
if (writeState == Ready || writeState == Written_OK || writeState == Written_FAIL)
{
LaboxModes::Restart(SILENTRETURNTOMAIN);
}
DIAGWRITE("select end");
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::eventSelect.. End");
return MENUEXIT;
}
/*!
@brief Setup HMI class and start HMI
@param None
@return None (void).
@note
*/
void menuTrainCvWrite::begin()
{
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::begin.. Begin");
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::begin.. End");
}
/*!
@brief update, call to refresh screen
@param None
@return None (void).
@note
*/
void menuTrainCvWrite::update()
{
_HMIDEBUG_FCT_PRINTLN(F("menuTrainCvWrite::update.. Begin"));
if(!writeDisplayInProgress)
{
display->clearDisplay();
writeDisplayInProgress = true;
}
display->setTextSize(1);
display->setCursor(5, 6);
display->println(TXT_CVWRITE_LOGO);
display->display();
display->setCursor(20, 26);
switch (writeState)
{
case Written_OK:
display->println(TXT_CVRW_CVOK);
break;
case Written_FAIL:
display->println(TXT_CVRW_CVFAIL);
break;
default:
display->println(TXT_CVRW_CVVALUE);
break;
}
char mess1[10];
if (writeCVAddress > 0)
sprintf(mess1, "%03d:", writeCVAddress);
else
sprintf(mess1, "---:");
if (writeCVValue >= 0)
sprintf(messageWrite, "%s%03d", mess1, writeCVValue);
else
sprintf(messageWrite, "%s---", mess1);
display->setTextColor(WHITE);
display->setTextSize(2);
display->setCursor(20, 38);
display->println(messageWrite);
display->setTextSize(1);
display->setCursor(5, 56);
//display->fillRect(5, 56, 128-5, 64-56, 0);
switch (writeState)
{
case Ready:
display->println(TXT_CVRW_READY);
break;
case FixingAddress:
display->println(TXT_CVWRITE_ADDRESS);
break;
case FixingValue:
display->println(TXT_CVWRITE_VALUE);
break;
case Writing:
display->println(TXT_CVWRITE_WRITING);
break;
case Written_OK:
display->println(TXT_CVRW_OK);
break;
case Written_FAIL:
display->println(TXT_CVRW_FAIL);
break;
}
display->display();
_HMIDEBUG_FCT_PRINTLN(F("menuTrainCvWrite::update.. End"));
}
/*!
@brief resetMenu,
@param None
@return None (void).
@note
*/
void menuTrainCvWrite::resetMenu()
{
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::resetMenu.. Begin");
menuObject::resetMenu();
writeDisplayInProgress = false;
_HMIDEBUG_FCT_PRINTLN("menuTrainCvWrite::resetMenu.. End");
}
#endif