forked from pmiguelpinto90/BasketScoreboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoreboard.ino
168 lines (149 loc) · 4.29 KB
/
scoreboard.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
#include <ShiftDisplay.h>
#include "commands.h"
const int DISPLAY_TYPE = INDIVIDUAL_ANODE; // TODO in lib
const int COUNTER_DISPLAY_SIZE = 4;
const int SCORE_SECTION_COUNT = 5;
const int SCORE_SECTION_SIZES[] = {2, 1, 2, 1, 1};
enum sections { // TODO
SECTION_POINTS_HOME,
SECTION_PERIOD,
SECTION_POINTS_VISIT,
SECTION_FOULS_HOME,
SECTION_FOULS_VISIT,
};
const int COUNTERS_COUNT = 2;
const int COUNTERS[] = {5, 8}; // in minutes
const int DEFAULT_COUNTER_ID = 0;
volatile bool counterOn;
volatile bool buttonCounterReset;
volatile bool possessionHome;
volatile bool possessionVisit;
ShiftDisplay counterDisplay(DISPLAY_TYPE, COUNTER_DISPLAY_SIZE);
ShiftDisplay scoreDisplay(DISPLAY_TYPE, SCORE_SECTION_COUNT, SCORE_SECTION_SIZES);
void setPointsHome(int points) {
scoreDisplay.setAt(SECTION_POINTS_HOME, points); // only the least 2 significant digits will be set
scoreDisplay.setDotAt(SECTION_POINTS_HOME, 1, points > 99); // character '1' is connected as the dot in home points index 1
scoreDisplay.setDotAt(SECTION_POINTS_HOME, 0, possessionHome); // home possession symbol is connected as the dot in home points index 0
}
void setPointsVisit(int points) {
scoreDisplay.setAt(SECTION_POINTS_VISIT, points); // only the least 2 significant digits will be set
scoreDisplay.setDotAt(SECTION_POINTS_VISIT, 1, points > 99); // character '1' is connected as the dot in visit points index 1
scoreDisplay.setDotAt(SECTION_POINTS_VISIT, 0, possessionVisit); // visit possession symbol is connected as the dot in visit points index 0
}
void setFoulsHome(int fouls) {
scoreDisplay.setAt(SECTION_FOULS_HOME, fouls);
}
void setFoulsVisit(int fouls) {
scoreDisplay.setAt(SECTION_FOULS_VISIT, fouls);
}
void setPeriod(int period) {
scoreDisplay.setAt(SECTION_PERIOD, period);
}
void setPossessionHome() {
scoreDisplay.setDotAt(SECTION_POINTS_VISIT, 0, false);
scoreDisplay.setDotAt(SECTION_POINTS_HOME, 0, true);
possessionVisit = false;
possessionHome = true;
}
void setPossessionVisit() {
scoreDisplay.setDotAt(SECTION_POINTS_HOME, 0, false);
scoreDisplay.setDotAt(SECTION_POINTS_VISIT, 0, true);
possessionHome = false;
possessionVisit = true;
}
void clearPossession() {
scoreDisplay.setDotAt(SECTION_POINTS_HOME, 0, false);
scoreDisplay.setDotAt(SECTION_POINTS_VISIT, 0, false);
possessionHome = false;
possessionVisit = false;
}
void receiveEvent(int size) {
int cmd = 0; //read(); // TODO
int arg = 0; //size > 1 ? read() : 0; // TODO
switch (cmd) {
case TOGGLE_COUNTER:
counterOn = !counterOn;
break;
case RESET_COUNTER:
buttonCounterReset = true;
break;
case SET_POINTS_HOME:
setPointsHome(arg);
break;
case SET_POINTS_VISIT:
setPointsVisit(arg);
break;
case SET_FOULS_HOME:
setFoulsHome(arg);
break;
case SET_FOULS_VISIT:
setFoulsVisit(arg);
break;
case SET_PERIOD:
setPeriod(arg);
break;
case SET_POSSESSION_HOME:
setPossessionHome();
break;
case SET_POSSESSION_VISIT:
setPossessionVisit();
break;
case CLEAR_POSSESSION:
clearPossession();
break;
}
}
void setup() {
counterOn, buttonCounterReset = false;
possessionHome, possessionVisit = false;
counterDisplay.set("0000"); // TODO
//onReceive(receiveEvent); // TODO
}
void loop() {
static int counterId = DEFAULT_COUNTER_ID;
static long counter = COUNTERS[counterId] * 60 * 100;
static bool counterSet = true;
static unsigned long lastTime = 0;
unsigned long currTime = millis() / 10;
if (counterOn && currTime != lastTime) {
counter--;
if (counter == 0) {
counterDisplay.set("0000");
counterOn = false;
} else if (counter < 10) {
//TODO
counterDisplay.set();
} else if (counter < 100) {
//TODO
counterDisplay.set();
} else if (counter < 6000) {
//TODO
counterDisplay.set();
} else {
int seconds = counter / 100;
int min = seconds / 60;
int sec = seconds % 60;
//TODO
counterDisplay.set();
}
counterSet = false;
lastTime = currTime;
}
if (buttonCounterReset) { // button pressed
if (counterSet) {
// iterate counter time
counterId = (counterId + 1) % COUNTERS_COUNT;
int minutes = COUNTERS[counterId];
//TODO
counterDisplay.set();
} else {
// stop and reset counter
counterOn = false;
counterSet = true;
int minutes = COUNTERS[counterId];
//TODO
counterDisplay.set();
}
buttonCounterReset = false;
}
}