configure sleep duration & fast refresh most of the time

This commit is contained in:
Gabriel Augendre 2024-09-16 14:12:51 +02:00
parent bdf828df44
commit dd1a7f274c
4 changed files with 76 additions and 19 deletions

View file

@ -96,10 +96,8 @@ func (e *EPD) sendDataSlice(data []byte) {
log.Fatalf("writing to spi: %v", err) log.Fatalf("writing to spi: %v", err)
} }
e.csPin.Out(gpio.High) e.csPin.Out(gpio.High)
log.Printf("sent chunk %v\n", cursor)
cursor = min(cursor+maxSize, toSend) cursor = min(cursor+maxSize, toSend)
} }
log.Printf("sent chunk %v\n", cursor)
} }
func (e *EPD) spiWrite(write []byte) ([]byte, error) { func (e *EPD) spiWrite(write []byte) ([]byte, error) {

12
main.go
View file

@ -11,6 +11,7 @@ import (
"golang.org/x/image/font/gofont/goregular" "golang.org/x/image/font/gofont/goregular"
"log" "log"
"os" "os"
"time"
) )
const fontName = "default" const fontName = "default"
@ -40,7 +41,16 @@ func main() {
CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"), CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"),
}) })
if err := run(ctx, transportsClient, feteClient, weatherClient); err != nil { const minSleep = 30 * time.Second
sleep, err := time.ParseDuration(os.Getenv("SLEEP_DURATION"))
if err != nil || sleep < minSleep {
sleep = minSleep
}
log.Printf("sleep duration: %v\n", sleep)
if err := run(ctx, sleep, transportsClient, feteClient, weatherClient); err != nil {
log.Fatal("error: ", err) log.Fatal("error: ", err)
} }

View file

@ -12,6 +12,7 @@ import (
func run( func run(
ctx context.Context, ctx context.Context,
_ time.Duration,
transportsClient *transports.Client, transportsClient *transports.Client,
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,

View file

@ -8,11 +8,18 @@ import (
"github.com/Crocmagnon/display-epaper/transports" "github.com/Crocmagnon/display-epaper/transports"
"github.com/Crocmagnon/display-epaper/weather" "github.com/Crocmagnon/display-epaper/weather"
"log" "log"
"os"
"periph.io/x/host/v3" "periph.io/x/host/v3"
"time" "time"
) )
func run(ctx context.Context, transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client) error { func run(
ctx context.Context,
sleep time.Duration,
transportsClient *transports.Client,
feteClient *fete.Client,
weatherClient *weather.Client,
) error {
_, err := host.Init() _, err := host.Init()
if err != nil { if err != nil {
return fmt.Errorf("initializing host: %w", err) return fmt.Errorf("initializing host: %w", err)
@ -31,6 +38,8 @@ func run(ctx context.Context, transportsClient *transports.Client, feteClient *f
default: default:
} }
log.Println("running loop")
err = loop( err = loop(
ctx, ctx,
display, display,
@ -42,8 +51,8 @@ func run(ctx context.Context, transportsClient *transports.Client, feteClient *f
log.Printf("error looping: %v\n", err) log.Printf("error looping: %v\n", err)
} }
log.Println("time.Sleep(30s)") log.Printf("time.Sleep(%v)\n", sleep)
time.Sleep(30 * time.Second) time.Sleep(sleep)
} }
} }
@ -54,19 +63,6 @@ func loop(
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,
) error { ) error {
defer func() {
if err := display.Sleep(); err != nil {
log.Printf("error sleeping: %v\n", err)
}
}()
err := display.Init()
if err != nil {
return fmt.Errorf("initializing display: %w", err)
}
display.Clear()
black, err := getBlack( black, err := getBlack(
ctx, ctx,
time.Now, time.Now,
@ -78,8 +74,60 @@ func loop(
return fmt.Errorf("getting black: %w", err) return fmt.Errorf("getting black: %w", err)
} }
defer func() {
if err := display.Sleep(); err != nil {
log.Printf("error sleeping: %v\n", err)
}
}()
err = initDisplay(display)
if err != nil {
return fmt.Errorf("initializing display: %w", err)
}
display.Clear()
display.Send(black) display.Send(black)
display.Refresh() display.Refresh()
return nil return nil
} }
const filename = "/perm/display-epaper-lastFullRefresh"
func initDisplay(display *epd.EPD) error {
if canInitFast() {
err := display.InitFast()
if err != nil {
return fmt.Errorf("running fast init: %w", err)
}
return nil
}
err := display.Init()
if err != nil {
return fmt.Errorf("running full init: %w", err)
}
markInitFull()
return nil
}
func canInitFast() bool {
stat, err := os.Stat(filename)
if err != nil {
return false
}
return stat.ModTime().Add(12 * time.Hour).After(time.Now())
}
func markInitFull() {
f, err := os.Create(filename)
if err != nil {
log.Printf("error marking full refresh: %v\n", err)
}
f.Close()
}