-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplaySpeed.cpp
52 lines (43 loc) · 1.6 KB
/
DisplaySpeed.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
/*
* DisplaySpeed.cpp
*
* Created on: Feb 5, 2013
* Author: dam7633
*/
#include "DisplaySpeed.h"
#include "DisplayDistance.h"
#include "CyclometerController.h"
#include "PulseScanner.h"
#include <math.h>
static void toggleTripMode(StateContext* context) {
((CyclometerController*)context)->getPulseScanner()->toggleTripMode();
}
DisplaySpeed::DisplaySpeed(StateParent* parent, StateContext* context) :
State(parent, context) {}
void DisplaySpeed::accept(Event event) {
if (event == evSetDepressed) {
parent->doTransition(new DisplaySpeed(parent, context), toggleTripMode);
} else if (event == evModeDepressed) {
parent->doTransition(new DisplayDistance(parent, context), NULL);
}
}
DisplayInfo DisplaySpeed::getData() {
DisplayInfo info;
// The pulse scanner knows the speeds.
PulseScanner* scanner = ((CyclometerController*)context)->getPulseScanner();
// Get and format the speeds to the appropriate digits.
// Hooray pointer arithmetic!
formatSpeed(scanner->currentSpeed(), info.val + 2, info.dp + 2);
formatSpeed(scanner->averageSpeed(), info.val, info.dp);
return info;
}
void DisplaySpeed::formatSpeed(double speed, int* digits, bool* decimals) {
// Given a speed "AB.CD", if the speed is >= 10,
// display "AB", otherwise display "B.C"
// Speeds are rounded to the nearest minimum sig fig.
bool isOverTen = lround(speed) >= 10;
digits[0] = isOverTen ? lround(speed) % 10 : lround(speed * 10) % 10;
digits[1] = isOverTen ? (lround(speed) / 10) % 10 : lround(speed) % 10;
decimals[0] = false;
decimals[1] = isOverTen ? false : true;
}