-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopWatch.c
More file actions
137 lines (128 loc) · 5.04 KB
/
StopWatch.c
File metadata and controls
137 lines (128 loc) · 5.04 KB
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
/*
* StopWatch.c
*
* Created on: 19 Nov 2016
* Author: David
*/
#include "StopWatch.h"
/*FUNCTION**********************************************************************
*
* Function Name : sw_HandleStopWatch
* Description : This function is used to produce stop watch functionality from
* the main loop.
*
* It uses 3 Timeholder structs to keep track of the current stopwatch time and
* 2 lap times.
*
* The function makes use of a PIT timer running in the background, to get timing
* accuracy to 1 millisecond.
*
* The console is refreshed every 100mS to prevent the console form being overloaded
*
*END**************************************************************************/
void sw_HandleStopWatch(char *inputChar) {
// Init Timeholder structs for running time and lap times
TimeHolder currentStopWatchTime;
TimeHolder lapTime[3];
TimeHolder temp[3];
// Init the timeholders to start of epoch (00:00:00 01-01-1997)
th_InitTimeHolder(¤tStopWatchTime);
th_InitTimeHolder(lapTime);
th_InitTimeHolder((lapTime + 1));
th_InitTimeHolder((lapTime + 2));
th_InitTimeHolder(temp);
th_InitTimeHolder((temp + 1));
th_InitTimeHolder((temp + 2));
*inputChar = 'z'; // stop input buffer from falsely selecting control
sw_PrintStopwatchTime(¤tStopWatchTime, lapTime, temp);
while (*inputChar != '0') // While user has not hit exit key
{
if (char_received())// Check if a char is received form the console - Non Blocking
{
*inputChar = GETCHAR(); // Read the inputted char
}
if (*inputChar == '1') {
sw_RunStopWatch(inputChar, ¤tStopWatchTime, lapTime, temp);
PIT_DRV_StopTimer(0, 0);
}
}
}
/*FUNCTION**********************************************************************
*
* Function Name : sw_RunStopWatch
* Description : This function increments the stopwatch timer
*
* It also allows for lap times to be recorded with both total time taken and
* time sense last lap displayed. Up to 3 laps can be displayed at a time.
*
*END**************************************************************************/
void sw_RunStopWatch(char *inputChar, TimeHolder *currentStopWatchTime,
TimeHolder *lapTime, TimeHolder *temp) {
*inputChar = 'z';
PIT_DRV_StartTimer(0, 0);
while (*inputChar != '2' && *inputChar != '0') {
if (char_received()) {
*inputChar = GETCHAR();
if (*inputChar == '3') { // Lap record
// Knockon effect of time Saves.
th_SaveTime((lapTime + 1), (lapTime + 2));
th_SaveTime((temp + 1), (temp + 2));
th_SaveTime((lapTime), (lapTime + 1));
th_SaveTime((temp), (temp + 1));
th_SaveTime(currentStopWatchTime, (temp));
th_GetRawSecond((lapTime));
th_DecrementRawSecond((temp), lapTime->rawSecond);
th_DecrementMillisecond((temp), (lapTime)->millisecond);
th_SaveTime(currentStopWatchTime, (lapTime));// Save the current time to the first laptime
}
}
if (g_updateTime) // second interrupt has occurred
{
g_updateTime = 0; // reset flag
__disable_irq(); // disable interrupts
g_millis = 0; // reset millis
__enable_irq(); // enable interrupts
th_IncrementRawSecond(currentStopWatchTime, 1); // Increment the time by 1 second
}
// Prints the current time in 12/24hr format to the console
currentStopWatchTime->millisecond = g_millis;// Save the current millis from the PIT timer
if (g_millis % 100 == 0)// every 100mS refresh the stopwatch on the console
{
sw_PrintStopwatchTime(currentStopWatchTime, lapTime, temp);
if (g_millis > 999) // Stop the millis from going over 3 decimels for the console print out
{
__disable_irq(); // Disable interrupts
g_millis = 0; // Reset millis value
__enable_irq(); // Enable interrupts
}
}
}
}
/*FUNCTION**********************************************************************
*
* Function Name : sw_PrintStopwatchTime
* Description : This function displays a formatted view of the stopwatch
* time as well as all the lap times.
*
*END**************************************************************************/
void sw_PrintStopwatchTime(TimeHolder *currentStopWatchTime,
TimeHolder *lapTime, TimeHolder *temp) {
// Console print out to user
VT100_terminalClearFormat(g_terminalColour);
PRINTF("Stop watch: %02d:%02d:%03d\n\r", currentStopWatchTime->t.minute,
currentStopWatchTime->t.second, currentStopWatchTime->millisecond);
PRINTF("Total Lap Time\t\t\tSince Last Lap Time\n\r");
PRINTF("Lap 1: %02d:%02d:%03d\t\t%02d:%02d%:%03d\n\r", lapTime->t.minute,
lapTime->t.second, lapTime->millisecond, temp->t.minute,
temp->t.second, temp->millisecond);
PRINTF("Lap 2: %02d:%02d:%03d\t\t%02d:%02d%:%03d\n\r",
(lapTime + 1)->t.minute, (lapTime + 1)->t.second,
(lapTime + 1)->millisecond, (temp + 1)->t.minute,
(temp + 1)->t.second, (temp + 1)->millisecond);
PRINTF("Lap 3: %02d:%02d:%03d\t\t%02d:%02d%:%03d\n\r",
(lapTime + 2)->t.minute, (lapTime + 2)->t.second,
(lapTime + 2)->millisecond, (temp + 2)->t.minute,
(temp + 2)->t.second, (temp + 2)->millisecond);
PRINTF(
"\n\n\rOptions: \n\r- 1 -- Run\n\r- 2 -- Stop\n\r- 3 -- Record Lap Time\n\r- 0 -- Exit");
}