VLight Embedded SDK Guide
1. Introduction
The VLight Embedded SDK allows embedded developers to author robust, high-performance ESP32 firmware in pure V, transpiling it to freestanding C99 and compiling it into production binaries.
The SDK abstracts:
- Microcontroller initialization and FreeRTOS task life cycles.
- Hardware-precise WS2812B LED strip timing (RMT peripheral at 800kHz).
- Gamma 2.2 perceptual color correction and dynamic power limiting.
- Local multi-frame animation generators (Solid, Pulse, Rainbow, Breathing, Chase, Comet, Twinkle).
- Wi-Fi connection, auto-reconnect, and UDP server discovery.
- Secure token persistence in Non-Volatile Storage (NVS).
2. Hello World: WS2812B Strip
module main
import vlight
import vlight.led
fn on_start(mut ctx vlight.Context) {
ctx.log('Setting warm ambient glow...')
mut strip := led.WS2812B.new(
pin: 18 // Data GPIO
count: 60 // Number of LEDs
)
strip.fill(led.Color{ r: 255, g: 120, b: 30 })
strip.set_brightness(180)
strip.show()
ctx.log('LED strip active: 60 pixels.')
}
fn main() {
// 1. Initialize device instance
mut device := vlight.Device.new(
name: 'Living Room Shelf'
board: .esp32c3
)
// 2. Register lifecycle hook and run event loop
device.on_start(on_start)
device.run()
}
Tip
For a complete, step-by-step walkthrough covering scaffolding, configuration, building, and local hardware-free testing with esp-emulator, see the Firmware Development & Emulation Guide.
3. Working with Colors
RGB & HSV Conversions
import led
// Create RGB color
c1 := led.Color{ r: 255, g: 0, b: 128 }
// Create color from hex
c2 := led.from_hex('#00FF7F') or { led.Color{} }
// Convert HSV (Hue 0-360, Sat 0-1, Val 0-1) to RGB
c3 := led.hsv_to_rgb(180.0, 1.0, 0.8)
Gamma 2.2 Perceptual Correction
WS2812B LEDs have a non-linear brightness response to the human eye. The VLight SDK automatically applies an 8-bit lookup table curve:
corrected_byte := led.gamma8(raw_byte)
Dynamic Power Limiter
Addressable LEDs draw up to 60mA per pixel at full white. A 300-LED strip could draw 18A, exceeding typical 5V power supplies. The SDK calculates power estimation and scales pixel brightness automatically:
est_ma := strip.estimate_power_ma()
println('Current draw: ${est_ma} mA')
4. Local Animation Engine
Instead of saturating your Wi-Fi network by streaming 60 FPS frame packets, the VLight SDK calculates animation frames locally on the ESP32:
import effects
mut anim := effects.AnimationEngine.new(60)
// Apply rainbow wave across 60 pixels
anim.apply(mut strip, 'rainbow', speed: 12, step: frame_index)
strip.show()
Supported effects:
'solid': Static color.'pulse': Sine-wave brightness modulation.'rainbow': Flowing hue spectrum gradient.'breathing': Soft breathing ambient illumination.'chase': Traveling dot with trailing fade.'comet': High-speed meteor flash.'twinkle': Stochastic sparkle animation.
5. Building & Flashing
From your firmware project directory:
# Build firmware binary
vlight build --target esp32s3
# Flash over USB UART
vlight flash --port /dev/ttyUSB0 --target esp32s3
# View serial telemetry
vlight monitor --port /dev/ttyUSB0