This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ino
308 lines (265 loc) · 7.92 KB
/
main.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
/* Include PCH (contians most commonly used headers) */
#include "src/PCH.h"
/* Include Managers */
#include "src/Managers/ManagerRelay.h"
#include "src/Managers/ManagerEngine.h"
#include "src/Managers/ManagerAlarm.h"
/* Include Libraries */
#include "src/SoftwareSerial/SoftwareSerial.h" /* custom */
SoftwareSerial BT(BLUETOOTH_RX, BLUETOOTH_TX);
ManagerRelay RelayManager = ManagerRelay();
ManagerAlarm AlarmManager = ManagerAlarm(&RelayManager);
ManagerEngine EngineManager = ManagerEngine(&RelayManager, &AlarmManager);
void setup()
{
/*
Clock Setups
*/
if (F_CPU == 8000000)
clock_prescale_set(clock_div_2);
if (F_CPU == 4000000)
clock_prescale_set(clock_div_4);
if (F_CPU == 2000000)
clock_prescale_set(clock_div_8);
if (F_CPU == 1000000)
clock_prescale_set(clock_div_16);
/*
Pin Setups
*/
pinMode(STARTER, OUTPUT);
pinMode(IGNITION, OUTPUT);
pinMode(HORN, OUTPUT);
pinMode(HAZARDSA, OUTPUT);
pinMode(HAZARDSB, OUTPUT);
pinMode(ALARM_TRIPA, INPUT_PULLUP);
pinMode(ALARM_TRIPB, INPUT_PULLUP);
pinMode(ALARM_TRIPC, INPUT_PULLUP);
pinMode(ALARM_SENSORPOWER, OUTPUT);
pinMode(BLUETOOTH_STATE, INPUT_PULLUP);
Helpers::pciEnablePin(BLUETOOTH_STATE);
digitalWrite(STARTER, HIGH);
digitalWrite(IGNITION, HIGH);
digitalWrite(HORN, HIGH);
digitalWrite(HAZARDSA, HIGH);
digitalWrite(HAZARDSB, HIGH);
Serial.begin(GLOBALBAUDRATE);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB debugging only
}
BT.begin(GLOBALBAUDRATE);
Serial.println(F("Controller Ready"));
/*
Power Savings
*/
for (int i = 0; i < lowPinsLen; i++)
{
pinMode(lowPins[i], OUTPUT);
digitalWrite(lowPins[i], LOW);
}
power_spi_disable();
power_adc_disable();
//power_usart0_disable();
power_timer1_disable();
power_timer2_disable();
power_twi_disable();
}
ISR(PCINT2_vect) // handle pin change interrupt for D0 to D7 here
{
AlarmManager.AlarmISR(); /* Checks pins to see if alarm is tripped etc and acts appropriately */
sleep_disable();
}
#pragma region MainLoop
char incomingByte;
bool expectingPassword;
String incomingPassword, incomingCommand;
void loop()
{
/*
Read Commands
*/
expectingPassword = false;
while (BT.available() > 0)
{
incomingByte = BT.read();
if (incomingByte == '\n' || incomingByte == -1)
break;
if ((char)incomingByte == ',')
{
expectingPassword = true;
}
else if (expectingPassword)
{
incomingPassword += (char)incomingByte;
}
else
{
incomingCommand += (char)incomingByte;
}
}
/*
Process Commands
*/
if (incomingCommand.length() > 0)
{
if (incomingPassword != PASSWORD)
{
BT.println(F("Unauthorized Command"));
incomingCommand = "";
incomingPassword = "";
return;
}
bool isStart = incomingCommand.startsWith("START");
bool isStop = incomingCommand.startsWith("STOP");
if (isStart || isStop)
{
EngineFail Response = EngineFail::EngineSuccess;
if (isStart)
{
Response = EngineManager.StartEngine();
}
if (isStop)
{
Response = EngineManager.StopEngine();
}
if (Response == EngineFail::EngineSuccess)
{
BT.println(isStart ? F("Starting engine") : F("Stopping engine"));
}
else if (Response == EngineFail::EngineAlarmPrimed)
{
BT.println(F("Please disarm the alarm first."));
}
else if (Response == EngineFail::EngineAlreadyOff)
{
BT.println(F("Engine already stopped, the command has been sent anyway."));
}
else if (Response == EngineFail::EngineAlreadyOn)
{
BT.println(F("Engine already running."));
}
else if (Response == EngineFail::EngineStarting)
{
BT.println(F("Engine is currently starting."));
}
else if (Response == EngineFail::EngineUnknown)
{
BT.println(F("Unknown failure when starting"));
}
}
if (incomingCommand.startsWith("ALARM"))
{
AlarmFail Response = AlarmManager.StartAlarm();
if (Response == AlarmFail::AlarmArmed)
{
BT.println(F("Alarm armed"));
}
else if (Response == AlarmFail::AlarmFailed)
{
BT.println(F("Alarm already running"));
}
}
if (incomingCommand.startsWith("TOGGLEALARM"))
{
AlarmFail Response = AlarmManager.ToggleAlarmPrimed();
if (Response == AlarmFail::AlarmArmed)
{
BT.println(F("Alarm armed"));
}
else if (Response == AlarmFail::AlarmDisarmed)
{
BT.println(F("Alarm disarmed"));
}
else if (Response == AlarmFail::AlarmFailed)
{
BT.println(F("Alarm failed to toggle"));
}
}
if (incomingCommand.startsWith("STATUS"))
{
String s1 = "Alarm Primed: " + String(AlarmManager.IsAlarmPrimed() ? "Yes" : "No");
String s2 = "\nAlarm Sounding: " + String(AlarmManager.IsAlarmRunning() ? "Yes" : "No");
String s3 = "\nEngine Running: " + String(EngineManager.IsEngineRunning() ? "Yes" : "No");
String s4 = "\nHazards Running: " + String(RelayManager.IsHazardsRunning() ? "Yes" : "No");
String s5 = "\nAlarm tripped " + String(AlarmManager.GetTrippedCounter()) + " time(s) since last connection";
String sfinal = s1 + s2 + s3 + s4 + s5;
BT.println(F("-Printing Status-"));
BT.println(sfinal);
BT.println(F("-End Printing Status-"));
}
if (incomingCommand.startsWith("HAZARDS"))
{
if (RelayManager.ToggleHazardsBlinking(LockedOption::LockedUser))
{
BT.println(F("Toggling Hazards"));
}
else
{
BT.println(F("Hazards are locked by another task"));
}
}
incomingCommand = "";
incomingPassword = "";
}
unsigned long currentMillis = (unsigned long)millis();
/*
Tick Order
The tick order should be preserved as the engine manager relies on the alarm manager letting it know if its primed etc so it must first be ticked to know this etc...
once all states have been determined it is then time for the relay manager to tick and take action on the current states.
*/
AlarmManager.Tick(currentMillis);
EngineManager.Tick(currentMillis);
RelayManager.Tick(currentMillis); /* Should be last */
if (CanSleep(currentMillis, true))
{
/* Wakes up here */
yield();
}
}
bool CanSleep(unsigned long DeltaTime, bool EnterSleepIfTrue)
{
if (digitalRead(BLUETOOTH_STATE)) {
AlarmManager.LastConnectionNow(DeltaTime);
return false;
}
if (AlarmManager.IsAlarmRunning())
return false;
if (EngineManager.IsEngineRunning())
return false;
if (RelayManager.IsEngineStarting())
return false;
if (DeltaTime - AlarmManager.GetLastConnection() >= SLEEPTIMEOUT)
{
if (EnterSleepIfTrue)
{
EnterSleep(DeltaTime);
AlarmManager.LastConnectionNow(DeltaTime);
}
return true;
}
else
{
return false;
}
}
void EnterSleep(unsigned long DeltaTime)
{
if (!CanSleep(DeltaTime, false))
return;
noInterrupts();
Serial.println(F("Entering Sleep"));
EngineManager.StopEngine();
AlarmManager.StopAlarm();
RelayManager.SetHornBlinking(false, LockedOption::FORCEUNLOCKSLEEP);
RelayManager.SetHazardsBlinking(false, LockedOption::FORCEUNLOCKSLEEP);
Serial.flush();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
interrupts();
delay(1000);
sleep_cpu();
Serial.println(F("Exiting Sleep"));
/* Reset information about how many times it has been tripped if a connection has been detected, then save the old value to be printed */
if (digitalRead(BLUETOOTH_STATE))
AlarmManager.ResetTrippedCounter();
}