rgb.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. intdevice "netdome.biz/paul/qmk-rgb/internal/device"
  7. intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
  8. "netdome.biz/paul/qmk-rgb/internal/via"
  9. )
  10. type zoneProtocol interface {
  11. SetValue(via.LEDType, uint8, uint8) error
  12. SetColor(via.LEDType, uint8, uint8) error
  13. }
  14. type rgbProtocol interface {
  15. zoneProtocol
  16. GetValue(via.LEDType, uint8) ([]byte, error)
  17. Close() error
  18. }
  19. var openRGBProtocol = func() (rgbProtocol, error) {
  20. return OpenDevice()
  21. }
  22. func forEachSelectedZone(zones []intrgb.Zone, fn func(intrgb.Zone, via.LEDType) error) error {
  23. channels := zoneChannels(zones)
  24. for i, zone := range zones {
  25. if err := fn(zone, channels[i]); err != nil {
  26. return err
  27. }
  28. }
  29. return nil
  30. }
  31. func setValueOnZones(proto zoneProtocol, zones []intrgb.Zone, param, value uint8) error {
  32. return forEachSelectedZone(zones, func(_ intrgb.Zone, channel via.LEDType) error {
  33. return proto.SetValue(channel, param, value)
  34. })
  35. }
  36. func setBrightnessOnZones(proto zoneProtocol, zones []intrgb.Zone, value uint8) error {
  37. return setValueOnZones(proto, zones, uint8(intrgb.Brightness), value)
  38. }
  39. // zoneResult records what was requested for a zone and what the keyboard
  40. // reported afterwards. The two differ on real hardware: the Impact 80 clamps
  41. // brightness at 160 on the logo and side channels and scales it up to 255 on
  42. // the backlight channel.
  43. type zoneResult struct {
  44. Zone intrgb.Zone
  45. Requested uint8
  46. Applied uint8
  47. }
  48. // Mismatch reports whether the keyboard applied something other than the
  49. // requested value.
  50. func (r zoneResult) Mismatch() bool { return r.Applied != r.Requested }
  51. // anyMismatch reports whether any zone deviated from the request.
  52. func anyMismatch(results []zoneResult) bool {
  53. for _, r := range results {
  54. if r.Mismatch() {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. // formatResults renders one line stating what each selected zone actually
  61. // holds, so a partial application is visible instead of being summarised as
  62. // the value that was asked for.
  63. func formatResults(label string, results []zoneResult) string {
  64. var b strings.Builder
  65. b.WriteString(label)
  66. for _, r := range results {
  67. fmt.Fprintf(&b, " %s %d", r.Zone, r.Applied)
  68. }
  69. if len(results) > 0 {
  70. fmt.Fprintf(&b, " (requested %d)", results[0].Requested)
  71. }
  72. return b.String()
  73. }
  74. // setValueVerified writes one value ID to every zone and reads each back, so a
  75. // clamped or rescaled value is reported instead of silently claimed as set.
  76. // The firmware transform differs per channel and per value ID, so the read-back
  77. // is not optional for any parameter the keyboard rescales.
  78. func setValueVerified(proto rgbProtocol, zones []intrgb.Zone, param, value uint8) ([]zoneResult, error) {
  79. if err := setValueOnZones(proto, zones, param, value); err != nil {
  80. return nil, err
  81. }
  82. results := make([]zoneResult, 0, len(zones))
  83. for _, zone := range zones {
  84. raw, err := proto.GetValue(via.LEDType(zone.Channel()), param)
  85. if err != nil {
  86. return nil, fmt.Errorf("read back value 0x%02x for %s: %w", param, zone, err)
  87. }
  88. if len(raw) == 0 {
  89. return nil, fmt.Errorf("read back value 0x%02x for %s: empty response", param, zone)
  90. }
  91. results = append(results, zoneResult{Zone: zone, Requested: value, Applied: raw[0]})
  92. }
  93. return results, nil
  94. }
  95. // setBrightnessVerified writes the brightness and reads every zone back.
  96. func setBrightnessVerified(proto rgbProtocol, zones []intrgb.Zone, value uint8) ([]zoneResult, error) {
  97. return setValueVerified(proto, zones, uint8(intrgb.Brightness), value)
  98. }
  99. // setSpeedVerified writes the speed and reads every zone back.
  100. func setSpeedVerified(proto rgbProtocol, zones []intrgb.Zone, value uint8) ([]zoneResult, error) {
  101. return setValueVerified(proto, zones, uint8(intrgb.Speed), value)
  102. }
  103. func setSpeedOnZones(proto zoneProtocol, zones []intrgb.Zone, value uint8) error {
  104. return setValueOnZones(proto, zones, uint8(intrgb.Speed), value)
  105. }
  106. func setColorOnZones(proto zoneProtocol, zones []intrgb.Zone, hue, saturation uint8) error {
  107. return forEachSelectedZone(zones, func(_ intrgb.Zone, channel via.LEDType) error {
  108. return proto.SetColor(channel, hue, saturation)
  109. })
  110. }
  111. func disableLightingOnZones(proto zoneProtocol, zones []intrgb.Zone) error {
  112. return forEachSelectedZone(zones, func(_ intrgb.Zone, channel via.LEDType) error {
  113. if err := proto.SetValue(channel, uint8(intrgb.EffectID), 0); err != nil {
  114. return err
  115. }
  116. return proto.SetValue(channel, uint8(intrgb.Brightness), 0)
  117. })
  118. }
  119. func enableLightingOnZones(proto zoneProtocol, zones []intrgb.Zone) error {
  120. return forEachSelectedZone(zones, func(zone intrgb.Zone, channel via.LEDType) error {
  121. if err := proto.SetValue(channel, uint8(intrgb.EffectID), intrgb.DefaultEffect(zone)); err != nil {
  122. return err
  123. }
  124. return proto.SetValue(channel, uint8(intrgb.Brightness), 160)
  125. })
  126. }
  127. func setModeOnZones(proto zoneProtocol, zones []intrgb.Zone, value uint8) error {
  128. return setValueOnZones(proto, zones, uint8(intrgb.EffectID), value)
  129. }
  130. // OpenDevice discovers the connected QMK keyboards and opens the one selected
  131. // by --device. Without --device it only proceeds when exactly one keyboard is
  132. // connected, so a command can never hit an unintended keyboard.
  133. func OpenDevice() (*via.Protocol, error) {
  134. if _, err := selectedZones(); err != nil {
  135. return nil, err
  136. }
  137. devices, err := intdevice.DiscoverAll()
  138. if err != nil {
  139. return nil, fmt.Errorf("discover: %w", err)
  140. }
  141. dev, err := selectDevice(devices, targetDevice)
  142. if err != nil {
  143. return nil, err
  144. }
  145. proto, err := via.New(dev)
  146. if err != nil {
  147. return nil, fmt.Errorf("open protocol: %w", err)
  148. }
  149. return proto, nil
  150. }
  151. func lightingEnabled(mode, brightness uint8) bool {
  152. return mode != 0 && brightness > 0
  153. }
  154. func ParseUint8(s string) (uint8, error) {
  155. v, err := strconv.ParseUint(s, 10, 8)
  156. if err != nil {
  157. return 0, fmt.Errorf("invalid value: %w", err)
  158. }
  159. return uint8(v), nil
  160. }