rgbmode.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package core
  2. import (
  3. "bytes"
  4. "github.com/charmbracelet/log"
  5. "github.com/sstallion/go-hid"
  6. )
  7. const BaseLightningByte = 0xE0
  8. const Rainbow = 0x05
  9. const RainbowMorph = 0x04
  10. const RainbowSecond = 0xFF
  11. const StaticFirst = 0x01
  12. const StaticSecond = 0x02
  13. // Change device light mode by sending mode bytes
  14. func changeMode(color []byte, device hid.DeviceInfo) {
  15. // Iterate through all ports (4) on hub and change their lightning
  16. for i := 0; i < 4; i++ {
  17. DeviceWrite(device, append([]byte{BaseLightningByte, byte(0x10 + i)}, color...))
  18. }
  19. }
  20. // Change device light mode with RGB arguments
  21. func changeRGBMode(device hid.DeviceInfo, color []byte, mode byte) {
  22. for i := 0; i < 4; i++ {
  23. var rgb []byte
  24. for i := 0; i < 38; i++ {
  25. rgb = append(rgb, color...)
  26. }
  27. rgb = append(rgb, bytes.Repeat([]byte{0x00}, 200)...)
  28. colorPacket := append([]byte{0xe0, byte(0x30 + i)}, rgb...)
  29. DeviceWrite(*Devs[0], []byte{0xe0, byte(0x10 + i), 0x32, mode})
  30. DeviceWrite(*Devs[0], colorPacket)
  31. }
  32. }
  33. // Set device light mode to Static
  34. func static(device hid.DeviceInfo, color []byte) {
  35. changeRGBMode(device, color, 0x03)
  36. changeMode(append([]byte{StaticFirst, StaticSecond}, bytes.Repeat([]byte{0x00}, 257)...), device)
  37. }
  38. // Set device light mode to Breathing
  39. func breathing(device hid.DeviceInfo, color []byte) {
  40. changeRGBMode(device, color, 0x03)
  41. changeMode(append([]byte{0x02, 0xff}, bytes.Repeat([]byte{0x00}, 257)...), device)
  42. }
  43. // Set device light mode to Meteor
  44. func meteor(device hid.DeviceInfo, color []byte) {
  45. for i := 0; i < 4; i++ {
  46. var rgb []byte
  47. for i := 0; i < 38; i++ {
  48. rgb = append(rgb, color...)
  49. }
  50. rgb = append(rgb, bytes.Repeat([]byte{0x00}, 200)...)
  51. colorPacket := append([]byte{0xe0, byte(0x30 + i), 0xaa, 0x11, 0xff,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
  53. 0xd1, 0xaa, 0x11, 0xff, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0xff, 0xff, 0xff, 0xaa, 0x11, 0xff,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
  56. 0xd1, 0xaa, 0x11, 0xff, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00})
  96. DeviceWrite(*Devs[0], []byte{0xe0, byte(0x10 + i), 0x32, 0x13})
  97. DeviceWrite(*Devs[0], colorPacket)
  98. }
  99. changeMode(append([]byte{0x24, 0xff}, bytes.Repeat([]byte{0x00}, 257)...), device)
  100. }
  101. // Set device light mode to Rainbow
  102. func rainbow(device hid.DeviceInfo) {
  103. changeMode([]byte{Rainbow, RainbowSecond}, device)
  104. }
  105. // Set device light mode to Morph
  106. func rainbowMorph(device hid.DeviceInfo) {
  107. changeMode([]byte{RainbowMorph, RainbowSecond}, device)
  108. }
  109. func SetLightMode(device hid.DeviceInfo, mode string, rgb ...byte) {
  110. log.Debugf("Setting %s mode for device %s", mode, device.ProductStr)
  111. switch mode {
  112. case "rainbow":
  113. rainbow(device)
  114. case "morph":
  115. rainbowMorph(device)
  116. case "static":
  117. static(device, rgb)
  118. case "breathing":
  119. breathing(device, rgb)
  120. default:
  121. log.Fatalf("Unknown mode %s, available: rainbow, morph, static, breathing", mode)
  122. }
  123. }