Skip to content

Commit f576414

Browse files
committed
refactor: create renderer
1 parent 4f99e2e commit f576414

File tree

7 files changed

+275
-231
lines changed

7 files changed

+275
-231
lines changed

Spresense-CycleComputer.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "src/drivers/OLED.h"
66
#include "src/ui/Input.h"
77

8-
drivers::OLED display(Wire);
8+
drivers::OLED display;
99
ui::Input input;
1010
application::CycleComputer<drivers::OLED, drivers::Gnss, ui::Input> computer(display, drivers::Gnss::getInstance(), input);
1111

src/CycleComputer.h

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#include "Config.h"
44
#include "domain/Clock.h"
55
#include "domain/Trip.h"
6-
#include "ui/DisplayData.h"
76
#include "ui/InputEvent.h"
87
#include "ui/Mode.h"
8+
#include "ui/Renderer.h"
99
#include <cstdio>
1010

1111
namespace application {
@@ -18,6 +18,7 @@ template <typename DisplayT, typename GnssT, typename InputT> class CycleCompute
1818
ui::Mode mode;
1919
domain::Trip trip;
2020
domain::Clock clock;
21+
ui::Renderer renderer;
2122
unsigned long lastDisplayUpdate = 0;
2223
bool forceUpdate = false;
2324

@@ -70,48 +71,7 @@ template <typename DisplayT, typename GnssT, typename InputT> class CycleCompute
7071
lastDisplayUpdate = currentMillis;
7172
forceUpdate = false;
7273

73-
char buf[32];
74-
ui::DisplayDataType type;
75-
getDisplayData(mode.get(), type, buf, sizeof(buf));
76-
77-
display.show(type, buf);
78-
}
79-
80-
void getDisplayData(ui::Mode::ID modeId, ui::DisplayDataType &type, char *buf, size_t size) {
81-
switch (modeId) {
82-
case ui::Mode::ID::SPEED:
83-
type = ui::DisplayDataType::SPEED;
84-
trip.getSpeedStr(buf, size);
85-
break;
86-
case ui::Mode::ID::MAX_SPEED:
87-
type = ui::DisplayDataType::MAX_SPEED;
88-
trip.getMaxSpeedStr(buf, size);
89-
break;
90-
case ui::Mode::ID::AVG_SPEED:
91-
type = ui::DisplayDataType::AVG_SPEED;
92-
trip.getAvgSpeedStr(buf, size);
93-
break;
94-
case ui::Mode::ID::DISTANCE:
95-
type = ui::DisplayDataType::DISTANCE;
96-
trip.getDistanceStr(buf, size);
97-
break;
98-
case ui::Mode::ID::TIME:
99-
type = ui::DisplayDataType::TIME;
100-
clock.getTimeStr(buf, size);
101-
break;
102-
case ui::Mode::ID::MOVING_TIME:
103-
type = ui::DisplayDataType::MOVING_TIME;
104-
trip.getMovingTimeStr(buf, size);
105-
break;
106-
case ui::Mode::ID::ELAPSED_TIME:
107-
type = ui::DisplayDataType::ELAPSED_TIME;
108-
trip.getElapsedTimeStr(buf, size);
109-
break;
110-
default:
111-
type = ui::DisplayDataType::INVALID;
112-
buf[0] = '\0';
113-
break;
114-
}
74+
renderer.render(display, trip, clock, mode.get());
11575
}
11676
};
11777

src/drivers/OLED.h

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,93 +3,78 @@
33
#include <Adafruit_GFX.h>
44
#include <Adafruit_SSD1306.h>
55
#include <Arduino.h>
6+
#include <Wire.h>
67

78
#include "../Config.h"
8-
#include "../ui/DisplayData.h"
9+
#include "../ui/GraphicsContext.h"
910

