From 8be7b20062986ca1ca7f97bf51ad80e2c6d12599 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 15 Sep 2024 09:01:13 +0200 Subject: [PATCH] split files --- .gitignore | 130 ++++++++++++++++++++++++++++++++++++++++ epd/epd.go | 47 +++++---------- font.go | 21 +++++++ img.go | 72 ++++++++++++++++++++++ main.go | 141 +------------------------------------------- run_darwin_arm64.go | 24 ++++++++ run_linux_arm64.go | 47 +++++++++++++++ 7 files changed, 311 insertions(+), 171 deletions(-) create mode 100644 .gitignore create mode 100644 font.go create mode 100644 img.go create mode 100644 run_darwin_arm64.go create mode 100644 run_linux_arm64.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec96d29 --- /dev/null +++ b/.gitignore @@ -0,0 +1,130 @@ +### 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 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Go template +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +### macOS template +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + diff --git a/epd/epd.go b/epd/epd.go index 35a3bce..3fb2d62 100644 --- a/epd/epd.go +++ b/epd/epd.go @@ -74,7 +74,7 @@ func New() (*EPD, error) { return epd, nil } -func (e *EPD) TurnOff() error { +func (e *EPD) turnOff() error { log.Println("turning off...") if err := e.spiReg.Close(); err != nil { return fmt.Errorf("closing SPI: %w", err) @@ -220,26 +220,12 @@ func (e *EPD) InitFast() { func (e *EPD) Clear() { log.Println("clearing epd") - redBuf := make([]byte, Width*Height/8) - for i := range redBuf { - redBuf[i] = 0x00 - } + e.Fill(White) - blackBuf := make([]byte, Width*Height/8) - for i := range blackBuf { - blackBuf[i] = 0xff - } - - e.sendCommand(0x10) - e.sendDataSlice(blackBuf) - - e.sendCommand(0x13) - e.sendDataSlice(redBuf) - - //e.refresh() + //e.Refresh() } -func (e *EPD) refresh() { +func (e *EPD) Refresh() { log.Println("refreshing...") e.sendCommand(0x12) time.Sleep(100 * time.Millisecond) @@ -255,7 +241,7 @@ func (e *EPD) Sleep() error { e.sendData(0xa5) time.Sleep(2 * time.Second) - if err := e.TurnOff(); err != nil { + if err := e.turnOff(); err != nil { return fmt.Errorf("turning off: %w", err) } @@ -271,19 +257,19 @@ const ( ) func (e *EPD) Fill(c Color) { - log.Println("filling... (not doing anything yet)") + log.Println("filling...") - //switch c { - //case White: - // e.Draw(nil, nil) - //case Black: - // e.Draw(image.Black, nil) - //case Red: - // e.Draw(nil, image.Black) - //} + 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) Draw(black image.Image, red image.Image) { +func (e *EPD) Send(black image.Image, red image.Image) { log.Println("drawing...") if black != nil { log.Println("sending black") @@ -295,9 +281,6 @@ func (e *EPD) Draw(black image.Image, red image.Image) { e.sendCommand(0x13) // write red data e.sendImg(red) } - if black != nil || red != nil { - e.refresh() - } } func (e *EPD) sendImg(img image.Image) { diff --git a/font.go b/font.go new file mode 100644 index 0000000..8d31304 --- /dev/null +++ b/font.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "github.com/golang/freetype/truetype" + "github.com/llgcode/draw2d" +) + +type MyFontCache map[string]*truetype.Font + +func (fc MyFontCache) Store(fd draw2d.FontData, font *truetype.Font) { + fc[fd.Name] = font +} + +func (fc MyFontCache) Load(fd draw2d.FontData) (*truetype.Font, error) { + font, stored := fc[fd.Name] + if !stored { + return nil, fmt.Errorf("font %s is not stored in font cache", fd.Name) + } + return font, nil +} diff --git a/img.go b/img.go new file mode 100644 index 0000000..66340e2 --- /dev/null +++ b/img.go @@ -0,0 +1,72 @@ +package main + +import ( + "github.com/Crocmagnon/display-epaper/epd" + "github.com/llgcode/draw2d" + "github.com/llgcode/draw2d/draw2dimg" + "image" + "image/color" +) + +func getBlack() (*image.RGBA, error) { + img := newWhite() + + gc := draw2dimg.NewGraphicContext(img) + + gc.SetFillColor(color.RGBA{0, 0, 0, 255}) + gc.SetStrokeColor(color.RGBA{0, 0, 0, 255}) + gc.SetLineWidth(2) + + text(gc, "Hello, world", 18, 110, 50) + + return img, nil +} + +func getRed() (*image.RGBA, error) { + img := newBlack() + gc := draw2dimg.NewGraphicContext(img) + gc.SetFillColor(color.RGBA{0, 0, 0, 255}) + gc.SetStrokeColor(color.RGBA{255, 255, 255, 255}) + gc.SetLineWidth(2) + + rect(gc, 10, 10, 50, 50) + rect(gc, 60, 10, 100, 50) + + return img, nil +} + +func text(gc *draw2dimg.GraphicContext, s string, size, x, y float64) { + gc.SetFontData(draw2d.FontData{Name: fontName}) + gc.SetFontSize(size) + gc.StrokeStringAt(s, x, y) +} + +func newWhite() *image.RGBA { + img := image.NewRGBA(image.Rect(0, 0, epd.Width, epd.Height)) + for y := 0; y < epd.Height; y++ { + for x := 0; x < epd.Width; x++ { + img.Set(x, y, color.White) + } + } + return img +} + +func newBlack() *image.RGBA { + img := image.NewRGBA(image.Rect(0, 0, epd.Width, epd.Height)) + for y := 0; y < epd.Height; y++ { + for x := 0; x < epd.Width; x++ { + img.Set(x, y, color.Black) + } + } + return img +} + +func rect(gc *draw2dimg.GraphicContext, x1, y1, x2, y2 float64) { + gc.BeginPath() + gc.MoveTo(x1, y1) + gc.LineTo(x2, y1) + gc.LineTo(x2, y2) + gc.LineTo(x1, y2) + gc.Close() + gc.FillStroke() +} diff --git a/main.go b/main.go index 9784292..881fe67 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,11 @@ package main import ( - "fmt" - "github.com/Crocmagnon/display-epaper/epd" "github.com/golang/freetype/truetype" "github.com/llgcode/draw2d" - "github.com/llgcode/draw2d/draw2dimg" _ "golang.org/x/image/bmp" "golang.org/x/image/font/gofont/goregular" - "image" - "image/color" "log" - "os" - "periph.io/x/host/v3" ) const fontName = "default" @@ -28,139 +21,9 @@ func main() { fontCache.Store(draw2d.FontData{Name: fontName}, font) draw2d.SetFontCache(fontCache) - if len(os.Args) < 2 { - if err := run(); err != nil { - log.Fatal("error: ", err) - } - } else { - img, err := getBlack() - if err != nil { - log.Fatal(err) - } - draw2dimg.SaveToPngFile("black.png", img) - - img, err = getRed() - if err != nil { - log.Fatal(err) - } - draw2dimg.SaveToPngFile("red.png", img) - + if err := run(); err != nil { + log.Fatal("error: ", err) } log.Println("done") } - -func run() error { - _, err := host.Init() - if err != nil { - return fmt.Errorf("initializing host: %w", err) - } - - display, err := epd.New() - if err != nil { - return fmt.Errorf("initializing epd: %w", err) - } - - //display.Init() - display.InitFast() - display.Clear() - - black, err := getBlack() - if err != nil { - return fmt.Errorf("getting black: %w", err) - } - - red, err := getRed() - if err != nil { - return fmt.Errorf("getting red: %w", err) - } - - display.Draw(black, red) - - log.Println("sleeping...") - - if err := display.Sleep(); err != nil { - return fmt.Errorf("sleeping: %w", err) - } - - log.Println("done") - - return nil -} - -func getBlack() (*image.RGBA, error) { - img := newWhite() - - gc := draw2dimg.NewGraphicContext(img) - - gc.SetFillColor(color.RGBA{0, 0, 0, 255}) - gc.SetStrokeColor(color.RGBA{0, 0, 0, 255}) - gc.SetLineWidth(2) - - text(gc, "Hello, world", 18, 110, 50) - - return img, nil -} - -func getRed() (*image.RGBA, error) { - img := newBlack() - gc := draw2dimg.NewGraphicContext(img) - gc.SetFillColor(color.RGBA{0, 0, 0, 255}) - gc.SetStrokeColor(color.RGBA{255, 255, 255, 255}) - gc.SetLineWidth(2) - - rect(gc, 10, 10, 50, 50) - rect(gc, 60, 10, 100, 50) - - return img, nil -} - -func text(gc *draw2dimg.GraphicContext, s string, size, x, y float64) { - gc.SetFontData(draw2d.FontData{Name: fontName}) - gc.SetFontSize(size) - gc.StrokeStringAt(s, x, y) -} - -func newWhite() *image.RGBA { - img := image.NewRGBA(image.Rect(0, 0, epd.Width, epd.Height)) - for y := 0; y < epd.Height; y++ { - for x := 0; x < epd.Width; x++ { - img.Set(x, y, color.White) - } - } - return img -} - -func newBlack() *image.RGBA { - img := image.NewRGBA(image.Rect(0, 0, epd.Width, epd.Height)) - for y := 0; y < epd.Height; y++ { - for x := 0; x < epd.Width; x++ { - img.Set(x, y, color.Black) - } - } - return img -} - -func rect(gc *draw2dimg.GraphicContext, x1, y1, x2, y2 float64) { - gc.BeginPath() - gc.MoveTo(x1, y1) - gc.LineTo(x2, y1) - gc.LineTo(x2, y2) - gc.LineTo(x1, y2) - gc.Close() - gc.FillStroke() -} - -type MyFontCache map[string]*truetype.Font - -func (fc MyFontCache) Store(fd draw2d.FontData, font *truetype.Font) { - fc[fd.Name] = font -} - -func (fc MyFontCache) Load(fd draw2d.FontData) (*truetype.Font, error) { - font, stored := fc[fd.Name] - if !stored { - return nil, fmt.Errorf("font %s is not stored in font cache", fd.Name) - } - return font, nil -} diff --git a/run_darwin_arm64.go b/run_darwin_arm64.go new file mode 100644 index 0000000..111d89f --- /dev/null +++ b/run_darwin_arm64.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/llgcode/draw2d/draw2dimg" + "log" +) + +func run() error { + img, err := getBlack() + if err != nil { + log.Fatal(err) + } + draw2dimg.SaveToPngFile("out/black.png", img) + + img, err = getRed() + if err != nil { + log.Fatal(err) + } + draw2dimg.SaveToPngFile("out/red.png", img) + + log.Println("done") + + return nil +} diff --git a/run_linux_arm64.go b/run_linux_arm64.go new file mode 100644 index 0000000..1b56a1a --- /dev/null +++ b/run_linux_arm64.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "github.com/Crocmagnon/display-epaper/epd" + "log" + "periph.io/x/host/v3" +) + +func run() error { + _, err := host.Init() + if err != nil { + return fmt.Errorf("initializing host: %w", err) + } + + display, err := epd.New() + if err != nil { + return fmt.Errorf("initializing epd: %w", err) + } + + display.Init() + //display.InitFast() + display.Clear() + display.Refresh() + + black, err := getBlack() + if err != nil { + return fmt.Errorf("getting black: %w", err) + } + + red, err := getRed() + if err != nil { + return fmt.Errorf("getting red: %w", err) + } + + display.Send(black, red) + + log.Println("sleeping...") + + if err := display.Sleep(); err != nil { + return fmt.Errorf("sleeping: %w", err) + } + + log.Println("done") + + return nil +}