Compare commits

..

No commits in common. "df0ccc536d54944c8e3d455fe0cebb25fed647cf" and "c6ae3f97ef8276eb9a83f75210a621c0995705ac" have entirely different histories.

4 changed files with 16 additions and 64 deletions

2
img.go
View file

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

18
main.go
View file

@ -41,30 +41,16 @@ func main() {
CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"), CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"),
}) })
const minSleep = 1 * time.Second const minSleep = 30 * 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 {
sleep = minSleep sleep = minSleep
} }
const minInitFastThreshold = 1 * time.Second
initFastThreshold, err := time.ParseDuration(os.Getenv("INIT_FAST_THRESHOLD"))
if err != nil || initFastThreshold < minInitFastThreshold {
initFastThreshold = minInitFastThreshold
}
log.Printf("sleep duration: %v\n", sleep) log.Printf("sleep duration: %v\n", sleep)
if err := run( if err := run(ctx, sleep, transportsClient, feteClient, weatherClient); err != nil {
ctx,
sleep,
initFastThreshold,
transportsClient,
feteClient,
weatherClient,
); err != nil {
log.Fatal("error: ", err) log.Fatal("error: ", err)
} }

View file

@ -13,12 +13,11 @@ import (
func run( func run(
ctx context.Context, ctx context.Context,
_ time.Duration, _ time.Duration,
_ time.Duration,
transportsClient *transports.Client, transportsClient *transports.Client,
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,
) error { ) error {
img, err := getImg( img, err := getBlack(
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,7 +7,6 @@ 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"
@ -17,7 +16,6 @@ import (
func run( func run(
ctx context.Context, ctx context.Context,
sleep time.Duration, sleep time.Duration,
initFastThreshold time.Duration,
transportsClient *transports.Client, transportsClient *transports.Client,
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,
@ -32,8 +30,6 @@ 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():
@ -44,11 +40,9 @@ func run(
log.Println("running loop") log.Println("running loop")
img, err := loop( err = loop(
ctx, ctx,
display, display,
initFastThreshold,
currentImg,
transportsClient, transportsClient,
feteClient, feteClient,
weatherClient, weatherClient,
@ -57,8 +51,6 @@ 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)
} }
@ -67,13 +59,11 @@ func run(
func loop( func loop(
ctx context.Context, ctx context.Context,
display *epd.EPD, display *epd.EPD,
initFastThreshold time.Duration,
currentImg image.Image,
transportsClient *transports.Client, transportsClient *transports.Client,
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,
) (image.Image, error) { ) error {
img, err := getImg( black, err := getBlack(
ctx, ctx,
time.Now, time.Now,
transportsClient, transportsClient,
@ -81,12 +71,7 @@ func loop(
weatherClient, weatherClient,
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("getting black: %w", err) return 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() {
@ -95,23 +80,23 @@ func loop(
} }
}() }()
err = initDisplay(display, initFastThreshold) err = initDisplay(display)
if err != nil { if err != nil {
return nil, fmt.Errorf("initializing display: %w", err) return fmt.Errorf("initializing display: %w", err)
} }
display.Clear() display.Clear()
display.Send(img) display.Send(black)
display.Refresh() display.Refresh()
return img, nil return nil
} }
const filename = "/perm/display-epaper-lastFullRefresh" const filename = "/perm/display-epaper-lastFullRefresh"
func initDisplay(display *epd.EPD, threshold time.Duration) error { func initDisplay(display *epd.EPD) error {
if canInitFast(threshold) { if canInitFast() {
err := display.InitFast() err := display.InitFast()
if err != nil { if err != nil {
return fmt.Errorf("running fast init: %w", err) return fmt.Errorf("running fast init: %w", err)
@ -129,13 +114,13 @@ func initDisplay(display *epd.EPD, threshold time.Duration) error {
return nil return nil
} }
func canInitFast(threshold time.Duration) bool { func canInitFast() bool {
stat, err := os.Stat(filename) stat, err := os.Stat(filename)
if err != nil { if err != nil {
return false return false
} }
return stat.ModTime().Add(threshold).After(time.Now()) return stat.ModTime().Add(12 * time.Hour).After(time.Now())
} }
func markInitFull() { func markInitFull() {
@ -146,21 +131,3 @@ 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
}