display-test/main.go
2024-10-06 21:31:23 +03:00

242 lines
5.5 KiB
Go

package main
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"gitea.unprism.ru/yotia/display-test/api"
"gitea.unprism.ru/yotia/display-test/api/progress"
"gitea.unprism.ru/yotia/display-test/components"
"gitea.unprism.ru/yotia/display-test/display"
"gitea.unprism.ru/yotia/display-test/drawer"
"gitea.unprism.ru/KRBL/sim-modem/api/modem"
"gitea.unprism.ru/KRBL/sim-modem/api/modem/gps"
)
const (
displayUpdateTimeout = 10 * time.Millisecond
)
func main() {
log.Println("CGSG forever!!!")
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
// Just for logs
go func(ctx context.Context) {
<-ctx.Done()
log.Println("GOT INTERUPT SIGNAL")
}(ctx)
if err := mainE(ctx); err != nil {
log.Println("MAIN finished with error:", err.Error())
}
log.Println("END")
}
var logger *log.Logger
var dev display.Display
var d drawer.Drawer
var m modem.Modem
func Init(ctx context.Context) error {
// Display init
var err error
dev, err = display.New(log.New(log.Writer(), "display", log.LstdFlags), display.MT12232A)
if err != nil {
return fmt.Errorf("new display: %w", err)
}
logger.Println("Display inited")
// Create drawer
d := drawer.New(dev)
d.Clear()
//d.FillBar(0, 0, 10, 10, 1)
d.PutText(2, 0, "12345678901234567890")
d.PutText(2, drawer.LineH, "WWWWWWWWWWWWWWWWWWWWWWW")
d.PutText(1, drawer.LineH*2, "Progress bar")
d.Flush()
time.Sleep(time.Second)
pbc, err := components.NewProgressBar(d, 0, drawer.LineH*3, d.W(), drawer.LineH)
if err != nil {
return fmt.Errorf("new progress bar: %w", err)
}
pb := progress.NewHandler(pbc, 10)
for range 10 {
select {
case <-ctx.Done():
return nil
case <-time.After(time.Duration(rand.Int()%1000) * time.Millisecond):
}
pb.Checkpoint()
}
time.Sleep(400 * time.Millisecond)
pb.GetBar().Clear()
pb.Close()
d.FillBar(0, drawer.LineH*2, d.W(), drawer.LineH*3, 0)
d.PutText(1, drawer.LineH*2, "Done")
d.Flush()
time.Sleep(100 * time.Millisecond)
//d.PutText(0, 0, "можно 4 строки впихнуть")
//d.PutText(0, drawer.FontCharH+drawer.LineGap, "каждая буква 5x7")
//d.PutText(0, (drawer.FontCharH+drawer.LineGap)*2, "+ русский через жопу")
//d.PutText(0, (drawer.FontCharH+drawer.LineGap)*3, "KRBL forever!!")
// Modem init
//mInitCtx, mInitCtxCancel := context.WithCancel(ctx)
//go DrawProgress(mInitCtx, 13*time.Second)
//defer mInitCtxCancel()
//
//m = modem.New(log.New(logger.Writer(), "modem : ", log.LstdFlags))
//if err := m.Init(); err != nil {
// return fmt.Errorf("modem init: %w", err)
//}
//logger.Println("Display inited")
//mInitCtxCancel()
//
//d.Clear()
//d.PutText(0, d.GetFont().Metrics().Height.Ceil(), "Modem inited.")
//d.PutText(0, d.GetFont().Metrics().Height.Ceil()*2, "Starting test...")
//d.Flush()
return nil
}
func Close() {
//m.Close()
//if d != nil {
// d.Clear()
// d.Flush()
//}
dev.Close()
}
//func MainLoop(ctx context.Context) error {
// mainLoopTimeout := time.Second
//
// frameCounter := 0
// deadline := time.Now().Add(100 * time.Millisecond)
// for {
// select {
// case <-ctx.Done():
// return nil
// case <-time.After(time.Until(deadline)):
// deadline = time.Now().Add(mainLoopTimeout)
//
// st, _ := m.Gps().GetStatus()
// data := m.GetData()
//
// coordsStr := fmt.Sprintf("%03d°%02.0f'%s %03d°%02.0f'%s",
// int(data.Latitude), (data.Latitude-float64(int(data.Latitude)))*100, data.LatitudeIndicator,
// int(data.Longitude), (data.Longitude-float64(int(data.Longitude)))*100, data.LongitudeIndicator)
// var str1 string
// var str2 string
//
// if frameCounter%4 < 2 {
// str2 = fmt.Sprintf("Sat. count: %d", st.FoundSatelitesCount)
// } else {
// str2 = coordsStr
// }
// str1 = fmt.Sprintf("%s", time.Now().Format("15:04:05"))
//
// d.Clear()
// d.PutText(0, d.GetFont().Metrics().Height.Ceil(), str1)
// d.PutText(0, d.GetFont().Metrics().Height.Ceil()*2, str2)
// d.Flush()
// frameCounter++
// }
// }
//}
func mainE(ctx context.Context) error {
logger = log.New(os.Stdout, "main : ", log.LstdFlags)
//
//if err := Init(ctx); err != nil {
// return err
//}
////if err := dev.Test(ctx); err != nil {
//// return err
////}
//defer Close()
// if err := MainLoop(ctx); err != nil {
// return err
// }
logger.Println("")
a, err := api.New()
if err != nil {
return fmt.Errorf("new api: %w", err)
}
defer a.Close()
logger.Println("Inited")
// !!! Init example
// Setup init page
ip := a.SwitchToInitPage()
ip.SetTitle("KRBL")
pb := progress.NewHandler(ip.GetProgressBar(), 10)
// Simulate init process
for range 10 {
select {
case <-ctx.Done():
return nil
case <-time.After(60 * time.Millisecond):
}
pb.Checkpoint()
}
time.Sleep(200 * time.Millisecond)
// !!! Status page simulating
// Switch
services := []string{"NO SIGNAL", "GSM", "WCDMA", "LTE", "TDS"}
sp := a.SwitchToStatusPage()
for {
select {
case <-ctx.Done():
return nil
case <-time.After(1700 * time.Millisecond):
// Random coords
lat := float64(rand.Int()%10000) + float64(rand.Int()%1000000)/1000000
log := float64(rand.Int()%100000) + float64(rand.Int()%1000000)/1000000
latI := "N"
if rand.Int()%2 == 0 {
latI = "S"
}
logI := "E"
if rand.Int()%2 == 0 {
logI = "W"
}
sp.SetGps(gps.Data{
Latitude: lat,
LatitudeIndicator: latI,
Longitude: log,
LongitudeIndicator: logI,
})
// Random rssi
sp.SetRssi(rand.Int() % 31)
sp.SetService(services[rand.Int()%len(services)])
}
}
}