-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathformgps_sections.cpp
316 lines (289 loc) · 12.4 KB
/
formgps_sections.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
// Copyright (C) 2024 Michael Torrie and the QtAgOpenGPS Dev Team
// SPDX-License-Identifier: GNU General Public License v3.0 or later
//
// Mainly concerned with module communication on section related PGN data
#include "formgps.h"
#include "qmlutil.h"
#include "common.h"
#include "cpgn.h"
/* SectionSetPosition(), SectionCalcWidths(), and SectionCalcMulti() are all in CTool */
void FormGPS::BuildMachineByte()
{
if (tool.isSectionsNotZones)
{
p_254.pgn[p_254.sc1to8] = 0;
p_254.pgn[p_254.sc9to16] = 0;
int number = 0;
for (int j = 0; j < 8; j++)
{
if (tool.section[j].isSectionOn)
number |= 1 << j;
}
p_254.pgn[p_254.sc1to8] = (char)number;
number = 0;
for (int j = 8; j < 16; j++)
{
if (tool.section[j].isSectionOn)
number |= 1 << (j-8);
}
p_254.pgn[p_254.sc9to16] = (char)number;
//machine pgn
p_239.pgn[p_239.sc9to16] = p_254.pgn[p_254.sc9to16];
p_239.pgn[p_239.sc1to8] = p_254.pgn[p_254.sc1to8];
p_229.pgn[p_229.sc1to8] = p_254.pgn[p_254.sc1to8];
p_229.pgn[p_229.sc9to16] = p_254.pgn[p_254.sc9to16];
p_229.pgn[p_229.toolLSpeed] = (char)(tool.farLeftSpeed * 10);
p_229.pgn[p_229.toolRSpeed] = (char)(tool.farRightSpeed * 10);
}
else
{
//zero all the bytes - set only if on
for (int i = 5; i < 13; i++)
{
p_229.pgn[i] = 0;
}
int number = 0;
for (int k = 0; k < 8; k++)
{
for (int j = 0; j < 8; j++)
{
if (tool.section[j + k * 8].isSectionOn)
number |= 1 << j;
}
p_229.pgn[5 + k] = (char)number;
number = 0;
}
//tool speed to calc ramp
p_229.pgn[p_229.toolLSpeed] = (char)(tool.farLeftSpeed * 10);
p_229.pgn[p_229.toolRSpeed] = (char)(tool.farRightSpeed * 10);
p_239.pgn[p_239.sc1to8] = p_229.pgn[p_229.sc1to8];
p_239.pgn[p_239.sc9to16] = p_229.pgn[p_229.sc9to16];
p_254.pgn[p_254.sc1to8] = p_229.pgn[p_229.sc1to8];
p_254.pgn[p_254.sc9to16] = p_229.pgn[p_229.sc9to16];
}
p_239.pgn[p_239.speed] = (char)(vehicle.avgSpeed * 10);
p_239.pgn[p_239.tram] = (char)tram.controlByte;
}
void FormGPS::DoRemoteSwitches()
{
//MTZ8302 Feb 2020
if (isJobStarted)
{
//MainSW was used
if (mc.ss[mc.swMain] != mc.ssP[mc.swMain])
{
//Main SW pressed
if ((mc.ss[mc.swMain] & 1) == 1)
{
autoBtnState = btnStates::Off;
} // if Main SW ON
//if Main SW in Arduino is pressed OFF
if ((mc.ss[mc.swMain] & 2) == 2)
{
autoBtnState = btnStates::Auto;
} // if Main SW OFF
mc.ssP[mc.swMain] = mc.ss[mc.swMain];
} //Main or Rate SW
if (tool.isSectionsNotZones)
{
if (mc.ss[mc.swOnGr0] != 0)
{
// ON Signal from Arduino
if ((mc.ss[mc.swOnGr0] & 128) == 128 && tool.numOfSections > 7)
{
tool.sectionButtonState.set(7,btnStates::On);
tool.section[7].sectionBtnState = btnStates::On;
//TODO: not sure why we have redundant states like that
}
if ((mc.ss[mc.swOnGr0] & 64) == 64 && tool.numOfSections > 6)
{
tool.sectionButtonState.set(6,btnStates::On);
tool.section[6].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr0] & 32) == 32 && tool.numOfSections > 5)
{
tool.sectionButtonState.set(5,btnStates::On);
tool.section[5].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr0] & 16) == 16 && tool.numOfSections > 4)
{
tool.sectionButtonState.set(4,btnStates::On);
tool.section[4].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr0] & 8) == 8 && tool.numOfSections > 3)
{
tool.sectionButtonState.set(3,btnStates::On);
tool.section[3].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr0] & 4) == 4 && tool.numOfSections > 2)
{
tool.sectionButtonState.set(2,btnStates::On);
tool.section[2].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr0] & 2) == 2 && tool.numOfSections > 1)
{
tool.sectionButtonState.set(1,btnStates::On);
tool.section[1].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr0] & 1) == 1)
{
tool.sectionButtonState.set(0,btnStates::On);
tool.section[0].sectionBtnState = btnStates::On;
}
mc.ssP[mc.swOnGr0] = mc.ss[mc.swOnGr0];
} //if swONLo != 0
else { if (mc.ssP[mc.swOnGr0] != 0) { mc.ssP[mc.swOnGr0] = 0; } }
if (mc.ss[mc.swOnGr1] != 0)
{
// sections ON signal from Arduino
if ((mc.ss[mc.swOnGr1] & 128) == 128 && tool.numOfSections > 15)
{
tool.sectionButtonState.set(15,btnStates::On);
tool.section[15].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr1] & 64) == 64 && tool.numOfSections > 14)
{
tool.sectionButtonState.set(14,btnStates::On);
tool.section[14].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr1] & 32) == 32 && tool.numOfSections > 13)
{
tool.sectionButtonState.set(13,btnStates::On);
tool.section[13].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr1] & 16) == 16 && tool.numOfSections > 12)
{
tool.sectionButtonState.set(12,btnStates::On);
tool.section[12].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr1] & 8) == 8 && tool.numOfSections > 11)
{
tool.sectionButtonState.set(11,btnStates::On);
tool.section[11].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr1] & 4) == 4 && tool.numOfSections > 10)
{
tool.sectionButtonState.set(10,btnStates::On);
tool.section[10].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr1] & 2) == 2 && tool.numOfSections > 9)
{
tool.sectionButtonState.set(9,btnStates::On);
tool.section[9].sectionBtnState = btnStates::On;
}
if ((mc.ss[mc.swOnGr1] & 1) == 1 && tool.numOfSections > 8)
{
tool.sectionButtonState.set(8,btnStates::On);
tool.section[8].sectionBtnState = btnStates::On;
}
mc.ssP[mc.swOnGr1] = mc.ss[mc.swOnGr1];
} //if swONHi != 0
else { if (mc.ssP[mc.swOnGr1] != 0) { mc.ssP[mc.swOnGr1] = 0; } }
// Switches have changed
if (mc.ss[mc.swOffGr0] != mc.ssP[mc.swOffGr0])
{
//if Main = Auto then change section to Auto if Off signal from Arduino stopped
if (autoBtnState == btnStates::Auto)
{
for(int s=0; s< 8; s++) {
if ((mc.ssP[mc.swOffGr0] & (1 << s)) && !(mc.ss[mc.swOffGr0] & (1 << s)) && (tool.sectionButtonState.get(s) == btnStates::Off))
{
tool.sectionButtonState.set(s,btnStates::Auto);
tool.section[s].sectionBtnState = btnStates::Auto;
}
}
}
mc.ssP[mc.swOffGr0] = mc.ss[mc.swOffGr0];
}
if (mc.ss[mc.swOffGr1] != mc.ssP[mc.swOffGr1])
{
//if Main = Auto then change section to Auto if Off signal from Arduino stopped
if (autoBtnState == btnStates::Auto)
{
for(int s=8; s< 16; s++) {
if ((mc.ssP[mc.swOffGr1] & (1 << s)) && !(mc.ss[mc.swOffGr1] & (1 << s)) && (tool.sectionButtonState.get(s+8) == btnStates::Off))
{
tool.sectionButtonState.set(s+8,btnStates::Auto);
tool.section[s+8].sectionBtnState = btnStates::Auto;
}
}
}
mc.ssP[mc.swOffGr1] = mc.ss[mc.swOffGr1];
}
// OFF Signal from Arduino
if (mc.ss[mc.swOffGr0] != 0)
{
//if section SW in Arduino is switched to OFF; check always, if switch is locked to off GUI should not change
for(int s=0; s< 8; s++) {
if ((mc.ss[mc.swOffGr0] & (1 << s)) && tool.sectionButtonState.get(s) != btnStates::Off)
{
tool.sectionButtonState.set(s,btnStates::Off);
tool.section[s].sectionBtnState = btnStates::Off;
}
}
} // if swOFFLo !=0
if (mc.ss[mc.swOffGr1] != 0)
{
//if section SW in Arduino is switched to OFF; check always, if switch is locked to off GUI should not change
for (int s=0; s<8; s++) {
if ((mc.ss[mc.swOffGr0] & (1 << s)) && tool.sectionButtonState.get(s+8) != btnStates::Off)
{
tool.sectionButtonState.set(s+8,btnStates::Off);
tool.section[s+8].sectionBtnState = btnStates::Off;
}
}
} // if swOFFHi !=0
}//if serial or udp port open
else
{
//DoZones
int Bit;
// zones to on
if (mc.ss[mc.swOnGr0] != 0)
{
for (int i = 0; i < 8; i++)
{
Bit = 1 << i;
if ((tool.zoneRanges[i + 1] > 0) && ((mc.ss[mc.swOnGr0] & Bit) == Bit))
{
tool.section[tool.zoneRanges[i + 1] - 1].sectionBtnState = btnStates::On;
tool.sectionButtonState.set(tool.zoneRanges[i + 1] - 1,btnStates::On);
}
}
mc.ssP[mc.swOnGr0] = mc.ss[mc.swOnGr0];
}
else { if (mc.ssP[mc.swOnGr0] != 0) { mc.ssP[mc.swOnGr0] = 0; } }
// zones to auto
if (mc.ss[mc.swOffGr0] != mc.ssP[mc.swOffGr0])
{
if (autoBtnState == btnStates::Auto)
{
for (int i = 0; i < 8; i++)
{
Bit = 1 << i;
if ((tool.zoneRanges[i + 1] > 0) && ((mc.ssP[mc.swOffGr0] & Bit) == Bit)
&& ((mc.ss[mc.swOffGr0] & Bit) != Bit) && (tool.section[tool.zoneRanges[i + 1] - 1].sectionBtnState == btnStates::Off))
{
tool.section[tool.zoneRanges[i + 1] - 1].sectionBtnState = btnStates::Auto;
tool.sectionButtonState.set(tool.zoneRanges[i + 1] - 1,btnStates::Auto);
}
}
}
mc.ssP[mc.swOffGr0] = mc.ss[mc.swOffGr0];
}
// zones to off
if (mc.ss[mc.swOffGr0] != 0)
{
for (int i = 0; i < 8; i++)
{
Bit = 1 << i;
if ((tool.zoneRanges[i + 1] > 0) && ((mc.ss[mc.swOffGr0] & Bit) == Bit) && (tool.section[tool.zoneRanges[i + 1] - 1].sectionBtnState != btnStates::Off))
{
tool.section[tool.zoneRanges[i + 1] - 1].sectionBtnState = btnStates::Off;
tool.sectionButtonState.set(tool.zoneRanges[i + 1] - 1,btnStates::Off);
}
}
}
}
}
}