Compare commits

..

6 commits

7 changed files with 198 additions and 93 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
.idea
### JetBrains template ### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

9
Makefile Normal file
View file

@ -0,0 +1,9 @@
GOK := gok -i display
run:
${GOK} run
update:
${GOK} update
logs:
${GOK} logs -s display-epaper
get:
${GOK} get --update_all

View file

@ -50,11 +50,11 @@ func New() (*EPD, error) {
func (e *EPD) reset() { func (e *EPD) reset() {
e.resetPin.Out(gpio.High) e.resetPin.Out(gpio.High)
time.Sleep(200 * time.Millisecond) time.Sleep(20 * time.Millisecond)
e.resetPin.Out(gpio.Low) e.resetPin.Out(gpio.Low)
time.Sleep(4 * time.Millisecond) time.Sleep(2 * time.Millisecond)
e.resetPin.Out(gpio.High) e.resetPin.Out(gpio.High)
time.Sleep(200 * time.Millisecond) time.Sleep(20 * time.Millisecond)
} }
func (e *EPD) sendCommand(cmd byte) { func (e *EPD) sendCommand(cmd byte) {
@ -96,10 +96,8 @@ func (e *EPD) sendDataSlice(data []byte) {
log.Fatalf("writing to spi: %v", err) log.Fatalf("writing to spi: %v", err)
} }
e.csPin.Out(gpio.High) e.csPin.Out(gpio.High)
log.Printf("sent chunk %v\n", cursor)
cursor = min(cursor+maxSize, toSend) cursor = min(cursor+maxSize, toSend)
} }
log.Printf("sent chunk %v\n", cursor)
} }
func (e *EPD) spiWrite(write []byte) ([]byte, error) { func (e *EPD) spiWrite(write []byte) ([]byte, error) {
@ -114,13 +112,11 @@ func (e *EPD) spiWrite(write []byte) ([]byte, error) {
func (e *EPD) readBusy() { func (e *EPD) readBusy() {
e.sendCommand(0x71) e.sendCommand(0x71)
busy := e.busyPin.Read() for e.busyPin.Read() == gpio.Low {
for busy == gpio.Low { time.Sleep(20 * time.Millisecond)
e.sendCommand(0x71) e.sendCommand(0x71)
busy = e.busyPin.Read()
time.Sleep(200 * time.Millisecond)
} }
time.Sleep(200 * time.Millisecond) time.Sleep(20 * time.Millisecond)
} }
func (e *EPD) turnOn() error { func (e *EPD) turnOn() error {
@ -163,18 +159,18 @@ func (e *EPD) Init() error {
e.reset() e.reset()
e.sendCommand(0x01)
e.sendDataSlice([]byte{0x07, 0x07, 0x3f, 0x3f})
e.sendCommand(0x06) e.sendCommand(0x06)
e.sendDataSlice([]byte{0x17, 0x17, 0x28, 0x17}) e.sendDataSlice([]byte{0x17, 0x17, 0x28, 0x17})
e.sendCommand(0x01)
e.sendDataSlice([]byte{0x07, 0x07, 0x28, 0x17})
e.sendCommand(0x04) e.sendCommand(0x04)
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
e.readBusy() e.readBusy()
e.sendCommand(0x00) e.sendCommand(0x00)
e.sendData(0x0f) e.sendData(0x1f)
e.sendCommand(0x61) e.sendCommand(0x61)
e.sendDataSlice([]byte{0x03, 0x20, 0x01, 0xe0}) e.sendDataSlice([]byte{0x03, 0x20, 0x01, 0xe0})
@ -183,7 +179,7 @@ func (e *EPD) Init() error {
e.sendData(0x00) e.sendData(0x00)
e.sendCommand(0x50) e.sendCommand(0x50)
e.sendDataSlice([]byte{0x11, 0x07}) e.sendDataSlice([]byte{0x10, 0x07})
e.sendCommand(0x60) e.sendCommand(0x60)
e.sendData(0x22) e.sendData(0x22)
@ -201,7 +197,10 @@ func (e *EPD) InitFast() error {
e.reset() e.reset()
e.sendCommand(0x00) e.sendCommand(0x00)
e.sendData(0x0f) e.sendData(0x1f)
e.sendCommand(0x50)
e.sendDataSlice([]byte{0x10, 0x07})
e.sendCommand(0x04) e.sendCommand(0x04)
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
@ -216,15 +215,12 @@ func (e *EPD) InitFast() error {
e.sendCommand(0xe5) e.sendCommand(0xe5)
e.sendData(0x5a) e.sendData(0x5a)
e.sendCommand(0x50)
e.sendDataSlice([]byte{0x11, 0x07})
return nil return nil
} }
func (e *EPD) Clear() { func (e *EPD) Clear() {
log.Println("clearing epd") log.Println("clearing epd")
e.Fill(White) e.Send(image.White)
} }
func (e *EPD) Refresh() { func (e *EPD) Refresh() {
@ -263,43 +259,15 @@ func (e *EPD) turnOff() error {
return nil return nil
} }
type Color int func (e *EPD) Send(img image.Image) {
if img == nil {
const ( log.Println("empty img")
White Color = iota return
Red
Black
)
func (e *EPD) Fill(c Color) {
log.Println("filling...")
switch c {
case White:
e.Send(image.White, image.Black)
case Black:
e.Send(image.Black, image.Black)
case Red:
e.Send(image.White, image.White)
} }
}
func (e *EPD) Send(black image.Image, red image.Image) {
if black != nil {
log.Println("sending black")
e.sendCommand(0x10) // write bw data
e.sendImg(black)
}
if red != nil {
log.Println("sending red")
e.sendCommand(0x13) // write red data
e.sendImg(red)
}
}
func (e *EPD) sendImg(img image.Image) {
log.Println("sending img...") log.Println("sending img...")
// TODO check img size toSend := make([]byte, 0, e.height*e.width/8)
toSend2 := make([]byte, 0, e.height*e.width/8)
for row := 0; row < e.height; row++ { for row := 0; row < e.height; row++ {
for col := 0; col < e.width; col += 8 { for col := 0; col < e.width; col += 8 {
// this loop converts individual pixels into a single byte // this loop converts individual pixels into a single byte
@ -311,9 +279,16 @@ func (e *EPD) sendImg(img image.Image) {
b &= ^(0x80 >> (px % 8)) b &= ^(0x80 >> (px % 8))
} }
} }
e.sendData(b) toSend = append(toSend, ^b)
toSend2 = append(toSend2, b)
} }
} }
e.sendCommand(0x10)
e.sendDataSlice(toSend2)
e.sendCommand(0x13)
e.sendDataSlice(toSend)
} }
func isdark(r, g, b, _ uint32) bool { func isdark(r, g, b, _ uint32) bool {

100
img.go
View file

@ -17,6 +17,7 @@ import (
"math" "math"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
) )
@ -35,26 +36,83 @@ func getBlack(
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,
) (*image.RGBA, error) { ) (*image.RGBA, error) {
bus, err := transportsClient.GetTCLPassages(ctx, 290) var (
if err != nil { bus *transports.Passages
log.Println("error getting bus:", err) tram *transports.Passages
} velovRoc *transports.Station
tram, err := transportsClient.GetTCLPassages(ctx, 34068) fetes *fete.Fete
if err != nil { wthr *weather.Prevision
log.Println("error getting tram:", err) )
}
velovRoc, err := transportsClient.GetVelovStation(ctx, 10044) wg := &sync.WaitGroup{}
if err != nil { wg.Add(5)
log.Println("error getting velov:", err)
} go func() {
fetes, err := feteClient.GetFete(ctx, nowFunc()) defer wg.Done()
if err != nil {
log.Println("error getting fetes:", err) ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
} defer cancel()
wthr, err := weatherClient.GetWeather(ctx)
if err != nil { var err error
log.Println("error getting weather:", err)
} bus, err = transportsClient.GetTCLPassages(ctx, 290)
if err != nil {
log.Println("error getting bus:", err)
}
}()
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var err error
tram, err = transportsClient.GetTCLPassages(ctx, 34068)
if err != nil {
log.Println("error getting tram:", err)
}
}()
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var err error
velovRoc, err = transportsClient.GetVelovStation(ctx, 10044)
if err != nil {
log.Println("error getting velov:", err)
}
}()
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var err error
fetes, err = feteClient.GetFete(ctx, nowFunc())
if err != nil {
log.Println("error getting fetes:", err)
}
}()
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var err error
wthr, err = weatherClient.GetWeather(ctx)
if err != nil {
log.Println("error getting weather:", err)
}
}()
quote := quotes.GetQuote(nowFunc()) quote := quotes.GetQuote(nowFunc())
img := newWhite() img := newWhite()
@ -65,6 +123,8 @@ func getBlack(
gc.SetFillColor(color.RGBA{255, 255, 255, 255}) gc.SetFillColor(color.RGBA{255, 255, 255, 255})
gc.SetStrokeColor(color.RGBA{0, 0, 0, 255}) gc.SetStrokeColor(color.RGBA{0, 0, 0, 255})
wg.Wait()
drawTCL(gc, bus, 55) drawTCL(gc, bus, 55)
drawTCL(gc, tram, 190) drawTCL(gc, tram, 190)
drawVelov(gc, velovRoc, 350) drawVelov(gc, velovRoc, 350)

12
main.go
View file

@ -11,6 +11,7 @@ import (
"golang.org/x/image/font/gofont/goregular" "golang.org/x/image/font/gofont/goregular"
"log" "log"
"os" "os"
"time"
) )
const fontName = "default" const fontName = "default"
@ -40,7 +41,16 @@ func main() {
CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"), 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) log.Fatal("error: ", err)
} }

View file

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

View file

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