-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.go
55 lines (48 loc) · 1.22 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
// SPDX-FileCopyrightText: 2020 Kent Gibson <[email protected]>
//
// SPDX-License-Identifier: MIT
//go:build linux
// A simple example that toggles an output pin.
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/warthog618/go-gpiocdev"
)
// This example drives line 22 on gpiochip0.
// The pin is toggled high and low at 1Hz with a 50% duty cycle.
// DO NOT run this on a device which has this pin externally driven.
func main() {
offset := 22
chip := "gpiochip0"
v := 0
l, err := gpiocdev.RequestLine(chip, offset, gpiocdev.AsOutput(v))
if err != nil {
panic(err)
}
// revert line to input on the way out.
defer func() {
l.Reconfigure(gpiocdev.AsInput)
fmt.Printf("Input pin %s:%d\n", chip, offset)
l.Close()
}()
values := map[int]string{0: "inactive", 1: "active"}
fmt.Printf("Set pin %s:%d %s\n", chip, offset, values[v])
// capture exit signals to ensure pin is reverted to input on exit.
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(quit)
for {
select {
case <-time.After(500 * time.Millisecond):
v ^= 1
l.SetValue(v)
fmt.Printf("Set pin %s:%d %s\n", chip, offset, values[v])
case <-quit:
return
}
}
}