Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for attached motion sensor on GPIO #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cmd/hkcam/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package main

import (
"flag"

"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/log"
"github.com/brutella/hc/service"

"github.com/stianeikeland/go-rpio/v4"

"image"
"runtime"

"time"

"github.com/brutella/hkcam"
"github.com/brutella/hkcam/ffmpeg"
)
Expand All @@ -21,13 +27,15 @@ func main() {
var loopbackFilename *string
var h264Encoder *string
var h264Decoder *string
var motionPin int = -1

if runtime.GOOS == "linux" {
inputDevice = flag.String("input_device", "v4l2", "video input device")
inputFilename = flag.String("input_filename", "/dev/video0", "video input device filename")
loopbackFilename = flag.String("loopback_filename", "/dev/video1", "video loopback device filename")
h264Decoder = flag.String("h264_decoder", "", "h264 video decoder")
h264Encoder = flag.String("h264_encoder", "h264_omx", "h264 video encoder")
flag.IntVar(&motionPin, "motion_pin", -1, "GPIO pin of any attached motion sensor")
} else if runtime.GOOS == "darwin" { // macOS
inputDevice = flag.String("input_device", "avfoundation", "video input device")
inputFilename = flag.String("input_filename", "default", "video input device filename")
Expand Down Expand Up @@ -63,6 +71,25 @@ func main() {
MultiStream: *multiStream,
}

// Add Motion Sensor
if motionPin != -1 {
err := rpio.Open()
if err != nil {
log.Info.Panic(err)
}
pin := rpio.Pin(10)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that must be:

Suggested change
pin := rpio.Pin(10)
pin := rpio.Pin(motionPin)

pin.Input()

motionSensor := service.NewMotionSensor()
cam.AddService(motionSensor.Service)

go func() {
for range time.Tick(time.Second) {
motionSensor.MotionDetected.SetValue(pin.Read() == rpio.High)
}
}()
}

ffmpeg := hkcam.SetupFFMPEGStreaming(cam, cfg)

// Add a custom camera control service to record snapshots
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
github.com/brutella/hc v1.2.0
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/radovskyb/watcher v1.0.6
github.com/stianeikeland/go-rpio/v4 v4.4.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/radovskyb/watcher v1.0.6 h1:8WIQ9UxEYMZjem1OwU7dVH94DXXk9mAIE1i8eqHD+IY=
github.com/radovskyb/watcher v1.0.6/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
github.com/stianeikeland/go-rpio/v4 v4.4.0 h1:LScvNyXHF412co42LG5t7bvBDbtDAhLF828ebaGqmjA=
github.com/stianeikeland/go-rpio/v4 v4.4.0/go.mod h1:BkK52zk+FRk8wCTDf88/86Sojc+NfUiCAHd1ZV3RuTM=
github.com/tadglines/go-pkgs v0.0.0-20140924210655-1f86682992f1 h1:ms/IQpkxq+t7hWpgKqCE5KjAUQWC24mqBrnL566SWgE=
github.com/tadglines/go-pkgs v0.0.0-20140924210655-1f86682992f1/go.mod h1:roo6cZ/uqpwKMuvPG0YmzI5+AmUiMWfjCBZpGXqbTxE=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0=
Expand Down