rgb.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. intdevice "netdome.biz/paul/impact-80/internal/device"
  7. intrgb "netdome.biz/paul/impact-80/internal/rgb"
  8. "netdome.biz/paul/impact-80/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. // setBrightnessVerified writes the brightness and reads every zone back, so a
  75. // clamped or rescaled value is reported instead of silently claimed as set.
  76. func setBrightnessVerified(proto rgbProtocol, zones []intrgb.Zone, value uint8) ([]zoneResult, error) {
  77. if err := setBrightnessOnZones(proto, zones, value); err != nil {
  78. return nil, err
  79. }
  80. results := make([]zoneResult, 0, len(zones))
  81. for _, zone := range zones {
  82. raw, err := proto.GetValue(via.LEDType(zone.Channel()), uint8(intrgb.Brightness))
  83. if err != nil {
  84. return nil, fmt.Errorf("read back brightness for %s: %w", zone, err)
  85. }
  86. if len(raw) == 0 {
  87. return nil, fmt.Errorf("read back brightness for %s: empty response", zone)
  88. }
  89. results = append(results, zoneResult{Zone: zone, Requested: value, Applied: raw[0]})
  90. }
  91. return results, nil
  92. }
  93. func setSpeedOnZones(proto zoneProtocol, zones []intrgb.Zone, value uint8) error {
  94. return setValueOnZones(proto, zones, uint8(intrgb.Speed), value)
  95. }
  96. func setColorOnZones(proto zoneProtocol, zones []intrgb.Zone, hue, saturation uint8) error {
  97. return forEachSelectedZone(zones, func(_ intrgb.Zone, channel via.LEDType) error {
  98. return proto.SetColor(channel, hue, saturation)
  99. })
  100. }
  101. func disableLightingOnZones(proto zoneProtocol, zones []intrgb.Zone) error {
  102. return forEachSelectedZone(zones, func(_ intrgb.Zone, channel via.LEDType) error {
  103. if err := proto.SetValue(channel, uint8(intrgb.EffectID), 0); err != nil {
  104. return err
  105. }
  106. return proto.SetValue(channel, uint8(intrgb.Brightness), 0)
  107. })
  108. }
  109. func enableLightingOnZones(proto zoneProtocol, zones []intrgb.Zone) error {
  110. return forEachSelectedZone(zones, func(zone intrgb.Zone, channel via.LEDType) error {
  111. if err := proto.SetValue(channel, uint8(intrgb.EffectID), intrgb.DefaultEffect(zone)); err != nil {
  112. return err
  113. }
  114. return proto.SetValue(channel, uint8(intrgb.Brightness), 160)
  115. })
  116. }
  117. func setModeOnZones(proto zoneProtocol, zones []intrgb.Zone, value uint8) error {
  118. return setValueOnZones(proto, zones, uint8(intrgb.EffectID), value)
  119. }
  120. // OpenDevice discovers the connected QMK keyboards and opens the one selected
  121. // by --device. Without --device it only proceeds when exactly one keyboard is
  122. // connected, so a command can never hit an unintended keyboard.
  123. func OpenDevice() (*via.Protocol, error) {
  124. if _, err := selectedZones(); err != nil {
  125. return nil, err
  126. }
  127. devices, err := intdevice.DiscoverAll()
  128. if err != nil {
  129. return nil, fmt.Errorf("discover: %w", err)
  130. }
  131. dev, err := selectDevice(devices, targetDevice)
  132. if err != nil {
  133. return nil, err
  134. }
  135. proto, err := via.New(dev)
  136. if err != nil {
  137. return nil, fmt.Errorf("open protocol: %w", err)
  138. }
  139. return proto, nil
  140. }
  141. func lightingEnabled(mode, brightness uint8) bool {
  142. return mode != 0 && brightness > 0
  143. }
  144. func ParseUint8(s string) (uint8, error) {
  145. v, err := strconv.ParseUint(s, 10, 8)
  146. if err != nil {
  147. return 0, fmt.Errorf("invalid value: %w", err)
  148. }
  149. return uint8(v), nil
  150. }