Add text rendering for Stream Deck buttons with toggle demo

This commit is contained in:
Ian Gulliver
2026-02-11 19:23:25 -08:00
parent c9bed33c75
commit b4d147b07e
2 changed files with 95 additions and 13 deletions

View File

@@ -3,7 +3,6 @@ package main
import (
"fmt"
"image/color"
"math/rand/v2"
"os"
"os/signal"
"syscall"
@@ -11,6 +10,42 @@ import (
"qrun/lib/streamdeck"
)
var palette = []color.RGBA{
{220, 50, 50, 255},
{50, 180, 50, 255},
{50, 100, 220, 255},
{220, 160, 30, 255},
{180, 50, 180, 255},
{50, 180, 180, 255},
{220, 120, 50, 255},
{100, 100, 200, 255},
}
func labelForKey(key int) string {
row := key / streamdeck.KeyCols()
col := key % streamdeck.KeyCols()
switch row {
case 0:
return fmt.Sprintf("Ch %d\nSelect", col+1)
case 1:
return fmt.Sprintf("Ch %d\nMute", col+1)
case 2:
return fmt.Sprintf("Ch %d\nSolo", col+1)
case 3:
return fmt.Sprintf("Ch %d\nRec", col+1)
}
return fmt.Sprintf("Key %d", key)
}
func drawKey(dev *streamdeck.Device, key int, bg color.RGBA) {
dim := color.RGBA{bg.R / 3, bg.G / 3, bg.B / 3, 255}
dev.SetKeyText(key, dim, color.White, labelForKey(key))
}
func drawKeyActive(dev *streamdeck.Device, key int, bg color.RGBA) {
dev.SetKeyText(key, bg, color.White, labelForKey(key))
}
func main() {
dev, err := streamdeck.Open()
if err != nil {
@@ -24,12 +59,12 @@ func main() {
dev.SetBrightness(80)
for i := 0; i < streamdeck.KeyCount(); i++ {
r := uint8(rand.IntN(256))
g := uint8(rand.IntN(256))
b := uint8(rand.IntN(256))
dev.SetKeyColor(i, color.RGBA{r, g, b, 255})
col := i % streamdeck.KeyCols()
drawKey(dev, i, palette[col])
}
active := make([]bool, streamdeck.KeyCount())
keys := make(chan streamdeck.KeyEvent, 64)
go func() {
if err := dev.ReadKeys(keys); err != nil {
@@ -43,15 +78,17 @@ func main() {
for {
select {
case ev := <-keys:
if ev.Pressed {
fmt.Printf("Key %d pressed\n", ev.Key)
r := uint8(rand.IntN(256))
g := uint8(rand.IntN(256))
b := uint8(rand.IntN(256))
dev.SetKeyColor(ev.Key, color.RGBA{r, g, b, 255})
} else {
fmt.Printf("Key %d released\n", ev.Key)
if !ev.Pressed {
continue
}
col := ev.Key % streamdeck.KeyCols()
active[ev.Key] = !active[ev.Key]
if active[ev.Key] {
drawKeyActive(dev, ev.Key, palette[col])
} else {
drawKey(dev, ev.Key, palette[col])
}
fmt.Printf("Key %d toggled %v\n", ev.Key, active[ev.Key])
case <-sig:
fmt.Println()
return