-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from landgenoot/golang
Translation to golang
- Loading branch information
Showing
23 changed files
with
824 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.