1011
namespace drivers {
1112

12-
class OLED {
13+
class OLED : public ui::GraphicsContext {
1314
private:
14-
Adafruit_SSD1306 display;
15-
TwoWire &wire;
16-
int batteryLevel = 85;
17-
int satelliteCount = 5;
15+
Adafruit_SSD1306 ssd1306;
1816

1917
public:
20-
OLED(TwoWire &i2c) : display(Config::OLED::WIDTH, Config::OLED::HEIGHT, &i2c, -1), wire(i2c) {}
18+
OLED() : ssd1306(Config::OLED::WIDTH, Config::OLED::HEIGHT, &Wire, -1) {}
2119

2220
void begin() {
23-
if (!display.begin(SSD1306_SWITCHCAPVCC, Config::OLED::ADDRESS))
21+
if (!ssd1306.begin(SSD1306_SWITCHCAPVCC, Config::OLED::ADDRESS))
2422
for (;;);
25-
display.clearDisplay();
26-
display.display();
23+
ssd1306.clearDisplay();
24+
ssd1306.display();
2725
}
2826

29-
void show(ui::DisplayDataType type, const char *value) {
30-
display.clearDisplay();
27+
// GraphicsContext implementation
28+
void clear() override {
29+
ssd1306.clearDisplay();
30+
}
3131

32-
drawHeader();
33-
ui::DisplayMetadata meta = ui::getDisplayMetadata(type);
34-
drawMainArea(meta.title.c_str(), value, meta.unit.c_str());
35-
drawFooter();
32+
void display() override {
33+
ssd1306.display();
34+
}
3635

37-
display.display();
36+
void setTextSize(int size) override {
37+
ssd1306.setTextSize(size);
3838
}
3939

40-
private:
41-
void drawHeader() {
42-
display.setTextSize(1);
43-
display.setTextColor(SSD1306_WHITE);
44-
display.setCursor(0, 0);
45-
display.print("GNSS ON");
40+
void setTextColor(int color) override {
41+
ssd1306.setTextColor(color);
42+
}
4643

47-
drawSatelliteIcon(100, 0, satelliteCount);
48-
drawBatteryIcon(115, 0, batteryLevel);
44+
void setCursor(int x, int y) override {
45+
ssd1306.setCursor(x, y);
46+
}
4947

50-
display.drawLine(0, 10, Config::OLED::WIDTH, 10, SSD1306_WHITE);
48+
void print(const char *text) override {
49+
ssd1306.print(text);
5150
}
5251

53-
void drawFooter() {
54-
display.drawLine(0, Config::OLED::HEIGHT - 10, Config::OLED::WIDTH, Config::OLED::HEIGHT - 10, SSD1306_WHITE);
52+
void drawLine(int x0, int y0, int x1, int y1, int color) override {
53+
ssd1306.drawLine(x0, y0, x1, y1, color);
54+
}
55+
56+
void drawRect(int x, int y, int w, int h, int color) override {
57+
ssd1306.drawRect(x, y, w, h, color);
58+
}
5559

56-
display.setTextSize(1);
57-
display.setCursor(0, Config::OLED::HEIGHT - 8);
58-
display.print("Ready"); // Placeholder for status
60+
void fillRect(int x, int y, int w, int h, int color) override {
61+
ssd1306.fillRect(x, y, w, h, color);
5962
}
6063

61-
void drawMainArea(const char *title, const char *value, const char *unit) {
62-
// Title
63-
display.setTextSize(1);
64-
display.setCursor(0, 14);
65-
display.print(title);
66-
67-
// Value (Large)
68-
display.setTextSize(2); // Make value bigger
69-
int16_t x1, y1;
70-
uint16_t w, h;
71-
display.getTextBounds(value, 0, 0, &x1, &y1, &w, &h);
72-
display.setCursor((Config::OLED::WIDTH - w) / 2, 28);
73-
display.print(value);
74-
75-
if (strlen(unit) == 0) return;
76-
77-
// Unit
78-
display.setTextSize(1);
79-
display.setCursor(Config::OLED::WIDTH - 24, 45); // Bottom right of main area
80-
display.print(unit);
64+
void drawCircle(int x0, int y0, int r, int color) override {
65+
ssd1306.drawCircle(x0, y0, r, color);
8166
}
8267

83-
void drawBatteryIcon(int x, int y, int percentage) {
84-
display.drawRect(x, y, 12, 6, SSD1306_WHITE);
85-
display.fillRect(x + 12, y + 2, 2, 2, SSD1306_WHITE); // Battery positive terminal
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 {
69+
ssd1306.getTextBounds(string, x, y, x1, y1, w, h);
70+
}
8671

87-
int width = map(percentage, 0, 100, 0, 10);
88-
display.fillRect(x + 1, y + 1, width, 4, SSD1306_WHITE);
72+
int getWidth() const override {
73+
return Config::OLED::WIDTH;
8974
}
9075

91-
void drawSatelliteIcon(int x, int y, int count) {
92-
display.drawCircle(x + 3, y + 3, 2, SSD1306_WHITE); // Placeholder for satellite icon
76+
int getHeight() const override {
77+
return Config::OLED::HEIGHT;
9378
}
9479
};
9580

src/ui/DisplayData.h

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

src/ui/GraphicsContext.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
namespace ui {
6+
7+
class GraphicsContext {
8+
public:
9+
virtual ~GraphicsContext() = default;
10+
11+
virtual void clear() = 0;
12+
virtual void display() = 0;
13+
virtual void setTextSize(int size) = 0;
14+
virtual void setTextColor(int color) = 0;
15+
virtual void setCursor(int x, int y) = 0;
16+
virtual void print(const char *text) = 0;
17+
virtual void drawLine(int x0, int y0, int x1, int y1, int color) = 0;
18+
virtual void drawRect(int x, int y, int w, int h, int color) = 0;
19+
virtual void fillRect(int x, int y, int w, int h, int color) = 0;
20+
virtual void drawCircle(int x0, int y0, int r, int color) = 0;
21+
virtual void getTextBounds(const char *string, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) = 0;
22+
23+
virtual int getWidth() const = 0;
24+
virtual int getHeight() const = 0;
25+
};
26+
27+
} // namespace ui

0 commit comments

Comments
 (0)