refresh only when image changes

This commit is contained in:
Gabriel Augendre 2024-09-16 18:09:00 +02:00
parent c6ae3f97ef
commit 2fa57a73a1
4 changed files with 39 additions and 16 deletions

2
img.go
View file

@ -29,7 +29,7 @@ const (
rightX = 530 rightX = 530
) )
func getBlack( func getImg(
ctx context.Context, ctx context.Context,
nowFunc func() time.Time, nowFunc func() time.Time,
transportsClient *transports.Client, transportsClient *transports.Client,

View file

@ -41,7 +41,7 @@ func main() {
CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"), CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"),
}) })
const minSleep = 30 * time.Second const minSleep = 1 * time.Second
sleep, err := time.ParseDuration(os.Getenv("SLEEP_DURATION")) sleep, err := time.ParseDuration(os.Getenv("SLEEP_DURATION"))
if err != nil || sleep < minSleep { if err != nil || sleep < minSleep {

View file

@ -17,7 +17,7 @@ func run(
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,
) error { ) error {
img, err := getBlack( img, err := getImg(
ctx, ctx,
func() time.Time { func() time.Time {
t, err := time.Parse(time.DateOnly, "2024-08-01zzz") t, err := time.Parse(time.DateOnly, "2024-08-01zzz")

View file

@ -7,6 +7,7 @@ import (
"github.com/Crocmagnon/display-epaper/fete" "github.com/Crocmagnon/display-epaper/fete"
"github.com/Crocmagnon/display-epaper/transports" "github.com/Crocmagnon/display-epaper/transports"
"github.com/Crocmagnon/display-epaper/weather" "github.com/Crocmagnon/display-epaper/weather"
"image"
"log" "log"
"os" "os"
"periph.io/x/host/v3" "periph.io/x/host/v3"
@ -30,6 +31,8 @@ func run(
return fmt.Errorf("initializing epd: %w", err) return fmt.Errorf("initializing epd: %w", err)
} }
var currentImg image.Image
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -40,9 +43,10 @@ func run(
log.Println("running loop") log.Println("running loop")
err = loop( img, err := loop(
ctx, ctx,
display, display,
currentImg,
transportsClient, transportsClient,
feteClient, feteClient,
weatherClient, weatherClient,
@ -51,19 +55,15 @@ func run(
log.Printf("error looping: %v\n", err) log.Printf("error looping: %v\n", err)
} }
currentImg = img
log.Printf("time.Sleep(%v)\n", sleep) log.Printf("time.Sleep(%v)\n", sleep)
time.Sleep(sleep) time.Sleep(sleep)
} }
} }
func loop( func loop(ctx context.Context, display *epd.EPD, currentImg image.Image, transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client) (image.Image, error) {
ctx context.Context, img, err := getImg(
display *epd.EPD,
transportsClient *transports.Client,
feteClient *fete.Client,
weatherClient *weather.Client,
) error {
black, err := getBlack(
ctx, ctx,
time.Now, time.Now,
transportsClient, transportsClient,
@ -71,7 +71,12 @@ func loop(
weatherClient, weatherClient,
) )
if err != nil { if err != nil {
return fmt.Errorf("getting black: %w", err) return nil, fmt.Errorf("getting black: %w", err)
}
if imgEqual(currentImg, img, epd.Width, epd.Height) {
log.Println("Images are equal, doing nothing.")
return img, nil
} }
defer func() { defer func() {
@ -82,15 +87,15 @@ func loop(
err = initDisplay(display) err = initDisplay(display)
if err != nil { if err != nil {
return fmt.Errorf("initializing display: %w", err) return nil, fmt.Errorf("initializing display: %w", err)
} }
display.Clear() display.Clear()
display.Send(black) display.Send(img)
display.Refresh() display.Refresh()
return nil return img, nil
} }
const filename = "/perm/display-epaper-lastFullRefresh" const filename = "/perm/display-epaper-lastFullRefresh"
@ -131,3 +136,21 @@ func markInitFull() {
f.Close() f.Close()
} }
func imgEqual(img1, img2 image.Image, width, height int) bool {
if img1 == nil || img2 == nil {
return false
}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r1, g1, b1, a1 := img1.At(x, y).RGBA()
r2, g2, b2, a2 := img2.At(x, y).RGBA()
if r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2 {
return false
}
}
}
return true
}