Skip to content

Commit b84c331

Browse files
committed
refactor: rename classes
1 parent 9b36dc3 commit b84c331

25 files changed

+520
-505
lines changed

CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1111
# ensuring compilation on host works
1212
set(MOCK_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tests/host/mocks")
1313

14-
# Recursively find all source files in src/
15-
file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
16-
17-
# Create an object library for the code in src/
18-
add_library(spresense_core OBJECT ${SOURCES})
14+
# Create an interface library for the header-only code in src/
15+
add_library(spresense_core INTERFACE)
1916

2017
# Configure include directories for the core library
21-
target_include_directories(spresense_core PUBLIC
18+
target_include_directories(spresense_core INTERFACE
2219
src
2320
${MOCK_INCLUDE_DIR}
2421
)

Spresense-CycleComputer.ino

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
#include "src/drivers/OLEDDriver.h"
1+
#include <LowPower.h>
2+
3+
#include "src/drivers/Gnss.h"
4+
#include "src/drivers/OLED.h"
25
#include "src/system/CycleComputer.h"
3-
#include "src/system/GPSManager.h"
4-
#include "src/system/InputManager.h"
6+
#include "src/system/Input.h"
57

6-
drivers::OLEDDriver display(Wire);
7-
application::GPSManager gps;
8-
application::InputManager inputManager;
9-
application::CycleComputer<drivers::OLEDDriver, application::GPSManager, application::InputManager> computer(display, gps, inputManager);
8+
drivers::OLED display(Wire);
9+
drivers::Gnss gps;
10+
application::Input input;
11+
application::CycleComputer<drivers::OLED, drivers::Gnss, application::Input> computer(display, gps, input);
1012

1113
void setup() {
14+
LowPower.begin();
15+
LowPower.clockMode(CLOCK_MODE_32MHz);
1216
computer.begin();
1317
}
1418

src/Config.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Config {
66

7+
constexpr unsigned long DEBOUNCE_DELAY = 50;
8+
constexpr unsigned long DISPLAY_UPDATE_INTERVAL_MS = 100;
9+
710
namespace Pin {
811

912
constexpr int BTN_A = PIN_D00;
@@ -12,9 +15,6 @@ constexpr int WARN_LED = PIN_D02;
1215

1316
} // namespace Pin
1417

15-
constexpr unsigned long DEBOUNCE_DELAY = 50;
16-
constexpr unsigned long DISPLAY_UPDATE_INTERVAL_MS = 100;
17-
1818
namespace OLED {
1919

2020
constexpr int WIDTH = 128;
@@ -29,12 +29,4 @@ constexpr int JST_OFFSET = 9;
2929

3030
}
3131

32-
namespace Power {
33-
34-
constexpr int BATTERY_LOW_THRESHOLD = 3600; // 3.6V
35-
constexpr int BATTERY_CHECK_INTERVAL_MS = 10000;
36-
constexpr int LED_BLINK_INTERVAL_MS = 500;
37-
38-
} // namespace Power
39-
4032
} // namespace Config
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
namespace drivers {
88

9-
class ButtonDriver {
9+
class Button {
1010
private:
1111
const int pinNumber;
1212
bool stablePinLevel;
1313
bool lastPinLevel;
1414
unsigned long lastDebounceTime;
1515

1616
public:
17-
ButtonDriver(int pin) : pinNumber(pin) {}
17+
Button(int pin) : pinNumber(pin) {}
1818

1919
void begin() {
2020
pinMode(pinNumber, INPUT_PULLUP);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
#include "../Config.h"
77

8-
namespace application {
8+
namespace drivers {
99

10-
class GPSManager {
10+
class Gnss {
1111
private:
1212
SpGnss gnss;
1313
SpNavData navData;
@@ -45,4 +45,4 @@ class GPSManager {
4545
}
4646
};
4747

48-
} // namespace application
48+
} // namespace drivers
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
namespace drivers {
1111

12-
class OLEDDriver {
12+
class OLED {
1313
private:
1414
Adafruit_SSD1306 display;
1515
TwoWire &wire;
1616
int batteryLevel = 85;
1717
int satelliteCount = 5;
1818

1919
public:
20-
OLEDDriver(TwoWire &i2c) : display(Config::OLED::WIDTH, Config::OLED::HEIGHT, &i2c, -1), wire(i2c) {}
20+
OLED(TwoWire &i2c) : display(Config::OLED::WIDTH, Config::OLED::HEIGHT, &i2c, -1), wire(i2c) {}
2121

2222
void begin() {
2323
if (!display.begin(SSD1306_SWITCHCAPVCC, Config::OLED::ADDRESS))

src/system/CycleComputer.h

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,36 @@
33
#include "../Config.h"
44
#include "DisplayData.h"
55
#include "InputEvent.h"
6-
#include "ModeManager.h"
7-
#include "PowerManager.h"
8-
#include "TripManager.h"
6+
#include "Mode.h"
7+
#include "Trip.h"
98
#include <cstdio>
109

1110
namespace application {
1211

1312
template <typename DisplayT, typename GnssT, typename InputT> class CycleComputer {
1413
private:
1514
DisplayT &display;
16-
InputT &inputProvider;
17-
GnssT &gnssProvider;
18-
ModeManager modeManager;
19-
TripManager tripManager;
20-
PowerManager powerManager;
15+
InputT &input;
16+
GnssT &gnss;
17+
Mode mode;
18+
Trip trip;
2119
unsigned long lastDisplayUpdate = 0;
2220
bool forceUpdate = false;
2321

2422
public:
25-
CycleComputer(DisplayT &displayData, GnssT &gnss, InputT &input) : display(displayData), gnssProvider(gnss), inputProvider(input) {}
23+
CycleComputer(DisplayT &displayData, GnssT &gnss, InputT &input) : display(displayData), gnss(gnss), input(input) {}
2624

2725
void begin() {
2826
display.begin();
29-
inputProvider.begin();
30-
gnssProvider.begin();
31-
tripManager.begin();
32-
powerManager.begin();
27+
input.begin();
28+
gnss.begin();
29+
trip.begin();
3330
}
3431

3532
void update() {
3633
handleInput();
37-
gnssProvider.update();
38-
tripManager.update(gnssProvider.getSpeedKmh(), millis());
39-
powerManager.update();
34+
gnss.update();
35+
trip.update(gnss.getSpeedKmh(), millis());
4036
updateDisplay();
4137
}
4238

@@ -48,19 +44,19 @@ template <typename DisplayT, typename GnssT, typename InputT> class CycleCompute
4844
}
4945

5046
void handleInput() {
51-
InputEvent event = inputProvider.update();
47+
InputEvent event = input.update();
5248

5349
switch (event) {
5450
case InputEvent::BTN_A:
55-
modeManager.nextMode();
51+
mode.next();
5652
forceUpdate = true;
5753
break;
5854
case InputEvent::BTN_B:
59-
modeManager.prevMode();
55+
mode.prev();
6056
forceUpdate = true;
6157
break;
6258
case InputEvent::BTN_BOTH:
63-
tripManager.reset();
59+
trip.reset();
6460
forceUpdate = true;
6561
break;
6662
default:
@@ -78,40 +74,40 @@ template <typename DisplayT, typename GnssT, typename InputT> class CycleCompute
7874

7975
char buf[20];
8076
DisplayDataType type;
81-
getDisplayData(modeManager.getMode(), type, buf, sizeof(buf));
77+
getDisplayData(mode.get(), type, buf, sizeof(buf));
8278

8379
display.show(type, buf);
8480
}
8581

86-
void getDisplayData(Mode mode, DisplayDataType &type, char *buf, size_t size) {
87-
switch (mode) {
88-
case Mode::SPEED:
82+
void getDisplayData(Mode::ID modeId, DisplayDataType &type, char *buf, size_t size) {
83+
switch (modeId) {
84+
case Mode::ID::SPEED:
8985
type = DisplayDataType::SPEED;
90-
formatFloat(gnssProvider.getSpeedKmh(), 4, 1, buf, size);
86+
formatFloat(gnss.getSpeedKmh(), 4, 1, buf, size);
9187
break;
92-
case Mode::TIME:
88+
case Mode::ID::TIME:
9389
type = DisplayDataType::TIME;
94-
gnssProvider.getTimeJST(buf, size);
90+
gnss.getTimeJST(buf, size);
9591
break;
96-
case Mode::MAX_SPEED:
92+
case Mode::ID::MAX_SPEED:
9793
type = DisplayDataType::MAX_SPEED;
98-
formatFloat(tripManager.getMaxSpeedKmh(), 4, 1, buf, size);
94+
formatFloat(trip.getMaxSpeedKmh(), 4, 1, buf, size);
9995
break;
100-
case Mode::DISTANCE:
96+
case Mode::ID::DISTANCE:
10197
type = DisplayDataType::DISTANCE;
102-
formatFloat(tripManager.getDistanceKm(), 5, 2, buf, size);
98+
formatFloat(trip.getDistanceKm(), 5, 2, buf, size);
10399
break;
104-
case Mode::MOVING_TIME:
100+
case Mode::ID::MOVING_TIME:
105101
type = DisplayDataType::MOVING_TIME;
106-
tripManager.getMovingTimeStr(buf, size);
102+
trip.getMovingTimeStr(buf, size);
107103
break;
108-
case Mode::ELAPSED_TIME:
104+
case Mode::ID::ELAPSED_TIME:
109105
type = DisplayDataType::ELAPSED_TIME;
110-
tripManager.getElapsedTimeStr(buf, size);
106+
trip.getElapsedTimeStr(buf, size);
111107
break;
112-
case Mode::AVG_SPEED:
108+
case Mode::ID::AVG_SPEED:
113109
type = DisplayDataType::AVG_SPEED;
114-
formatFloat(tripManager.getAvgSpeedKmh(), 4, 1, buf, size);
110+
formatFloat(trip.getAvgSpeedKmh(), 4, 1, buf, size);
115111
break;
116112
default:
117113
type = DisplayDataType::INVALID;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#pragma once
22

3-
#include "../drivers/ButtonDriver.h"
3+
#include "../drivers/Button.h"
44

55
#include "InputEvent.h"
66

77
namespace application {
88

9-
class InputManager {
9+
class Input {
1010
private:
11-
drivers::ButtonDriver btnA;
12-
drivers::ButtonDriver btnB;
11+
drivers::Button btnA;
12+
drivers::Button btnB;
1313

1414
public:
15-
InputManager() : btnA(Config::Pin::BTN_A), btnB(Config::Pin::BTN_B) {}
15+
Input() : btnA(Config::Pin::BTN_A), btnB(Config::Pin::BTN_B) {}
1616

1717
void begin() {
1818
btnA.begin();

src/system/InputEvent.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ enum class InputEvent {
77
BTN_A,
88
BTN_B,
99
BTN_BOTH,
10-
BTN_LONG_A,
1110
};
1211

1312
} // namespace application

src/system/Mode.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
namespace application {
4+
5+
class Mode {
6+
public:
7+
enum class ID {
8+
SPEED,
9+
MAX_SPEED,
10+
AVG_SPEED,
11+
TIME,
12+
MOVING_TIME,
13+
ELAPSED_TIME,
14+
DISTANCE,
15+
};
16+
17+
private:
18+
ID currentID;
19+
20+
public:
21+
Mode() : currentID(ID::SPEED) {}
22+
23+
void next() {
24+
currentID = static_cast<ID>((static_cast<int>(currentID) + 1) % 7);
25+
}
26+
27+
void prev() {
28+
currentID = static_cast<ID>((static_cast<int>(currentID) + 6) % 7);
29+
}
30+
31+
ID get() const {
32+
return currentID;
33+
}
34+
};
35+
36+
} // namespace application

0 commit comments

Comments
 (0)