nimphea/hid/led

Source   Edit  

LED Control

Single LED control with software PWM and gamma correction.

Features:

  • Software PWM for brightness control
  • Automatic gamma correction (cubic curve)
  • Configurable update rate
  • Inversion support (for active-low LEDs)

Usage:

import nimphea/hid/led

var led: Led
led.init(D10, false, 1000.0)  # Pin D10, not inverted, 1kHz update rate

# Set brightness (0.0 to 1.0, gamma corrected automatically)
led.set(0.5)

# In main loop at 1kHz
while true:
  led.update()  # Must call at specified sample rate!
  hw.delayMs(1)

Types

Led {.importcpp: "daisy::Led", header: "hid/led.h".} = object
Single LED with software PWM control Source   Edit  

Procs

proc init(this: var Led; pin: Pin; invert: bool; samplerate: cfloat = 1000.0) {.
    importcpp: "#.Init(#, #, #)", ...raises: [], tags: [], forbids: [].}

Initialize LED on specified pin

Parameters:

  • pin - Hardware pin for LED
  • invert - True to invert brightness (for active-low LEDs)
  • samplerate - Rate at which update() will be called in Hz (default: 1000Hz)

Example:

led.init(D10, false, 1000.0)  # Update at 1kHz

Source   Edit  
proc set(this: var Led; val: cfloat) {.importcpp: "#.Set(#)", ...raises: [],
                                       tags: [], forbids: [].}

Set LED brightness

Parameters:

  • val - Brightness (0.0 = off, 1.0 = full brightness)

Note: Value is cubed for gamma correction, then quantized to 8-bit for PWM

Example:

led.set(0.0)   # Off
led.set(0.5)   # Half brightness (gamma corrected)
led.set(1.0)   # Full brightness

Source   Edit  
proc setSampleRate(this: var Led; sample_rate: cfloat) {.
    importcpp: "#.SetSampleRate(#)", ...raises: [], tags: [], forbids: [].}

Change update sample rate without reinitializing

Parameters:

  • sample_rate - New update rate in Hz
Source   Edit  
proc update(this: var Led) {.importcpp: "#.Update()", ...raises: [], tags: [],
                             forbids: [].}

Update LED PWM state

Must be called at the sample rate specified in init()!

Example:

# If samplerate was 1000Hz, call this every 1ms
while true:
  led.update()
  hw.delayMs(1)

Source   Edit