319 lines
8.1 KiB
Markdown
319 lines
8.1 KiB
Markdown
# Project File Structure and Purpose
|
|
|
|
## Overview
|
|
|
|
This document explains what each file does and why it's in its location.
|
|
|
|
---
|
|
|
|
## Source Files (`src/`)
|
|
|
|
### `src/main.cpp` ⭐ Main Application Entry Point
|
|
|
|
**Purpose**: Application coordinator and main entry point
|
|
|
|
**Responsibilities**:
|
|
- Initialize serial communication
|
|
- Call hardware initialization (`DisplayInit::init()`)
|
|
- Load SquareLine Studio UI (`ui_init()`)
|
|
- Create FreeRTOS tasks
|
|
- Main loop (can be used for data processing, UI updates, etc.)
|
|
|
|
**What it does NOT do**:
|
|
- ❌ Direct hardware control (that's in modules)
|
|
- ❌ Drawing graphics (that's in `include/ui/`)
|
|
- ❌ LVGL configuration (that's in `lv_conf.h`)
|
|
|
|
**Typical flow**:
|
|
```cpp
|
|
setup() {
|
|
Serial.begin() // Start serial
|
|
DisplayInit::init() // Initialize display hardware
|
|
ui_init() // Load SquareLine UI
|
|
xTaskCreate() // Start LVGL task
|
|
}
|
|
|
|
loop() {
|
|
// Your application logic here
|
|
// Update UI with data
|
|
// Handle events
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
### `src/display_init.cpp` 🖥️ Display Hardware Driver
|
|
|
|
**Purpose**: Hardware-specific display initialization and control
|
|
|
|
**Responsibilities**:
|
|
- Initialize LovyanGFX display driver
|
|
- Configure ST7789 display controller
|
|
- Configure SPI bus (pins, speed)
|
|
- Initialize LVGL graphics library
|
|
- Provide display buffer to LVGL
|
|
- Handle LVGL flush callbacks
|
|
- Provide brightness control functions
|
|
|
|
**What it does NOT do**:
|
|
- ❌ Draw UI elements (that's LVGL's job via `include/ui/`)
|
|
- ❌ Show test patterns (removed - UI only from SquareLine)
|
|
- ❌ Handle application logic (that's in `main.cpp`)
|
|
|
|
**Key functions**:
|
|
```cpp
|
|
bool init() // Initialize display hardware + LVGL
|
|
void update() // Update LVGL tick and handle tasks
|
|
void setBrightness(uint8_t) // Control backlight brightness
|
|
uint8_t getBrightness() // Get current brightness
|
|
```
|
|
|
|
**This file is in `src/` because**:
|
|
- It contains implementation code (`.cpp`)
|
|
- It's a hardware abstraction layer
|
|
- It needs to access hardware-specific display instance
|
|
|
|
---
|
|
|
|
### `src/main_minimal.cpp.bak` 🔧 Backup Test File
|
|
|
|
**Purpose**: Minimal test code for hardware debugging
|
|
|
|
**Status**: Backup file (not compiled)
|
|
|
|
**Use case**: If you need to test basic hardware without UI/LVGL
|
|
|
|
To use it:
|
|
```bash
|
|
mv src/main.cpp src/main_full.cpp
|
|
mv src/main_minimal.cpp.bak src/main.cpp
|
|
pio run --target upload
|
|
```
|
|
|
|
---
|
|
|
|
## Header Files (`include/`)
|
|
|
|
### `include/modules/display_init.h` 📋 Display Module Interface
|
|
|
|
**Purpose**: Public interface for display initialization module
|
|
|
|
**Contains**:
|
|
- Function declarations for `DisplayInit` namespace
|
|
- Forward declarations for LVGL types
|
|
- DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_RADIUS defines
|
|
|
|
**This file is in `include/` because**:
|
|
- Header files belong in `include/`
|
|
- Can be included by other modules
|
|
- Provides public API
|
|
|
|
**Used by**: `main.cpp` and any module that needs display control
|
|
|
|
---
|
|
|
|
### `include/protocol/automotive_data.h` 📡 UART Protocol Definition
|
|
|
|
**Purpose**: Define UART packet structure for Controller A ↔ Controller B communication
|
|
|
|
**Contains**:
|
|
- `automotive_data_t` structure
|
|
- `uart_packet_t` structure
|
|
- Checksum functions
|
|
- Packet validation functions
|
|
|
|
**Status**: Ready for Phase 2 (UART reception)
|
|
|
|
**This file is in `include/protocol/` because**:
|
|
- It's a shared protocol definition
|
|
- Will be used by both Controller A and B
|
|
- Separates communication protocol from implementation
|
|
|
|
---
|
|
|
|
### `include/config/lv_conf.h` ⚙️ LVGL Configuration
|
|
|
|
**Purpose**: Configure LVGL library settings
|
|
|
|
**Contains**:
|
|
- Color depth (RGB565)
|
|
- Memory allocation (64KB heap)
|
|
- Enabled widgets (Arc, Label, Meter, etc.)
|
|
- Font settings
|
|
- Performance options
|
|
|
|
**This file is in `include/config/` because**:
|
|
- LVGL requires `lv_conf.h` in include path
|
|
- Separates configuration from code
|
|
- Easy to modify LVGL settings
|
|
|
|
**Configured via**: `platformio.ini` with `-I include/config`
|
|
|
|
---
|
|
|
|
## SquareLine Studio UI (`include/ui/`)
|
|
|
|
### Why is the UI in `include/ui/` and not `src/`?
|
|
|
|
**Reason**: SquareLine Studio exports contain both `.h` and `.c` files
|
|
|
|
**Structure**:
|
|
```
|
|
include/ui/
|
|
├── ui.h # Main UI header (declarations)
|
|
├── ui.c # Main UI implementation
|
|
├── ui_helpers.h/.c # Helper functions
|
|
├── ui_events.h # Event handler declarations
|
|
├── screens/
|
|
│ ├── ui_Home_Screen.h # Screen headers
|
|
│ └── ui_Home_Screen.c # Screen implementations
|
|
├── components/ # Custom components
|
|
└── fonts/ # Custom fonts
|
|
```
|
|
|
|
**Why this works**:
|
|
- PlatformIO's `build_src_filter` includes `+<../include/ui/*>`
|
|
- This tells the compiler to build `.c` files from `include/ui/`
|
|
- Headers are naturally in the include path
|
|
|
|
**Configured in**: `platformio.ini` line 50:
|
|
```ini
|
|
build_src_filter = +<*> +<modules/*> +<../include/ui/*> +<../include/display/*>
|
|
```
|
|
|
|
---
|
|
|
|
## Configuration Files (Root)
|
|
|
|
### `platformio.ini` ⚙️ Build Configuration
|
|
|
|
**Purpose**: Configure PlatformIO build system
|
|
|
|
**Contains**:
|
|
- Board selection
|
|
- Platform version
|
|
- Build flags (includes, defines)
|
|
- Library dependencies
|
|
- Upload settings
|
|
- Source file filters
|
|
|
|
---
|
|
|
|
### `lv_conf.h` (Removed - Moved to `include/config/`)
|
|
|
|
Previously at project root, now in `include/config/lv_conf.h`
|
|
|
|
---
|
|
|
|
## Documentation Files (Root)
|
|
|
|
All `.md` files in project root:
|
|
- `README.md` - Project overview
|
|
- `PROJECT_STRUCTURE.md` - Detailed structure
|
|
- `PIN_CONFIG.md` - Pin configuration
|
|
- `PINS_VERIFIED.md` - Verified pins from demo
|
|
- `TROUBLESHOOTING.md` - Debug guide
|
|
- `SQUARELINE_UI_GUIDE.md` - UI integration guide
|
|
- `BRIGHTNESS_CONTROL.md` - Brightness control guide
|
|
- `FILE_STRUCTURE.md` - This file
|
|
|
|
---
|
|
|
|
## Module Organization Pattern
|
|
|
|
### Current Modules
|
|
|
|
```
|
|
include/modules/ # Module headers (public interfaces)
|
|
└── display_init.h
|
|
|
|
src/ # Module implementations
|
|
└── display_init.cpp
|
|
```
|
|
|
|
### Future Modules (Phase 2+)
|
|
|
|
```
|
|
include/modules/
|
|
├── display_init.h
|
|
├── uart_receiver.h # UART data reception
|
|
├── data_validator.h # Packet validation
|
|
└── settings_manager.h # NVS settings
|
|
|
|
src/
|
|
├── display_init.cpp
|
|
├── uart_receiver.cpp
|
|
├── data_validator.cpp
|
|
└── settings_manager.cpp
|
|
```
|
|
|
|
---
|
|
|
|
## Build Process Flow
|
|
|
|
1. **PlatformIO reads** `platformio.ini`
|
|
2. **Compiler includes**:
|
|
- `include/`
|
|
- `include/config/`
|
|
- `include/modules/`
|
|
- `include/protocol/`
|
|
- `include/ui/`
|
|
- `src/`
|
|
|
|
3. **Compiler builds**:
|
|
- `src/*.cpp` (main, display_init)
|
|
- `include/ui/*.c` (SquareLine UI files)
|
|
|
|
4. **Linker combines** all object files
|
|
5. **Upload** firmware to ESP32-S3
|
|
|
|
---
|
|
|
|
## Key Principles
|
|
|
|
### ✅ Good Practices
|
|
|
|
1. **Separation of concerns**:
|
|
- Hardware drivers in `src/display_init.cpp`
|
|
- UI design in `include/ui/` (from SquareLine)
|
|
- App logic in `src/main.cpp`
|
|
|
|
2. **Headers in `include/`, implementations in `src/`**:
|
|
- Exception: SquareLine UI has both in `include/ui/`
|
|
|
|
3. **No graphics in hardware drivers**:
|
|
- `display_init.cpp` only initializes hardware
|
|
- UI comes from SquareLine Studio
|
|
|
|
4. **Configuration in `include/config/`**:
|
|
- Separates config from code
|
|
- Easy to modify
|
|
|
|
### ❌ What to Avoid
|
|
|
|
1. ❌ Drawing UI in `display_init.cpp`
|
|
2. ❌ Hardware access in `main.cpp` (use modules)
|
|
3. ❌ Mixing UI design with hardware code
|
|
4. ❌ Hardcoding settings (use config files)
|
|
|
|
---
|
|
|
|
## Summary Table
|
|
|
|
| File/Folder | Purpose | Type | Built? |
|
|
|-------------|---------|------|--------|
|
|
| `src/main.cpp` | Application entry point | C++ | ✅ |
|
|
| `src/display_init.cpp` | Display hardware driver | C++ | ✅ |
|
|
| `include/modules/display_init.h` | Display module interface | Header | No |
|
|
| `include/config/lv_conf.h` | LVGL configuration | Header | No |
|
|
| `include/protocol/automotive_data.h` | UART protocol | Header | No |
|
|
| `include/ui/*.c` | SquareLine UI implementation | C | ✅ |
|
|
| `include/ui/*.h` | SquareLine UI headers | Header | No |
|
|
| `platformio.ini` | Build configuration | Config | No |
|
|
| `*.md` | Documentation | Markdown | No |
|
|
|
|
---
|
|
|
|
**Last Updated**: 2025-11-08
|
|
**Status**: Clean structure, no test patterns, UI-only from SquareLine
|