-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
59 lines (50 loc) · 1.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2016 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// gpio-write sets a GPIO pin to low or high.
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/gpio/gpioreg"
"periph.io/x/host/v3"
)
func mainImpl() error {
verbose := flag.Bool("v", false, "verbose mode")
flag.Parse()
if !*verbose {
log.SetOutput(ioutil.Discard)
}
log.SetFlags(log.Lmicroseconds)
if flag.NArg() != 2 {
return errors.New("specify GPIO pin to write to and its level (0 or 1)")
}
args := flag.Args()
l := gpio.Low
switch args[1] {
case "0":
case "1":
l = gpio.High
default:
return errors.New("specify level as 0 or 1")
}
if _, err := host.Init(); err != nil {
return err
}
p := gpioreg.ByName(args[0])
if p == nil {
return errors.New("invalid GPIO pin number")
}
return p.Out(l)
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "gpio-write: %s.\n", err)
os.Exit(1)
}
}