Skip to content

Commit

Permalink
Merge pull request #2 from landgenoot/golang
Browse files Browse the repository at this point in the history
Translation to golang
  • Loading branch information
landgenoot authored Jul 13, 2023
2 parents 075aa59 + 0fb0437 commit c7ca7c6
Show file tree
Hide file tree
Showing 23 changed files with 824 additions and 191 deletions.
43 changes: 24 additions & 19 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ on:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Build the Docker image
run: docker build . --file Dockerfile --tag kindle-compile-toolchain:$(date +%s)

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
compile:
name: Cross compile using landgenoot/kindle-cross-compile-toolchain
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2

- name: Run the build process with Docker
uses: addnab/docker-run-action@v3
with:
image: landgenoot/kindle-cross-compile-toolchain:latest
options: -v ${{ github.workspace }}:/workdir
run: |
cd /workdir
CGO_LDFLAGS="-Wl,-rpath=/mnt/us/linkss/lib" CC="arm-linux-gnueabi-gcc -fopenmp" PKG_CONFIG_LIBDIR=/usr/lib/arm-linux-gnueabi/pkgconfig GOOS=linux GOARCH=arm CGO_ENABLED=1 go build -o photoframe -buildvcs=false
mv assets/linkss linkss
- name: Upload release
uses: actions/upload-artifact@v3
with:
name: kindle-synology-photos-photoframe-${{ github.sha }}
path: |
photoframe
linkss
File renamed without changes
Binary file added assets/linkss/lib/libMagickCore-6.Q8.so.7
Binary file not shown.
Binary file added assets/linkss/lib/libMagickWand-6.Q8.so.7
Binary file not shown.
Binary file added assets/linkss/lib/libjpeg.so.62
Binary file not shown.
Binary file removed bin/convert
Binary file not shown.
Binary file removed bin/ht
Binary file not shown.
Binary file removed bin/next-wakeup
Binary file not shown.
2 changes: 0 additions & 2 deletions cache/.gitignore

This file was deleted.

5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module photoframe

go 1.18

require gopkg.in/gographics/imagick.v3 v3.4.2 // indirect
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/gographics/imagick.v3 v3.4.2 h1:vk6oildvhRBVSBfQ4X3raJstApYSeK6CZsyzoSOZk58=
gopkg.in/gographics/imagick.v3 v3.4.2/go.mod h1:+Q9nyA2xRZXrDyTtJ/eko+8V/5E7bWYs08ndkZp8UmA=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
116 changes: 116 additions & 0 deletions kindle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"fmt"
"log"
"os/exec"
"regexp"
"runtime"
"strconv"
"time"
)

var refreshCount = 0

// Suspend device and use real time clock alarm to wake it up.
// If our wake up time is more or less 24 hours away, we can put it to
// sleep immediately. Otherwise, we will wait another 30 seconds, which enables us
// to abort the process.
func suspendToRam(duration int) {
if runtime.GOARCH != "arm" {
return // Skip if not on Kindle
}
cmd1 := exec.Command("sh", "-c", "echo \"\" > /sys/class/rtc/rtc1/wakealarm")
err1 := cmd1.Run()
if err1 != nil {
log.Fatal(err1)
}
cmd2 := exec.Command("sh", "-c", fmt.Sprintf("echo \"+%d\" > /sys/class/rtc/rtc1/wakealarm", duration))
err2 := cmd2.Run()
if err2 != nil {
log.Fatal(err2)
}

// Check if we are waken up manually, give us time to abort the process
if duration < 3600*24-60 {
log.Println("Waiting 30 seconds before going back to sleep")
time.Sleep(30 * time.Second)
}

log.Println("Suspending to RAM")

cmd3 := exec.Command("sh", "-c", "echo \"mem\" > /sys/power/state")
err3 := cmd3.Run()
if err3 != nil {
log.Fatal(err3)
}
}

func initPowersave() {
exec.Command("sh", "-c", "stop framework").Run()
exec.Command("sh", "-c", "initctl stop webreader >/dev/null 2>&1").Run()
exec.Command("sh", "-c", "echo powersave >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor").Run()
exec.Command("sh", "-c", "lipc-set-prop com.lab126.powerd preventScreenSaver 1").Run()
}

func getBatteryLevel() string {
if runtime.GOARCH != "arm" {
return "100%" // Skip if not on Kindle
}
out, err := exec.Command("/usr/bin/gasgauge-info", "-c").Output()
state := string(out)
if err != nil {
log.Fatal(err)
}
return state
}

// Count seconds till next wake up time. Formatted as clock
// time in 24H format. E.g. 6, 30 means 6:30 AM.
func nextWakeup(now time.Time, hour int, minutes int) int {
yyyy, mm, dd := now.Date()
if now.Hour() > hour || now.Hour() == hour && now.Minute() >= minutes {
dd++ // Jump to tomorrow, if wakeup time has already passed.
}
tomorrow := time.Date(yyyy, mm, dd, hour, minutes, 0, 0, now.Location())
return int(tomorrow.Sub(now).Seconds())
}

func drawToScreen(imagePath string) {
if runtime.GOARCH != "arm" {
return // Skip if not on Kindle
}
flags := "-g"
if refreshCount%4 == 0 {
flags = "-fg" // full refresh after 4 refreshes
}
err := exec.Command("/usr/sbin/eips", flags, imagePath).Run()
if err != nil {
log.Fatal(err)
}
refreshCount++
}

// Draw a small black box in the left bottom corner of the screen
func drawLowBatteryIndicator() {
if runtime.GOARCH != "arm" {
return // Skip if not on Kindle
}
cmd := exec.Command("/usr/sbin/eips", "-d", "l=0,w=50,h=65")
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}

func parseBatteryLevel(state string) (int, error) {
re := regexp.MustCompile(`\d*%`)
value := re.FindString(state)
if value == "" {
err := fmt.Errorf("Could not parse battery level %s", state)
return -1, err
}
numericValue := value[:len(value)-1] // Ommit % manually, because golang Regex does not support lookarounds.
i, _ := strconv.Atoi(numericValue)
return i, nil
}
40 changes: 40 additions & 0 deletions kindle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"testing"
"time"
)

func TestNextWakeupToday(t *testing.T) {
now := time.Date(2009, time.November, 5, 5, 0, 0, 0, time.UTC)
want := 3600
got := nextWakeup(now, 6, 0)
if got != want {
t.Fatalf(`nextWakeup() = %v, want match for %#v, nil`, got, want)
}
}

func TestNextWakeupTomorrow(t *testing.T) {
now := time.Date(2009, time.November, 5, 7, 0, 0, 0, time.UTC)
want := 82800
got := nextWakeup(now, 6, 0)
if got != want {
t.Fatalf(`nextWakeup() = %v, want match for %#v, nil`, got, want)
}
}

func TestParseBatteryLevel(t *testing.T) {
want := 30
got, err := parseBatteryLevel("30%")
if got != want && err != nil {
t.Fatalf(`parseBatteryLevel() = %v, %v, want match for %#v, nil`, got, err, want)
}
}

func TestParseBadBatteryLevel(t *testing.T) {
want := -1
got, err := parseBatteryLevel("abc")
if got != want && err != nil {
t.Fatalf(`parseBatteryLevel() = %v, %v, want match for %#v, nil`, got, err, want)
}
}
1 change: 0 additions & 1 deletion logs/.gitignore

This file was deleted.

Loading

0 comments on commit c7ca7c6

Please sign in to comment.