| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- package main
- import (
- "fmt"
- "strconv"
- "strings"
- intdevice "netdome.biz/paul/qmk-rgb/internal/device"
- intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
- "netdome.biz/paul/qmk-rgb/internal/via"
- )
- type zoneProtocol interface {
- SetValue(via.LEDType, uint8, uint8) error
- SetColor(via.LEDType, uint8, uint8) error
- }
- type rgbProtocol interface {
- zoneProtocol
- GetValue(via.LEDType, uint8) ([]byte, error)
- Close() error
- }
- var openRGBProtocol = func() (rgbProtocol, error) {
- return OpenDevice()
- }
- func forEachSelectedZone(zones []intrgb.Zone, fn func(intrgb.Zone, via.LEDType) error) error {
- channels := zoneChannels(zones)
- for i, zone := range zones {
- if err := fn(zone, channels[i]); err != nil {
- return err
- }
- }
- return nil
- }
- func setValueOnZones(proto zoneProtocol, zones []intrgb.Zone, param, value uint8) error {
- return forEachSelectedZone(zones, func(_ intrgb.Zone, channel via.LEDType) error {
- return proto.SetValue(channel, param, value)
- })
- }
- func setBrightnessOnZones(proto zoneProtocol, zones []intrgb.Zone, value uint8) error {
- return setValueOnZones(proto, zones, uint8(intrgb.Brightness), value)
- }
- // zoneResult records what was requested for a zone and what the keyboard
- // reported afterwards. The two differ on real hardware: the Impact 80 clamps
- // brightness at 160 on the logo and side channels and scales it up to 255 on
- // the backlight channel.
- type zoneResult struct {
- Zone intrgb.Zone
- Requested uint8
- Applied uint8
- }
- // Mismatch reports whether the keyboard applied something other than the
- // requested value.
- func (r zoneResult) Mismatch() bool { return r.Applied != r.Requested }
- // anyMismatch reports whether any zone deviated from the request.
- func anyMismatch(results []zoneResult) bool {
- for _, r := range results {
- if r.Mismatch() {
- return true
- }
- }
- return false
- }
- // formatResults renders one line stating what each selected zone actually
- // holds, so a partial application is visible instead of being summarised as
- // the value that was asked for.
- func formatResults(label string, results []zoneResult) string {
- var b strings.Builder
- b.WriteString(label)
- for _, r := range results {
- fmt.Fprintf(&b, " %s %d", r.Zone, r.Applied)
- }
- if len(results) > 0 {
- fmt.Fprintf(&b, " (requested %d)", results[0].Requested)
- }
- return b.String()
- }
- // setValueVerified writes one value ID to every zone and reads each back, so a
- // clamped or rescaled value is reported instead of silently claimed as set.
- // The firmware transform differs per channel and per value ID, so the read-back
- // is not optional for any parameter the keyboard rescales.
- func setValueVerified(proto rgbProtocol, zones []intrgb.Zone, param, value uint8) ([]zoneResult, error) {
- if err := setValueOnZones(proto, zones, param, value); err != nil {
- return nil, err
- }
- results := make([]zoneResult, 0, len(zones))
- for _, zone := range zones {
- raw, err := proto.GetValue(via.LEDType(zone.Channel()), param)
- if err != nil {
- return nil, fmt.Errorf("read back value 0x%02x for %s: %w", param, zone, err)
- }
- if len(raw) == 0 {
- return nil, fmt.Errorf("read back value 0x%02x for %s: empty response", param, zone)
- }
- results = append(results, zoneResult{Zone: zone, Requested: value, Applied: raw[0]})
- }
- return results, nil
- }
- // setBrightnessVerified writes the brightness and reads every zone back.
- func setBrightnessVerified(proto rgbProtocol, zones []intrgb.Zone, value uint8) ([]zoneResult, error) {
- return setValueVerified(proto, zones, uint8(intrgb.Brightness), value)
- }
- // setSpeedVerified writes the speed and reads every zone back.
- func setSpeedVerified(proto rgbProtocol, zones []intrgb.Zone, value uint8) ([]zoneResult, error) {
- return setValueVerified(proto, zones, uint8(intrgb.Speed), value)
- }
- func setSpeedOnZones(proto zoneProtocol, zones []intrgb.Zone, value uint8) error {
- return setValueOnZones(proto, zones, uint8(intrgb.Speed), value)
- }
- func setColorOnZones(proto zoneProtocol, zones []intrgb.Zone, hue, saturation uint8) error {
- return forEachSelectedZone(zones, func(_ intrgb.Zone, channel via.LEDType) error {
- return proto.SetColor(channel, hue, saturation)
- })
- }
- func disableLightingOnZones(proto zoneProtocol, zones []intrgb.Zone) error {
- return forEachSelectedZone(zones, func(_ intrgb.Zone, channel via.LEDType) error {
- if err := proto.SetValue(channel, uint8(intrgb.EffectID), 0); err != nil {
- return err
- }
- return proto.SetValue(channel, uint8(intrgb.Brightness), 0)
- })
- }
- func enableLightingOnZones(proto zoneProtocol, zones []intrgb.Zone) error {
- return forEachSelectedZone(zones, func(zone intrgb.Zone, channel via.LEDType) error {
- if err := proto.SetValue(channel, uint8(intrgb.EffectID), intrgb.DefaultEffect(zone)); err != nil {
- return err
- }
- return proto.SetValue(channel, uint8(intrgb.Brightness), 160)
- })
- }
- func setModeOnZones(proto zoneProtocol, zones []intrgb.Zone, value uint8) error {
- return setValueOnZones(proto, zones, uint8(intrgb.EffectID), value)
- }
- // OpenDevice discovers the connected QMK keyboards and opens the one selected
- // by --device. Without --device it only proceeds when exactly one keyboard is
- // connected, so a command can never hit an unintended keyboard.
- func OpenDevice() (*via.Protocol, error) {
- if _, err := selectedZones(); err != nil {
- return nil, err
- }
- devices, err := intdevice.DiscoverAll()
- if err != nil {
- return nil, fmt.Errorf("discover: %w", err)
- }
- dev, err := selectDevice(devices, targetDevice)
- if err != nil {
- return nil, err
- }
- proto, err := via.New(dev)
- if err != nil {
- return nil, fmt.Errorf("open protocol: %w", err)
- }
- return proto, nil
- }
- func lightingEnabled(mode, brightness uint8) bool {
- return mode != 0 && brightness > 0
- }
- func ParseUint8(s string) (uint8, error) {
- v, err := strconv.ParseUint(s, 10, 8)
- if err != nil {
- return 0, fmt.Errorf("invalid value: %w", err)
- }
- return uint8(v), nil
- }
|