forked from warthog618/go-gpiocdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blinker.go
55 lines (48 loc) · 1.21 KB
/
blinker.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
// +build linux
// A simple example that toggles an output pin.
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/warthog618/gpiod"
"github.com/warthog618/gpiod/device/rpi"
)
// This example drives GPIO 22, which is pin J8-15 on a Raspberry Pi.
// 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 := rpi.J8p15
v := 0
l, err := gpiod.RequestLine("gpiochip0", offset, gpiod.AsOutput(v))
if err != nil {
panic(err)
}
// revert line to input on the way out.
defer func() {
l.Reconfigure(gpiod.AsInput)
l.Close()
}()
values := map[int]string{0: "inactive", 1: "active"}
fmt.Printf("Set pin %d %s\n", 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(2 * time.Second):
v ^= 1
l.SetValue(v)
fmt.Printf("Set pin %d %s\n", offset, values[v])
case <-quit:
return
}
}
}