Skip to content

Commit 7938887

Browse files
committed
refactor: delete GraphicsContext
1 parent 4b53b6a commit 7938887

File tree

5 files changed

+48
-76
lines changed

5 files changed

+48
-76
lines changed

src/CycleComputer.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ namespace application {
1313

1414
template <typename DisplayT, typename GnssT, typename ButtonT> class CycleComputer {
1515
private:
16-
DisplayT &display;
17-
ui::Input<ButtonT> input;
18-
GnssT &gnss;
19-
ui::Mode mode;
20-
domain::Trip trip;
21-
domain::Clock clock;
22-
ui::Renderer renderer;
23-
unsigned long lastDisplayUpdate = 0;
24-
bool forceUpdate = false;
16+
DisplayT &display;
17+
ui::Input<ButtonT> input;
18+
GnssT &gnss;
19+
ui::Mode mode;
20+
domain::Trip trip;
21+
domain::Clock clock;
22+
ui::Renderer<DisplayT> renderer;
23+
unsigned long lastDisplayUpdate = 0;
24+
bool forceUpdate = false;
2525

2626
public:
2727
CycleComputer(DisplayT &displayData, GnssT &gnss, ButtonT &btnA, ButtonT &btnB) : display(displayData), input(btnA, btnB), gnss(gnss) {}

src/drivers/OLED.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
#include <Wire.h>
77

88
#include "../Config.h"
9-
#include "../ui/GraphicsContext.h"
109

1110
namespace drivers {
1211

13-
class OLED : public ui::GraphicsContext {
12+
class OLED {
1413
private:
1514
Adafruit_SSD1306 ssd1306;
1615

@@ -25,55 +24,55 @@ class OLED : public ui::GraphicsContext {
2524
}
2625

2726
// GraphicsContext implementation
28-
void clear() override {
27+
void clear() {
2928
ssd1306.clearDisplay();
3029
}
3130

32-
void display() override {
31+
void display() {
3332
ssd1306.display();
3433
}
3534

36-
void setTextSize(int size) override {
35+
void setTextSize(int size) {
3736
ssd1306.setTextSize(size);
3837
}
3938

40-
void setTextColor(int color) override {
39+
void setTextColor(int color) {
4140
ssd1306.setTextColor(color);
4241
}
4342

44-
void setCursor(int x, int y) override {
43+
void setCursor(int x, int y) {
4544
ssd1306.setCursor(x, y);
4645
}
4746

48-
void print(const char *text) override {
47+
void print(const char *text) {
4948
ssd1306.print(text);
5049
}
5150

52-
void drawLine(int x0, int y0, int x1, int y1, int color) override {
51+
void drawLine(int x0, int y0, int x1, int y1, int color) {
5352
ssd1306.drawLine(x0, y0, x1, y1, color);
5453
}
5554

56-
void drawRect(int x, int y, int w, int h, int color) override {
55+
void drawRect(int x, int y, int w, int h, int color) {
5756
ssd1306.drawRect(x, y, w, h, color);
5857
}
5958

60-
void fillRect(int x, int y, int w, int h, int color) override {
59+
void fillRect(int x, int y, int w, int h, int color) {
6160
ssd1306.fillRect(x, y, w, h, color);
6261
}
6362

64-
void drawCircle(int x0, int y0, int r, int color) override {
63+
void drawCircle(int x0, int y0, int r, int color) {
6564
ssd1306.drawCircle(x0, y0, r, color);
6665
}
6766

68-
void getTextBounds(const char *string, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) override {
67+
void getTextBounds(const char *string, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
6968
ssd1306.getTextBounds(string, x, y, x1, y1, w, h);
7069
}
7170

72-
int getWidth() const override {
71+
int getWidth() const {
7372
return Config::OLED::WIDTH;
7473
}
7574

76-
int getHeight() const override {
75+
int getHeight() const {
7776
return Config::OLED::HEIGHT;
7877
}
7978
};

src/ui/GraphicsContext.h

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/ui/Renderer.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
#include "../domain/Clock.h"
77
#include "../domain/Trip.h"
8-
#include "GraphicsContext.h"
98
#include "Mode.h"
109

1110
namespace ui {
1211

13-
class Renderer {
12+
template <typename ContextT> class Renderer {
1413
private:
1514
int batteryLevel = 85;
1615
int satelliteCount = 5;
@@ -21,7 +20,7 @@ class Renderer {
2120
};
2221

2322
public:
24-
void render(GraphicsContext &ctx, const domain::Trip &trip, const domain::Clock &clock, Mode::ID modeId) {
23+
void render(ContextT &ctx, const domain::Trip &trip, const domain::Clock &clock, Mode::ID modeId) {
2524
ctx.clear();
2625

2726
drawHeader(ctx);
@@ -89,7 +88,7 @@ class Renderer {
8988
}
9089

9190
// Taken from OLED.h
92-
void drawHeader(GraphicsContext &ctx) {
91+
void drawHeader(ContextT &ctx) {
9392
ctx.setTextSize(1);
9493
ctx.setTextColor(1); // WHITE
9594
ctx.setCursor(0, 0);
@@ -101,15 +100,15 @@ class Renderer {
101100
ctx.drawLine(0, 10, ctx.getWidth(), 10, 1); // WHITE
102101
}
103102

104-
void drawFooter(GraphicsContext &ctx) {
103+
void drawFooter(ContextT &ctx) {
105104
ctx.drawLine(0, ctx.getHeight() - 10, ctx.getWidth(), ctx.getHeight() - 10, 1); // WHITE
106105

107106
ctx.setTextSize(1);
108107
ctx.setCursor(0, ctx.getHeight() - 8);
109108
ctx.print("Ready"); // Placeholder for status
110109
}
111110

112-
void drawMainArea(GraphicsContext &ctx, const char *title, const char *value, const char *unit) {
111+
void drawMainArea(ContextT &ctx, const char *title, const char *value, const char *unit) {
113112
// Title
114113
ctx.setTextSize(1);
115114
ctx.setCursor(0, 14);
@@ -131,15 +130,15 @@ class Renderer {
131130
ctx.print(unit);
132131
}
133132

134-
void drawBatteryIcon(GraphicsContext &ctx, int x, int y, int percentage) {
133+
void drawBatteryIcon(ContextT &ctx, int x, int y, int percentage) {
135134
ctx.drawRect(x, y, 12, 6, 1);
136135
ctx.fillRect(x + 12, y + 2, 2, 2, 1); // Battery positive terminal
137136

138137
int width = map(percentage, 0, 100, 0, 10);
139138
ctx.fillRect(x + 1, y + 1, width, 4, 1);
140139
}
141140

142-
void drawSatelliteIcon(GraphicsContext &ctx, int x, int y, int count) {
141+
void drawSatelliteIcon(ContextT &ctx, int x, int y, int count) {
143142
ctx.drawCircle(x + 3, y + 3, 2, 1); // Placeholder for satellite icon
144143
}
145144
};

tests/host/CycleComputerTest.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ using ::testing::ReturnRef;
1414
using ::testing::StrEq;
1515

1616
// Mock OLED
17-
class MockOLED : public ui::GraphicsContext {
17+
class MockOLED {
1818
public:
19-
MOCK_METHOD(void, clear, (), (override));
20-
MOCK_METHOD(void, display, (), (override));
21-
MOCK_METHOD(void, setTextSize, (int), (override));
22-
MOCK_METHOD(void, setTextColor, (int), (override));
23-
MOCK_METHOD(void, setCursor, (int, int), (override));
24-
MOCK_METHOD(void, print, (const char *), (override));
25-
MOCK_METHOD(void, drawLine, (int, int, int, int, int), (override));
26-
MOCK_METHOD(void, drawRect, (int, int, int, int, int), (override));
27-
MOCK_METHOD(void, fillRect, (int, int, int, int, int), (override));
28-
MOCK_METHOD(void, drawCircle, (int, int, int, int), (override));
29-
30-
MOCK_METHOD(void, getTextBounds, (const char *, int16_t, int16_t, int16_t *, int16_t *, uint16_t *, uint16_t *), (override));
31-
32-
MOCK_METHOD(int, getWidth, (), (const, override));
33-
MOCK_METHOD(int, getHeight, (), (const, override));
19+
MOCK_METHOD(void, clear, ());
20+
MOCK_METHOD(void, display, ());
21+
MOCK_METHOD(void, setTextSize, (int));
22+
MOCK_METHOD(void, setTextColor, (int));
23+
MOCK_METHOD(void, setCursor, (int, int));
24+
MOCK_METHOD(void, print, (const char *));
25+
MOCK_METHOD(void, drawLine, (int, int, int, int, int));
26+
MOCK_METHOD(void, drawRect, (int, int, int, int, int));
27+
MOCK_METHOD(void, fillRect, (int, int, int, int, int));
28+
MOCK_METHOD(void, drawCircle, (int, int, int, int));
29+
30+
MOCK_METHOD(void, getTextBounds, (const char *, int16_t, int16_t, int16_t *, int16_t *, uint16_t *, uint16_t *));
31+
32+
MOCK_METHOD(int, getWidth, (), (const));
33+
MOCK_METHOD(int, getHeight, (), (const));
3434

3535
MockOLED() {
3636
ON_CALL(*this, getWidth()).WillByDefault(Return(Config::OLED::WIDTH));
@@ -83,7 +83,8 @@ class CycleComputerTest : public ::testing::Test {
8383
ON_CALL(mockBtnB, isPressed()).WillByDefault(Return(false));
8484
ON_CALL(mockBtnB, isHeld()).WillByDefault(Return(false));
8585

86-
computer = new application::CycleComputer<NiceMock<MockOLED>, NiceMock<MockGnssProvider>, NiceMock<MockButton>>(mockDisplay, mockGnss, mockBtnA, mockBtnB);
86+
computer = new application::CycleComputer<NiceMock<MockOLED>, NiceMock<MockGnssProvider>, NiceMock<MockButton>>(mockDisplay, mockGnss, mockBtnA,
87+
mockBtnB);
8788
}
8889

8990
void TearDown() override {

0 commit comments

Comments
 (0)