mirror of
https://github.com/Crocmagnon/display-epaper.git
synced 2024-11-24 06:58:03 +01:00
Compare commits
2 commits
c6ae3f97ef
...
df0ccc536d
Author | SHA1 | Date | |
---|---|---|---|
df0ccc536d | |||
2fa57a73a1 |
4 changed files with 64 additions and 16 deletions
2
img.go
2
img.go
|
@ -29,7 +29,7 @@ const (
|
|||
rightX = 530
|
||||
)
|
||||
|
||||
func getBlack(
|
||||
func getImg(
|
||||
ctx context.Context,
|
||||
nowFunc func() time.Time,
|
||||
transportsClient *transports.Client,
|
||||
|
|
18
main.go
18
main.go
|
@ -41,16 +41,30 @@ func main() {
|
|||
CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"),
|
||||
})
|
||||
|
||||
const minSleep = 30 * time.Second
|
||||
const minSleep = 1 * time.Second
|
||||
|
||||
sleep, err := time.ParseDuration(os.Getenv("SLEEP_DURATION"))
|
||||
if err != nil || 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)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
|
@ -13,11 +13,12 @@ import (
|
|||
func run(
|
||||
ctx context.Context,
|
||||
_ time.Duration,
|
||||
_ time.Duration,
|
||||
transportsClient *transports.Client,
|
||||
feteClient *fete.Client,
|
||||
weatherClient *weather.Client,
|
||||
) error {
|
||||
img, err := getBlack(
|
||||
img, err := getImg(
|
||||
ctx,
|
||||
func() time.Time {
|
||||
t, err := time.Parse(time.DateOnly, "2024-08-01zzz")
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/Crocmagnon/display-epaper/fete"
|
||||
"github.com/Crocmagnon/display-epaper/transports"
|
||||
"github.com/Crocmagnon/display-epaper/weather"
|
||||
"image"
|
||||
"log"
|
||||
"os"
|
||||
"periph.io/x/host/v3"
|
||||
|
@ -16,6 +17,7 @@ import (
|
|||
func run(
|
||||
ctx context.Context,
|
||||
sleep time.Duration,
|
||||
initFastThreshold time.Duration,
|
||||
transportsClient *transports.Client,
|
||||
feteClient *fete.Client,
|
||||
weatherClient *weather.Client,
|
||||
|
@ -30,6 +32,8 @@ func run(
|
|||
return fmt.Errorf("initializing epd: %w", err)
|
||||
}
|
||||
|
||||
var currentImg image.Image
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
@ -40,9 +44,11 @@ func run(
|
|||
|
||||
log.Println("running loop")
|
||||
|
||||
err = loop(
|
||||
img, err := loop(
|
||||
ctx,
|
||||
display,
|
||||
initFastThreshold,
|
||||
currentImg,
|
||||
transportsClient,
|
||||
feteClient,
|
||||
weatherClient,
|
||||
|
@ -51,6 +57,8 @@ func run(
|
|||
log.Printf("error looping: %v\n", err)
|
||||
}
|
||||
|
||||
currentImg = img
|
||||
|
||||
log.Printf("time.Sleep(%v)\n", sleep)
|
||||
time.Sleep(sleep)
|
||||
}
|
||||
|
@ -59,11 +67,13 @@ func run(
|
|||
func loop(
|
||||
ctx context.Context,
|
||||
display *epd.EPD,
|
||||
initFastThreshold time.Duration,
|
||||
currentImg image.Image,
|
||||
transportsClient *transports.Client,
|
||||
feteClient *fete.Client,
|
||||
weatherClient *weather.Client,
|
||||
) error {
|
||||
black, err := getBlack(
|
||||
) (image.Image, error) {
|
||||
img, err := getImg(
|
||||
ctx,
|
||||
time.Now,
|
||||
transportsClient,
|
||||
|
@ -71,7 +81,12 @@ func loop(
|
|||
weatherClient,
|
||||
)
|
||||
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() {
|
||||
|
@ -80,23 +95,23 @@ func loop(
|
|||
}
|
||||
}()
|
||||
|
||||
err = initDisplay(display)
|
||||
err = initDisplay(display, initFastThreshold)
|
||||
if err != nil {
|
||||
return fmt.Errorf("initializing display: %w", err)
|
||||
return nil, fmt.Errorf("initializing display: %w", err)
|
||||
}
|
||||
|
||||
display.Clear()
|
||||
|
||||
display.Send(black)
|
||||
display.Send(img)
|
||||
display.Refresh()
|
||||
|
||||
return nil
|
||||
return img, nil
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -114,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() {
|
||||
|
@ -131,3 +146,21 @@ func markInitFull() {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue