diff options
author | Martin Ashby <martin@martin-laptop.lan> | 2018-05-28 16:29:18 +0100 |
---|---|---|
committer | Martin Ashby <martin@martin-laptop.lan> | 2018-05-28 16:29:18 +0100 |
commit | f20896ef3b147ac07769eb36c67ba436c6d2ed22 (patch) | |
tree | cfc84980cf2de46784049191d5250fa1aa88f345 /unicorn/Unicorn2.go | |
parent | 4ec7e493520d558dd05e911b58ea354d25f33f45 (diff) | |
download | unicornpaint-f20896ef3b147ac07769eb36c67ba436c6d2ed22.tar.gz unicornpaint-f20896ef3b147ac07769eb36c67ba436c6d2ed22.tar.bz2 unicornpaint-f20896ef3b147ac07769eb36c67ba436c6d2ed22.tar.xz unicornpaint-f20896ef3b147ac07769eb36c67ba436c6d2ed22.zip |
New unicorns for displaing animated gifs! Tests pass on both old & new for fakes
Diffstat (limited to 'unicorn/Unicorn2.go')
-rw-r--r-- | unicorn/Unicorn2.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/unicorn/Unicorn2.go b/unicorn/Unicorn2.go index 716fe4d..04722f7 100644 --- a/unicorn/Unicorn2.go +++ b/unicorn/Unicorn2.go @@ -4,21 +4,32 @@ package unicorn import ( + "image" "image/gif" + "time" ) // Unicorn2 ... // Interface for interacting with the Unicorn HAT HD // Implemented by both real & fake unicorns. type Unicorn2 interface { + // Change the image GetGif() *gif.GIF SetGif(*gif.GIF) + // Starts the rendering goroutine StartRender() + + // Must be implemented to actually render the image to device + renderImage(image.Image) + // Required for os to not think we're stuck MainLoop() } +// BaseUnicorn2 ... +// Common to both real & fake unicorns! +// timing code for rendering & stopping rendering type BaseUnicorn2 struct { g *gif.GIF } @@ -30,3 +41,32 @@ func (u *BaseUnicorn2) GetGif() *gif.GIF { 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 { + stopChan := make(chan bool) + go func() { + timer := time.NewTimer(0) + imageIndex := 0 + running := true + for running { + select { + case <-stopChan: + timer.Stop() + running = false + case <-timer.C: + gf := u.GetGif() + im := gf.Image[imageIndex] + delay := gf.Delay[imageIndex] //100ths of a second, 10^-2 + u.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 |