-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtruckLightAndFunction.ino
320 lines (291 loc) · 13.2 KB
/
truckLightAndFunction.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
/************************************
* truckLightAndFunction v0.1.0
* Date: 18.09.2022 | 00:06
* <Truck Light and function module>
* Copyright (C) 2020 Marina Egner <[email protected]>
*
* 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 <https://www.gnu.org/licenses/>.
************************************/
#define US 0
#define EU 1
/************************************
* Configuration Programm
************************************/
#include "config.h" // Configuration File
#include "ioMapping.h" // IO Mapping File
/************************************
* Include Module and Library Files
************************************/
#include "readPPMdata.h" // read Data from Buffer
#include "lightFunctions.h"
#if (DEBUGLEVEL >=1)
#include "debugging.h" // Handles debbuging info
#endif
#include "serialCommMaster.h"
struct {
uint8_t poti[2];
uint8_t lowerSwitch[2];
uint8_t upperSwitch[4];
} channel1;
struct {
uint8_t lowerSwitch[2];
uint8_t upperSwitch[4];
} channel2;
struct Lights {
bool state;
bool out;
};
Lights parkLight;
Lights lowBeamLight;
Lights highBeamLight;
Lights highBeamLightFlash;
Lights leftFlashLight;
Lights rightFlashLight;
Lights fogLight;
Lights hazardLight;
Lights beaconLight;
Lights auxLight;
Lights brakeLight;
Lights reverseLight;
uint8_t channel3Switch = 0;
//Setup Serial and check if Board is UNO with one Serial or Leonardo/Micro with to Serials
#ifdef HAVE_HWSERIAL1 //if serial ports 1 exist then the arduino has more than one serial port
#ifndef SerialUSB //if not allready defined
#define SerialUSB SERIAL_PORT_MONITOR //then define monitor port
#endif
#else
#if (DEBUGLEVEL >1) //if serial ports are the same debuging is not possible (for example on UNO)
#define DEBUGLEVEL 1 //do not change!!!
#endif
#endif
#ifndef SerialHW //if not allready defined
#define SerialHW SERIAL_PORT_HARDWARE //then define hardware port
#endif
void setup() {
// put your setup code here, to run once:
/************************************
* Setup Inputs
************************************/
pinMode(inFunction1ControlPPM, INPUT_PULLUP);
pinMode(inFunction2ControlPPM, INPUT_PULLUP);
pinMode(inSoundPPM, INPUT);
pinMode(inBrakeSignal, INPUT_PULLUP);
pinMode(inReverseSignal, INPUT_PULLUP);
/************************************
* Setup Outputs
************************************/
initLightOutput();
setupLightOutput(outParkingLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outLowBeamLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outHighBeamLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outFogLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outFrontLeftFlashLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outFrontRightFlashLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outRearLeftFlashLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outRearRightFlashLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outReverseLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outBrakeLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
setupLightOutput(outAuxLight, LIGHT_FADE_ON_TIME, LIGHT_FADE_OFF_TIME);
/************************************
* Setup Functions
************************************/
initInterrupts(inFunction1ControlPPM, inFunction2ControlPPM, inSoundPPM);
#if (DEBUGLEVEL >=1)
debuggingInit(DEBUGLEVEL, outStatusLed);
#endif
#if (SERIAL_COM)
serialConfigure(&SerialHW, // Serial interface on arduino
19200, // Baudrate
SERIAL_8N1, // e.g. SERIAL_8N1 | start bit, data bit, stop bit
1000,
SERIAL_TURNAROUND,
outTxEnablePin // Pin to switch between Transmit and Receive
);
#endif
}
void loop() { // put your main code here, to run repeatedly:
bool errorFlag = false;
/*
* Read Switches and Potis from Multiswitches
* Some switches are commented as they are not yet in use.
*/
channel1.poti[0] = getChannel1Poti(0, 0);
channel1.poti[1] = getChannel1Poti(1, 0);
channel1.lowerSwitch[0] = getChannel1Switch(0, DIRECTION_MID); // Function to get the value of the Switches from Channel 1
channel1.lowerSwitch[1] = getChannel1Switch(1, DIRECTION_MID); // Function to get the value of the Switches from Channel 1
channel1.upperSwitch[0] = getChannel1Switch(2, DIRECTION_DOWN); // Function to get the value of the Switches from Channel 1
channel1.upperSwitch[1] = getChannel1Switch(3, DIRECTION_DOWN); // Function to get the value of the Switches from Channel 1
channel1.upperSwitch[2] = getChannel1Switch(4, DIRECTION_DOWN); // Function to get the value of the Switches from Channel 1
channel1.upperSwitch[3] = getChannel1Switch(5, DIRECTION_MID); // Function to get the value of the Switches from Channel 1
channel2.lowerSwitch[0] = getChannel2Switch(5, DIRECTION_DOWN); // Function to get the value of the Switches from Channel 2
channel2.lowerSwitch[1] = getChannel2Switch(4, DIRECTION_DOWN); // Function to get the value of the Switches from Channel 2
channel2.upperSwitch[0] = getChannel2Switch(3, DIRECTION_DOWN); // Function to get the value of the Switches from Channel 2
channel2.upperSwitch[1] = getChannel2Switch(2, DIRECTION_UP); // Function to get the value of the Switches from Channel 2
channel2.upperSwitch[2] = getChannel2Switch(1, DIRECTION_DOWN); // Function to get the value of the Switches from Channel 2
channel2.upperSwitch[3] = getChannel2Switch(0, DIRECTION_DOWN); // Function to get the value of the Switches from Channel 2
channel3Switch = getChannel3Signal();
/*
* Map switches to Functions
*/
highBeamLight.state = mapSwitchToFunction(channel1.upperSwitch[3], true, false, false); // Function to map a Key [Down, Mid, Up]
highBeamLightFlash.state = mapSwitchToFunction(channel1.upperSwitch[3], false, false, true); // Function to map a Key [Down, Mid, Up]
leftFlashLight.state = mapSwitchToFunction(channel1.lowerSwitch[1], true, false, false); // Function to map a Key [Down, Mid, Up]
rightFlashLight.state = mapSwitchToFunction(channel1.lowerSwitch[1], false, false, true); // Function to map a Key [Down, Mid, Up]
parkLight.state = mapSwitchToFunction(channel2.lowerSwitch[0], false, true, true); // Function to map a Key [Down, Mid, Up]
lowBeamLight.state = mapSwitchToFunction(channel2.lowerSwitch[0], false, false, true); // Function to map a Key [Down, Mid, Up]
fogLight.state = mapSwitchToFunction(channel2.upperSwitch[0], false, false, true); // Function to map a Key [Down, Mid, Up]
hazardLight.state = mapSwitchToFunction(channel2.upperSwitch[1], false, false, true); // Function to map a Key [Down, Mid, Up]
beaconLight.state = mapSwitchToFunction(channel2.upperSwitch[2], false, false, true); // Function to map a Key [Down, Mid, Up]
auxLight.state = mapSwitchToFunction(channel2.upperSwitch[3], false, false, true); // Function to map a Key [Down, Mid, Up]
reverseLight.state = !digitalRead(inReverseSignal);
brakeLight.state = !digitalRead(inBrakeSignal);
/*
* Write Light function state to the output var
*/
parkLight.out = directlyToOutput(parkLight.state);
lowBeamLight.out = directlyToOutput(lowBeamLight.state);
highBeamLight.out = highBeamFlash(highBeamLight.state, highBeamLightFlash.state, HIGH_BEAM_FLASH_FREQUENCY);
fogLight.out = directlyToOutput(fogLight.state);
beaconLight.out = directlyToOutput(beaconLight.state);
auxLight.out = directlyToOutput(auxLight.state);
reverseLight.out = directlyToOutput(reverseLight.state);
brakeLight.out = directlyToOutput(brakeLight.state);
setFlasherLight(leftFlashLight.state, rightFlashLight.state, hazardLight.state, &leftFlashLight.out, &rightFlashLight.out, BLINKER_FREQUENCY);
bool starterDiming = false;
if(channel3Switch == DIRECTION_DOWN) starterDiming = true;
/*
* Set Outputs
*/
uint8_t normalDimming = starterDimming(starterDiming, SOFT_PWM_HIGH, STARTER_DIMM_DIVISOR, STARTER_DIMM_MULTI1);
uint8_t parkDimming = starterDimming(starterDiming, PARKING_DIMM, STARTER_DIMM_DIVISOR, STARTER_DIMM_MULTI1);
setBooleanLight(outParkingLight, parkLight.out, parkDimming);
#if (HEADLIGHT_IS_PARKING && HEADLIGHT_IS_HIGHBEAM)
uint8_t headParkDimming = starterDimming(starterDiming, HEADLIGHT_PARKING_VALUE, STARTER_DIMM_DIVISOR, STARTER_DIMM_MULTI1);
uint8_t headLowDimming = starterDimming(starterDiming, HEADLIGHT_LOWBEAM_VALUE, STARTER_DIMM_DIVISOR, STARTER_DIMM_MULTI1);
uint8_t headHighDimming = starterDimming(starterDiming, HEADLIGHT_HIGHBEAM_VALUE, STARTER_DIMM_DIVISOR, STARTER_DIMM_MULTI1);
setCombinedHeadlightAll(outLowBeamLight,
parkLight.out,
lowBeamLight.out,
highBeamLight.out,
headParkDimming,
headLowDimming,
headHighDimming);
#elif (HEADLIGHT_IS_HIGHBEAM)
uint8_t headLowDimming = starterDimming(starterDiming, HEADLIGHT_LOWBEAM_VALUE, STARTER_DIMM_DIVISOR, STARTER_DIMM_MULTI1);
uint8_t headHighDimming = starterDimming(starterDiming, HEADLIGHT_HIGHBEAM_VALUE, STARTER_DIMM_DIVISOR, STARTER_DIMM_MULTI1);
setCombinedHeadlightHighOnly(outLowBeamLight,
lowBeamLight.out,
highBeamLight.out,
headLowDimming,
headHighDimming);
#elif (HEADLIGHT_IS_PARKING)
uint8_t headParkDimming = starterDimming(starterDiming, HEADLIGHT_PARKING_VALUE, STARTER_DIMM_DIVISOR, STARTER_DIMM_MULTI1);
uint8_t headLowDimming = starterDimming(starterDiming, HEADLIGHT_LOWBEAM_VALUE, STARTER_DIMM_DIVISOR, STARTER_DIMM_MULTI1);
setCombinedHeadlightParkOnly(outLowBeamLight,
parkLight.out,
lowBeamLight.out,
headParkDimming,
headLowDimming);
#else
setBooleanLight(outLowBeamLight, lowBeamLight.out, normalDimming);
#endif
setBooleanLight(outHighBeamLight, highBeamLight.out, normalDimming);
setBooleanLight(outFogLight, fogLight.out, normalDimming);
setBooleanLight(outAuxLight, auxLight.out, normalDimming);
setBooleanLight(outFrontLeftFlashLight, leftFlashLight.out, normalDimming);
setBooleanLight(outFrontRightFlashLight, rightFlashLight.out, normalDimming);
setBooleanLight(outReverseLight, reverseLight.out, normalDimming);
setBooleanLight(outBrakeLight, brakeLight.out, normalDimming);
#if (COUNTRY_OPTION == EU)
setBooleanLight(outRearLeftFlashLight, leftFlashLight.out, normalDimming);
setBooleanLight(outRearRightFlashLight, rightFlashLight.out, normalDimming);
setBooleanLight(outBrakeLight, brakeLight.out, normalDimming);
#endif
setBooleanLight(outReverseLight, reverseLight.out, normalDimming);
/*
* Setup serial communication
*/
// Function 1 is Light information data which has only one byte
/* 0 -> parking light,
1 -> brake light,
2 -> reversing lights,
3 -> right blinker,
4 -> left blinker,
5 -> auxiliary light,
6 -> beacon light
7 -> dimm light
*/
setLightData(PARKLIGHT, parkLight.out);
setLightData(BRAKELIGHT, brakeLight.out);
setLightData(REVERSELIGHT, reverseLight.out);
setLightData(RIGHTBLINK, rightFlashLight.out);
setLightData(LEFTBLINK, leftFlashLight.out);
setLightData(AUXLIGHT, auxLight.out);
setLightData(BEACONLIGHT, beaconLight.out);
setLightData(DIMMLIGHTS, starterDiming);
//setServoData();
serialUpdate();
/*
* Setup Debugging
*/
#if (DEBUGLEVEL >=1)
controllerStatus(errorFlag, outStatusLed);
#endif
#if (DEBUGLEVEL == 2)
debugChannelEvaluation(channel1.poti[0],
channel1.poti[1],
channel1.lowerSwitch[0],
channel1.lowerSwitch[1],
channel1.upperSwitch[0],
channel1.upperSwitch[1],
channel1.upperSwitch[2],
channel1.upperSwitch[3],
channel2.lowerSwitch[0],
channel2.lowerSwitch[1],
channel2.upperSwitch[0],
channel2.upperSwitch[1],
channel2.upperSwitch[2],
channel2.upperSwitch[3]);
#endif
#if(DEBUGLEVEL == 3)
debugFunctionState(parkLight.state,
lowBeamLight.state,
highBeamLight.state,
highBeamLightFlash.state,
fogLight.state,
beaconLight.state,
auxLight.state,
hazardLight.state,
leftFlashLight.state,
rightFlashLight.state,
reverseLight.state,
brakeLight.state);
#endif
#if(DEBUGLEVEL == 4)
debugFunctionOut(parkLight.out,
lowBeamLight.out,
highBeamLight.out,
highBeamLightFlash.out,
fogLight.out,
beaconLight.out,
auxLight.out,
hazardLight.out,
leftFlashLight.out,
rightFlashLight.out,
reverseLight.state,
brakeLight.state);
#endif
#if (DEBUGLEVEL == 5)
SerialUSB.println(starterDiming);
//SerialUSB.println(digitalRead(inSoundPPM));
#endif
}