Skip to content

Commit c860d9d

Browse files
committed
refactor: add namespace
1 parent f11c262 commit c860d9d

28 files changed

+729
-639
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515

1616
## 2. 基本スタイル (Basic Style)
1717

18-
- **インデント**: スペース 2 つ
19-
- **中括弧の位置**: 同じ行に置く (Attach)
20-
- OK: `void loop() {`
21-
- NG: `void loop()\n{`
22-
- **スペース**: `if`, `while`, `for` のキーワードの直後にスペースを入れる
23-
- OK: `if (condition)`
24-
- NG: `if(condition)`
25-
- **行の長さ**: 最大 200 文字
18+
`clang-format` を使用します。設定ファイル (`.clang-format`) がリポジトリに含まれています。
19+
コミット前にフォーマットを適用することを推奨します。
2620

2721
## 3. ハードウェア定義 (Hardware Definitions)
2822

@@ -39,8 +33,3 @@
3933
4034
- **非ブロッキング処理**: `delay()` の使用は最小限に留め、可能な限り `millis()` を使用した非ブロッキング処理を実装してください。
4135
- **ライブラリ** : 使用するライブラリのバージョン依存がある場合は、コメント等でバージョンを明記してください。
42-
43-
## 5. フォーマッタ (Formatter)
44-
45-
本プロジェクトでは `clang-format` を使用します。設定ファイル (`.clang-format`) がリポジトリに含まれています。
46-
コミット前にフォーマットを適用することを推奨します。

Spresense-CycleComputer.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
#include "src/system/CycleComputer.h"
21
#include "src/drivers/OLEDDriver.h"
2+
#include "src/system/CycleComputer.h"
33

4-
OLEDDriver display;
5-
CycleComputer computer(&display);
4+
drivers::OLEDDriver display(Wire);
5+
application::CycleComputer computer(&display);
66

77
void setup() {
8-
computer.begin();
8+
Serial.begin(115200);
9+
computer.begin();
910
}
1011

1112
void loop() {
12-
computer.update();
13+
computer.update();
1314
}

src/Config.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,45 @@
55
namespace Config {
66

77
namespace Pin {
8+
89
// ボタン
910
constexpr int BTN_A = PIN_D00;
1011
constexpr int BTN_B = PIN_D01;
1112

12-
// OLED (I2C)
13-
constexpr int OLED_SCL = PIN_D15;
14-
constexpr int OLED_SDA = PIN_D14;
15-
1613
// システム
1714
constexpr int WARN_LED = PIN_D02;
15+
1816
} // namespace Pin
1917

2018
constexpr unsigned long DEBOUNCE_DELAY = 50;
2119
constexpr unsigned long DISPLAY_UPDATE_INTERVAL_MS = 100;
2220

2321
namespace OLED {
22+
2423
constexpr int WIDTH = 128;
2524
constexpr int HEIGHT = 64;
2625
constexpr int ADDRESS = 0x3C;
26+
2727
} // namespace OLED
2828

2929
namespace Time {
30+
3031
constexpr int JST_OFFSET = 9;
32+
3133
}
3234

3335
namespace Trip {
36+
3437
constexpr float MOVE_THRESHOLD_KMH = 3.0f; // 「移動中」とみなすための閾値
38+
3539
}
3640

3741
namespace Power {
42+
3843
constexpr int BATTERY_LOW_THRESHOLD = 3600; // 3.6V
3944
constexpr int BATTERY_CHECK_INTERVAL_MS = 10000;
4045
constexpr int LED_BLINK_INTERVAL_MS = 500;
46+
4147
} // namespace Power
48+
4249
} // namespace Config

src/drivers/Button.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "Button.h"
22

