check if there are any results before finishing race.

main
DustyP 9 months ago
parent 7e1303cb9c
commit bcb9c77e6b

@ -161,7 +161,7 @@ func (dc *DerbyClock) Results() []*Result {
// readLoop continuously reads from the serial port // readLoop continuously reads from the serial port
func (dc *DerbyClock) readLoop() { func (dc *DerbyClock) readLoop() {
buffer := make([]byte, 0, 256) buffer := make([]byte, 0, 256)
results := make([]*Result, 0, 10)
for { for {
select { select {
case <-dc.stopReading: case <-dc.stopReading:
@ -206,18 +206,20 @@ func (dc *DerbyClock) readLoop() {
Result: result, Result: result,
} }
} }
results = append(results, result)
// Clear the buffer after a result is extracted // Clear the buffer after a result is extracted
buffer = buffer[:0] buffer = buffer[:0]
dc.mu.Unlock() dc.mu.Unlock()
} else if b[0] == '\n' { } else if b[0] == '\n' {
// Check if we should consider the race complete // Check if we should consider the race complete
dc.mu.Lock() dc.mu.Lock()
if dc.status == StatusRunning { if dc.status == StatusRunning && results != nil {
dc.status = StatusFinished dc.status = StatusFinished
dc.mu.Unlock() dc.mu.Unlock()
// Send race complete event // Send race complete event
dc.eventChan <- Event{Type: EventRaceComplete} dc.eventChan <- Event{Type: EventRaceComplete}
results = nil
} else { } else {
dc.mu.Unlock() dc.mu.Unlock()
} }

Loading…
Cancel
Save