Skip to content

Commit 81bf5de

Browse files
authored
Merge pull request #4 from ESPToolKit/formatter-baseline
Align formatter baseline with esptoolkit-template
2 parents e94dbf9 + 73f0f24 commit 81bf5de

19 files changed

Lines changed: 462 additions & 293 deletions

File tree

.clang-format

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
BasedOnStyle: LLVM
2+
ColumnLimit: 100
3+
BinPackArguments: false
4+
BinPackParameters: false
5+
AllowAllArgumentsOnNextLine: false
6+
AlignAfterOpenBracket: BlockIndent
7+
UseTab: ForIndentation
8+
IndentWidth: 4
9+
TabWidth: 4
10+
ContinuationIndentWidth: 4
11+
AllowShortFunctionsOnASingleLine: None

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
charset = utf-8
7+
8+
[*.{c,cc,cpp,h,hpp,ino}]
9+
indent_style = tab
10+
indent_size = tab
11+
tab_width = 4

.vscode/bin/clang-format

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
if command -v clang-format >/dev/null 2>&1; then
6+
exec clang-format "$@"
7+
fi
8+
9+
_home_dir="${HOME:-}"
10+
if [ -n "$_home_dir" ]; then
11+
_candidate="$(ls -1d "$_home_dir"/.vscode/extensions/ms-vscode.cpptools-*-linux-x64/LLVM/bin/clang-format 2>/dev/null | tail -n 1 || true)"
12+
if [ -n "$_candidate" ] && [ -x "$_candidate" ]; then
13+
exec "$_candidate" "$@"
14+
fi
15+
fi
16+
17+
echo "clang-format executable not found." >&2
18+
echo "Install clang-format system-wide or install/update ms-vscode.cpptools." >&2
19+
exit 127

.vscode/extensions.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"recommendations": [
3+
"pioarduino.pioarduino-ide",
4+
"xaver.clang-format"
5+
],
6+
"unwantedRecommendations": [
7+
"ms-vscode.cpptools-extension-pack"
8+
]
9+
}

.vscode/settings.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"files.associations": {
3+
"*.ino": "cpp"
4+
},
5+
"editor.defaultFormatter": "xaver.clang-format",
6+
"C_Cpp.formatting": "Disabled",
7+
"clang-format.style": "file",
8+
"clang-format.executable": "${workspaceRoot}/.vscode/bin/clang-format",
9+
"[cpp]": {
10+
"editor.defaultFormatter": "xaver.clang-format",
11+
"editor.detectIndentation": false,
12+
"editor.insertSpaces": false,
13+
"editor.tabSize": 4,
14+
"editor.formatOnSave": true
15+
},
16+
"[c]": {
17+
"editor.defaultFormatter": "xaver.clang-format",
18+
"editor.detectIndentation": false,
19+
"editor.insertSpaces": false,
20+
"editor.tabSize": 4,
21+
"editor.formatOnSave": true
22+
},
23+
"[arduino]": {
24+
"editor.defaultFormatter": "xaver.clang-format",
25+
"editor.detectIndentation": false,
26+
"editor.insertSpaces": false,
27+
"editor.tabSize": 4,
28+
"editor.formatOnSave": true
29+
}
30+
}

.vscode/tasks.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Format Firmware Sources",
6+
"type": "shell",
7+
"command": "bash ${workspaceFolder}/scripts/format_cpp.sh",
8+
"group": "build",
9+
"problemMatcher": []
10+
}
11+
]
12+
}

CONTRIBUTING.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,9 @@ Please keep these in mind when contributing:
134134
- **I/O**: Use `StreamUtils::WriteBufferingStream` for buffered writes.
135135
- **Validation**: Run schema hooks on create/update; on failure revert and return a validation error.
136136
- **Naming**: lowerCamelCase for methods/vars, UpperCamelCase for types, ALL_CAPS for simple constants/enums.
137-
- **Formatting**: Use a consistent `clang-format` (LLVM/Google); keep lines ≤ 120 cols.
137+
- **Formatting**: Follow the repository `.clang-format` + `.editorconfig` baseline from `esptoolkit-template` (LLVM-derived style, `ColumnLimit: 100`, tabs with width `4`, `BinPackArguments/Parameters: false`, `AllowShortFunctionsOnASingleLine: None`).
138138
- **Allocations**: Avoid hidden allocations in hot paths and inside event callbacks & sync loops.
139139

140-
Optional `.clang-format` starter (Google-like):
141-
```yaml
142-
BasedOnStyle: Google
143-
IndentWidth: 4
144-
ColumnLimit: 120
145-
DerivePointerAlignment: false
146-
PointerAlignment: Left
147-
AllowShortFunctionsOnASingleLine: Empty
148-
```
149-
150140
---
151141

