Added README; cleaned up the repo
This commit is contained in:
33 files changed
+270
-9330
No files matched your search
+56
-5
@@ -2,12 +2,16 @@ package beatglow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"image/color"
|
||||
"image/gif"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/BeatGlow/audio"
|
||||
|
||||
"github.com/BeatGlow/beatglow/app"
|
||||
"github.com/BeatGlow/beatglow/app/layout"
|
||||
"github.com/BeatGlow/beatglow/config"
|
||||
"github.com/BeatGlow/beatglow/hardware"
|
||||
@@ -17,9 +21,13 @@ import (
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
ctx controllerContext
|
||||
encoder input.Encoder
|
||||
frameRate time.Duration
|
||||
ctx controllerContext
|
||||
encoder input.Encoder
|
||||
frameRate time.Duration
|
||||
recorder *gif.GIF
|
||||
recorderStart time.Time
|
||||
recorderFrames int
|
||||
recorderPalette color.Palette
|
||||
}
|
||||
|
||||
func New(config *config.Config) (*Controller, error) {
|
||||
@@ -29,12 +37,18 @@ func New(config *config.Config) (*Controller, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &Controller{}
|
||||
c := &Controller{
|
||||
recorderStart: time.Now().Add(10 * time.Second),
|
||||
recorderFrames: 100,
|
||||
}
|
||||
|
||||
if err := c.configureDisplay(log, &config.Display); err != nil {
|
||||
log.WithError(err).Error("fatal error setting up display")
|
||||
return nil, err
|
||||
}
|
||||
c.recorderPalette = app.PaletteFor(c.ctx.display.ColorModel())
|
||||
log.Debugf("palette (%d colors): %+v", len(c.recorderPalette), c.recorderPalette)
|
||||
|
||||
if err := c.configureAudio(&config.Audio); err != nil {
|
||||
log.WithError(err).Error("fatal error setting up audio source")
|
||||
c.teardown(log)
|
||||
@@ -166,7 +180,7 @@ func (c *Controller) Run(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) onFrame(_ time.Time) {
|
||||
func (c *Controller) onFrame(now time.Time) {
|
||||
var refresh bool
|
||||
for _, layout := range c.ctx.layouts {
|
||||
for _, widget := range layout.Widgets() {
|
||||
@@ -183,11 +197,48 @@ func (c *Controller) onFrame(_ time.Time) {
|
||||
if err := c.ctx.display.Refresh(); err != nil {
|
||||
logrus.WithError(err).Warn("display refresh failed")
|
||||
}
|
||||
|
||||
if c.recorder != nil {
|
||||
c.recorder.Image = append(c.recorder.Image, app.ImageToPaletted(nil, c.ctx.display, c.recorderPalette))
|
||||
c.recorder.Delay = append(c.recorder.Delay, int(now.Sub(c.recorderStart).Seconds()/1000))
|
||||
if len(c.recorder.Image) > c.recorderFrames {
|
||||
go c.recorderSave()
|
||||
}
|
||||
} else if now.After(c.recorderStart) {
|
||||
logrus.Info("recorder starting")
|
||||
c.recorder = new(gif.GIF)
|
||||
}
|
||||
|
||||
} else {
|
||||
logrus.Trace("display refresh skipped, widgets not changed")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) recorderSave() {
|
||||
r := c.recorder
|
||||
c.recorder = nil
|
||||
c.recorderStart = time.Now().Add(24000 * time.Hour) // never? :P
|
||||
|
||||
name := "beatglow-recording.gif"
|
||||
log := logrus.WithField("path", name)
|
||||
log.Debugf("saving %d frames", len(r.Image))
|
||||
|
||||
f, err := os.Create(name)
|
||||
if err != nil {
|
||||
log.WithError(err).Warn("can't save recording")
|
||||
return
|
||||
}
|
||||
|
||||
if err = gif.EncodeAll(f, r); err != nil {
|
||||
logrus.WithError(err).Warn("can't save recording")
|
||||
_ = f.Close()
|
||||
_ = os.Remove(name)
|
||||
return
|
||||
}
|
||||
|
||||
f.Close()
|
||||
}
|
||||
|
||||
func (c *Controller) onInput(log *logrus.Entry, event hardware.InputEvent) {
|
||||
log = log.WithField("event", event.String())
|
||||
log.Debug("input event")
|
||||
|
||||
Reference in new issue
Block a user