# baihengrui/esp32c3_usb_meter architecture

> Architecture diagram of the GitHub repository baihengrui/esp32c3_usb_meter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/baihengrui/esp32c3_usb_meter
- Repository: https://github.com/baihengrui/esp32c3_usb_meter

## Overview

This firmware turns an ESP32-C3 USB meter into a live measurement instrument: it initializes the hardware, samples an INA226/INA228, updates threshold timing and graph data, and presents readings and system state on the LCD. Separate FreeRTOS tasks handle button interaction, host commands, UI rendering, and queued offline recording. The documented product branches include settings, waveforms, threshold-triggered sessions, LittleFS export, and USB CDC data transfer. Main task wiring and storage behavior are sampled; several HAL/UI details are supported by the README, not inspected source excerpts.

Main entry points: [`src/main.cpp`](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/main.cpp) (FreeRTOS tasks), [`src/hal/hal_storage.cpp`](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_storage.cpp) (Recording service), [`src/hal/hal_uart.cpp`](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_uart.cpp) (UART commands), [`src/hal/globals.cpp`](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/globals.cpp) (Shared meter state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime and HAL"]
  node_scheduler["FreeRTOS tasks<br/>[main.cpp]"]
  node_hal["HAL application<br/>[hal.cpp]"]
  node_globals["Shared meter state<br/>[globals.cpp]"]
  node_nvs[("Persistent settings<br/>[hal_nvs.cpp]")]
end

subgraph group_measurement["Measurement"]
  node_ina["INA acquisition<br/>[hal_ina.cpp]"]
  node_threshold["Threshold timing<br/>[hal_timer.cpp]"]
  node_graphdata["Graph sampling<br/>[hal.cpp]"]
end

subgraph group_interaction["Interaction and display"]
  node_buttonlogic["Button events<br/>[hal_button.cpp]"]
  node_uart["UART commands<br/>[hal_uart.cpp]"]
  node_menu["Settings menu<br/>[lcd_menu.cpp]"]
  node_ui["Meter UI<br/>[ui.cpp]"]
  node_lcdhal["LCD control<br/>[hal_lcd.cpp]"]
  node_protocol["USB protocol hook<br/>[hal_protocol.cpp]"]
end

subgraph group_records["Offline records"]
  node_storage["Recording service<br/>[hal_storage.cpp]"]
  node_recordqueue["Record queue<br/>[hal_storage.cpp]"]
  node_recordfiles[("Session files<br/>[hal_storage.cpp]")]
end

node_operator(("Meter operator"))
node_host(("Host computer"))
node_sensor{{"INA226/228 sensor"}}
node_buttons{{"Physical buttons"}}
node_lcd{{"TFT display"}}
node_littlefs[("LittleFS")]

node_operator -->|"presses"| node_buttons
node_buttons -->|"generates events"| node_buttonlogic
node_scheduler -->|"initializes system"| node_hal
node_scheduler -->|"runs acquisition task"| node_ina
node_scheduler -->|"updates thresholds"| node_threshold
node_scheduler -->|"runs storage task"| node_storage
node_scheduler -->|"polls buttons"| node_buttonlogic
node_scheduler -->|"polls commands"| node_uart
node_scheduler -->|"updates graph data"| node_graphdata
node_scheduler -->|"runs app rendering"| node_ui
node_ina -->|"reads measurements"| node_sensor
node_ina -->|"updates readings"| node_globals
node_threshold -->|"updates timing state"| node_globals
node_graphdata -->|"updates graph buffer"| node_globals
node_buttonlogic -->|"controls settings"| node_menu
node_buttonlogic -->|"changes app state"| node_globals
node_uart -->|"updates settings"| node_globals
node_uart -->|"persists settings"| node_nvs
node_uart -->|"sends CDC data"| node_host
node_host -->|"sends commands"| node_uart
node_uart -->|"requests record actions"| node_storage
node_storage -->|"receives samples"| node_recordqueue
node_ina -->|"samples for recording"| node_storage
node_storage -->|"writes sessions"| node_recordfiles
node_storage -->|"reads and writes"| node_littlefs
node_storage -->|"streams exports"| node_host
node_ui -->|"reads meter state"| node_globals
node_ui -->|"renders screens"| node_lcd
node_hal -->|"initializes display"| node_lcdhal
node_lcdhal -->|"controls display"| node_lcd
node_menu -->|"reads and writes"| node_nvs
node_threshold -->|"triggers recording"| node_storage
node_uart -.->|"delegates USB protocol"| node_protocol

click node_scheduler "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/main.cpp"
click node_hal "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal.cpp"
click node_globals "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/globals.cpp"
click node_ina "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_ina.cpp"
click node_threshold "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_timer.cpp"
click node_graphdata "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal.cpp"
click node_buttonlogic "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_button.cpp"
click node_uart "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_uart.cpp"
click node_menu "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/lcd_menu.cpp"
click node_ui "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/ui/ui.cpp"
click node_lcdhal "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_lcd.cpp"
click node_nvs "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_nvs.cpp"
click node_storage "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_storage.cpp"
click node_recordqueue "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_storage.cpp"
click node_recordfiles "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_storage.cpp"
click node_protocol "https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_protocol.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_scheduler,node_hal,node_globals,node_nvs toneBlue
class node_ina,node_threshold,node_graphdata,node_littlefs toneAmber
class node_buttonlogic,node_uart,node_menu,node_ui,node_lcdhal,node_protocol toneMint
class node_storage,node_recordqueue,node_recordfiles toneRose
class node_operator,node_host,node_sensor,node_buttons,node_lcd toneIndigo
```

## Components

### Runtime and HAL

- [FreeRTOS tasks](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/main.cpp): `src/main.cpp`
- [HAL application](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal.cpp): `src/hal/hal.cpp`
- [Shared meter state](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/globals.cpp): `src/hal/globals.cpp`
- [Persistent settings](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_nvs.cpp): `src/hal/hal_nvs.cpp`

### Measurement

- [INA acquisition](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_ina.cpp): `src/hal/hal_ina.cpp`
- [Threshold timing](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_timer.cpp): `src/hal/hal_timer.cpp`
- [Graph sampling](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal.cpp): `src/hal/hal.cpp`

### Interaction and display

- [Button events](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_button.cpp): `src/hal/hal_button.cpp`
- [UART commands](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_uart.cpp): `src/hal/hal_uart.cpp`
- [Settings menu](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/lcd_menu.cpp): `src/hal/lcd_menu.cpp`
- [Meter UI](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/ui/ui.cpp): `src/ui/ui.cpp`
- [LCD control](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_lcd.cpp): `src/hal/hal_lcd.cpp`
- [USB protocol hook](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_protocol.cpp): `src/hal/hal_protocol.cpp`

### Offline records

- [Recording service](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_storage.cpp): `src/hal/hal_storage.cpp`
- [Record queue](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_storage.cpp): `src/hal/hal_storage.cpp`
- [Session files](https://github.com/baihengrui/esp32c3_usb_meter/blob/main/src/hal/hal_storage.cpp): `src/hal/hal_storage.cpp`

### Other components

- **Meter operator**
- **Host computer**
- **INA226/228 sensor**
- **Physical buttons**
- **TFT display**
- **LittleFS**

## Connections

- Meter operator → Physical buttons: presses
- Physical buttons → Button events: generates events
- FreeRTOS tasks → HAL application: initializes system
- FreeRTOS tasks → INA acquisition: runs acquisition task
- FreeRTOS tasks → Threshold timing: updates thresholds
- FreeRTOS tasks → Recording service: runs storage task
- FreeRTOS tasks → Button events: polls buttons
- FreeRTOS tasks → UART commands: polls commands
- FreeRTOS tasks → Graph sampling: updates graph data
- FreeRTOS tasks → Meter UI: runs app rendering
- INA acquisition → INA226/228 sensor: reads measurements
- INA acquisition → Shared meter state: updates readings
- Threshold timing → Shared meter state: updates timing state
- Graph sampling → Shared meter state: updates graph buffer
- Button events → Settings menu: controls settings
- Button events → Shared meter state: changes app state
- UART commands → Shared meter state: updates settings
- UART commands → Persistent settings: persists settings
- UART commands → Host computer: sends CDC data
- Host computer → UART commands: sends commands
- UART commands → Recording service: requests record actions
- Recording service → Record queue: receives samples
- INA acquisition → Recording service: samples for recording
- Recording service → Session files: writes sessions
- Recording service → LittleFS: reads and writes
- Recording service → Host computer: streams exports
- Meter UI → Shared meter state: reads meter state
- Meter UI → TFT display: renders screens
- HAL application → LCD control: initializes display
- LCD control → TFT display: controls display
- Settings menu → Persistent settings: reads and writes
- Threshold timing → Recording service: triggers recording
- UART commands → USB protocol hook: delegates USB protocol