152142
## Commit messages & branches

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ if (ESPStoreCodec::decodeLocalDateTime(doc["localTime"], local)) {
152152
- `examples/ClearAndReseed` – clear the store and seed with defaults.
153153
- `examples/LocalDateTime` – store LocalDateTime as `{ epochSeconds, offsetMinutes }`.
154154
155+
## Formatting Baseline
156+
157+
This repository follows the firmware formatting baseline from `esptoolkit-template`:
158+
- `.clang-format` is the source of truth for C/C++/INO layout.
159+
- `.editorconfig` enforces tabs (`tab_width = 4`), LF endings, and final newline.
160+
- Format all tracked firmware sources with `bash scripts/format_cpp.sh`.
161+
155162
## License
156163
MIT — see [LICENSE.md](LICENSE.md).
157164

examples/ClearAndReseed/ClearAndReseed.ino

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,43 @@ ESPJsonDB db;
55
ESPStore store;
66

77
void setup() {
8-
Serial.begin(115200);
8+
Serial.begin(115200);
99

10-
if (!db.init("/db").ok()) {
11-
Serial.println("DB init failed");
12-
return;
13-
}
10+
if (!db.init("/db").ok()) {
11+
Serial.println("DB init failed");
12+
return;
13+
}
1414

15-
store.init(&db, "systemConf");
15+
store.init(&db, "systemConf");
1616

17-
JsonDocument cfg;
18-
cfg["mode"] = "normal";
19-
cfg["retries"] = 3;
17+
JsonDocument cfg;
18+
cfg["mode"] = "normal";
19+
cfg["retries"] = 3;
2020

21-
auto st = store.set(cfg.as<JsonVariantConst>());
22-
Serial.printf("Seed set: %s\n", st.ok() ? "OK" : st.message);
21+
auto st = store.set(cfg.as<JsonVariantConst>());
22+
Serial.printf("Seed set: %s\n", st.ok() ? "OK" : st.message);
2323

24-
st = store.syncNow();
25-
Serial.printf("Sync: %s\n", st.ok() ? "OK" : st.message);
24+
st = store.syncNow();
25+
Serial.printf("Sync: %s\n", st.ok() ? "OK" : st.message);
2626

27-
st = store.clear();
28-
Serial.printf("Clear: %s\n", st.ok() ? "OK" : st.message);
27+
st = store.clear();
28+
Serial.printf("Clear: %s\n", st.ok() ? "OK" : st.message);
2929

30-
JsonDocument fallback;
31-
fallback["mode"] = "safe";
32-
fallback["retries"] = 1;
30+
JsonDocument fallback;
31+
fallback["mode"] = "safe";
32+
fallback["retries"] = 1;
3333

34-
bool usedDefault = false;
35-
auto res = store.getOr(fallback.as<JsonVariantConst>(), &usedDefault);
36-
Serial.printf("After clear getOr: %s (%s)\n",
37-
res.ok() ? "OK" : res.message(),
38-
usedDefault ? "default" : "stored");
34+
bool usedDefault = false;
35+
auto res = store.getOr(fallback.as<JsonVariantConst>(), &usedDefault);
36+
Serial.printf(
37+
"After clear getOr: %s (%s)\n",
38+
res.ok() ? "OK" : res.message(),
39+
usedDefault ? "default" : "stored"
40+
);
3941

40-
serializeJsonPretty(res.data, Serial);
41-
store.deinit();
42+
serializeJsonPretty(res.data, Serial);
43+
store.deinit();
4244
}
4345

44-
void loop() {}
46+
void loop() {
47+
}

examples/CodecAll/CodecAll.ino

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,72 @@
1+
#include <ESPDate.h>
12
#include <ESPJsonDB.h>
23
#include <ESPStore.h>
34
#include <ESPStoreCodec.h>
4-
#include <ESPDate.h>
55

66
ESPJsonDB db;
77
ESPStore store;
88
ESPDate date;
99

1010
void setup() {
11-
Serial.begin(115200);
12-
13-
if (!db.init("/db").ok()) {
14-
Serial.println("DB init failed");
15-
return;
16-
}
17-
18-
store.init(&db, "codecDemo");
19-
20-
JsonDocument doc;
21-
22-
IPAddress ip(192, 168, 1, 42);
23-
ESPStoreCodec::encodeIpString(doc["ipString"], ip);
24-
ESPStoreCodec::encodeIpArray(doc["ipArray"], ip);
25-
26-
DateTime dt{};
27-
dt.epochSeconds = 1730000000; // fixed sample epoch
28-
ESPStoreCodec::encodeDateTimeEpoch(doc["timeEpoch"], dt);
29-
ESPStoreCodec::encodeDateTimeIso(doc["timeIso"], dt, date);
30-
31-
LocalDateTime local = date.nowLocal();
32-
ESPStoreCodec::encodeLocalDateTime(doc["localTime"], local);
33-
34-
auto st = store.set(doc.as<JsonVariantConst>());
35-
Serial.printf("Store set: %s\n", st.ok() ? "OK" : st.message);
36-
37-
auto res = store.get();
38-
if (!res.ok()) {
39-
Serial.printf("Store get failed: %s\n", res.message());
40-
return;
41-
}
42-
43-
IPAddress ipStr;
44-
IPAddress ipArr;
45-
ESPStoreCodec::decodeIpString(res.data["ipString"], ipStr);
46-
ESPStoreCodec::decodeIpArray(res.data["ipArray"], ipArr);
47-
48-
DateTime dtEpoch{};
49-
DateTime dtIso{};
50-
ESPStoreCodec::decodeDateTimeEpoch(res.data["timeEpoch"], dtEpoch);
51-
ESPStoreCodec::decodeDateTimeIso(res.data["timeIso"], dtIso, date);
52-
53-
LocalDateTime localDecoded{};
54-
bool localOk = ESPStoreCodec::decodeLocalDateTime(res.data["localTime"], localDecoded);
55-
56-
Serial.printf("IP string: %s\n", ipStr.toString().c_str());
57-
Serial.printf("IP array: %s\n", ipArr.toString().c_str());
58-
Serial.printf("Epoch: %lld\n", static_cast<long long>(dtEpoch.epochSeconds));
59-
Serial.printf("ISO epoch: %lld\n", static_cast<long long>(dtIso.epochSeconds));
60-
if (localOk) {
61-
Serial.printf("Local epoch: %lld offset: %d\n",
62-
static_cast<long long>(localDecoded.utc.epochSeconds),
63-
localDecoded.offsetMinutes);
64-
}
65-
66-
store.deinit();
11+
Serial.begin(115200);
12+
13+
if (!db.init("/db").ok()) {
14+
Serial.println("DB init failed");
15+
return;
16+
}
17+
18+
store.init(&db, "codecDemo");
19+
20+
JsonDocument doc;
21+
22+
IPAddress ip(192, 168, 1, 42);
23+
ESPStoreCodec::encodeIpString(doc["ipString"], ip);
24+
ESPStoreCodec::encodeIpArray(doc["ipArray"], ip);
25+
26+
DateTime dt{};
27+
dt.epochSeconds = 1730000000; // fixed sample epoch
28+
ESPStoreCodec::encodeDateTimeEpoch(doc["timeEpoch"], dt);
29+
ESPStoreCodec::encodeDateTimeIso(doc["timeIso"], dt, date);
30+
31+
LocalDateTime local = date.nowLocal();
32+
ESPStoreCodec::encodeLocalDateTime(doc["localTime"], local);
33+
34+
auto st = store.set(doc.as<JsonVariantConst>());
35+
Serial.printf("Store set: %s\n", st.ok() ? "OK" : st.message);
36+
37+
auto res = store.get();
38+
if (!res.ok()) {
39+
Serial.printf("Store get failed: %s\n", res.message());
40+
return;
41+
}
42+
43+
IPAddress ipStr;
44+
IPAddress ipArr;
45+
ESPStoreCodec::decodeIpString(res.data["ipString"], ipStr);
46+
ESPStoreCodec::decodeIpArray(res.data["ipArray"], ipArr);
47+
48+
DateTime dtEpoch{};
49+
DateTime dtIso{};
50+
ESPStoreCodec::decodeDateTimeEpoch(res.data["timeEpoch"], dtEpoch);
51+
ESPStoreCodec::decodeDateTimeIso(res.data["timeIso"], dtIso, date);
52+
53+
LocalDateTime localDecoded{};
54+
bool localOk = ESPStoreCodec::decodeLocalDateTime(res.data["localTime"], localDecoded);
55+
56+
Serial.printf("IP string: %s\n", ipStr.toString().c_str());
57+
Serial.printf("IP array: %s\n", ipArr.toString().c_str());
58+
Serial.printf("Epoch: %lld\n", static_cast<long long>(dtEpoch.epochSeconds));
59+
Serial.printf("ISO epoch: %lld\n", static_cast<long long>(dtIso.epochSeconds));
60+
if (localOk) {
61+
Serial.printf(
62+
"Local epoch: %lld offset: %d\n",
63+
static_cast<long long>(localDecoded.utc.epochSeconds),
64+
localDecoded.offsetMinutes
65+
);
66+
}
67+
68+
store.deinit();
6769
}
6870

69-
void loop() {}
71+
void loop() {
72+
}

0 commit comments

Comments
 (0)