aboutsummaryrefslogtreecommitdiff
path: root/unicorn
diff options
context:
space:
mode:
Diffstat (limited to 'unicorn')
-rw-r--r--unicorn/BaseFakeUnicorn.go2
-rw-r--r--unicorn/FakeUnicorn2.go10
-rw-r--r--unicorn/RealUnicorn2.go10
-rw-r--r--unicorn/Unicorn2.go40
4 files changed, 49 insertions, 13 deletions
diff --git a/unicorn/BaseFakeUnicorn.go b/unicorn/BaseFakeUnicorn.go
index 7910814..19c1d59 100644
--- a/unicorn/BaseFakeUnicorn.go
+++ b/unicorn/BaseFakeUnicorn.go
@@ -1,3 +1,5 @@
+// +build linux,386 linux,amd64
+
package unicorn
import "github.com/veandco/go-sdl2/sdl"
diff --git a/unicorn/FakeUnicorn2.go b/unicorn/FakeUnicorn2.go
index 27629d2..8728c15 100644
--- a/unicorn/FakeUnicorn2.go
+++ b/unicorn/FakeUnicorn2.go
@@ -1,3 +1,5 @@
+// +build linux,386 linux,amd64
+
package unicorn
import (
@@ -7,7 +9,7 @@ import (
)
type FakeUnicorn2 struct {
- BaseUnicorn2
+ *BaseUnicorn2
*BaseFakeUnicorn
}
@@ -37,13 +39,17 @@ func (u *FakeUnicorn2) renderImage(im image.Image) {
u.renderer.Present()
}
+func (u *FakeUnicorn2) StartRender() chan bool {
+ return u.StartRenderBase(u.renderImage)
+}
+
func NewUnicorn2() (*FakeUnicorn2, error) {
baseFake, err := NewBaseFakeUnicorn(300, 300)
if err != nil {
return nil, err
}
return &FakeUnicorn2{
- BaseUnicorn2{},
+ NewBaseUnicorn2(),
baseFake,
}, nil
}
diff --git a/unicorn/RealUnicorn2.go b/unicorn/RealUnicorn2.go
index 991e79f..72b4ed7 100644
--- a/unicorn/RealUnicorn2.go
+++ b/unicorn/RealUnicorn2.go
@@ -13,7 +13,7 @@ import (
)
type RealUnicorn2 struct {
- BaseUnicorn2
+ *BaseUnicorn2
device *spi.Device
}
@@ -55,11 +55,17 @@ func NewUnicorn2() (*RealUnicorn2, error) {
return nil, err
}
return &RealUnicorn2{
- BaseUnicorn2{},
+ NewBaseUnicorn2(),
dev,
}, nil
}
+// StartRender ...
+// Passes through to base to actually do the render
+func (u *RealUnicorn2) StartRender() chan bool {
+ return u.StartRenderBase(u.renderImage)
+}
+
// MainLoop ...
// Just blocks until sigterm
func (u *RealUnicorn2) MainLoop() {
diff --git a/unicorn/Unicorn2.go b/unicorn/Unicorn2.go
index 04722f7..5dcc6c2 100644
--- a/unicorn/Unicorn2.go
+++ b/unicorn/Unicorn2.go
@@ -5,6 +5,7 @@ package unicorn
import (
"image"
+ "image/color/palette"
"image/gif"
"time"
)
@@ -18,7 +19,7 @@ type Unicorn2 interface {
SetGif(*gif.GIF)
// Starts the rendering goroutine
- StartRender()
+ StartRender() chan bool
// Must be implemented to actually render the image to device
renderImage(image.Image)
@@ -42,11 +43,26 @@ func (u *BaseUnicorn2) SetGif(g *gif.GIF) {
u.g = g
}
-// StartRender ...
-// Starts rendering the image. If it's an animated image,
-// renders animation frames. Return a channel to stop the
-// image rendering.
-func (u *FakeUnicorn2) StartRender() chan bool {
+func NewBaseUnicorn2() *BaseUnicorn2 {
+ im := image.NewPaletted(
+ image.Rect(0, 0, 16, 16),
+ palette.WebSafe)
+
+ gf := &gif.GIF{
+ Image: []*image.Paletted{im},
+ Delay: []int{50},
+ Disposal: []byte{gif.DisposalBackground},
+ BackgroundIndex: 0, // This is black in the websafe palette
+ }
+
+ return &BaseUnicorn2{
+ g: gf,
+ }
+}
+
+// StartRenderBase ...
+// Deals with the timing aspect of animated GIFs
+func (u *BaseUnicorn2) StartRenderBase(renderImage func(image.Image)) chan bool {
stopChan := make(chan bool)
go func() {
timer := time.NewTimer(0)
@@ -59,14 +75,20 @@ func (u *FakeUnicorn2) StartRender() chan bool {
running = false
case <-timer.C:
gf := u.GetGif()
+
+ // Image could change underneath us, but there should always be 1 image at least.
+ if imageIndex >= len(gf.Image) {
+ imageIndex = 0
+ }
+
im := gf.Image[imageIndex]
delay := gf.Delay[imageIndex] //100ths of a second, 10^-2
- u.renderImage(im)
-
+ renderImage(im)
+
timer.Reset(time.Duration(delay * 10000000)) // nanoseconds 10^-9 sec
imageIndex = (imageIndex + 1) % len(gf.Image)
}
}
}()
return stopChan
-} \ No newline at end of file
+}