-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrid.go
66 lines (57 loc) · 1.56 KB
/
grid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"github.com/draeron/golaunchpad/examples/common"
"github.com/draeron/golaunchpad/pkg/grid"
"github.com/draeron/golaunchpad/pkg/launchpad"
"github.com/draeron/golaunchpad/pkg/launchpad/button"
"github.com/draeron/golaunchpad/pkg/layout"
"github.com/draeron/gopkgs/color"
"github.com/draeron/gopkgs/logger"
"time"
)
var log = logger.NewLogrus("main")
var device launchpad.Controller
func main() {
log.Info("starting grid example")
defer log.Info("exiting grid example")
device = common.Setup()
defer device.Close()
setup()
common.WaitExit()
}
func setup() {
mask := launchpad.Mask{
button.User: true,
}
gryd := grid.NewGrid(16, 16, true, mask)
gryd.Layout.SetName("grid")
gryd.SetHoldTimer(layout.ArrowHold, time.Millisecond*20)
gryd.SetHandler(func(grd *grid.Grid, x, y int, event grid.EventType) {
if event != grid.Pressed {
return
}
col := color.FromStdColor(gryd.Color(x, y))
if col.Equal(color.Black) {
grd.SetColor(x, y, common.RandColor())
} else {
grd.SetColor(x, y, color.Black)
}
grd.UpdateDevice()
})
// Add a toggle on the user btn
wrapped := false
gryd.SetColorMany(button.Modes(), color.Black)
gryd.Layout.SetColor(button.User, color.Yellow)
gryd.Layout.SetHandler(layout.ModePressed, layout.MaskedHandler(mask, func(layout layout.Layout, btn button.Button) {
wrapped = !wrapped
gryd.Wrap(wrapped)
if wrapped {
gryd.Layout.SetColor(button.User, color.Green)
} else {
gryd.Layout.SetColor(button.User, color.Yellow)
}
}))
gryd.UpdateDevice()
gryd.Connect(device)
gryd.Activate()
}