Skip to content

Commit

Permalink
extend examples
Browse files Browse the repository at this point in the history
  • Loading branch information
warthog618 committed Mar 18, 2024
1 parent aabce06 commit 70fc8ea
Show file tree
Hide file tree
Showing 24 changed files with 443 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SPDX-License-Identifier: MIT
## [Unreleased](https://github.com/warthog618/gpiod/compare/v0.9.0...HEAD)

- add *FindLine* functions to *Chip* and global.
- rework and extend examples.

## v0.9.0 - 2024-03-16

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ l.SetValue(1) // Set line active
l.SetValue(0) // Set line inactive
```

Also refer to the [blinker](example/blinker/blinker.go) example.
Also refer to the [toggle_line_value](examples/toggle_line_value/main.go) example.

For collections of lines, all lines are set simultaneously using the
[*SetValues*](https://pkg.go.dev/github.com/warthog618/go-gpiocdev#Lines.SetValues)
Expand Down Expand Up @@ -214,7 +214,7 @@ Note that the *Close* waits for the event handler to return and so must not be
called from the event handler context - it should be called from a separate
goroutine.

Also see the [watcher](example/watcher/watcher.go) example.
Also see the [watch_line_value](examples/watch_line_value/main.go) example.

### Line Configuration

Expand Down
4 changes: 4 additions & 0 deletions examples/find_line_by_name/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2019 Kent Gibson <[email protected]>.
#
# SPDX-License-Identifier: CC0-1.0
find_line_by_name
30 changes: 30 additions & 0 deletions examples/find_line_by_name/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2024 Kent Gibson <[email protected]>
//
// SPDX-License-Identifier: MIT

//go:build linux
// +build linux

// A simple example that finds a line by name.
package main

import (
"fmt"
"os"

"github.com/warthog618/go-gpiocdev"
)

// Finds the chip and offset of a named line.
func main() {
name := "GPIO22"
if len(os.Args) > 1 {
name = os.Args[1]
}
chip, offset, err := gpiocdev.FindLine(name)
if err != nil {
fmt.Printf("Finding line %s returned error: %s\n", name, err)
os.Exit(1)
}
fmt.Printf("%s:%d %s\n", chip, offset, name)
}
4 changes: 4 additions & 0 deletions examples/get_chip_info/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2019 Kent Gibson <[email protected]>.
#
# SPDX-License-Identifier: CC0-1.0
get_chip_info
28 changes: 28 additions & 0 deletions examples/get_chip_info/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2020 Kent Gibson <[email protected]>
//
// SPDX-License-Identifier: MIT

//go:build linux
// +build linux

// A simple example that read the info for gpiochip0.
package main

import (
"fmt"
"os"

"github.com/warthog618/go-gpiocdev"
)

// Reads the info for gpiochip0.
func main() {
c, err := gpiocdev.NewChip("gpiochip0")
if err != nil {
fmt.Printf("Opening chip returned error: %s\n", err)
os.Exit(1)
}
defer c.Close()

fmt.Printf("%s (%s): %d lines\n", c.Name, c.Label, c.Lines())
}
4 changes: 4 additions & 0 deletions examples/get_line_info/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2019 Kent Gibson <[email protected]>.
#
# SPDX-License-Identifier: CC0-1.0
get_line_info
33 changes: 33 additions & 0 deletions examples/get_line_info/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-FileCopyrightText: 2020 Kent Gibson <[email protected]>
//
// SPDX-License-Identifier: MIT

//go:build linux
// +build linux

// A simple example that read the info for line 22 on gpiochip0.
package main

import (
"fmt"
"os"

"github.com/warthog618/go-gpiocdev"
)

// Reads the info for line 22 on gpiochip0.
func main() {
c, err := gpiocdev.NewChip("gpiochip0")
if err != nil {
fmt.Printf("Opening chip returned error: %s\n", err)
os.Exit(1)
}
defer c.Close()

info, err := c.LineInfo(22)
if err != nil {
fmt.Printf("Reading line info returned error: %s\n", err)
os.Exit(1)
}
fmt.Printf("%v\n", info)
}
4 changes: 4 additions & 0 deletions examples/get_line_value/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2019 Kent Gibson <[email protected]>
#
# SPDX-License-Identifier: CC0-1.0
get_line_value
30 changes: 30 additions & 0 deletions examples/get_line_value/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2020 Kent Gibson <[email protected]>
//
// SPDX-License-Identifier: MIT

//go:build linux
// +build linux

// A simple example that reads an input pin.
package main

import (
"fmt"

"github.com/warthog618/go-gpiocdev"
)

// This example reads line 22 on gpiochip0.
func main() {
offset := 22
chip := "gpiochip0"
l, err := gpiocdev.RequestLine(chip, offset, gpiocdev.AsInput)
if err != nil {
panic(err)
}
defer l.Close()

values := map[int]string{0: "inactive", 1: "active"}
v, err := l.Value()
fmt.Printf("%s:%d %s\n", chip, offset, values[v])
}
4 changes: 4 additions & 0 deletions examples/get_multiple_line_values/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2019 Kent Gibson <[email protected]>
#
# SPDX-License-Identifier: CC0-1.0
get_multiple_line_values
39 changes: 39 additions & 0 deletions examples/get_multiple_line_values/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2020 Kent Gibson <[email protected]>
//
// SPDX-License-Identifier: MIT

//go:build linux
// +build linux

// A simple example that reads multiple input pins.
package main

import (
"fmt"
"os"

"github.com/warthog618/go-gpiocdev"
)

// This example reads lines 21 and 22 on gpiochip0.
func main() {
offsets := []int{21, 22}
chip := "gpiochip0"
l, err := gpiocdev.RequestLines(chip, offsets, gpiocdev.AsInput)
if err != nil {
fmt.Printf("Requesting lines returned error: %s\n", err)
os.Exit(1)
}
defer l.Close()

values := map[int]string{0: "inactive", 1: "active"}
vv := []int{0, 0}
err = l.Values(vv)
if err != nil {
fmt.Printf("Reading values returned error: %s\n", err)
os.Exit(1)
}
for i, o := range offsets {
fmt.Printf("%s:%d %s\n", chip, o, values[vv[i]])
}
}
File renamed without changes.
4 changes: 4 additions & 0 deletions examples/reconfigure_input_to_output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2019 Kent Gibson <[email protected]>
#
# SPDX-License-Identifier: CC0-1.0
reconfigure_input_to_output
45 changes: 45 additions & 0 deletions examples/reconfigure_input_to_output/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: 2020 Kent Gibson <[email protected]>
//
// SPDX-License-Identifier: MIT

//go:build linux
// +build linux

// A simple example that requests a line as an input and subsequently switches it to an output.
// DO NOT run this on a platform where that line is externally driven.
package main

import (
"fmt"
"time"

"github.com/warthog618/go-gpiocdev"
)

// This example requests line 23 on gpiochip0 as an input then switches it to an output.
// DO NOT run this on a platform where that line is externally driven.
func main() {
offset := 23
chip := "gpiochip0"
l, err := gpiocdev.RequestLine(chip, offset, gpiocdev.AsInput)
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"}
v, err := l.Value()
fmt.Printf("Read pin: %s:%d %s\n", chip, offset, values[v])

l.Reconfigure(gpiocdev.AsOutput(v))
fmt.Printf("Set pin %s:%d %s\n", chip, offset, values[v])
time.Sleep(500 * time.Millisecond)
v ^= 1
l.SetValue(v)
fmt.Printf("Set pin %s:%d %s\n", chip, offset, values[v])
}
14 changes: 7 additions & 7 deletions examples/select_watch_line_value/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"time"

"github.com/warthog618/go-gpiocdev"
"github.com/warthog618/go-gpiocdev/device/rpi"
)

func printEvent(evt gpiocdev.LineEvent) {
Expand All @@ -45,7 +44,7 @@ func printEvent(evt gpiocdev.LineEvent) {
}
}

// Watches GPIO 23 (Raspberry Pi J8-16) and reports when it changes state.
// Watches line 23 on gpiochip0 and reports when it changes state.
func main() {
echan := make(chan gpiocdev.LineEvent, 6)

Expand All @@ -57,12 +56,13 @@ func main() {
// if you want the handler to block, rather than dropping
// events when the channel fills then <- ctx.Done() instead
// to ensure that the handler can't be left blocked
fmt.Printf("event chan overflow - discarding event")
fmt.Println("event chan overflow - discarding event")
}
}

offset := rpi.J8p16
l, err := gpiocdev.RequestLine("gpiochip0", offset,
offset := 23
chip := "gpiochip0"
l, err := gpiocdev.RequestLine(chip, offset,
gpiocdev.WithPullUp,
gpiocdev.WithBothEdges,
gpiocdev.WithEventHandler(eh))
Expand All @@ -74,15 +74,15 @@ func main() {
os.Exit(1)
}

fmt.Printf("Watching Pin %d...\n", offset)
fmt.Printf("Watching Pin %s:%d...\n", chip, offset)
done := false
for !done {
select {
// depending on the application other cases could deal with other channels
case evt := <-echan:
printEvent(evt)
case <-ctx.Done():
fmt.Println("exiting...")
fmt.Println("select_watch_line_value exiting...")
l.Close()
done = true
}
Expand Down
17 changes: 9 additions & 8 deletions examples/toggle_line_value/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@ import (
"time"

"github.com/warthog618/go-gpiocdev"
"github.com/warthog618/go-gpiocdev/device/rpi"
)

// This example drives GPIO 22, which is pin J8-15 on a Raspberry Pi.
// 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.
// DO NOT run this on a device which has this pin externally driven.
func main() {
offset := rpi.J8p15
offset := 22
chip := "gpiochip0"
v := 0
l, err := gpiocdev.RequestLine("gpiochip0", offset, gpiocdev.AsOutput(v))
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 %d %s\n", offset, values[v])
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)
Expand All @@ -44,10 +45,10 @@ func main() {

for {
select {
case <-time.After(2 * time.Second):
case <-time.After(500 * time.Millisecond):
v ^= 1
l.SetValue(v)
fmt.Printf("Set pin %d %s\n", offset, values[v])
fmt.Printf("Set pin %s:%d %s\n", chip, offset, values[v])
case <-quit:
return
}
Expand Down
4 changes: 4 additions & 0 deletions examples/toggle_multiple_line_values/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2019 Kent Gibson <[email protected]>
#
# SPDX-License-Identifier: CC0-1.0
toggle_multiple_line_values
Loading

0 comments on commit 70fc8ea

Please sign in to comment.