-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDispa.ino
434 lines (365 loc) · 10.8 KB
/
Dispa.ino
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/****************************************************************************
Copyright (C) 2010 Stefan Bormann, Olaf Funke, Heiko Herholz, Martin Pischky
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************
IMPORTANT:
Note: The sale any LocoNet device hardware (including bare PCB's) that
uses this or any other LocoNet software, requires testing and certification
by Digitrax Inc. and will be subject to a licensing agreement.
Please contact Digitrax Inc. for details.
*****************************************************************************
Title : Main module of "Dispa" project.
Author: Heiko Herholz <heiko@modellbahnfrokler.de>
Date: 04-September-2010
Software: AVR-GCC
Target: AtMega168 (Prototype on "Watti", newer Hardware intended)
DESCRIPTION
This module implements the main loop, lcd access and loconet handling.
"Dispa" is intended for setting the loco address and mode that is
controlled by a simple loconet throttle as FRED, FREDI via the "dispatch"
protocol of loconet.
*****************************************************************************
Adapted: 2019-10-27 by Michael Zimmermann
- adapted and ported for use on Arduino UNO or compatible
- with OLED (or LCD)
- and display ThrottleID on OLED
- DEBUG-support added
2023-11-17 V2.2 by Michael Zimmermann
- added functionality for read and show of most interesting FrediSVs
2024-08-11 V2.3 by Michael Zimmermann
- added functionality for read and show of SV8...10, FREDI-Test added
2024-08-29 V2.4 by Michael Zimmermann
- added functionality for QRCode-Reader
used I²C-Addresses:
- 0x78 OLED-Panel ggf. mit
- 0x23 Button am OLED-Panel
- 0x39 Keypad 4x4
discrete In/Outs used for functionalities:
- 0 (used by USB)
- 1 (used by USB)
- 2
- 3 Out LCD-Panel D7
- 4 Out LCD-Panel D6
- 5 Out LCD-Panel D5
- 6 Out used by HeartBeat
- 7 Out used by LocoNet [TxD]
- 8 In used by LocoNet [RxD]
- 9 Out LCD-Panel D4
- 10 In RxD (SofwareSerial, TxD von QR-Code-Scanner)
- 11 Out LCD-Panel Enable
- 12 Out LCD-Panel R/W
- 13 Out TxD (SofwareSerial, RxD von QR-Code-Scanner)
- 14 In Button (QR-Code-Scanner)
- 15
- 16 Out LED (QR-Code-Scanner)
- 17
- 18 (used by I²C: SDA)
- 19 (used by I²C: SCL)
*****************************************************************************/
//#define DEBUG
//#define LCD
#define OLED
#if defined LCD && defined OLED
#error LCD and OLED defined
#endif
#if !defined OLED && !defined LCD
#error Neither LCD nor OLED defined
#endif
// keep following defines - unless there are problems with them:
#define FREDI_SV
#define QRCODE
#define SEND_QRCODE_DATA
#include <HeartBeat.h>
HeartBeat oHeartbeat;
//========================================================
#define SW_VERSION F("2.6")
#define SW_YEAR F("2024")
#include <LocoNet.h> // used to include ln_opc.h
#include "LocoNetFrediSV.h"
#if not defined BUTTON_SELECT
#define BUTTON_SELECT 0x01
#define BUTTON_KEYPAD 0x80
#define BUTTON_STAR 0x40
#endif
enum SPECIAL_BUTTON { NONE = 66, HASH = 98, STAR = 99 };
typedef struct
{
unsigned char ucSTAT; // slot status
unsigned char ucADR; // least significant 7 bits or short address
unsigned char ucSPD; // Speed
unsigned char ucDIRF; // direction & lower functions
unsigned char ucSS2; // slot status 2
unsigned char ucADR2; // most significant 7 bit or zero for short address
unsigned char ucSND; // the good old "upper" function group
unsigned char ucID1;
unsigned char ucID2;
} SSlotData_t;
const uint8_t DEC_STEPS(1);
const uint8_t SKIP_SELF_TEST(0x55);
const uint8_t EMERGENCY_STOP(0x01);
const uint8_t SLOTMAX(28);
static SSlotData_t SlotTabelle[SLOTMAX];
static uint8_t trk(0x05);
uint16_t ui16_ThrottleId(0);
uint16_t zahl(0);
uint8_t stelle(0);
uint8_t fahrstufen(DEC_STEPS);
uint8_t iyLineOffset(0);
// 0x01 = E5 can be send
// 0x02 = E5 sended, waits for answer
// 0x03 = E5 answer is ok
// 0x04 = E5 sends function states
// 0x80 = E5 answer is faulty
uint8_t ui8FlagSendingDisptach(0);
bool bFREDITestActive(false);
bool bReadFromQRCode(false);
boolean bShowFrediSV(false);
#ifdef FREDI_SV
const uint8_t FCT_GROUPS(4);
uint8_t ui8_currentLocoSpeed(0);
uint8_t ui8_currentFunctions[FCT_GROUPS] {0};
uint8_t ui8_mirrorLocoSpeed(0);
uint8_t ui8_mirrorFunctions[FCT_GROUPS] {0};
#endif
uint8_t ret_slot(uint8_t adr, uint8_t adr2)
{
for (uint8_t n = 1; n < SLOTMAX; n++)
{
if ((SlotTabelle[n].ucADR == adr) && (SlotTabelle[n].ucADR2 == adr2))
return n;
}
return 0;
}
uint8_t get_slot(uint8_t adr, uint8_t adr2)
{
for (uint8_t n = 1; n < SLOTMAX; n++)
{
if (((!(SlotTabelle[n].ucSTAT & (1 << 5)))) && (!(SlotTabelle[n].ucSTAT & (1 << 4))))
{
SlotTabelle[n].ucADR = adr;
SlotTabelle[n].ucADR2 = adr2;
return n;
}
}
return 0;
}
typedef struct
{
unsigned char stat1Val;
char stringVal[5];
} StatTable_t;
const uint8_t COUNT(6);
static const StatTable_t statTable[COUNT] =
{
{ DEC_MODE_128A, "128A" }, // 0000 0111 (STAT1_SL_SPDEX | STAT1_SL_SPD14 | STAT1_SL_SPD28)
{ DEC_MODE_28A, "28A " }, // 0000 0100 (STAT1_SL_SPDEX)
{ DEC_MODE_128, "128 " }, // 0000 0011 (STAT1_SL_SPD14 | STAT1_SL_SPD28)
{ DEC_MODE_28, "28 " }, // 0000 0010 (STAT1_SL_SPD14)
{ DEC_MODE_14, "14 " }, // 0000 0001 (STAT1_SL_SPD28)
{ DEC_MODE_28TRI, "28M " } // 0000 0000
};
static const char* Stat1ValToString(unsigned char s)
{
for(uint8_t i = 0; i < COUNT; i++)
if( ( s & DEC_MODE_MASK ) == statTable[i].stat1Val )
return statTable[i].stringVal;
return "? ";
}
static void lcd_ThrottleId()
{
lcd_clrxy(0, 3, 21); // is line 7 with iyLineOffset = 4
lcd_write(F("ThrottleID: 0x"));
lcd_wordAsHex(ui16_ThrottleId);
}
static void adr_lcd(uint8_t slot)
{
if (slot < SLOTMAX)
ui16_ThrottleId = (SlotTabelle[slot].ucID2 << 8) + SlotTabelle[slot].ucID1;
ui8FlagSendingDisptach = 0;
if(bShowFrediSV)
return;
lcd_clearLine(0);
lcd_goto(0, 0);
lcd_write(F("Alt: "));
if (slot < SLOTMAX)
{
if (SlotTabelle[slot].ucADR2 > 0)
lcd_wordAsDec((SlotTabelle[slot].ucADR2 * 128 + SlotTabelle[slot].ucADR));
else
lcd_wordAsDec(SlotTabelle[slot].ucADR);
lcd_goto(12, 0);
lcd_write(Stat1ValToString(SlotTabelle[slot].ucSTAT));
}
#if defined OLED
lcd_ThrottleId();
#endif
}
static void SlotManager(uint8_t adr, uint8_t adr2)
{
uint8_t slot(ret_slot(adr, adr2));
if (slot == 0)
slot = get_slot(adr, adr2);
if (slot > 0)
Slot_SL_RD(slot);
}
static void disp_put()
{
uint16_t ui16High(zahl / 128);
uint16_t ui16Low(zahl - (ui16High * 128));
SlotTabelle[2].ucADR = ui16Low;
SlotTabelle[2].ucADR2 = ui16High;
SlotTabelle[2].ucSPD = EMERGENCY_STOP;
}
static void AdresseSet()
{
lcd_clrxy(5, 1, 4);
lcd_goto(5, 1);
lcd_wordAsDec(zahl);
disp_put();
}
static void readzahl(uint8_t aktwert)
{
if (stelle == 0)
zahl = aktwert;
else
if (stelle < 4)
zahl = (zahl * 10) + aktwert;
stelle++;
AdresseSet();
}
uint8_t get_Tastatur()
{
uint16_t ui16_EditValue(0);
uint8_t ui8_buttons(0);
// returns non-zero-value in ui8_buttons when key is released after pressing him
getEditValueFromKeypad(true, 9, &ui16_EditValue, &ui8_buttons);
if(ui8_buttons)
{
lcd_clrxy(0, 2, 21); // is line 6 with iyLineOffset = 4
bReadFromQRCode = false;
ui8FlagSendingDisptach = 0;
#if defined OLED
lcd_clearLine(6);
#endif
}
if(ui8_buttons & BUTTON_SELECT) // '#'
return SPECIAL_BUTTON::HASH;
if(ui8_buttons & BUTTON_STAR) // '*'
return SPECIAL_BUTTON::STAR;
if(ui8_buttons & BUTTON_KEYPAD) // '0'...'9'
return (uint8_t)(ui16_EditValue);
return SPECIAL_BUTTON::NONE;
}
static void FahrstufenSet()
{
SlotTabelle[2].ucSTAT &= ~DEC_MODE_MASK;
SlotTabelle[2].ucSTAT |= statTable[fahrstufen].stat1Val;
lcd_goto(12, 1);
lcd_write(Stat1ValToString(SlotTabelle[2].ucSTAT));
}
void fahrstufenCHG()
{
fahrstufen++;
if (fahrstufen == COUNT)
fahrstufen = 0;
FahrstufenSet();
}
void returnToDispatchMode()
{
bShowFrediSV = false;
bFREDITestActive = false;
#if defined OLED
iyLineOffset = 0;
#endif
lcd_title();
show_new();
}
void show_new()
{
lcd_goto(0, 1);
lcd_write(F("Neu: "));
fahrstufen = DEC_STEPS;
fahrstufenCHG();
}
void HandleTastatur()
{
#if defined FREDI_SV
if(bShowFrediSV)
lcd_Handle_Dynamic_TestPage(); // dynamic page, handle always when requested
#endif
uint8_t taste(get_Tastatur());
if (taste == SPECIAL_BUTTON::NONE)
return;
if(!bShowFrediSV)
{
if (taste == SPECIAL_BUTTON::STAR) // '*'
{
stelle = 0;
zahl = 0;
disp_put();
lcd_clrxy(5, 1, 7);
}
else if (taste == SPECIAL_BUTTON::HASH) // '#'
fahrstufenCHG();
else
readzahl(taste);
}
#if defined FREDI_SV
else
{
if (taste == SPECIAL_BUTTON::STAR) // '*'
// switch from SV-Mode to dispatch-mode:
returnToDispatchMode();
else
lcd_Handle_Static_SVPages(taste);
}
#endif
}
void setup()
{
#if defined DEBUG
Serial.begin(57600);
#endif
for (uint8_t lauf = 0; lauf < SLOTMAX; lauf++)
SlotTabelle[lauf].ucSTAT=0x00;
trk = 0x07;
SlotTabelle[2].ucSTAT = 0x33;
//---- init LocoNet
InitLocoNet();
//---- init Keypad
CheckAndInitKeypad();
//---- init LCD
lcd_init();
lcd_title();
#if defined QRCODE
//---- init serial / QRCode-Reader
serial_init();
#endif
#if defined FREDI_SV
setupForFrediSV();
if(!bShowFrediSV)
#endif
show_new();
}
void loop()
{
// light the Heartbeat LED
oHeartbeat.beat();
HandleLocoNetMessages();
HandleTastatur();
#if defined QRCODE
HandleSerial();
#endif
}
// to keep heartbeat alive during delay
void yield(void) { oHeartbeat.beat(); }