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
# 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

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() {
e.resetPin.Out(gpio.High)
time.Sleep(200 * time.Millisecond)
time.Sleep(20 * time.Millisecond)
e.resetPin.Out(gpio.Low)
time.Sleep(4 * time.Millisecond)
time.Sleep(2 * time.Millisecond)
e.resetPin.Out(gpio.High)
time.Sleep(200 * time.Millisecond)
time.Sleep(20 * time.Millisecond)
}
func (e *EPD) sendCommand(cmd byte) {
@ -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) {
@ -114,13 +112,11 @@ func (e *EPD) spiWrite(write []byte) ([]byte, error) {
func (e *EPD) readBusy() {
e.sendCommand(0x71)
busy := e.busyPin.Read()
for busy == gpio.Low {
for e.busyPin.Read() == gpio.Low {
time.Sleep(20 * time.Millisecond)
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 {
@ -163,18 +159,18 @@ func (e *EPD) Init() error {
e.reset()
e.sendCommand(0x01)
e.sendDataSlice([]byte{0x07, 0x07, 0x3f, 0x3f})
e.sendCommand(0x06)
e.sendDataSlice([]byte{0x17, 0x17, 0x28, 0x17})
e.sendCommand(0x01)
e.sendDataSlice([]byte{0x07, 0x07, 0x28, 0x17})
e.sendCommand(0x04)
time.Sleep(100 * time.Millisecond)
e.readBusy()
e.sendCommand(0x00)
e.sendData(0x0f)
e.sendData(0x1f)
e.sendCommand(0x61)
e.sendDataSlice([]byte{0x03, 0x20, 0x01, 0xe0})
@ -183,7 +179,7 @@ func (e *EPD) Init() error {
e.sendData(0x00)
e.sendCommand(0x50)
e.sendDataSlice([]byte{0x11, 0x07})
e.sendDataSlice([]byte{0x10, 0x07})
e.sendCommand(0x60)
e.sendData(0x22)
@ -201,7 +197,10 @@ func (e *EPD) InitFast() error {
e.reset()
e.sendCommand(0x00)
e.sendData(0x0f)
e.sendData(0x1f)
e.sendCommand(0x50)
e.sendDataSlice([]byte{0x10, 0x07})
e.sendCommand(0x04)
time.Sleep(100 * time.Millisecond)
@ -216,15 +215,12 @@ func (e *EPD) InitFast() error {
e.sendCommand(0xe5)
e.sendData(0x5a)
e.sendCommand(0x50)
e.sendDataSlice([]byte{0x11, 0x07})
return nil
}
func (e *EPD) Clear() {
log.Println("clearing epd")
e.Fill(White)
e.Send(image.White)
}
func (e *EPD) Refresh() {
@ -263,43 +259,15 @@ func (e *EPD) turnOff() error {
return nil
}
type Color int
const (
White Color = iota
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(img image.Image) {
if img == nil {
log.Println("empty img")
return
}
}
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...")
// 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 col := 0; col < e.width; col += 8 {
// this loop converts individual pixels into a single byte
@ -311,9 +279,16 @@ func (e *EPD) sendImg(img image.Image) {
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 {

100
img.go
View file

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

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(10m)")
time.Sleep(10 * time.Minute)
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)
}
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()
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()
}