Skip to content

Commit be03c75

Browse files
Feature/phase4 dynamic sampling (#4)
* Phase 2: add simulated sensor and sampling task * Phase 3: power manager, CLI dashboard, and CI workflow * Phase 4: Dynamic sensor sampling tied to power mode + updated CLI, docs, and task logic
1 parent 314fe13 commit be03c75

File tree

19 files changed

+825
-157
lines changed

19 files changed

+825
-157
lines changed

Core/Src/main.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,17 @@ int main(void)
9393
MX_USART2_UART_Init();
9494
/* USER CODE BEGIN 2 */
9595
Log_Init(&huart2);
96-
9796
CLI_Init(&huart2);
9897

99-
LOG_INFO("=== Smart Sensor Hub - Phase 1 startup ===\n");
98+
LOG_INFO("=== Smart Sensor Hub startup ===");
10099
App_MainInit();
101100
/* USER CODE END 2 */
102101

103102
/* Infinite loop */
104103
/* USER CODE BEGIN WHILE */
105104
while (1)
106105
{
107-
App_MainLoop();
106+
App_MainLoop();
108107
/* USER CODE END WHILE */
109108

110109
/* USER CODE BEGIN 3 */

Docs/ARCHITECTURE.md

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Contains high-level program control:
2828

2929
- `App_MainInit()`
3030
Initializes all modules:
31-
- Logging
31+
- Logging subsystem
3232
- CLI
33-
- Sensors
33+
- Sensors (via `SensorIF_t`)
3434
- Power Manager
3535
- Task Manager
3636

@@ -65,6 +65,19 @@ Registered Tasks:
6565
- `PowerManager` — manages power modes
6666
- `CLI` — processes UART command input
6767

68+
The **SensorSample** task period is not hard-coded; it is derived from the
69+
current power mode, using:
70+
71+
```c
72+
#define SENSOR_PERIOD_ACTIVE_MS (1000U)
73+
#define SENSOR_PERIOD_IDLE_MS (5000U)
74+
#define SENSOR_PERIOD_SLEEP_MS (30000U)
75+
#define SENSOR_PERIOD_STOP_MS (0U) // 0 => no sampling in STOP
76+
```
77+
78+
When `POWER_MODE_STOP` is active, the period is 0 and the sampling
79+
task is effectively disabled (no sensor reads).
80+
6881
Each run produces `DEBUG` logs showing scheduling behavior.
6982
7083
---
@@ -80,7 +93,7 @@ Features:
8093
- Standardized format:
8194
8295
```
83-
[00123456 ms][INF][../app/app_main.c:152][App_Task] Message...
96+
[00123456 ms][INF][../app/app_main.c:152][App_TaskSensorSample] Message...
8497
```
8598

8699
- Cursor management so logs never corrupt CLI input
@@ -100,9 +113,13 @@ Features:
100113
- `status`
101114
- `help`
102115

116+
The `status` command reports the **effective sensor sampling period**
117+
that is in use based on the current power mode.
118+
103119
### Thread safety glue (`newlib_lock_glue.c`)
104120

105-
Makes newlib thread-safe for interrupts/RTOS usage in future.
121+
Provides newlib lock hooks to make standard library functions safe for
122+
future RTOS/interrupt use.
106123

107124
---
108125

@@ -120,10 +137,15 @@ typedef struct {
120137
```
121138

122139
Provides:
123-
- Simulated temperature sensor (`sensor_if.c`)
124-
- Clean pathway to drop in real I2C/SPI sensors later.
140+
- `sensor_if.h` with the generic `SensorIF_t` API
141+
- Simulated temperature sensor backend:
142+
- `sensor_sim_temp.c/.h`
143+
- Implements `Sensor_GetInterface()` returning a `SensorIF_t`
125144

126-
---
145+
This design makes it trivial to drop in real I²C/SPI/ADC sensors later
146+
without changing application code.
147+
148+
Manages abstract power modes:
127149

128150
## 5. Power Management Layer (`power/`)
129151

@@ -140,29 +162,34 @@ typedef enum {
140162

141163
Tracks:
142164
- Current power mode
143-
- Requested mode
165+
- Requested power mode
144166
- Idle cycle counts
145167
- Mode transitions (logged via logging subsystem)
146168

147-
Future phases will integrate real STM32 low-power entry logic.
169+
The power mode indirectly drives the **SensorSample** task behavior by
170+
selecting one of the configured sampling periods. In STOP mode, sampling
171+
is fully disabled (period 0).
172+
173+
Future phases will integrate real STM32 low-power entry logic (sleep/stop)
174+
and wakeup sources.
148175

149176
---
150177

151-
## 6. CLI Dashboard interaction
178+
## 6. CLI Dashboard Interaction
152179

153-
The logger and CLI share the same UART.
180+
The logger and CLI share the same UART.
154181
To maintain readability:
155182

156-
- Logs begin with `
157-
` to reset cursor to column 0
183+
- Logs begin with `\r` to reset cursor to column 0
158184
- After printing logs, `Log_Print()` calls `CLI_OnExternalOutput()`
159185
- CLI reprints:
160186

161-
```
187+
```text
162188
> <current_input>
163189
```
164190

165-
This creates a pseudo-dashboard effect.
191+
This creates a pseudo-dashboard effect where logs scroll *above* and the
192+
command line remains anchored at the bottom.
166193

167194
---
168195

@@ -179,4 +206,4 @@ Performs:
179206
- Compile-only firmware build
180207
- Ensures clean code on every push/PR
181208

182-
Enforces professional software development discipline.
209+
Enforces professional software development discipline for the project.

Docs/CLI_COMMANDS.md

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# CLI Commands
2+
3+
The Smart Sensor Hub exposes a UART-based command-line interface (CLI) over the ST-LINK VCP (USART2).
4+
Default settings: **115200 baud, 8 data bits, no parity, 1 stop bit (115200 8N1)**.
5+
6+
On reset you should see:
7+
8+
```text
9+
Smart Sensor Hub CLI ready.
10+
Type 'help' for a list of commands.
11+
>
12+
```
13+
14+
You can then type commands followed by Enter.
15+
16+
---
17+
18+
## General Behavior
19+
20+
- Input is **line-based**: the command is processed when you press Enter.
21+
- Backspace is supported.
22+
- Unknown commands generate a clear error message.
23+
- The CLI prompt is always kept at the bottom like a dashboard:
24+
- Log messages scroll above.
25+
- Prompt is redrawn after each log line.
26+
27+
---
28+
29+
## Command Reference
30+
31+
### `help`
32+
33+
**Usage:**
34+
35+
```text
36+
> help
37+
```
38+
39+
**Description:**
40+
Lists all available commands with a short description.
41+
42+
---
43+
44+
### `log off`
45+
46+
Disables all **task logging** (logs from `LOG_*` macros).
47+
CLI output remains active.
48+
49+
```text
50+
> log off
51+
Task logging disabled.
52+
```
53+
54+
---
55+
56+
### `log error`
57+
58+
Enables task logging with **ERROR** level only.
59+
60+
```text
61+
> log error
62+
Task logging enabled, level=ERROR.
63+
```
64+
65+
---
66+
67+
### `log warn`
68+
69+
Enables task logging with **WARN and ERROR** messages.
70+
71+
```text
72+
> log warn
73+
Task logging enabled, level=WARN.
74+
```
75+
76+
---
77+
78+
### `log info`
79+
80+
Enables task logging with **INFO, WARN, and ERROR** messages.
81+
82+
```text
83+
> log info
84+
Task logging enabled, level=INFO.
85+
```
86+
87+
---
88+
89+
### `log debug`
90+
91+
Enables task logging with **DEBUG, INFO, WARN, and ERROR** messages.
92+
93+
```text
94+
> log debug
95+
Task logging enabled, level=DEBUG.
96+
```
97+
98+
This is the most verbose setting and is very useful while developing.
99+
100+
---
101+
102+
### `log pause`
103+
104+
Temporarily **pauses task logging** while remembering the previous log level and enabled state.
105+
106+
```text
107+
> log pause
108+
Task logging paused. Use 'log resume' to restore.
109+
```
110+
111+
Use this when you want to type or read CLI output without logs scrolling.
112+
113+
---
114+
115+
### `log resume`
116+
117+
Restores the logging configuration saved by `log pause`.
118+
119+
```text
120+
> log resume
121+
Task logging resumed.
122+
```
123+
124+
If logging was not paused, you see:
125+
126+
```text
127+
Task logging is not paused.
128+
```
129+
130+
---
131+
132+
### `pmode <mode>`
133+
134+
Requests a change in the system **power mode**.
135+
136+
Supported modes:
137+
138+
- `active`
139+
- `idle`
140+
- `sleep`
141+
- `stop`
142+
143+
**Example:**
144+
145+
```text
146+
> pmode idle
147+
Requested power mode change: idle
148+
```
149+
150+
Currently (v0.4.0) the Power Manager:
151+
152+
- Logs mode transitions.
153+
- Tracks idle cycles.
154+
- Drives the **sensor sampling period** used by the `SensorSample` task:
155+
- ACTIVE → 1000 ms
156+
- IDLE → 5000 ms
157+
- SLEEP → 30000 ms
158+
- STOP → 0 ms (no sampling)
159+
160+
Future phases may map these modes to real STM32 low-power states.
161+
162+
---
163+
164+
### `status`
165+
166+
Displays the current logging configuration, power mode, and effective sensor sample period.
167+
168+
```text
169+
> status
170+
171+
Status:
172+
Task logging: ENABLED
173+
LogLevel: 1 (0=DEBUG,1=INFO,2=WARN,3=ERROR)
174+
PowerMode: 2 (0=ACTIVE,1=IDLE,2=SLEEP,3=STOP)
175+
Sensor sample period: 30000 ms
176+
```
177+
178+
Where:
179+
180+
- **Task logging** → whether task logs are enabled
181+
- **LogLevel** → current minimum log level
182+
- **PowerMode** → current abstract mode
183+
- **Sensor sample period** → effective period based on power mode
184+
(0 in STOP mode, meaning sampling is disabled)
185+
186+
---
187+
188+
## Example Session
189+
190+
```text
191+
Smart Sensor Hub CLI ready.
192+
Type 'help' for a list of commands.
193+
194+
> help
195+
Available commands:
196+
help - Show this help text
197+
log off - Disable all task logging
198+
log error - Task logs at ERROR level only
199+
log warn - Task logs at WARN and above
200+
log info - Task logs at INFO and above
201+
log debug - Task logs at DEBUG and above
202+
log pause - Temporarily pause all task logs
203+
log resume - Resume task logs to previous state
204+
pmode active - Request POWER_MODE_ACTIVE
205+
pmode idle - Request POWER_MODE_IDLE
206+
pmode sleep - Request POWER_MODE_SLEEP
207+
pmode stop - Request POWER_MODE_STOP
208+
status - Show logging and power status
209+
210+
> log debug
211+
Task logging enabled, level=DEBUG.
212+
213+
[00001234 ms][DBG][../app/app_task_manager.c:77][AppTaskManager_RunOnce] Running task 'Heartbeat' (elapsed: 500 ms)
214+
215+
> pmode sleep
216+
Requested power mode change: sleep
217+
218+
> status
219+
Status:
220+
Task logging: ENABLED
221+
LogLevel: 0 (0=DEBUG,1=INFO,2=WARN,3=ERROR)
222+
PowerMode: 2 (0=ACTIVE,1=IDLE,2=SLEEP,3=STOP)
223+
Sensor sample period: 30000 ms
224+
225+
> pmode stop
226+
Requested power mode change: stop
227+
228+
> status
229+
Status:
230+
Task logging: ENABLED
231+
LogLevel: 0 (0=DEBUG,1=INFO,2=WARN,3=ERROR)
232+
PowerMode: 3 (0=ACTIVE,1=IDLE,2=SLEEP,3=STOP)
233+
Sensor sample period: 0 ms (sampling disabled in STOP)
234+
```
235+
236+
This file serves as a quick reference for anyone interacting with the firmware via UART.

0 commit comments

Comments
 (0)