-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.cpp
252 lines (206 loc) · 4.25 KB
/
clock.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
// clock.cpp ///////////////////////////////////////////////////////////////////
// The timeclock object for the table hockey game, /////////////////////////////
// interfaces with the adafruit lcd libraries //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//#include <std_files>
//#include "Headers.h"
//#include "Source.c"
//#include "Headers.hpp"
//#include "Source.cpp"
#include "clock.h"
#ifdef PRESEASON
#include <iostream>
void print(String output)
{
std::cout << output << std::endl;
}
void print(String output, int constructor_arg, String fin)
{
std::cout << output << constructor_arg << fin << std::endl;
}
#else
void print(String output)
{
}
void print(String output, int constructor_arg, String fin)
{
}
#endif
bool Flip(bool input)
{ if(input == true)
{ return false;
}
else
{ return true;
}
}
bool Equal(score team1, score team2)
{ if(team1 == team2)
{ return true;
}
return false;
}
// Period class ////////////////////////////////////////////////////////////////
Period::Period()
{
Time = -1;
// flag that something is wrong since we are constructed without
// sufficient arguments using the default constructor
homeScore = 0;
awayScore = 0;
clockRunning = false;
periodStarted = false;
periodEnded = false;
}
Period::Period(seconds init_time, int period_minutes)
{ if(init_time < (period_minutes*60))
{ Time = init_time;
}
else
{ Time = (period_minutes*60);
// lets sanity/sanitize check here and not let the user screw
// up things
}
homeScore = 0;
awayScore = 0;
clockRunning = false;
periodStarted = false;
periodEnded = false;
}
Period::Period(int period_minutes)
{
Time = period_minutes*60;
homeScore = 0;
awayScore = 0;
clockRunning = false;
periodStarted = false;
periodEnded = false;
}
Period::Period(const Period& other)
{ Time = other.Time;
homeScore = other.homeScore;
awayScore = other.awayScore;
clockRunning = other.clockRunning;
periodStarted = other.periodStarted;
periodEnded = other.periodEnded;
}
Period Period::operator= (const Period p)
{
print("Calling Period::operator=");
Time = p.Time;
clockRunning = p.clockRunning;
homeScore = p.homeScore;
awayScore = p.awayScore;
periodEnded = p.periodEnded;
return (*this);
}
void Period::setPeriodTime(seconds new_time)
{ Time = new_time;
}
void Period::Update(seconds deltat)
{ if(clockRunning == true)
{
Time -= deltat;
if(Time < 0)
{
print("period has ended");
Time = 0;
// no negative times
this->stopClock();
this->endPeriod();
// stop the clock at zero and set the state
}
}
}
seconds Period::getPeriodTime()
{
return Time;
}
bool Period::periodOver()
{ return periodEnded;
}
void Period::endPeriod()
{ periodEnded = true;
}
bool Period::isPeriodStarted()
{ return periodStarted;
}
score Period::getHomeScore()
{ return homeScore;
}
score Period::getAwayScore()
{ return awayScore;
}
void Period::Faceoff()
{
this->startClock();
}
void Period::Goal(player scoredBy)
{
this->stopClock();
if(scoredBy == away)
{ awayScore += 1;
}
else if(scoredBy == home)
{ homeScore += 1;
}
}
void Period::stopClock()
{
if(clockRunning == true)
{ clockRunning = Flip(clockRunning);
}
}
bool Period::clockIsRunning()
{ return clockRunning;
}
void Period::startClock()
{
if(periodEnded == false)
{ if(clockRunning == false)
{
clockRunning = Flip(clockRunning);
}
}
else
{ //std::cout << "Unable to resume period, is over" << std::endl;
}
}
String Period::getClockOutput()
{
int minutes = 0;
int timeval = Time;
if(timeval >= 60)
{ while(timeval>= 60)
{ minutes += 1;
timeval -= 60;
}
}
#ifdef PRESEASON
// ahh, we need something to test here
std::string output = std::to_string(minutes);
output.append(":");
if(timeval < 10)
{ // tack on leading 0 so it looks right
output.append("0");
}
output.append(std::to_string(timeval));
return output;
#else
// real deal here running on the arduino, so we need to properly
// use the arduino string type provided for us
String output = "";
output.concat(minutes);
output.concat(":");
if(timeval < 10)
{ // tack on leading 0 so it looks right
output.concat("0");
}
output.concat(timeval);
return output;
// there we go
#endif
}
Period::~Period()
{
}