197 lines
4.2 KiB
Go
197 lines
4.2 KiB
Go
package visual
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/BeatGlow/beatglow/draw"
|
|
"github.com/BeatGlow/beatglow/draw/pixel"
|
|
"github.com/BeatGlow/beatglow/player"
|
|
"github.com/BeatGlow/beatglow/ui"
|
|
"github.com/BeatGlow/display"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
playingTitleY = 12
|
|
playingArtistY = 30
|
|
)
|
|
|
|
type Playing struct {
|
|
display display.Display
|
|
canvas *draw.Canvas
|
|
bounds image.Rectangle
|
|
styles ui.Styles
|
|
|
|
status struct {
|
|
player *player.Status
|
|
track *player.Track
|
|
}
|
|
|
|
scroll *draw.Scroller
|
|
}
|
|
|
|
func NewPlaying(display display.Display) *Playing {
|
|
size := display.Bounds().Size()
|
|
styles := ui.StylesFor(display.ColorModel())
|
|
|
|
v := &Playing{
|
|
display: display,
|
|
canvas: draw.NewCanvas(display).SetStyle(styles.Track),
|
|
bounds: display.Bounds(),
|
|
styles: styles,
|
|
}
|
|
|
|
// Setup font sizes
|
|
v.styles.Track.Font.SetPointSize(24)
|
|
v.styles.Progress.Font.SetPointSize(9)
|
|
|
|
// Setup scroller
|
|
v.scroll = draw.NewScroller(display, size.X, true)
|
|
v.scroll.Canvas.SetStyle(v.styles.Track)
|
|
|
|
// Player offline text
|
|
v.Update(nil)
|
|
|
|
return v
|
|
}
|
|
|
|
func (Playing) Halt() error {
|
|
return nil
|
|
}
|
|
|
|
func (Playing) Type() string {
|
|
return "playing"
|
|
}
|
|
|
|
func (v *Playing) Render() error {
|
|
v.display.Clear()
|
|
|
|
var (
|
|
rows = [...]int{
|
|
0, // warnings (?)
|
|
12, // title
|
|
30, // artist
|
|
55, // status
|
|
43, // progress
|
|
}
|
|
/*
|
|
fonts = [...]*font.Font{
|
|
font.Monaco.WithPointSize(8),
|
|
font.MicroGrotesk.WithPointSize(16),
|
|
font.MicroGrotesk.WithPointSize(12),
|
|
font.Monaco.WithPointSize(8),
|
|
}
|
|
/*
|
|
fonts = [...]*font.Font{
|
|
font.Fixed5x7,
|
|
font.Unifont,
|
|
font.Fixed5x7,
|
|
font.Fixed5x7,
|
|
}
|
|
*/
|
|
)
|
|
|
|
v.scroll.Advance()
|
|
|
|
var (
|
|
bounds = v.display.Bounds()
|
|
width = bounds.Dx()
|
|
status = v.status.player
|
|
track = v.status.track
|
|
)
|
|
|
|
if status != nil {
|
|
var (
|
|
format = "Dunno"
|
|
elapsedText = "--:-- / --:--"
|
|
progress float64
|
|
)
|
|
|
|
if track != nil {
|
|
elapsedText = fmt.Sprintf("%s / %s", hms(status.Elapsed), hms(track.Duration))
|
|
progress = float64(status.Elapsed) / float64(track.Duration)
|
|
}
|
|
|
|
if status.Format.Encoding != "" {
|
|
format = strings.ToUpper(status.Format.Encoding)
|
|
} else if track != nil && track.Path != "" {
|
|
format = strings.ToUpper(filepath.Ext(track.Path)[1:])
|
|
}
|
|
//format += fmt.Sprintf("-%d", status.Format.BitRate)
|
|
|
|
v.canvas.WithStyle(v.styles.Progress, func() {
|
|
v.canvas.Text(1, 0, format)
|
|
v.canvas.Text(40, 0, elapsedText)
|
|
v.canvas.WithGradient(width-120-2, 8, func() {
|
|
v.canvas.Progess(120, 0, width-120-2, progress)
|
|
})
|
|
})
|
|
|
|
if status.Paused {
|
|
draw.Sprite(v.display, pixel.On, 1, rows[3], pauseSprite)
|
|
} else {
|
|
draw.Sprite(v.display, pixel.On, 1, rows[3], playSprite)
|
|
}
|
|
}
|
|
|
|
return v.display.Refresh()
|
|
}
|
|
|
|
func (v *Playing) TrackChanged(track *player.Track) {
|
|
logrus.WithField("visual", "playing").Debug("track changed")
|
|
if track == nil {
|
|
v.scroll.SetDirection(0)
|
|
} else {
|
|
v.scroll.SetDirection(+1)
|
|
switch {
|
|
case track.Artist != "" && track.Title != "":
|
|
v.scroll.Text(0, playingTitleY, fmt.Sprintf("%s - %s", track.Artist, track.Title))
|
|
case track.Title != "":
|
|
v.scroll.Text(0, playingTitleY, fmt.Sprintf("Unknown Artist - %s", track.Title))
|
|
case track.Artist != "":
|
|
v.scroll.Text(0, playingTitleY, fmt.Sprintf("%s - Unknown", track.Title))
|
|
default:
|
|
v.scroll.Text(0, playingTitleY, "Unknown")
|
|
}
|
|
}
|
|
v.scroll.Reset()
|
|
v.status.track = track
|
|
}
|
|
|
|
func (v *Playing) StatusChanged(status *player.Status) {
|
|
logrus.WithField("visual", "playing").Debug("status changed")
|
|
v.status.player = status
|
|
}
|
|
|
|
func hms(d time.Duration) string {
|
|
var (
|
|
v = int(d / time.Second)
|
|
h = v / 3600
|
|
m = (v - h*3600) / 60
|
|
s = (v - m*60)
|
|
)
|
|
if h == 0 {
|
|
return fmt.Sprintf("%02d:%02d", m, s)
|
|
}
|
|
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
|
|
}
|
|
|
|
func (v *Playing) Update(status *player.Status) {
|
|
v.status.player = status
|
|
|
|
if status == nil {
|
|
v.scroll.Text(0, playingTitleY, "Player is offline")
|
|
}
|
|
}
|
|
|
|
var (
|
|
playSprite = []byte{8, 8, 0x00, 0xFF, 0xFF, 0x7E, 0x3C, 0x18, 0x00, 0x00}
|
|
pauseSprite = []byte{8, 8, 0x00, 0x7F, 0x7F, 0x00, 0x00, 0x7F, 0x7F, 0x00}
|
|
repeatSprite = []byte{8, 8, 0x00, 0x79, 0x7B, 0x63, 0x63, 0x63, 0x6F, 0x2F}
|
|
volumeSprite = []byte{8, 8, 0x00, 0x18, 0x18, 0x3C, 0x7E, 0xFF, 0x00, 0x00}
|
|
)
|