結構苦労したけど Go で blink(1) を操作できるようになったー!!!
blink(1)
blink(1) は今年の夏頃に買った光る USB デバイスで,RSpec と組み合わせたり,CI の XFD として連携したり,自由に遊べる.今まで Ruby で操作していて,LT タイマー Gem を実装してみたりしたけど,Go でも操作してみたいなと思ってた.
go-blink1 で操作できた
GoBlink
がダメな件は最後に書くとして,現在 Go から blink(1) を操作するなら go-blink1
を使うのが良さそう.ただこの go-blink1
も公開されたのが2日前で,待望の!って感じだった.
コード
以下の概念を意識しておけば問題ないと思う.ちなみに State
は Pattern
として使わず,単体で SetState()
して点灯させることもできる.
State type
- ステート(状態)を定義する
- RGB
- フェード時間
- 点灯時間
Pattern type
- 複数の
State type
からなる点滅パターンを定義する - リピート回数
- リピート遅延時間
- ステート配列
RunPattern()
でパターンを点滅させる
- 複数の
blink1.OffState
に関しては go-blink1
自体に定義されている State
で消灯を表している.
var OffState = State{Duration: time.Duration(10) * time.Millisecond}
ザッと書いてみた!
「赤→緑→青→オレンジ→紫」というパターンで計2セット点滅している.
package main import ( "github.com/hink/go-blink1" "time" ) func main() { blink, err := blink1.OpenNextDevice() if err != nil { panic(err) } // 赤ステート redState := blink1.State{ Red: 255, Green: 0, Blue: 0, Normal: 0, FadeTime: 2 * time.Second, Duration: 2 * time.Second, } // 緑ステート greenState := blink1.State{ Red: 0, Green: 255, Blue: 0, Normal: 0, FadeTime: 2 * time.Second, Duration: 2 * time.Second, } // 青ステート blueState := blink1.State{ Red: 0, Green: 0, Blue: 255, Normal: 0, FadeTime: 2 * time.Second, Duration: 2 * time.Second, } // オレンジステート orangeState := blink1.State{ Red: 243, Green: 152, Blue: 0, Normal: 0, FadeTime: 2 * time.Second, Duration: 2 * time.Second, } // 紫ステート purpleState := blink1.State{ Red: 167, Green: 87, Blue: 168, Normal: 0, FadeTime: 2 * time.Second, Duration: 2 * time.Second, } // 各ステートを連続で光らせる // ステートごとに消灯するために `OffState` を設定している pattern := &blink1.Pattern{ Repeat: 1, RepeatDelay: 0, States: []blink1.State{ redState, blink1.OffState, greenState, blink1.OffState, blueState, blink1.OffState, orangeState, blink1.OffState, purpleState, blink1.OffState, }, } err = blink.RunPattern(pattern) if err != nil { panic(err) } blink.Close() }
GoBlink
先週の段階だと go-blink1
が公開されていなかったので,当然ながら公式ライブラリの GoBlink
を試すことになるんだけど,go build
すら通らなくて意味不明だった.今は README が更新されていて,今後は go-blink1
でサポートしていくと書いてあった.
The original GoBlink library is a few years old now and doesn't support as many blink(1) features. The new go-blink1 library is go-gettable and is more actively supported.
ちなみに go build
すると以下のエラーが出た.
./hid.go:64: cannot use bl.dev (type *C.struct_usbDevice_t) as type *C.struct_usbDevice in argument to setRGB ./hid.go:68: cannot use bl.dev (type *C.struct_usbDevice_t) as type *C.struct_usbDevice in argument to setRGBN ./hid.go:85: cannot use b.dev (type *C.struct_usbDevice_t) as type *C.struct_usbDevice in argument to _Cfunc_usbhidCloseDevice ./hid.go:91: cannot use dev (type *C.struct_usbDevice_t) as type *C.struct_usbDevice in argument to fadeToRgbBlink1 ./hid.go:105: cannot use dev (type *C.struct_usbDevice_t) as type *C.struct_usbDevice in argument to setRGB ./hid.go:123: cannot use dev (type **C.struct_usbDevice_t) as type **C.struct_usbDevice in argument to _Cfunc_usbhidOpenDevice
続く
Go + blink(1) で遊ぶぞ!!!