display-test/api/api.go
2024-09-30 21:56:45 +03:00

98 lines
1.9 KiB
Go

package api
import (
"fmt"
"io"
"log"
"time"
"gitea.unprism.ru/yotia/display-test/api/pages"
"gitea.unprism.ru/yotia/display-test/display"
"gitea.unprism.ru/yotia/display-test/drawer"
)
const (
loopTimeout = 10 * time.Millisecond
)
type displayApi struct {
d drawer.Drawer
curPage pages.Page
initPage pages.InitPage
statusPage pages.StatusPage
}
// Work process:
// Display has pages that you can switch between. Switch returns page controller.
// Every page has values that you can change:
// Init page - title, progress bar
// Status page - gps, time, rssi, service, ...
type Display interface {
// Put them to different functions because of different returns
SwitchToInitPage() pages.InitPageContent
SwitchToStatusPage() pages.StatusPageContent
io.Closer
}
func New() (Display, error) {
// Device
dev, err := display.New(log.New(log.Writer(), "display", log.LstdFlags), display.MT12232A)
if err != nil {
return nil, fmt.Errorf("create display: %w", err)
}
// Drawer
d := drawer.New(dev)
// Pages
// Init page
initPage, err := pages.NewInitPage(d)
if err != nil {
return nil, fmt.Errorf("new init page: %w", err)
}
// Status page
statusPage, err := pages.NewStatusPage(d)
if err != nil {
return nil, fmt.Errorf("new init page: %w", err)
}
return &displayApi{
d: d,
initPage: initPage,
statusPage: statusPage,
}, nil
}
func (d *displayApi) switchToPage(p pages.Page) {
if d.curPage != nil {
d.curPage.Diactivate()
}
if p != nil {
p.Activate()
}
d.curPage = p
}
func (d *displayApi) SwitchToInitPage() pages.InitPageContent {
d.switchToPage(d.initPage)
return d.initPage
}
func (d *displayApi) SwitchToStatusPage() pages.StatusPageContent {
d.switchToPage(d.statusPage)
return d.statusPage
}
func (d *displayApi) Close() error {
// !!! TMP DEBUG BAD CODE
d.d.Clear()
time.Sleep(100 * time.Millisecond)
// !!!
d.switchToPage(nil) // To deactivate cur page
return d.d.GetDisplay().Close()
}