3+
namespace drivers {
4+
35
Button::Button(int pin) : pinNumber(pin) {}
46

57
void Button::begin() {
@@ -27,3 +29,17 @@ bool Button::isPressed() {
2729
lastPinLevel = rawPinLevel;
2830
return pressed;
2931
}
32+
33+
#ifdef UNIT_TEST
34+
void Button::resetMock() {
35+
// No static state to reset currently in Button class itself
36+
// Maybe reset Arduino mock pin states if we could access them?
37+
// For now do nothing or just rely on test setup to reset pins
38+
}
39+
40+
void Button::setMockState(int pin, int state) {
41+
setPinState(pin, state);
42+
}
43+
#endif
44+
45+
} // namespace drivers

src/drivers/Button.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "../Config.h"
44
#include "Arduino.h"
55

6+
namespace drivers {
7+
68
class Button {
79
private:
810
const int pinNumber;
@@ -26,4 +28,11 @@ class Button {
2628
inline bool isHeld() const {
2729
return stablePinLevel == LOW;
2830
}
31+
32+
#ifdef UNIT_TEST
33+
static void resetMock();
34+
static void setMockState(int pin, int state);
35+
#endif
2936
};
37+
38+
} // namespace drivers

src/drivers/GPSWrapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

7+
namespace drivers {
8+
79
bool GPSWrapper::begin() {
810
if (gnss.begin() != 0)
911
return false;
@@ -52,3 +54,5 @@ void GPSWrapper::setMockSpeed(float speedKmh) {
5254
SpGnss::mockVelocityData = speedKmh / 3.6f;
5355
}
5456
#endif
57+
58+
} // namespace drivers

src/drivers/GPSWrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <Arduino.h>
44
#include <GNSS.h>
55

6+
namespace drivers {
7+
68
class GPSWrapper {
79
public:
810
bool begin();
@@ -20,3 +22,5 @@ class GPSWrapper {
2022
static void setMockSpeed(float speedKmh);
2123
#endif
2224
};
25+
26+
} // namespace drivers

src/drivers/OLEDDriver.cpp

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,87 @@
44

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

7-
OLEDDriver::OLEDDriver() : display(Config::OLED::WIDTH, Config::OLED::HEIGHT, &Wire, -1) {
8-
currentType = DisplayDataType::INVALID;
9-
}
7+
namespace drivers {
108

11-
bool OLEDDriver::begin() {
12-
if (!display.begin(SSD1306_SWITCHCAPVCC, Config::OLED::ADDRESS))
13-
return false;
9+
OLEDDriver::OLEDDriver(TwoWire &i2c) : display(Config::OLED::WIDTH, Config::OLED::HEIGHT, &i2c, -1), wire(i2c) {}
1410

11+
void OLEDDriver::begin() {
12+
if (!display.begin(SSD1306_SWITCHCAPVCC, Config::OLED::ADDRESS)) {
13+
// Initialization failed
14+
for (;;)
15+
;
16+
}
1517
display.clearDisplay();
1618
display.display();
17-
display.setTextColor(SSD1306_WHITE);
18-
display.setTextSize(1);
19-
20-
currentValue = "";
21-
return true;
2219
}
2320

24-
void OLEDDriver::clear() {
21+
void OLEDDriver::show(application::DisplayDataType type, const char *value) {
2522
display.clearDisplay();
23+
24+
drawHeader();
25+
26+
application::DisplayMetadata meta = application::getDisplayMetadata(type);
27+
drawMainArea(meta.title.c_str(), value, meta.unit.c_str());
28+
29+
drawFooter();
30+
2631
display.display();
27-
currentValue = "";
28-
currentType = DisplayDataType::INVALID;
2932
}
3033

31-
void OLEDDriver::drawTitle(const String &title) {
34+
void OLEDDriver::drawHeader() {
3235
display.setTextSize(1);
36+
display.setTextColor(SSD1306_WHITE);
3337
display.setCursor(0, 0);
34-
display.println(title);
38+
display.print("GNSS ON");
39+
40+
drawSatelliteIcon(100, 0, satelliteCount);
41+
drawBatteryIcon(115, 0, batteryLevel);
42+
43+
display.drawLine(0, 10, Config::OLED::WIDTH, 10, SSD1306_WHITE);
3544
}
3645

37-
void OLEDDriver::drawUnit(const String &unit) {
38-
if (unit.length() <= 0)
39-
return;
46+
void OLEDDriver::drawFooter() {
47+
display.drawLine(0, Config::OLED::HEIGHT - 10, Config::OLED::WIDTH, Config::OLED::HEIGHT - 10, SSD1306_WHITE);
4048

41-
int16_t x1, y1;
42-
uint16_t w, h;
4349
display.setTextSize(1);
44-
display.getTextBounds(unit, 0, 0, &x1, &y1, &w, &h);
45-
display.setCursor(Config::OLED::WIDTH - w - 4, Config::OLED::HEIGHT - h - 2);
46-
display.print(unit);
50+
display.setCursor(0, Config::OLED::HEIGHT - 8);
51+
display.print("Ready"); // Placeholder for status
4752
}
4853

49-
void OLEDDriver::drawValue(const String &value) {
50-
int len = value.length();
51-
if (len < 5)
52-
display.setTextSize(3);
53-
else
54-
display.setTextSize(2);
54+
void OLEDDriver::drawMainArea(const char *title, const char *value, const char *unit) {
55+
// Title
56+
display.setTextSize(1);
57+
display.setCursor(0, 14);
58+
display.print(title);
5559

60+
// Value (Large)
61+
display.setTextSize(2); // Make value bigger
5662
int16_t x1, y1;
5763
uint16_t w, h;
5864
display.getTextBounds(value, 0, 0, &x1, &y1, &w, &h);
59-
60-
int x = (Config::OLED::WIDTH - w) / 2;
61-
int y = (Config::OLED::HEIGHT - h) / 2 + 8; // shift down a bit below title
62-
63-
display.setCursor(x, y);
65+
display.setCursor((Config::OLED::WIDTH - w) / 2, 28);
6466
display.print(value);
65-
}
66-
67-
void OLEDDriver::show(DisplayDataType type, const char *value) {
68-
if (type == currentType && currentValue.equals(value))
69-
return;
7067

71-
currentType = type;
72-
currentValue = String(value);
68+
// Unit
69+
if (strlen(unit) > 0) {
70+
display.setTextSize(1);
71+
display.setCursor(Config::OLED::WIDTH - 24, 45); // Bottom right of main area
72+
display.print(unit);
73+
}
74+
}
7375

74-
display.clearDisplay();
76+
void OLEDDriver::drawBatteryIcon(int x, int y, int percentage) {
77+
display.drawRect(x, y, 12, 6, SSD1306_WHITE);
78+
display.fillRect(x + 12, y + 2, 2, 2, SSD1306_WHITE); // Battery positive terminal
7579

76-
DisplayMetadata meta = getDisplayMetadata(type);
77-
drawTitle(meta.title);
78-
drawValue(currentValue);
79-
drawUnit(meta.unit);
80+
int width = map(percentage, 0, 100, 0, 10);
81+
display.fillRect(x + 1, y + 1, width, 4, SSD1306_WHITE);
82+
}
8083

81-
display.display();
84+
void OLEDDriver::drawSatelliteIcon(int x, int y, int count) {
85+
// Placeholder for satellite icon
86+
display.drawCircle(x + 3, y + 3, 2, SSD1306_WHITE); // Simple circle for now
87+
// Could display count next to it if needed
8288
}
89+
90+
} // namespace drivers

src/drivers/OLEDDriver.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@
77

88
#include "../system/DisplayData.h"
99

10+
namespace drivers {
11+
1012
class OLEDDriver {
13+
public:
14+
OLEDDriver(TwoWire &i2c);
15+
virtual void begin();
16+
virtual void show(application::DisplayDataType type, const char *value);
17+
1118
private:
1219
Adafruit_SSD1306 display;
13-
DisplayDataType currentType;
14-
String currentValue;
20+
TwoWire &wire;
1521

16-
void drawTitle(const String &title);
17-
void drawUnit(const String &unit);
18-
void drawValue(const String &value);
22+
void drawHeader();
23+
void drawFooter();
24+
void drawMainArea(const char *title, const char *value, const char *unit);
25+
void drawBatteryIcon(int x, int y, int percentage);
26+
void drawSatelliteIcon(int x, int y, int count);
1927

20-
public:
21-
OLEDDriver();
22-
virtual ~OLEDDriver() {}
23-
virtual bool begin();
24-
virtual void clear();
25-
virtual void show(DisplayDataType type, const char *value);
28+
int batteryLevel = 85;
29+
int satelliteCount = 5;
2630
};
31+
32+
} // namespace drivers

src/system/CycleComputer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
#include "../Config.h"
44
#include "../drivers/OLEDDriver.h"
55

6+
namespace application {
7+
68
inline void formatFloat(float val, int width, int prec, char *buf, size_t size) {
79
char fmt[6];
810
snprintf(fmt, sizeof(fmt), "%%%d.%df", width, prec);
911
snprintf(buf, size, fmt, val);
1012
}
1113

12-
CycleComputer::CycleComputer(OLEDDriver *display) : display(display) {}
14+
CycleComputer::CycleComputer(drivers::OLEDDriver *display) : display(display) {}
1315

1416
void CycleComputer::begin() {
1517
display->begin();
@@ -100,3 +102,5 @@ void CycleComputer::getDisplayData(Mode mode, DisplayDataType &type, char *buf,
100102
break;
101103
}
102104
}
105+
106+
} // namespace application

0 commit comments

Comments
 (0)