2015-04-06 00:07:16 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
rpio "github.com/stianeikeland/go-rpio"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2015-04-06 00:32:45 +02:00
|
|
|
var pinNumber = flag.Int("p", 0, " number of pin to use")
|
2015-04-06 00:07:16 +02:00
|
|
|
var high = flag.Bool("h", false, " return 0 if value is HIGH, omit to set value")
|
|
|
|
var low = flag.Bool("l", false, " return 0 if value is LOW, omit to set value")
|
|
|
|
var set = flag.Int("s", 0, " set pin value to high(1) or low(-1)")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if os.Geteuid() != 0 {
|
2015-04-06 00:12:19 +02:00
|
|
|
fmt.Println("This program have to be run as root, or SUID/GUID set to 0 on execution!")
|
2015-04-06 00:07:16 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOARCH != "arm" {
|
|
|
|
fmt.Println("This program can be unpredictable on other machines rather than Raspberry Pi")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
fmt.Println("This program have to be executed on linux only!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open and map memory to access gpio, check for errors
|
|
|
|
if err := rpio.Open(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmap gpio memory when done
|
|
|
|
defer rpio.Close()
|
|
|
|
|
2015-04-06 00:32:45 +02:00
|
|
|
if *pinNumber == 0 {
|
2015-04-06 00:07:16 +02:00
|
|
|
fmt.Println("Set the pin number to use!")
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
2015-04-06 00:12:19 +02:00
|
|
|
if *high || *low {
|
2015-04-06 00:07:16 +02:00
|
|
|
//we get the value for pin
|
2015-04-06 00:32:45 +02:00
|
|
|
pin := rpio.Pin(*pinNumber)
|
2015-04-06 00:07:16 +02:00
|
|
|
pin.Input() // Input mode
|
|
|
|
res := pin.Read()
|
2015-04-06 01:00:34 +02:00
|
|
|
fmt.Printf("Raspberry PI GPIO status for pin #%d - %d\n", *pinNumber, pin.Read())
|
2015-04-06 00:12:19 +02:00
|
|
|
if *high {
|
2015-04-06 00:07:16 +02:00
|
|
|
if res == rpio.High {
|
|
|
|
os.Exit(0)
|
|
|
|
} else {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-04-06 00:56:15 +02:00
|
|
|
} else {
|
2015-04-06 00:07:16 +02:00
|
|
|
if res == rpio.Low {
|
|
|
|
os.Exit(0)
|
|
|
|
} else {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-06 00:12:19 +02:00
|
|
|
switch *set {
|
2015-04-06 00:07:16 +02:00
|
|
|
case 1:
|
2015-04-06 00:32:45 +02:00
|
|
|
pin := rpio.Pin(*pinNumber)
|
2015-04-06 00:07:16 +02:00
|
|
|
pin.Output() // Output mode
|
|
|
|
pin.High()
|
|
|
|
os.Exit(0)
|
|
|
|
break
|
|
|
|
case -1:
|
|
|
|
//we set the value for pin
|
2015-04-06 00:32:45 +02:00
|
|
|
pin := rpio.Pin(*pinNumber)
|
2015-04-06 00:07:16 +02:00
|
|
|
pin.Output() // Output mode
|
|
|
|
pin.Low()
|
|
|
|
os.Exit(0)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|