Files
ESP32-S3-LCD-1.3/.docs/QUICK_BRIGHTNESS_EXAMPLE.md
T
2026-09-21 10:28:35 +02:00

144 lines
2.7 KiB
Markdown

# Quick Brightness Control Example
## Try It Now!
### Option 1: Set a Specific Brightness Level
Add this to your `setup()` in `main.cpp`, right after `ui_init()`:
```cpp
void setup() {
// ... existing code ...
ui_init();
Serial.println("[UI] SquareLine Studio UI initialized");
// Set brightness to 25% (dim)
DisplayInit::setBrightness(64);
Serial.println("Brightness set to 25%");
// ... rest of setup ...
}
```
**Brightness Values:**
- `32` = Very dim (12.5%)
- `64` = Dim (25%)
- `128` = Normal (50%) - current default
- `192` = Bright (75%)
- `255` = Maximum (100%)
---
### Option 2: Automatic Dimming Example
Replace your current `loop()` with this to see automatic dimming:
```cpp
void loop() {
static uint32_t last_update = 0;
static uint32_t loop_count = 0;
static bool is_bright = true;
// Toggle brightness every 3 seconds
if (millis() - last_update > 3000) {
last_update = millis();
if (is_bright) {
DisplayInit::setBrightness(50); // Very dim
Serial.println("Dimmed to 20%");
} else {
DisplayInit::setBrightness(255); // Max brightness
Serial.println("Brightened to 100%");
}
is_bright = !is_bright;
loop_count = 0;
}
loop_count++;
delay(10);
}
```
Upload this and watch the display brightness toggle every 3 seconds!
---
### Option 3: Smooth Fade Example
```cpp
void loop() {
static uint32_t last_fade = 0;
static int brightness = 255;
static int direction = -5; // Start by fading out
// Update brightness every 50ms for smooth fade
if (millis() - last_fade > 50) {
last_fade = millis();
brightness += direction;
// Reverse direction at limits
if (brightness <= 50) {
brightness = 50;
direction = 5; // Start fading in
} else if (brightness >= 255) {
brightness = 255;
direction = -5; // Start fading out
}
DisplayInit::setBrightness(brightness);
}
delay(10);
}
```
This creates a smooth breathing effect!
---
## Quick Commands to Copy-Paste
### Set to 25% brightness:
```cpp
DisplayInit::setBrightness(64);
```
### Set to 50% brightness (default):
```cpp
DisplayInit::setBrightness(128);
```
### Set to max brightness:
```cpp
DisplayInit::setBrightness(255);
```
### Get current brightness:
```cpp
uint8_t current = DisplayInit::getBrightness();
Serial.printf("Current brightness: %d\n", current);
```
---
## Build and Upload
After making changes:
```bash
pio run --target upload
```
Or use the helper script:
```bash
./upload_and_monitor.sh
```
---
**See `BRIGHTNESS_CONTROL.md` for more detailed examples and use cases!**