mirror of
https://github.com/Crocmagnon/display-epaper.git
synced 2024-11-25 07:28:02 +01:00
Compare commits
No commits in common. "c6ae3f97ef8276eb9a83f75210a621c0995705ac" and "6206898fb127a464aa191baf67075cf819f546f5" have entirely different histories.
c6ae3f97ef
...
6206898fb1
7 changed files with 94 additions and 199 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,5 +1,3 @@
|
|||
.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
9
Makefile
|
@ -1,9 +0,0 @@
|
|||
GOK := gok -i display
|
||||
run:
|
||||
${GOK} run
|
||||
update:
|
||||
${GOK} update
|
||||
logs:
|
||||
${GOK} logs -s display-epaper
|
||||
get:
|
||||
${GOK} get --update_all
|
87
epd/epd.go
87
epd/epd.go
|
@ -50,11 +50,11 @@ func New() (*EPD, error) {
|
|||
|
||||
func (e *EPD) reset() {
|
||||
e.resetPin.Out(gpio.High)
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
e.resetPin.Out(gpio.Low)
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
time.Sleep(4 * time.Millisecond)
|
||||
e.resetPin.Out(gpio.High)
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
|
||||
func (e *EPD) sendCommand(cmd byte) {
|
||||
|
@ -96,8 +96,10 @@ 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) {
|
||||
|
@ -112,11 +114,13 @@ func (e *EPD) spiWrite(write []byte) ([]byte, error) {
|
|||
|
||||
func (e *EPD) readBusy() {
|
||||
e.sendCommand(0x71)
|
||||
for e.busyPin.Read() == gpio.Low {
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
busy := e.busyPin.Read()
|
||||
for busy == gpio.Low {
|
||||
e.sendCommand(0x71)
|
||||
busy = e.busyPin.Read()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
|
||||
func (e *EPD) turnOn() error {
|
||||
|
@ -159,18 +163,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(0x1f)
|
||||
e.sendData(0x0f)
|
||||
|
||||
e.sendCommand(0x61)
|
||||
e.sendDataSlice([]byte{0x03, 0x20, 0x01, 0xe0})
|
||||
|
@ -179,7 +183,7 @@ func (e *EPD) Init() error {
|
|||
e.sendData(0x00)
|
||||
|
||||
e.sendCommand(0x50)
|
||||
e.sendDataSlice([]byte{0x10, 0x07})
|
||||
e.sendDataSlice([]byte{0x11, 0x07})
|
||||
|
||||
e.sendCommand(0x60)
|
||||
e.sendData(0x22)
|
||||
|
@ -197,10 +201,7 @@ func (e *EPD) InitFast() error {
|
|||
e.reset()
|
||||
|
||||
e.sendCommand(0x00)
|
||||
e.sendData(0x1f)
|
||||
|
||||
e.sendCommand(0x50)
|
||||
e.sendDataSlice([]byte{0x10, 0x07})
|
||||
e.sendData(0x0f)
|
||||
|
||||
e.sendCommand(0x04)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
@ -215,12 +216,15 @@ 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.Send(image.White)
|
||||
e.Fill(White)
|
||||
}
|
||||
|
||||
func (e *EPD) Refresh() {
|
||||
|
@ -259,15 +263,43 @@ func (e *EPD) turnOff() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (e *EPD) Send(img image.Image) {
|
||||
if img == nil {
|
||||
log.Println("empty img")
|
||||
return
|
||||
}
|
||||
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(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...")
|
||||
toSend := make([]byte, 0, e.height*e.width/8)
|
||||
toSend2 := make([]byte, 0, e.height*e.width/8)
|
||||
// TODO check img size
|
||||
for row := 0; row < e.height; row++ {
|
||||
for col := 0; col < e.width; col += 8 {
|
||||
// this loop converts individual pixels into a single byte
|
||||
|
@ -279,16 +311,9 @@ func (e *EPD) Send(img image.Image) {
|
|||
b &= ^(0x80 >> (px % 8))
|
||||
}
|
||||
}
|
||||
toSend = append(toSend, ^b)
|
||||
toSend2 = append(toSend2, b)
|
||||
e.sendData(b)
|
||||
}
|
||||
}
|
||||
|
||||
e.sendCommand(0x10)
|
||||
e.sendDataSlice(toSend2)
|
||||
|
||||
e.sendCommand(0x13)
|
||||
e.sendDataSlice(toSend)
|
||||
}
|
||||
|
||||
func isdark(r, g, b, _ uint32) bool {
|
||||
|
|
70
img.go
70
img.go
|
@ -17,7 +17,6 @@ import (
|
|||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -36,83 +35,26 @@ func getBlack(
|
|||
feteClient *fete.Client,
|
||||
weatherClient *weather.Client,
|
||||
) (*image.RGBA, error) {
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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())
|
||||
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)
|
||||
wthr, err := weatherClient.GetWeather(ctx)
|
||||
if err != nil {
|
||||
log.Println("error getting weather:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
quote := quotes.GetQuote(nowFunc())
|
||||
|
||||
img := newWhite()
|
||||
|
@ -123,8 +65,6 @@ 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
12
main.go
|
@ -11,7 +11,6 @@ import (
|
|||
"golang.org/x/image/font/gofont/goregular"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
const fontName = "default"
|
||||
|
@ -41,16 +40,7 @@ func main() {
|
|||
CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"),
|
||||
})
|
||||
|
||||
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 {
|
||||
if err := run(ctx, transportsClient, feteClient, weatherClient); err != nil {
|
||||
log.Fatal("error: ", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@ import (
|
|||
|
||||
func run(
|
||||
ctx context.Context,
|
||||
_ time.Duration,
|
||||
transportsClient *transports.Client,
|
||||
feteClient *fete.Client,
|
||||
weatherClient *weather.Client,
|
||||
|
|
|
@ -8,18 +8,11 @@ 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,
|
||||
sleep time.Duration,
|
||||
transportsClient *transports.Client,
|
||||
feteClient *fete.Client,
|
||||
weatherClient *weather.Client,
|
||||
) error {
|
||||
func run(ctx context.Context, transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client) error {
|
||||
_, err := host.Init()
|
||||
if err != nil {
|
||||
return fmt.Errorf("initializing host: %w", err)
|
||||
|
@ -38,8 +31,6 @@ func run(
|
|||
default:
|
||||
}
|
||||
|
||||
log.Println("running loop")
|
||||
|
||||
err = loop(
|
||||
ctx,
|
||||
display,
|
||||
|
@ -51,8 +42,8 @@ func run(
|
|||
log.Printf("error looping: %v\n", err)
|
||||
}
|
||||
|
||||
log.Printf("time.Sleep(%v)\n", sleep)
|
||||
time.Sleep(sleep)
|
||||
log.Println("time.Sleep(10m)")
|
||||
time.Sleep(10 * time.Minute)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,6 +54,19 @@ 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,
|
||||
|
@ -74,60 +78,8 @@ 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.Send(black, nil)
|
||||
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()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue