diff --git a/main.go b/main.go index 97453eb..7d15a95 100644 --- a/main.go +++ b/main.go @@ -48,9 +48,23 @@ func main() { 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) - if err := run(ctx, sleep, transportsClient, feteClient, weatherClient); err != nil { + if err := run( + ctx, + sleep, + initFastThreshold, + transportsClient, + feteClient, + weatherClient, + ); err != nil { log.Fatal("error: ", err) } diff --git a/run_darwin_arm64.go b/run_darwin_arm64.go index 1a5e2fc..b764ac0 100644 --- a/run_darwin_arm64.go +++ b/run_darwin_arm64.go @@ -13,6 +13,7 @@ import ( func run( ctx context.Context, _ time.Duration, + _ time.Duration, transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client, diff --git a/run_linux_arm64.go b/run_linux_arm64.go index 25fbbc9..72b69a1 100644 --- a/run_linux_arm64.go +++ b/run_linux_arm64.go @@ -17,6 +17,7 @@ import ( func run( ctx context.Context, sleep time.Duration, + initFastThreshold time.Duration, transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client, @@ -46,6 +47,7 @@ func run( img, err := loop( ctx, display, + initFastThreshold, currentImg, transportsClient, feteClient, @@ -62,7 +64,15 @@ func run( } } -func loop(ctx context.Context, display *epd.EPD, currentImg image.Image, transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client) (image.Image, error) { +func loop( + ctx context.Context, + display *epd.EPD, + initFastThreshold time.Duration, + currentImg image.Image, + transportsClient *transports.Client, + feteClient *fete.Client, + weatherClient *weather.Client, +) (image.Image, error) { img, err := getImg( ctx, time.Now, @@ -85,7 +95,7 @@ func loop(ctx context.Context, display *epd.EPD, currentImg image.Image, transpo } }() - err = initDisplay(display) + err = initDisplay(display, initFastThreshold) if err != nil { return nil, fmt.Errorf("initializing display: %w", err) } @@ -100,8 +110,8 @@ func loop(ctx context.Context, display *epd.EPD, currentImg image.Image, transpo const filename = "/perm/display-epaper-lastFullRefresh" -func initDisplay(display *epd.EPD) error { - if canInitFast() { +func initDisplay(display *epd.EPD, threshold time.Duration) error { + if canInitFast(threshold) { err := display.InitFast() if err != nil { return fmt.Errorf("running fast init: %w", err) @@ -119,13 +129,13 @@ func initDisplay(display *epd.EPD) error { return nil } -func canInitFast() bool { +func canInitFast(threshold time.Duration) bool { stat, err := os.Stat(filename) if err != nil { return false } - return stat.ModTime().Add(12 * time.Hour).After(time.Now()) + return stat.ModTime().Add(threshold).After(time.Now()) } func markInitFull() {