Skip to content

Commit

Permalink
Merge pull request #130 from CameronRP/fix-lepton-errors
Browse files Browse the repository at this point in the history
Wait for thermal-recorder socket
  • Loading branch information
CameronRP authored May 3, 2022
2 parents df40a8f + 410e396 commit c896b2d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
4 changes: 3 additions & 1 deletion _release/postinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ systemctl daemon-reload

systemctl enable thermal-recorder.service
systemctl enable leptond.service
systemctl restart leptond.service
systemctl stop leptond.service
systemctl restart thermal-recorder.service
sleep 1
systemctl start leptond.service
27 changes: 24 additions & 3 deletions cmd/leptond/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"log"
"net"
"os"
"os/exec"
"time"

Expand Down Expand Up @@ -100,6 +101,16 @@ func runMain() error {
return err
}

// Wait for socket to be available.
log.Print("waiting for socket to be available")
for {
if _, err := os.Stat(conf.FrameOutput); !os.IsNotExist(err) {
break
}
time.Sleep(time.Second)
resetWatchdog()
}

log.Print("dialing frame output socket")
conn, err := net.DialUnix("unix", nil, &net.UnixAddr{
Net: "unix",
Expand Down Expand Up @@ -140,7 +151,7 @@ func runMain() error {
}

for {
err = runCamera(conf, camera, conn)
err = runCamera(conf, camera, conn, service)
if err != nil {
if _, isNextFrameErr := err.(*nextFrameErr); !isNextFrameErr {
return err
Expand Down Expand Up @@ -222,7 +233,7 @@ func sendCameraSpecs(conf *Config, camera *lepton3.Lepton3, conn *net.UnixConn)
return nil
}

func runCamera(conf *Config, camera *lepton3.Lepton3, conn *net.UnixConn) error {
func runCamera(conf *Config, camera *lepton3.Lepton3, conn *net.UnixConn, service *leptondService) error {
conn.SetWriteBuffer(camera.ResX() * camera.ResY() * 2 * 20)
log.Print("reading frames")
frame := lepton3.NewRawFrame()
Expand All @@ -233,16 +244,26 @@ func runCamera(conf *Config, camera *lepton3.Lepton3, conn *net.UnixConn) error
}

if notifyCount++; notifyCount >= framesPerSdNotify {
daemon.SdNotify(false, "WATCHDOG=1")
resetWatchdog()
notifyCount = 0
}

if service.actions.reset {
service.actions.reset = false
log.Println("reset triggered through service")
return nil
}

if _, err := conn.Write(frame[:]); err != nil {
return err
}
}
}

func resetWatchdog() {
daemon.SdNotify(false, "WATCHDOG=1")
}

func logConfig(conf *Config) {
log.Printf("SPI speed: %d", conf.SPISpeed)
log.Printf("power pin: %s", conf.PowerPin)
Expand Down
16 changes: 14 additions & 2 deletions cmd/leptond/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ const (

var mu sync.Mutex

type actions struct {
reset bool
}

type leptondService struct {
camera *lepton3.Lepton3
camera *lepton3.Lepton3
actions *actions
}

func startService() (*leptondService, error) {
Expand All @@ -49,7 +54,7 @@ func startService() (*leptondService, error) {
if reply != dbus.RequestNameReplyPrimaryOwner {
return nil, errors.New("name already taken")
}
s := &leptondService{}
s := &leptondService{actions: &actions{reset: false}}
conn.Export(s, dbusPath, dbusName)
conn.Export(genIntrospectable(s), dbusPath, "org.freedesktop.DBus.Introspectable")
return s, nil
Expand Down Expand Up @@ -101,6 +106,13 @@ func (s leptondService) SetAutoFFC(automatic bool) *dbus.Error {
return nil
}

func (s leptondService) RestartCamera() *dbus.Error {
mu.Lock()
defer mu.Unlock()
s.actions.reset = true
return nil
}

func makeDbusError(name string, err error) *dbus.Error {
return &dbus.Error{
Name: dbusName + "." + name,
Expand Down
4 changes: 2 additions & 2 deletions cmd/thermal-recorder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ func handleConn(conn net.Conn, conf *Config) error {
Details: map[string]interface{}{"description": map[string]interface{}{"details": err.Error()}},
}
eventclient.AddEvent(event)
// this will cause camera power to be cycled
return err
log.Println("bad frame deteccted, requesting camera to restart")
leptondController.RestartCamera()
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions leptondController/leptondController.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ func RunFFC() error {
}
return obj.Call(methodBase+".RunFFC", 0).Store()
}

func RestartCamera() error {
obj, err := getDbusObj()
if err != nil {
return err
}
return obj.Call(methodBase+".RestartCamera", 0).Store()
}

0 comments on commit c896b2d

Please sign in to comment.