Skip to content

Instantly share code, notes, and snippets.

@orkShinnosuke
Last active March 6, 2016 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orkShinnosuke/c3091c1ca792bce81e7c to your computer and use it in GitHub Desktop.
Save orkShinnosuke/c3091c1ca792bce81e7c to your computer and use it in GitHub Desktop.
GoAlarm
package main
import (
"fmt"
"os"
"os/exec"
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/raspi"
)
// アラーム音鳴らす
func start() {
out, err := exec.Command("sh", "-c", "mpg321 -l 0 test.mp3 > /dev/null &").Output()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(out))
}
// アラーム音鳴り始めて1分経ったら停止 or スイッチ押下で停止
func stop() {
gbot := gobot.NewGobot()
r := raspi.NewRaspiAdaptor("raspi")
work := func() {
waitMillisecond := 100
i := 0;
gobot.Every(time.Duration(waitMillisecond)*time.Millisecond, func() {
v, _ := r.DigitalRead("15")
if v == 1 || waitMillisecond*i > 60000 {
out, err := exec.Command("killall", "mpg321").Output()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(out))
os.Exit(0)
}
i++
})
}
robot := gobot.NewRobot("blinkBot",
[]gobot.Connection{r},
nil,
work,
)
gbot.AddRobot(robot)
gbot.Start()
}
func main() {
start()
stop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment