Skip to content

Commit 41305be

Browse files
authored
Merge pull request magicmonkey#12 from lornajane/improve-examples
Start adding comments as documentation for examples
2 parents 0bed5cc + a80e08f commit 41305be

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

examples/client/client.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,58 @@ func main() {
1919
}
2020
fmt.Printf("Found device [%s]\n", sd.GetName())
2121

22+
// Button in position 2, changes to "Bye!" at the end of the program
23+
// When pressed, this prints "You pressed me" to the terminal
2224
myButton := buttons.NewTextButton("Hi world")
2325
myButton.SetActionHandler(&actionhandlers.TextPrintAction{Label: "You pressed me"})
2426
sd.AddButton(2, myButton)
2527

28+
// Button in position 3, prints "5" to the terminal when pressed
2629
myOtherButton := buttons.NewTextButton("4")
2730
myOtherButton.SetActionHandler(&actionhandlers.NumberPrintAction{Number: 5})
2831
sd.AddButton(3, myOtherButton)
2932

33+
// Button in position 7 (top right on Streamdeck XL), says 7
34+
// When pressed, changes to display "8"
3035
myNextButton := buttons.NewTextButton("7")
3136
myNextButton.SetActionHandler(&actionhandlers.TextLabelChangeAction{NewLabel: "8"})
3237
sd.AddButton(7, myNextButton)
3338

39+
// Image button, no action handler
3440
anotherButton, err := buttons.NewImageFileButton("examples/test/play.jpg")
3541
if err != nil {
3642
panic(err)
3743
}
3844
sd.AddButton(9, anotherButton)
3945

46+
// Yellow button, no action handler but it goes to blue at the end of the program
4047
cButton := buttons.NewColourButton(color.RGBA{255, 255, 0, 255})
4148
sd.AddButton(26, cButton)
4249

50+
// One button, two actions (uses ChainedAction)
51+
// Purple button, prints to the console and turns red when pressed
4352
multiActionButton := buttons.NewColourButton(color.RGBA{255, 0, 255, 255})
4453
thisActionHandler := &actionhandlers.ChainedAction{}
4554
thisActionHandler.AddAction(&actionhandlers.TextPrintAction{Label: "Purple press"})
4655
thisActionHandler.AddAction(&actionhandlers.ColourChangeAction{NewColour: color.RGBA{255, 0, 0, 255}})
4756
multiActionButton.SetActionHandler(thisActionHandler)
4857
sd.AddButton(27, multiActionButton)
4958

59+
// Text button, gets a red highlight after 2 seconds, then a green
60+
// highlight after another 2 seconds
5061
decoratedButton := buttons.NewTextButton("ABC")
5162
sd.AddButton(19, decoratedButton)
52-
5363
time.Sleep(2 * time.Second)
5464
decorator1 := decorators.NewBorder(10, color.RGBA{0, 255, 0, 255})
5565
sd.SetDecorator(19, decorator1)
5666
time.Sleep(2 * time.Second)
5767
decorator2 := decorators.NewBorder(5, color.RGBA{255, 0, 0, 255})
5868
sd.SetDecorator(19, decorator2)
59-
6069
time.Sleep(2 * time.Second)
6170

71+
// When this button says "Bye!", the program ends
6272
myButton.SetText("Bye!")
73+
// When this button goes blue, the program ends
6374
cButton.SetColour(color.RGBA{0, 255, 255, 255})
6475
sd.UnsetDecorator(19)
6576
}

examples/client2/client2.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,36 @@ import (
99
"github.com/magicmonkey/go-streamdeck/actionhandlers"
1010
"github.com/magicmonkey/go-streamdeck/buttons"
1111
"github.com/magicmonkey/go-streamdeck/decorators"
12+
_ "github.com/magicmonkey/go-streamdeck/devices"
1213
)
1314

