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)
}
e.csPin.Out(gpio.High)
log.Printf("sent chunk %v\n", cursor)
cursor = min(cursor+maxSize, toSend)
}
log.Printf("sent chunk %v\n", cursor)
}
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"
"log"
"os"
"time"
)
const fontName = "default"
@ -40,7 +41,16 @@ func main() {
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)
}

View file

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

View file

@ -8,11 +8,18 @@ import (
"github.com/Crocmagnon/display-epaper/transports"
"github.com/Crocmagnon/display-epaper/weather"
"log"
"os"
"periph.io/x/host/v3"
"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()
if err != nil {
return fmt.Errorf("initializing host: %w", err)
@ -31,6 +38,8 @@ func run(ctx context.Context, transportsClient *transports.Client, feteClient *f
default:
}
log.Println("running loop")
err = loop(
ctx,
display,
@ -42,8 +51,8 @@ func run(ctx context.Context, transportsClient *transports.Client, feteClient *f
log.Printf("error looping: %v\n", err)
}
log.Println("time.Sleep(30s)")
time.Sleep(30 * time.Second)
log.Printf("time.Sleep(%v)\n", sleep)
time.Sleep(sleep)
}
}
@ -54,19 +63,6 @@ func loop(
feteClient *fete.Client,
weatherClient *weather.Client,
) 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(
ctx,
time.Now,
@ -78,8 +74,60 @@ func loop(
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.Refresh()
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()
}