Skip to content

Commit 6fb60cf

Browse files
committed
Merge branch 'aron'
2 parents f1a1b79 + 987bda8 commit 6fb60cf

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
lines changed

Buggy/Lights.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "Lights.h"
22

3-
const int16_t Lights::loopDuration = 300; // ms
3+
const int16_t Lights::loopDuration = 600; // ms
44
const int16_t Lights::indicatorPeriod = 400; // ms
55

66
const int8_t Lights::leftIndicatorPin = 5;
@@ -38,11 +38,10 @@ void Lights::update() {
3838
setLightState(rightIndicatorPin, obstacle);
3939
} else { // going
4040
// Progress of the red loop
41-
const int16_t progress = ((millis() % loopDuration) / (loopDuration / nPins)) % nPins;
42-
bool pinOn;
41+
int16_t progress = (millis() % loopDuration) / (loopDuration / (2 * nPins));
42+
progress = (progress / nPins) ? (2 * nPins - 1 - progress) : progress;
4343
for (int8_t i = 0; i < nPins; i++) {
44-
pinOn = (progress == i/* || ((progress + nPins / 2) % nPins) == i*/);
45-
setLightState(pins[i], pinOn);
44+
setLightState(pins[i], i == progress);
4645
}
4746

4847
off(leftIndicatorPin);

Station/Station/Buggy.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Buggy
1111
{
1212
private int ID;
1313
private Direction direction;
14-
private int lastGantry = 0;
14+
private int lastGantry;
1515
private int lapsCompleted = 0;
1616
private int requiredLaps = 0;
1717

@@ -31,6 +31,11 @@ public Buggy(int ID, Direction direction, Station station, Communications comms)
3131
this.direction = direction;
3232
this.comms = comms;
3333
this.station = station;
34+
35+
if (direction == Direction.Clockwise)
36+
this.lastGantry = 2;
37+
else
38+
this.lastGantry = 1;
3439
}
3540
public void startOnlineCheck()
3641
{
@@ -96,7 +101,8 @@ private string getSectionName()
96101
section = lastGantry - 1;
97102
}
98103

99-
if ((direction == Direction.Clockwise && lastGantry == 2 && lapsCompleted >= requiredLaps)
104+
if ((direction == Direction.Clockwise && lastGantry == 2 &&
105+
(lapsCompleted == 0 || lapsCompleted >= requiredLaps))
100106
|| (direction == Direction.AntiClockwise && lastGantry == 1))
101107
return "Park Lane";
102108
else
@@ -143,13 +149,16 @@ public void onGantry(int currentGantry)
143149
lapsCompleted++;
144150
}
145151
// Missed the gantry that terminates the buggy's lap, updating late
146-
else if ((direction == Direction.Clockwise && (lastGantry == 1 && currentGantry != 2) || (lastGantry == 3 && currentGantry == 3))
147-
|| (direction == Direction.AntiClockwise && lastGantry == 2 && currentGantry == 2))
152+
else if ((direction == Direction.Clockwise &&
153+
(lastGantry == 1 && currentGantry != 2) ||
154+
(lastGantry == 3 && currentGantry == 3))
155+
|| (direction == Direction.AntiClockwise &&
156+
lastGantry == 2 && currentGantry == 2))
148157
{
149158
lapsCompleted++;
150159
}
151-
152-
lastGantry = currentGantry; // Lap detection needs the previous gantry ID, place this afterwards
160+
// Lap detection needs the previous gantry ID, place this afterwards
161+
lastGantry = currentGantry;
153162

154163
buggyAction("stopping at gantry " + currentGantry);
155164
buggyAction("entering track section: " + getSectionName());
@@ -218,8 +227,8 @@ public void onBuggyParked()
218227
{
219228
if (station.getNumberOfBuggies() == 1)
220229
buggyAction("parked! " + (lapsCompleted) + " lap(s) completed!");
221-
else
222-
buggyAction("parked! " + (lapsCompleted - 1) + " lap(s) completed!"); // Buggy 1 has to go an extra lap in 2-buggy mode
230+
else // Buggy 1 has to go an extra lap in 2-buggy mode
231+
buggyAction("parked! " + (lapsCompleted - 1) + " lap(s) completed!");
223232
}
224233
if (direction == Direction.Clockwise)
225234
Program.print("Challenge complete!", ConsoleColor.Yellow, ConsoleColor.Black);

Station/Station/Communications.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ class Communications
1313
{
1414
private SerialPort port = new SerialPort();
1515

16-
private Dictionary<Regex, Action<int, GroupCollection>> buggyhash = new Dictionary<Regex, Action<int, GroupCollection>>();
16+
private Dictionary<Regex, Action<int, GroupCollection>> buggyhash =
17+
new Dictionary<Regex, Action<int, GroupCollection>>();
1718
private Action<int, string> defaultHandler = null;
1819

19-
// Three objects required in the following arrays to enable using buggy IDs as indices (1 and 2)
20+
// Three objects required in the following arrays to enable using buggy IDs
21+
// as indices (1 and 2)
2022

2123
/// <summary>
2224
/// Flags indicating wether sent message has been received by the respective buggies
@@ -56,7 +58,8 @@ public Communications()
5658
/// </summary>
5759
/// <param name="buggy_id">Target buggy</param>
5860
/// <param name="command">Command to send</param>
59-
/// <param name="offlineHandler">Executed if the message is not received on the first try instead of the default user feedback</param>
61+
/// <param name="offlineHandler">Executed if the message is not received
62+
/// on the first try instead of the default user feedback</param>
6063
/// <returns>True if received on first attempt, false otherwise</returns>
6164
public bool send(int buggy_id, string command, Action offlineHandler = null)
6265
{
@@ -79,7 +82,8 @@ public bool send(int buggy_id, string command, Action offlineHandler = null)
7982
}
8083
else
8184
{
82-
Program.print("Command " + command + " not being recieved by buggy " + buggy_id);
85+
Program.print("Command " + command + " not being recieved by buggy "
86+
+ buggy_id);
8387
Program.print("Will keep sending command");
8488
}
8589
}

Station/Station/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ private static string readInput()
179179
inputBufferIndex = inputBuffer.Count - 1;
180180
}
181181

