Skip to content

Commit 50ed3ed

Browse files
authored
Merge pull request #5 from ESPToolKit/formatter-baseline
Align formatter baseline with esptoolkit-template
2 parents f20062f + a309170 commit 50ed3ed

16 files changed

Lines changed: 912 additions & 743 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+
}

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ When your feature shuts down (task exit, OTA handoff, mode switch), call `cpuMon
115115
## Tests
116116
- Build and run `examples/basic_monitor`, `examples/manual_sampling`, and `examples/json_export` via PlatformIO CI or Arduino CLI for a quick smoke test on ESP32 dev boards.
117117
118+
## Formatting Baseline
119+
120+
This repository follows the firmware formatting baseline from `esptoolkit-template`:
121+
- `.clang-format` is the source of truth for C/C++/INO layout.
122+
- `.editorconfig` enforces tabs (`tab_width = 4`), LF endings, and final newline.
123+
- Format all tracked firmware sources with `bash scripts/format_cpp.sh`.
124+
118125
## License
119126
MIT — see [LICENSE.md](LICENSE.md).
120127
Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,72 @@
11
#include <Arduino.h>
22
#include <ESPCpuMonitor.h>
3-
#include <esp_log.h>
43
#include <cmath>
4+
#include <esp_log.h>
55

66
ESPCpuMonitor cpuMonitor;
77

88
void setup() {
9-
Serial.begin(115200);
9+
Serial.begin(115200);
1010

11-
CpuMonitorConfig cfg;
12-
cfg.sampleIntervalMs = 1000; // 1s sampling cadence
13-
cfg.calibrationSamples = 5; // treat first 5 periods as 100% idle baseline
14-
cfg.historySize = 20;
15-
cfg.enableTemperature = true; // toggle temperature readings if your board supports it
16-
cfg.smoothingMode = CpuSmoothingMode::Ewma;
17-
cfg.smoothingAlpha = 0.25f; // smaller alpha => smoother trend
18-
cpuMonitor.init(cfg);
11+
CpuMonitorConfig cfg;
12+
cfg.sampleIntervalMs = 1000; // 1s sampling cadence
13+
cfg.calibrationSamples = 5; // treat first 5 periods as 100% idle baseline
14+
cfg.historySize = 20;
15+
cfg.enableTemperature = true; // toggle temperature readings if your board supports it
16+
cfg.smoothingMode = CpuSmoothingMode::Ewma;
17+
cfg.smoothingAlpha = 0.25f; // smaller alpha => smoother trend
18+
cpuMonitor.init(cfg);
1919

20-
cpuMonitor.onSample([](const CpuUsageSample &sample) {
20+
cpuMonitor.onSample([](const CpuUsageSample &sample) {
2121
#if portNUM_PROCESSORS > 1
22-
ESP_LOGI("CPU", "core0=%.1f%% core1=%.1f%% avg=%.1f%% smoothed=%.1f%% temp=%.1fC (avg %.1fC)",
23-
sample.perCore[0], sample.perCore[1], sample.average,
24-
std::isnan(sample.smoothedAverage) ? -1.0f : sample.smoothedAverage,
25-
std::isnan(sample.temperatureC) ? -1.0f : sample.temperatureC,
26-
std::isnan(sample.temperatureAvgC) ? -1.0f : sample.temperatureAvgC);
22+
ESP_LOGI(
23+
"CPU",
24+
"core0=%.1f%% core1=%.1f%% avg=%.1f%% smoothed=%.1f%% temp=%.1fC (avg %.1fC)",
25+
sample.perCore[0],
26+
sample.perCore[1],
27+
sample.average,
28+
std::isnan(sample.smoothedAverage) ? -1.0f : sample.smoothedAverage,
29+
std::isnan(sample.temperatureC) ? -1.0f : sample.temperatureC,
30+
std::isnan(sample.temperatureAvgC) ? -1.0f : sample.temperatureAvgC
31+
);
2732
#else
28-
ESP_LOGI("CPU", "core0=%.1f%% avg=%.1f%% smoothed=%.1f%% temp=%.1fC (avg %.1fC)",
29-
sample.perCore[0], sample.average,
30-
std::isnan(sample.smoothedAverage) ? -1.0f : sample.smoothedAverage,
31-
std::isnan(sample.temperatureC) ? -1.0f : sample.temperatureC,
32-
std::isnan(sample.temperatureAvgC) ? -1.0f : sample.temperatureAvgC);
33+
ESP_LOGI(
34+
"CPU",
35+
"core0=%.1f%% avg=%.1f%% smoothed=%.1f%% temp=%.1fC (avg %.1fC)",
36+
sample.perCore[0],
37+
sample.average,
38+
std::isnan(sample.smoothedAverage) ? -1.0f : sample.smoothedAverage,
39+
std::isnan(sample.temperatureC) ? -1.0f : sample.temperatureC,
40+
std::isnan(sample.temperatureAvgC) ? -1.0f : sample.temperatureAvgC
41+
);
3342
#endif
34-
});
43+
});
3544
}
3645

3746
void loop() {
38-
// Optional: pull the latest average every few seconds for a quick check
39-
static uint32_t lastPrint = 0;
40-
if (millis() - lastPrint > 3000) {
41-
lastPrint = millis();
42-
float avg = cpuMonitor.getLastAverage();
43-
float smoothed = cpuMonitor.getLastSmoothedAverage();
44-
if (avg >= 0.0f) {
45-
float temp = 0.0f;
46-
float tempAvg = 0.0f;
47-
if (cpuMonitor.getLastTemperature(temp, tempAvg)) {
48-
Serial.printf("[loop] CPU avg: %.1f%% smoothed: %.1f%% temp: %.1fC (avg %.1fC)\n",
49-
avg, smoothed, temp, tempAvg);
50-
} else {
51-
Serial.printf("[loop] CPU avg: %.1f%% smoothed: %.1f%% temp: n/a\n", avg, smoothed);
52-
}
53-
} else {
54-
Serial.println("[loop] calibrating...");
55-
}
56-
}
57-
delay(250);
47+
// Optional: pull the latest average every few seconds for a quick check
48+
static uint32_t lastPrint = 0;
49+
if (millis() - lastPrint > 3000) {
50+
lastPrint = millis();
51+
float avg = cpuMonitor.getLastAverage();
52+
float smoothed = cpuMonitor.getLastSmoothedAverage();
53+
if (avg >= 0.0f) {
54+
float temp = 0.0f;
55+
float tempAvg = 0.0f;
56+
if (cpuMonitor.getLastTemperature(temp, tempAvg)) {
57+
Serial.printf(
58+
"[loop] CPU avg: %.1f%% smoothed: %.1f%% temp: %.1fC (avg %.1fC)\n",
59+
avg,
60+
smoothed,
61+
temp,
62+
tempAvg
63+
);
64+
} else {
65+
Serial.printf("[loop] CPU avg: %.1f%% smoothed: %.1f%% temp: n/a\n", avg, smoothed);
66+
}
67+
} else {
68+
Serial.println("[loop] calibrating...");
69+
}
70+
}
71+
delay(250);
5872
}

examples/json_export/json_export.ino

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,44 @@ ESPCpuMonitor cpuMonitor;
1313
static const uint32_t HISTORY_LOG_PERIOD_MS = 5000;
1414

1515
void setup() {
16-
Serial.begin(115200);
17-
18-
CpuMonitorConfig cfg;
19-
cfg.sampleIntervalMs = 1000; // 1s cadence for telemetry
20-
cfg.historySize = 12; // keep a handful of points for charts/debug
21-
cfg.enableTemperature = true;
22-
cfg.smoothingMode = CpuSmoothingMode::Ewma;
23-
cfg.smoothingAlpha = 0.2f;
24-
cpuMonitor.init(cfg);
25-
26-
cpuMonitor.onSample([](const CpuUsageSample &sample) {
27-
StaticJsonDocument<256> doc;
28-
cpuMonitor.toJson(sample, doc);
29-
doc["avgRounded"] = static_cast<int>(roundf(sample.average)); // handy for dashboards
30-
31-
serializeJson(doc, Serial);
32-
Serial.println(); // newline-delimited JSON (NDJSON) stream
33-
});
16+
Serial.begin(115200);
17+
18+
CpuMonitorConfig cfg;
19+
cfg.sampleIntervalMs = 1000; // 1s cadence for telemetry
20+
cfg.historySize = 12; // keep a handful of points for charts/debug
21+
cfg.enableTemperature = true;
22+
cfg.smoothingMode = CpuSmoothingMode::Ewma;
23+
cfg.smoothingAlpha = 0.2f;
24+
cpuMonitor.init(cfg);
25+
26+
cpuMonitor.onSample([](const CpuUsageSample &sample) {
27+
StaticJsonDocument<256> doc;
28+
cpuMonitor.toJson(sample, doc);
29+
doc["avgRounded"] = static_cast<int>(roundf(sample.average)); // handy for dashboards
30+
31+
serializeJson(doc, Serial);
32+
Serial.println(); // newline-delimited JSON (NDJSON) stream
33+
});
3434
}
3535

3636
void loop() {
37-
static uint32_t lastHistoryMs = 0;
38-
39-
if (cpuMonitor.isReady() && millis() - lastHistoryMs >= HISTORY_LOG_PERIOD_MS) {
40-
lastHistoryMs = millis();
41-
const auto hist = cpuMonitor.history();
42-
if (!hist.empty()) {
43-
const auto &latest = hist.back();
44-
Serial.printf("[json_export] history_depth=%u last_avg=%.1f%% last_smoothed=%.1f%% last_temp=%.1fC\n",
45-
static_cast<unsigned>(hist.size()),
46-
latest.average,
47-
std::isnan(latest.smoothedAverage) ? -1.0f : latest.smoothedAverage,
48-
std::isnan(latest.temperatureC) ? -1.0f : latest.temperatureC);
49-
}
50-
}
51-
52-
delay(50);
37+
static uint32_t lastHistoryMs = 0;
38+
39+
if (cpuMonitor.isReady() && millis() - lastHistoryMs >= HISTORY_LOG_PERIOD_MS) {
40+
lastHistoryMs = millis();
41+
const auto hist = cpuMonitor.history();
42+
if (!hist.empty()) {
43+
const auto &latest = hist.back();
44+
Serial.printf(
45+
"[json_export] history_depth=%u last_avg=%.1f%% last_smoothed=%.1f%% "
46+
"last_temp=%.1fC\n",
47+
static_cast<unsigned>(hist.size()),
48+
latest.average,
49+
std::isnan(latest.smoothedAverage) ? -1.0f : latest.smoothedAverage,
50+
std::isnan(latest.temperatureC) ? -1.0f : latest.temperatureC
51+
);
52+
}
53+
}
54+
55+
delay(50);
5356
}

0 commit comments

Comments
 (0)