Last active
March 6, 2016 16:32
-
-
Save orkShinnosuke/c3091c1ca792bce81e7c to your computer and use it in GitHub Desktop.
GoAlarm
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
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