1415
func main() {
16+
// store the currently selected button
1517
var current int
1618

19+
// initialise the streamdeck
1720
sd, err := streamdeck.New()
1821
if err != nil {
1922
panic(err)
2023
}
2124

25+
// put a number onto each button
2226
btns := make([]*buttons.TextButton, 32)
2327
for i := 0; i < 32; i++ {
2428
btns[i] = buttons.NewTextButton(strconv.Itoa(i))
2529
sd.AddButton(i, btns[i])
2630
}
2731

32+
// create some decorators for later use
2833
greenBorder := decorators.NewBorder(10, color.RGBA{0, 255, 0, 255})
29-
3034
redBorder := decorators.NewBorder(5, color.RGBA{255, 0, 0, 255})
35+
36+
// add red borders to all buttons
3137
for i := 0; i < 32; i++ {
3238
sd.SetDecorator(i, redBorder)
3339
}
3440

41+
// add action handlers as an inline function
3542
for i := 0; i < 32; i++ {
3643
h := actionhandlers.NewCustomAction(func(btn streamdeck.Button) {
3744
sd.SetDecorator(current, redBorder)
@@ -41,8 +48,8 @@ func main() {
4148
btns[i].SetActionHandler(h)
4249
}
4350

51+
// don't end the program, keep running
4452
var wg sync.WaitGroup
4553
wg.Add(1)
4654
wg.Wait()
47-
4855
}

examples/client3/client3.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,31 @@ import (
1010
)
1111

1212
func main() {
13-
13+
// initialise the device
1414
sd, err := streamdeck.New()
1515
if err != nil {
1616
panic(err)
1717
}
1818

19+
// create a text button
1920
btn1 := buttons.NewTextButton("Hello")
2021
sd.AddButton(1, btn1)
22+
// create an image button
2123
btn2, _ := buttons.NewImageFileButton("examples/test/play.jpg")
2224
sd.AddButton(2, btn2)
2325

2426

27+
// set a green border on both buttons
2528
greenBorder := decorators.NewBorder(10, color.RGBA{0, 255, 0, 255})
2629
sd.SetDecorator(1, greenBorder)
2730
sd.SetDecorator(2, greenBorder)
2831

32+
// wait for one second
2933
time.Sleep(1 * time.Second)
3034

35+
// remove decorators
3136
sd.UnsetDecorator(1)
3237
sd.UnsetDecorator(2)
33-
}
38+
39+
// program exits
40+
}

examples/test/test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@ import (
1010
)
1111

1212
func main() {
13+
// connect to a streamdeck
1314
sd, err := streamdeck.Open()
1415
if err != nil {
1516
panic(err)
1617
}
1718
fmt.Printf("Found device [%s]\n", sd.GetName())
1819

20+
// remove all button content
1921
sd.ClearButtons()
2022

23+
// manage brightness level
2124
sd.SetBrightness(50)
2225

26+
// show text in increasing lengths on three buttons
2327
sd.WriteTextToButton(2, "Hi!", color.RGBA{0, 0, 0, 255}, color.RGBA{0, 255, 255, 255})
2428
sd.WriteTextToButton(3, "Hi again!", color.RGBA{0, 0, 0, 255}, color.RGBA{0, 255, 255, 255})
2529
sd.WriteTextToButton(4, "Hi again again!", color.RGBA{0, 0, 0, 255}, color.RGBA{0, 255, 255, 255})
2630

27-
//sd.WriteImageToButton(9, "something.png")
28-
//sd.WriteImageToButton(10, "play.jpg")
29-
//sd.WriteColorToButton(color.RGBA{255, 0, 255, 0}, 1)
31+
// when any button is pressed, clear all buttons, and set an image on the pressed button
3032
sd.ButtonPress(func(btnIndex int, sd *streamdeck.Device, err error) {
3133
if err != nil {
3234
panic(err)
@@ -35,6 +37,7 @@ func main() {
3537
sd.WriteImageToButton(btnIndex, "examples/test/play.jpg")
3638
})
3739

40+
// keep the program running
3841
var wg sync.WaitGroup
3942
wg.Add(1)
4043
wg.Wait()

0 commit comments

Comments
 (0)