182-
// Separate inner if statement necessary so that 'Enter' and 'Backspace' keys are never handled by the last branch
182+
// Separate inner if statement necessary so that 'Enter' and 'Backspace' keys
183+
// are never handled by the last branch
183184
if (key.Key == ConsoleKey.Enter)
184185
{
185186
if (inputBuffer.Last().Length != 0)
@@ -191,7 +192,8 @@ private static string readInput()
191192
else if (key.Key == ConsoleKey.Backspace)
192193
{
193194
if (inputBuffer.Last().Length != 0) {
194-
inputBuffer[inputBuffer.Count - 1] = inputBuffer.Last().Substring(0, inputBuffer.Last().Length - 1);
195+
inputBuffer[inputBuffer.Count - 1] =
196+
inputBuffer.Last().Substring(0, inputBuffer.Last().Length - 1);
195197
}
196198
}
197199
else if (key.KeyChar != '\u0000')

Station/Station/Station.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,27 @@ public Station()
2424
comms.addCommand("PONG", (int ID) => getBuggyForID(ID)?.onPongRecieved());
2525
comms.addCommand("GOING", (int ID) => getBuggyForID(ID)?.onGoing());
2626
comms.addCommand("STOPPED", (int ID) => getBuggyForID(ID)?.onStopped());
27-
comms.addCommand(new Regex(@"^GANTRY(?<GantryID>[123])$"), (int ID, GroupCollection groups) =>
27+
comms.addCommand(new Regex(@"^GANTRY(?<GantryID>[123])$"),
28+
(int ID, GroupCollection groups) =>
2829
{
2930
getBuggyForID(ID)?.onGantry(Int32.Parse(groups["GantryID"].Value));
3031
});
3132
comms.addCommand("GANTRY_INVALID", (int ID) => getBuggyForID(ID)?.onInvalidGantry());
3233
comms.addCommand("PARKED", (int ID) => getBuggyForID(ID)?.onBuggyParked());
3334
comms.addCommand("OBSTACLE", (int ID) => getBuggyForID(ID)?.onObstacle());
3435
comms.addCommand("PATHCLEAR", (int ID) => getBuggyForID(ID)?.onPathClear());
35-
comms.addCommand(new Regex(@"^IRLength: (?<Length>\d+)$"), (int ID, GroupCollection groups) =>
36+
comms.addCommand(new Regex(@"^IRLength: (?<Length>\d+)$"),
37+
(int ID, GroupCollection groups) =>
3638
{
37-
Program.print("Buggy " + ID + " Pulse length: " + groups["Length"].Value, getBuggyForID(ID)?.getColour());
39+
Program.print("Buggy " + ID + " Pulse length: " + groups["Length"].Value,
40+
getBuggyForID(ID)?.getColour());
3841
});
39-
comms.addCommand(new Regex(@"^INVALID: (?<Command>.*)$"), (int ID, GroupCollection groups) =>
42+
comms.addCommand(new Regex(@"^INVALID: (?<Command>.*)$"),
43+
(int ID, GroupCollection groups) =>
4044
{
41-
Program.print("Buggy " + ID + " received invalid command: " + groups["Command"].Value, getBuggyForID(ID)?.getColour());
45+
Program.print("Buggy " + ID + " received invalid command: "
46+
+ groups["Command"].Value,
47+
getBuggyForID(ID)?.getColour());
4248
});
4349

4450
setUp();
@@ -95,7 +101,8 @@ public int getNumberOfBuggies()
95101

96102
/// <summary>
97103
/// Sets up the environment for a fresh run
98-
/// Clears for any previously existing buggies, inputs number of buggies and laps, initialises new buggies and ensures they are online
104+
/// Clears for any previously existing buggies, inputs number of buggies and laps,
105+
/// initialises new buggies and ensures they are online
99106
/// </summary>
100107
public void setUp()
101108
{
@@ -118,7 +125,7 @@ public void setUp()
118125
Console.WriteLine("How many laps would you like to do? ");
119126
Int32.TryParse(Console.ReadLine(), out laps);
120127
}
121-
128+
122129
// Initialise new buggies
123130
buggy1 = new Buggy(1, Direction.Clockwise, this, comms);
124131
buggy1.syn(silent: true);

0 commit comments

Comments
 (0)