-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode6.cpp
204 lines (175 loc) · 5.62 KB
/
mode6.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
#include "uni_keypad.h"
#include "uni_gps.h"
#include "uni_display.h"
#include "uni_buzzer.h"
#include "uni_sensor.h"
#include "modes.h"
#include "recording.h"
#include "accurate_timing.h"
extern UniKeypad keypad;
extern UniGps gps;
extern UniDisplay display;
extern UniSensor sensor;
extern UniBuzzer buzzer;
#include <Fsm.h>
// ***************************************************** MODE 6 ***************************************
//### Mode 6 - Race Run (Finish Line)
//
//- When a sensor is triggered, display E1 to indicate that you need to enter 1 racer number.
// - It will beep periodically to indicate this
// - If you have 2 times recorded, it will beep twice periodically, etc.
//- when you press number keys, display the numbers on the display.
//- If you enter more than 3 digits, it will beep and clear
//- If you press "A", it will accept the input, and display the time and the racer number to SD
//- If you press "C", it will clear the display
//- If you press "B", it will duplicate the last time, and create E2 (only available from initial mode)
//- If you press D+* it will clear the last entry
#define NUMBER_PRESSED 1
#define DELETE 2
#define ACCEPT 3
#define CANCEL 4
#define SENSOR 5
void mode6_initial_entry();
void mode6_initial_check();
void mode6_initial_exit();
void mode6_digit_check();
void mode6_store_result();
State mode6_initial(&mode6_initial_entry, &mode6_initial_check, &mode6_initial_exit);
State mode6_digits_entered(NULL, &mode6_digit_check, NULL);
bool fsm_6_transition_setup_complete = false;
Fsm mode6_fsm(&mode6_initial);
#define MAX_RESULTS 20
TimeResult results_to_record[MAX_RESULTS];
int results_count = 0;
void store_data_result(TimeResult *data) {
if (results_count < MAX_RESULTS) {
results_to_record[results_count] = *data;
results_count ++;
Serial.println("stored new result");
} else {
Serial.println("Results cache is full");
}
}
// Are there any results in the buffer, if so,
// return the oldest one
bool retrieve_data(TimeResult *data) {
// TODO: Pause interrupts during this function?
if (results_count > 0) {
*data = results_to_record[0];
// Copy the remaining results up 1 slot
for (int i = 0; i < (results_count - 1); i++) {
results_to_record[i] = results_to_record[i + 1];
}
results_count--;
return true;
}
return false;
}
// Create a second entry of the most recently-recorded data
void duplicate_entry() {
if (results_count > 0 && (results_count < MAX_RESULTS)) {
results_to_record[results_count - 1] = results_to_record[results_count];
results_count += 1;
buzzer.beep();
display.showEntriesRemaining(results_count);
}
}
// If we want to remove an entry, for example: incorrectly counted 2 crossings.
void drop_last_entry() {
if (results_count > 0) {
results_count -= 1;
display.showEntriesRemaining(results_count);
}
}
void store_timing_data() {
Serial.println("SENSOR TRIGGERED");
Serial.println(sensor_interrupt_millis());
buzzer.beep();
// display.sens();
TimeResult data;
lastSensorTime(&data);
store_data_result(&data);
clear_sensor_interrupt_millis();
}
bool deleting = false;
// While waiting for a new datapoint
// Watch for sensor, etc
void mode6_initial_check() {
if (sensor_has_triggered()) {
mode6_fsm.trigger(SENSOR);
}
char last_key_pressed = keypad.readChar();
if (keypad.isDigit(last_key_pressed)) {
mode6_fsm.trigger(NUMBER_PRESSED);
} else if (last_key_pressed == 'B') {
duplicate_entry();
} else if (!deleting && keypad.keyPressed('D') && keypad.keyPressed('#')) { // D+#
drop_last_entry();
deleting = true;
} else if (deleting && !keypad.anyKeyPressed()) {
deleting = false;
}
#ifdef FSM_DEBUG
Serial.println("Initial Check ");
#endif
}
void mode6_initial_entry() {
display.showEntriesRemaining(results_count);
}
void mode6_initial_exit() {
}
void mode6_loop() {
mode6_fsm.run_machine();
}
void mode6_fsm_setup() {
mode6_fsm.add_transition(&mode6_initial, &mode6_digits_entered, NUMBER_PRESSED, &store_racer_number);
mode6_fsm.add_transition(&mode6_initial, &mode6_initial, SENSOR, &store_timing_data);
mode6_fsm.add_transition(&mode6_digits_entered, &mode6_initial, DELETE, &clear_racer_number);
mode6_fsm.add_transition(&mode6_digits_entered, &mode6_digits_entered, NUMBER_PRESSED, &store_racer_number);
mode6_fsm.add_transition(&mode6_digits_entered, &mode6_initial, ACCEPT, &mode6_store_result);
mode6_fsm.add_transition(&mode6_digits_entered, &mode6_digits_entered, SENSOR, &store_timing_data);
}
void mode6_setup() {
if (!fsm_6_transition_setup_complete) {
Serial.println("Mode6 Setup complete");
mode6_fsm_setup();
fsm_6_transition_setup_complete = true;
}
Serial.println("starting mode 6");
display.clear();
sensor.attach_interrupt();
}
void mode6_teardown() {
sensor.detach_interrupt();
}
// When a digit has been entered, monitor for A, C, #
void mode6_digit_check() {
if (sensor_has_triggered()) {
mode6_fsm.trigger(SENSOR);
}
char last_key_pressed = keypad.readChar();
if (keypad.isDigit(last_key_pressed)) {
if (maximum_digits_racer_number()) {
mode6_fsm.trigger(DELETE);
} else {
mode6_fsm.trigger(NUMBER_PRESSED);
}
} else if (keypad.keyPressed('C')) {
mode6_fsm.trigger(DELETE);
} else if (keypad.keyPressed('A')) {
mode6_fsm.trigger(ACCEPT);
}
#ifdef FSM_DEBUG
Serial.println("Digit Check ");
#endif
}
// Store the racer number and time together in a file
void mode6_store_result() {
Serial.println("STORE RESULT");
buzzer.beep();
TimeResult data;
if (retrieve_data(&data)) {
print_racer_data_to_sd(racer_number(), data);
clear_racer_number();
}
}