-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCControlSwitch.cpp
339 lines (285 loc) · 8.87 KB
/
TCControlSwitch.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
327
328
329
330
331
332
333
334
335
336
337
338
339
//
//
//
#include "TCUtilities.h"
#include "TCControlSwitch.h"
#include "TCTurnout.h"
#include "TCAction.h"
#include "TCConfig.h"
#include "TCDigitalIO.h"
#include "TCCANBus.h"
#include "TCMPR121.h"
#include "TCLED.h"
#include "TCAssert.h"
#include "TCTeensyTouch.h"
#define TOU_THRESH 0x06
#define REL_THRESH 0x0A
enum
{
eUpdateTimeUS = 50000,
eDownDownDelayMS = 1000
};
CModule_ControlSwitch gControlSwitch;
CModule_ControlSwitch::CModule_ControlSwitch(
)
:
CModule("cnsw", sizeof(controlSwitchArray) + sizeof(controlSwitchToTurnoutMapArray), eUpdateTimeUS)
{
}
void
CModule_ControlSwitch::Setup(
void)
{
touchMode = gConfig.GetVal(eConfigVar_TouchMode);
LoadDataFromEEPROM(controlSwitchArray, eepromOffset, sizeof(controlSwitchArray));
LoadDataFromEEPROM(controlSwitchToTurnoutMapArray, eepromOffset + sizeof(controlSwitchArray), sizeof(controlSwitchToTurnoutMapArray));
SMsg_Table dummy;
TableUpdate(0, dummy);
if(touchMode == eTouchMode_BuiltIn)
{
uint32_t touchBV = 0;
for(int i = 0; i < eMaxControlSwitchCount; ++i)
{
if(controlSwitchArray[i].touchID != 0xFF)
{
touchBV |= 1 << controlSwitchArray[i].touchID;
}
}
TeensyRegisterTouchSensor(touchBV, this);
}
else if(touchMode == eTouchMode_MPR121)
{
MPRRegisterTouchSensor(0, this);
}
}
void
CModule_ControlSwitch::TearDown(
void)
{
if(touchMode == eTouchMode_BuiltIn)
{
TeensyUnRegisterTouchSensor(this);
}
else if(touchMode == eTouchMode_MPR121)
{
MPRUnRegisterTouchSensor(0, this);
}
}
void
CModule_ControlSwitch::Update(
uint32_t inDeltaTimeUS)
{
for(int i = 0; i < eMaxControlSwitchCount; ++i)
{
if(controlSwitchArray[i].id >= eMaxControlSwitchID || controlSwitchArray[i].straightDInPin >= eDIOPinCount || controlSwitchArray[i].turnDInPin >= eDIOPinCount)
continue;
if(gDigitalIO.CheckInputActivated(controlSwitchArray[i].straightDInPin))
{
ControlSwitchActivated(controlSwitchArray[i].id, eTurnDir_Straight, true);
state[i].touchDown = false;
}
else if(gDigitalIO.CheckInputActivated(controlSwitchArray[i].turnDInPin))
{
ControlSwitchActivated(controlSwitchArray[i].id, eTurnDir_Turnout, true);
state[i].touchDown = false;
}
if(!state[i].touchActivated && state[i].touchDown && gCurTimeMS - state[i].touchDownTimeMS > eDownDownDelayMS)
{
state[i].touchActivated = true;
ControlSwitchTouchedID(controlSwitchArray[i].id, true, true);
}
else if(state[i].touchActivated && !state[i].touchDown)
{
state[i].touchActivated = false;
ControlSwitchTouchedID(controlSwitchArray[i].id, false, true);
}
}
}
void
CModule_ControlSwitch::Touch(
int inTouchID)
{
uint8_t index = touchIDToControlSwitchIndexMap[inTouchID];
DebugMsg(eDbgLevel_Verbose, "CS: Touch cs id %d index %d\n", inTouchID, index);
if(index < eMaxControlSwitchCount)
{
state[index].touchDown = true;
state[index].touchDownTimeMS = gCurTimeMS;
}
}
void
CModule_ControlSwitch::Release(
int inTouchID)
{
uint8_t index = touchIDToControlSwitchIndexMap[inTouchID];
DebugMsg(eDbgLevel_Verbose, "CS: Release cs id %d index %d\n", inTouchID, index);
if(index < eMaxControlSwitchCount)
{
state[index].touchDown = false;
state[index].touchDownTimeMS = 0;
}
}
void
CModule_ControlSwitch::ControlSwitchTouchedID(
uint16_t inControlSwitchID,
bool inTouched,
bool inBroadcast)
{
if(inControlSwitchID >= eMaxControlSwitchID)
{
return;
}
SControlSwitchToTurnoutIDList* turnoutIDList = controlSwitchIDToTurnoutIDMap + inControlSwitchID;
for(int i = 0; i < turnoutIDList->count; ++i)
{
gTurnout.ControlSwitchTouchedTurnoutID(turnoutIDList->turnoutIDList[i], inTouched, inBroadcast);
}
}
void
CModule_ControlSwitch::ControlSwitchActivated(
uint16_t inControlSwitchID,
uint8_t inDirection,
bool inBroadcast)
{
DebugMsg(eDbgLevel_Basic, "CS: act %d %s\n", inControlSwitchID, inDirection == eTurnDir_Straight ? "straight" : "turn");
if(inControlSwitchID >= eMaxControlSwitchID)
{
return;
}
if(inBroadcast)
{
SMsg_ControlSwitch msg;
//SendSerialMsg(gNodeID, "CC:%d cs activated id=%d dir=%s\n", gNodeID, inID, inTurnDirection == eTurnDir_Straight ? "straight" : "turnout");
msg.timeMS = gCurTimeMS;
msg.id = inControlSwitchID;
msg.direction = inDirection;
gCANBus.SendMsg(0xFF, eMsgType_ControlSwitch, 0, sizeof(msg), &msg);
}
SControlSwitchToTurnoutIDList* turnoutIDList = controlSwitchIDToTurnoutIDMap + inControlSwitchID;
for(int i = 0; i < turnoutIDList->count; ++i)
{
gTurnout.SetTurnoutDirection(turnoutIDList->turnoutIDList[i], inDirection);
}
}
bool
CModule_ControlSwitch::TableRead(
int8_t inSrcNode,
SMsg_Table const& inProgram)
{
if(inProgram.type == eTableType_ControlSwitch)
{
if(inProgram.index >= eMaxControlSwitchCount)
{
return false;
}
gAction.SendSerial(
inSrcNode,
"CC:%d control_switch index=%d id=%d straightPin=%d turnPin=%d touchID=%d\n",
gConfig.GetVal(eConfigVar_NodeID),
inProgram.index,
controlSwitchArray[inProgram.index].id,
controlSwitchArray[inProgram.index].straightDInPin,
controlSwitchArray[inProgram.index].turnDInPin,
controlSwitchArray[inProgram.index].touchID);
return true;
}
if(inProgram.type == eTableType_ControlSwitchToTurnoutMap)
{
if(inProgram.index >= eMaxControlSwitchToTurnoutMapCount)
{
return false;
}
gAction.SendSerial(
inSrcNode,
"CC:%d turnout_map index=%d cs_id=%d tt_id1=%d tt_id2=%d\n",
gConfig.GetVal(eConfigVar_NodeID),
inProgram.index,
controlSwitchToTurnoutMapArray[inProgram.index].controlSwitchID,
controlSwitchToTurnoutMapArray[inProgram.index].turnout1ID,
controlSwitchToTurnoutMapArray[inProgram.index].turnout2ID);
return true;
}
return false;
}
bool
CModule_ControlSwitch::TableWrite(
int8_t inSrcNode,
SMsg_Table const& inProgram)
{
if(inProgram.type == eTableType_ControlSwitch)
{
if(inProgram.index >= eMaxControlSwitchCount)
{
return false;
}
controlSwitchArray[inProgram.index].id = inProgram.controlSwitch.id;
controlSwitchArray[inProgram.index].straightDInPin = inProgram.controlSwitch.straightDInPin;
controlSwitchArray[inProgram.index].turnDInPin = inProgram.controlSwitch.turnDInPin;
controlSwitchArray[inProgram.index].touchID = inProgram.controlSwitch.touchID;
WriteDataToEEPROM(controlSwitchArray + inProgram.index, eepromOffset + inProgram.index * sizeof(SControlSwitchConfig), sizeof(SControlSwitchConfig));
}
else if(inProgram.type == eTableType_ControlSwitchToTurnoutMap)
{
if(inProgram.index >= eMaxControlSwitchToTurnoutMapCount)
{
return false;
}
controlSwitchToTurnoutMapArray[inProgram.index].controlSwitchID = inProgram.trackTurnoutMap.controlSwitchID;
controlSwitchToTurnoutMapArray[inProgram.index].turnout1ID = inProgram.trackTurnoutMap.turnout1ID;
controlSwitchToTurnoutMapArray[inProgram.index].turnout2ID = inProgram.trackTurnoutMap.turnout2ID;
WriteDataToEEPROM(controlSwitchToTurnoutMapArray + inProgram.index, eepromOffset + sizeof(controlSwitchArray) + inProgram.index * sizeof(SControlSwitchToTurnoutMapConfig), sizeof(SControlSwitchToTurnoutMapConfig));
}
return TableRead(inSrcNode, inProgram);
}
bool
CModule_ControlSwitch::TableUpdate(
int8_t inSrcNode,
SMsg_Table const& inProgram)
{
memset(touchIDToControlSwitchIndexMap, 0xFF, sizeof(touchIDToControlSwitchIndexMap));
memset(controlSwitchIDToTurnoutIDMap, 0, sizeof(controlSwitchIDToTurnoutIDMap));
for(int i = 0; i < eMaxControlSwitchCount; ++i)
{
if(controlSwitchArray[i].id >= eMaxControlSwitchID || controlSwitchArray[i].straightDInPin >= eDIOPinCount || controlSwitchArray[i].turnDInPin >= eDIOPinCount)
{
continue;
}
if(controlSwitchArray[i].touchID < eDIOPinCount)
{
touchIDToControlSwitchIndexMap[controlSwitchArray[i].touchID] = i;
}
gDigitalIO.SetPinMode(controlSwitchArray[i].straightDInPin, ePinMode_Input);
gDigitalIO.SetPinMode(controlSwitchArray[i].turnDInPin, ePinMode_Input);
}
for(int i = 0; i < eMaxControlSwitchToTurnoutMapCount; ++i)
{
if(controlSwitchToTurnoutMapArray[i].controlSwitchID >= eMaxControlSwitchID)
{
continue;
}
SControlSwitchToTurnoutIDList* csToTurnoutIDList = controlSwitchIDToTurnoutIDMap + controlSwitchToTurnoutMapArray[i].controlSwitchID;
if(controlSwitchToTurnoutMapArray[i].turnout1ID < eMaxTurnoutID)
{
if(csToTurnoutIDList->count >= eMaxTurnoutsPerSwitch)
{
DebugMsg(eDbgLevel_Basic, "CS: Too many turnouts mapped to one switch\n");
continue;
}
csToTurnoutIDList->turnoutIDList[csToTurnoutIDList->count++] = controlSwitchToTurnoutMapArray[i].turnout1ID;
}
if(controlSwitchToTurnoutMapArray[i].turnout2ID < eMaxTurnoutID)
{
if(csToTurnoutIDList->count >= eMaxTurnoutsPerSwitch)
{
DebugMsg(eDbgLevel_Basic, "CS: Too many turnouts mapped to one switch\n");
continue;
}
csToTurnoutIDList->turnoutIDList[csToTurnoutIDList->count++] = controlSwitchToTurnoutMapArray[i].turnout2ID;
}
}
gAction.SendSerial(
inSrcNode,
"CC:%d table_update control_switch\n",
gConfig.GetVal(eConfigVar_NodeID));
return true;
}