# `NanoKVMGo.Backlight`
[🔗](https://github.com/cocoa-xu/nano_kvm_go/blob/v0.4.0/lib/nano_kvm_go/backlight.ex#L1)

The LCD's backlight: LEDs that PWM1 channel 2 dims, through the kernel's
`pwm-backlight` driver. Brightness follows lightness: 50 looks half as
bright as 100.

    NanoKVMGo.Backlight.set_brightness(40)
    NanoKVMGo.Backlight.off()
    NanoKVMGo.Backlight.on()

The PWM runs at 25 kHz, or at a frequency of your own; brightness and
power stay as they are:

    NanoKVMGo.Backlight.set_frequency(100_000)
    NanoKVMGo.Backlight.info()
    #=> {:ok, %{on: true, brightness: 40, period: 10_000, frequency: 100_000.0,
    #=>         default_period: 40_000}}
    NanoKVMGo.Backlight.set_frequency(:default)

The frequency goes to the driver's `pwm_period_ns` attribute, which this
system's kernel adds. The timer counts 24 MHz, so a period is a whole
number of 41.7 ns steps, and fewer steps make coarser brightness. Below
about 100 Hz the light flickers. If the LEDs are switched by a small
bipolar transistor, as in the panel's reference design, each pulse lasts
about 0.3 µs longer than asked, which brightens low levels noticeably
from about 100 kHz.

The functions read and write the driver's attributes, with no process.

## Options

  * `:backlight` - the backlight's sysfs directory,
    `/sys/class/backlight/backlight` by default

# `info`

```elixir
@type info() :: %{
  on: boolean(),
  brightness: 0..100,
  period: pos_integer() | nil,
  frequency: float() | nil,
  default_period: pos_integer() | nil
}
```

# `option`

```elixir
@type option() :: {:backlight, Path.t()}
```

# `brightness`

```elixir
@spec brightness([option()]) :: {:ok, 0..100} | {:error, term()}
```

The brightness in percent, see `set_brightness/2`.

# `info`

```elixir
@spec info([option()]) :: {:ok, info()} | {:error, term()}
```

Whether the light is on, its brightness, and the PWM's period in ns and
frequency in Hz, now and by default; `nil` for what the kernel does not
tell.

# `off`

```elixir
@spec off([option()]) :: :ok | {:error, File.posix()}
```

Turns the light off, keeping its brightness for `on/1`.

# `on`

```elixir
@spec on([option()]) :: :ok | {:error, File.posix()}
```

Turns the light on, at the brightness it had.

# `set_brightness`

```elixir
@spec set_brightness(0..100, [option()]) :: :ok | {:error, term()}
```

Sets the brightness in percent: 0 is dark, 100 the brightest, and each
step looks as much brighter as the one before.

# `set_frequency`

```elixir
@spec set_frequency(number() | :default, [option()]) :: :ok | {:error, term()}
```

Runs the PWM at a frequency in Hz, up to 2 GHz, rounded to a period of
whole nanoseconds, or at the device tree's with `:default`. The kernel
refuses a period the timer cannot make with `{:error, :einval}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
