forked from WolfgangGitHub/DCCppESP32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCCppESP32.h
316 lines (284 loc) · 11.6 KB
/
DCCppESP32.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
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
/**********************************************************************
DCC COMMAND STATION FOR ESP32
COPYRIGHT (c) 2017-2019 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
**********************************************************************/
/////////////////////////////////////////////////////////////////////////////////////
// RELEASE VERSION
/////////////////////////////////////////////////////////////////////////////////////
#define VERSION "1.2.3"
/////////////////////////////////////////////////////////////////////////////////////
// PERFORMANCE CONFIGURATION
/////////////////////////////////////////////////////////////////////////////////////
// Maximum clients connected to
#define MAX_DCCPP_CLIENTS 10
/////////////////////////////////////////////////////////////////////////////////////
// S88 Timing values (in microseconds)
/////////////////////////////////////////////////////////////////////////////////////
#define S88_SENSOR_LOAD_PRE_CLOCK_TIME 50
#define S88_SENSOR_LOAD_POST_RESET_TIME 50
#define S88_SENSOR_CLOCK_PULSE_TIME 50
#define S88_SENSOR_CLOCK_PRE_RESET_TIME 50
#define S88_SENSOR_RESET_PULSE_TIME 50
#define S88_SENSOR_READ_TIME 25
#define S88_MAX_SENSORS_PER_BUS 512
#include <Arduino.h>
#include <algorithm>
#include <functional>
#include <StringArray.h>
#include <ArduinoJson.h>
#include <esp32-hal-log.h>
#include <SPI.h>
#include <FS.h>
#include <SPIFFS.h>
#include "Config.h"
// initialize default values for various pre-compiler checks to simplify logic in a lot of places
#if (defined(INFO_SCREEN_LCD) && INFO_SCREEN_LCD) || (defined(INFO_SCREEN_OLED) && INFO_SCREEN_OLED)
#define INFO_SCREEN_ENABLED true
#else
#define INFO_SCREEN_ENABLED false
#endif
#ifndef NEXTION_ENABLED
#define NEXTION_ENABLED false
#endif
#ifndef HC12_RADIO_ENABLED
#define HC12_RADIO_ENABLED false
#endif
#ifndef LCC_ENABLED
#define LCC_ENABLED false
#endif
#ifndef LOCONET_ENABLED
#define LOCONET_ENABLED false
#endif
#ifndef S88_ENABLED
#define S88_ENABLED false
#endif
#ifndef ENERGIZE_OPS_TRACK_ON_STARTUP
#define ENERGIZE_OPS_TRACK_ON_STARTUP false
#endif
#include "ConfigurationManager.h"
#include "WiFiInterface.h"
#include "InfoScreen.h"
#include "DCCppProtocol.h"
#include "DCCSignalGenerator.h"
#include "DCCSignalGenerator_Timer.h"
#if 0
#include "DCCSignalGenerator_RMT.h"
#endif
#include "DCCProgrammer.h"
#include "MotorBoard.h"
#include "Sensors.h"
#include "Locomotive.h"
#include "Outputs.h"
#include "NextionInterface.h"
#if LCC_ENABLED
#include <OpenMRN.h>
#include "LCCInterface.h"
#endif
extern std::vector<uint8_t> restrictedPins;
#if LOCONET_ENABLED
#include <LocoNetESP32UART.h>
extern LocoNetESP32Uart locoNet;
#endif
void esp32_restart();
extern bool otaComplete;
extern bool otaInProgress;
#define MUTEX_LOCK(mutex) do {} while (xSemaphoreTake(mutex, portMAX_DELAY) != pdPASS)
#define MUTEX_UNLOCK(mutex) xSemaphoreGive(mutex)
// Perform some basic configuration validations to prevent common mistakes
#if !defined(WIFI_SSID) || !defined(WIFI_PASSWORD)
#error "Invalid Configuration detected, Config_WiFi.h is a mandatory module."
#endif
#if !defined(MOTORBOARD_NAME_OPS) || !defined(MOTORBOARD_ENABLE_PIN_OPS) || \
!defined(MOTORBOARD_CURRENT_SENSE_OPS) || !defined(MOTORBOARD_TYPE_OPS) || \
!defined(MOTORBOARD_NAME_PROG) || !defined(MOTORBOARD_ENABLE_PIN_PROG) || \
!defined(MOTORBOARD_CURRENT_SENSE_PROG) || !defined(MOTORBOARD_TYPE_PROG) || \
!defined(DCC_SIGNAL_PIN_OPERATIONS) || !defined(DCC_SIGNAL_PIN_PROGRAMMING)
#error "Invalid Configuration detected, Config_MotorBoard.h is a mandatory module."
#endif
#if MOTORBOARD_ENABLE_PIN_OPS == MOTORBOARD_ENABLE_PIN_PROG
#error "Invalid Configuration detected, MOTORBOARD_ENABLE_PIN_OPS and MOTORBOARD_ENABLE_PIN_PROG must be unique."
#endif
#if DCC_SIGNAL_PIN_OPERATIONS == DCC_SIGNAL_PIN_PROGRAMMING
#error "Invalid Configuration detected, DCC_SIGNAL_PIN_OPERATIONS and DCC_SIGNAL_PIN_PROGRAMMING must be unique."
#endif
#if defined(INFO_SCREEN_OLED) && INFO_SCREEN_OLED && defined(INFO_SCREEN_LCD) && INFO_SCREEN_LCD
#error "Invalid Configuration detected, it is not supported to include both OLED and LCD support."
#endif
#if NEXTION_ENABLED
#if NEXTION_RX_PIN == NEXTION_TX_PIN
#error "Invalid Configuration detected, NEXTION_RX_PIN and NEXTION_TX_PIN must be unique."
#endif
#if HC12_RADIO_ENABLED
#if NEXTION_UART_NUM == HC12_UART_NUM
#error "Invalid Configuration detected, the Nextion and HC12 can not share the UART interface."
#endif
#if NEXTION_RX_PIN == HC12_RX_PIN
#error "Invalid Configuration detected, the Nextion and HC12 can not share the same RX Pin."
#endif
#if NEXTION_TX_PIN == HC12_TX_PIN
#error "Invalid Configuration detected, the Nextion and HC12 can not share the same TX Pin."
#endif
#endif
#if LOCONET_ENABLED
#if NEXTION_UART_NUM == LOCONET_UART
#error "Invalid Configuration detected, the Nextion and LocoNet can not share the UART interface."
#endif
#if NEXTION_RX_PIN == LOCONET_RX_PIN
#error "Invalid Configuration detected, the Nextion and LocoNet can not share the same RX Pin."
#endif
#if NEXTION_TX_PIN == LOCONET_TX_PIN
#error "Invalid Configuration detected, the Nextion and LocoNet can not share the same TX Pin."
#endif
#endif
#if S88_ENABLED
#if S88_CLOCK_PIN == NEXTION_RX_PIN
#error "Invalid Configuration detected, the Nextion RX Pin and S88_CLOCK_PIN must be unique."
#endif
#if S88_CLOCK_PIN == NEXTION_TX_PIN
#error "Invalid Configuration detected, the NEXTION_TX_PIN and S88_CLOCK_PIN must be unique."
#endif
#if S88_RESET_PIN == NEXTION_RX_PIN
#error "Invalid Configuration detected, the Nextion RX Pin and S88_RESET_PIN must be unique."
#endif
#if S88_RESET_PIN == NEXTION_TX_PIN
#error "Invalid Configuration detected, the NEXTION_TX_PIN and S88_RESET_PIN must be unique."
#endif
#if S88_LOAD_PIN == NEXTION_RX_PIN
#error "Invalid Configuration detected, the Nextion RX Pin and S88_LOAD_PIN must be unique."
#endif
#if S88_LOAD_PIN == NEXTION_TX_PIN
#error "Invalid Configuration detected, the NEXTION_TX_PIN and S88_LOAD_PIN must be unique."
#endif
#endif
#endif
#if HC12_RADIO_ENABLED
#if HC12_RX_PIN == HC12_TX_PIN
#error "Invalid Configuration detected, HC12_RX_PIN and HC12_TX_PIN must be unique."
#endif
#if S88_ENABLED
#if S88_CLOCK_PIN == HC12_RX_PIN
#error "Invalid Configuration detected, the HC12_RX_PIN and S88_CLOCK_PIN must be unique."
#endif
#if S88_CLOCK_PIN == HC12_TX_PIN
#error "Invalid Configuration detected, the HC12_TX_PIN and S88_CLOCK_PIN must be unique."
#endif
#if S88_RESET_PIN == HC12_RX_PIN
#error "Invalid Configuration detected, the HC12_RX_PIN and S88_RESET_PIN must be unique."
#endif
#if S88_RESET_PIN == HC12_TX_PIN
#error "Invalid Configuration detected, the HC12_TX_PIN and S88_RESET_PIN must be unique."
#endif
#if S88_LOAD_PIN == HC12_RX_PIN
#error "Invalid Configuration detected, the HC12_RX_PIN and S88_LOAD_PIN must be unique."
#endif
#if S88_LOAD_PIN == HC12_TX_PIN
#error "Invalid Configuration detected, the HC12_TX_PIN and S88_LOAD_PIN must be unique."
#endif
#endif
#if LOCONET_ENABLED
#if LOCONET_UART == HC12_UART_NUM
#error "Invalid Configuration detected, the LocoNet and HC12 can not share the UART interface."
#endif
#if LOCONET_RX_PIN == HC12_RX_PIN
#error "Invalid Configuration detected, the LOCONET_RX_PIN and HC12_RX_PIN must be unique."
#endif
#if LOCONET_TX_PIN == HC12_TX_PIN
#error "Invalid Configuration detected, the LOCONET_TX_PIN and HC12_TX_PIN must be unique."
#endif
#endif
#endif
#if LOCONET_ENABLED
#if S88_ENABLED
#if S88_CLOCK_PIN == LOCONET_RX_PIN
#error "Invalid Configuration detected, the LOCONET_RX_PIN and S88_CLOCK_PIN must be unique."
#endif
#if S88_CLOCK_PIN == LOCONET_TX_PIN
#error "Invalid Configuration detected, the LOCONET_TX_PIN and S88_CLOCK_PIN must be unique."
#endif
#if S88_RESET_PIN == LOCONET_RX_PIN
#error "Invalid Configuration detected, the LOCONET_RX_PIN and S88_RESET_PIN must be unique."
#endif
#if S88_RESET_PIN == LOCONET_TX_PIN
#error "Invalid Configuration detected, the LOCONET_TX_PIN and S88_RESET_PIN must be unique."
#endif
#if S88_LOAD_PIN == LOCONET_RX_PIN
#error "Invalid Configuration detected, the LOCONET_RX_PIN and S88_LOAD_PIN must be unique."
#endif
#if S88_LOAD_PIN == LOCONET_TX_PIN
#error "Invalid Configuration detected, the LOCONET_TX_PIN and S88_LOAD_PIN must be unique."
#endif
#endif
#endif
#if LCC_ENABLED
#if LCC_CAN_RX_PIN != -1
#if S88_ENABLED
#if S88_CLOCK_PIN == LCC_CAN_RX_PIN
#error "Invalid Configuration detected, LCC_CAN_RX_PIN and S88_CLOCK_PIN must be unique."
#endif
#if S88_CLOCK_PIN == LCC_CAN_RX_PIN
#error "Invalid Configuration detected, LCC_CAN_RX_PIN and S88_CLOCK_PIN must be unique."
#endif
#if S88_RESET_PIN == LCC_CAN_RX_PIN
#error "Invalid Configuration detected, LCC_CAN_RX_PIN and S88_CLOCK_PIN must be unique."
#endif
#endif
#if LOCONET_ENABLED
#if LCC_CAN_RX_PIN == LOCONET_RX_PIN
#error "Invalid Configuration detected, LCC_CAN_RX_PIN and LOCONET_RX_PIN must be unique."
#endif
#if LCC_CAN_RX_PIN == LOCONET_TX_PIN
#error "Invalid Configuration detected, LCC_CAN_RX_PIN and LOCONET_TX_PIN must be unique."
#endif
#endif
#if NEXTION_ENABLED
#if LCC_CAN_RX_PIN == NEXTION_RX_PIN
#error "Invalid Configuration detected, LCC_CAN_RX_PIN and NEXTION_RX_PIN must be unique."
#endif
#if LCC_CAN_RX_PIN == NEXTION_TX_PIN
#error "Invalid Configuration detected, LCC_CAN_RX_PIN and NEXTION_TX_PIN must be unique."
#endif
#endif
#endif
#if LCC_CAN_TX_PIN != -1
#if S88_ENABLED
#if S88_CLOCK_PIN == LCC_CAN_TX_PIN
#error "Invalid Configuration detected, LCC_CAN_TX_PIN and S88_CLOCK_PIN must be unique."
#endif
#if S88_CLOCK_PIN == LCC_CAN_TX_PIN
#error "Invalid Configuration detected, LCC_CAN_TX_PIN and S88_CLOCK_PIN must be unique."
#endif
#if S88_RESET_PIN == LCC_CAN_TX_PIN
#error "Invalid Configuration detected, LCC_CAN_TX_PIN and S88_CLOCK_PIN must be unique."
#endif
#endif
#if LOCONET_ENABLED
#if LCC_CAN_TX_PIN == LOCONET_RX_PIN
#error "Invalid Configuration detected, LCC_CAN_TX_PIN and LOCONET_RX_PIN must be unique."
#endif
#if LCC_CAN_TX_PIN == LOCONET_TX_PIN
#error "Invalid Configuration detected, LCC_CAN_TX_PIN and LOCONET_TX_PIN must be unique."
#endif
#endif
#if NEXTION_ENABLED
#if LCC_CAN_TX_PIN == NEXTION_RX_PIN
#error "Invalid Configuration detected, LCC_CAN_TX_PIN and NEXTION_RX_PIN must be unique."
#endif
#if LCC_CAN_TX_PIN == NEXTION_TX_PIN
#error "Invalid Configuration detected, LCC_CAN_TX_PIN and NEXTION_TX_PIN must be unique."
#endif
#endif
#endif
#if LCC_CAN_RX_PIN == LCC_CAN_TX_PIN && LCC_CAN_RX_PIN != -1 && LCC_CAN_TX_PIN != -1
#error "Invalid Configuration detected, LCC_CAN_RX_PIN and LCC_CAN_TX_PIN must be unique."
#endif
#endif