Skip to content

Commit

Permalink
Clear buffer (#119)
Browse files Browse the repository at this point in the history
* added message to clear buffer
  • Loading branch information
GP authored Jan 25, 2021
1 parent 589367d commit 9d84bc8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
5 changes: 3 additions & 2 deletions cmd/leptond/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
framesPerSdNotify = 5 * framesHz

telemetryBytes = 160 * 4 //this should be made public in lepton3
clearBuffer = "clear"
)

var version = "<not set>"
Expand Down Expand Up @@ -154,6 +155,8 @@ func runMain() error {
if err != nil {
return err
}
log.Print("Clearing Buffer")
conn.Write([]byte(clearBuffer))
}
}

Expand Down Expand Up @@ -217,13 +220,11 @@ func runCamera(conf *Config, camera *lepton3.Lepton3, conn *net.UnixConn) error
conn.SetWriteBuffer(camera.ResX() * camera.ResY() * 2 * 20)
log.Print("reading frames")
frame := lepton3.NewRawFrame()

notifyCount := 0
for {
if err := camera.NextFrame(frame); err != nil {
return &nextFrameErr{err}
}

if firstPixel(frame) == 0 {
event := eventclient.Event{
Timestamp: time.Now(),
Expand Down
17 changes: 14 additions & 3 deletions cmd/thermal-recorder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (

const (
cptvTempExt = "cptv.temp"
clearBuffer = "clear"
)

var (
Expand Down Expand Up @@ -137,7 +138,6 @@ func runMain() error {
}

func handleConn(conn net.Conn, conf *Config) error {

totalFrames := 0
reader := bufio.NewReader(conn)
header, err := headers.ReadHeaderInfo(reader)
Expand Down Expand Up @@ -174,15 +174,26 @@ func handleConn(conn net.Conn, conf *Config) error {
)

log.Print("reading frames")

frameLogIntervalFirstMin *= header.FPS()
frameLogInterval *= header.FPS()
rawFrame := make([]byte, header.FrameSize())
for {
_, err := io.ReadFull(reader, rawFrame[:])
_, err := io.ReadFull(reader, rawFrame[:5])
if err != nil {
return err
}
message := string(rawFrame[:5])
if message == clearBuffer {
log.Print("clearing motion buffer")
processor.Reset(header)
continue
}

_, err = io.ReadFull(reader, rawFrame[5:])
if err != nil {
return err
}
message = string(rawFrame[:5])
totalFrames++

if totalFrames%frameLogIntervalFirstMin == 0 &&
Expand Down
6 changes: 6 additions & 0 deletions motion/frameloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ type FrameLoop struct {
mu sync.Mutex
}

func (fl *FrameLoop) Reset() {
fl.currentIndex = 0
fl.oldest = 0
fl.bufferFull = false

}
func (fl *FrameLoop) nextIndexAfter(index int) int {
return (index + 1) % fl.size
}
Expand Down
7 changes: 7 additions & 0 deletions motion/motion.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ type motionDetector struct {
framesHz int
}

func (d *motionDetector) Reset(camera cptvframe.CameraSpec) {
d.backgroundFrames = 0
d.count = 0
d.flooredFrames.Reset()
d.diffFrames.Reset()
}

func (d *motionDetector) calculateThreshold(backAverage float64) {
if d.tempThreshMin != 0 {
d.tempThresh = uint16(math.Max(backAverage, float64(d.tempThreshMin)))
Expand Down
8 changes: 8 additions & 0 deletions motion/motionprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ type RecordingListener interface {
RecordingEnded()
}

func (mp *MotionProcessor) Reset(camera cptvframe.CameraSpec) {
mp.stopRecording()
mp.motionDetector.Reset(camera)
}

func (mp *MotionProcessor) Process(rawFrame []byte) error {
frame := mp.frameLoop.Current()
if err := mp.parseFrame(rawFrame, frame); err != nil {
Expand Down Expand Up @@ -169,6 +174,9 @@ func (mp *MotionProcessor) startRecording() error {
}

func (mp *MotionProcessor) stopRecording() error {
if !mp.isRecording {
return nil
}
if mp.listener != nil {
mp.listener.RecordingEnded()
}
Expand Down

0 comments on commit 9d84bc8

Please sign in to comment.