Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Golang Examples #256

Merged
merged 6 commits into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions golang/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Run a demo

As this is just a Go wrapper for the library you must clone this into your `$GOPATH` as you would any other Go program.
Your path to the project should be:
```
$GOPATH/src/github.com/jgraff/rpi_ws281x
```


As listed in the `ws2811.go` file ensure to copy `ws2811.h`, `rpihw.h`, and `pwm.h` in a GCC include path (e.g. `/usr/local/include`) and
`libws2811.a` in a GCC library path (e.g. `/usr/local/lib`).

To run the basic example run the following commands:
```
cd golang/examples
go build basic.go
sudo ./basic
```

If everything worked you should see a basic color wipe for the first 16 LEDs on your strip.
60 changes: 60 additions & 0 deletions golang/examples/basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"time"
"os"
"fmt"

"github.com/jgarff/rpi_ws281x/golang/ws2811"
)

const (
pin = 18
count = 16
brightness = 255
)

func main() {
defer ws2811.Fini()
err := ws2811.Init(pin, count, brightness)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Press Ctr-C to quit.")
fmt.Println("Creating blue color wipe")
err = colorWipe(uint32(0x000020))
if err != nil {
fmt.Println("Error during wipe " + err.Error())
os.Exit(-1)
}

fmt.Println("Creating red color wipe")
err = colorWipe(uint32(0x002000))
if err != nil {
fmt.Println("Error during wipe " + err.Error())
os.Exit(-1)
}

fmt.Println("Creating green color wipe")
err = colorWipe(uint32(0x200000))
if err != nil {
fmt.Println("Error during wipe " + err.Error())
os.Exit(-1)
}
}
}

func colorWipe(color uint32) error {
for i := 0; i < count; i++ {
ws2811.SetLed(i, color)
err := ws2811.Render()
if err != nil {
ws2811.Clear()
return err
}

time.Sleep(50 * time.Millisecond)
}

return nil
}
2 changes: 1 addition & 1 deletion golang/ws2811/ws2811.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/*
Interface to ws2811 chip (neopixel driver). Make sure that you have
ws2811.h and pwm.h in a GCC include path (e.g. /usr/local/include) and
ws2811.h, rpihw.h, and pwm.h in a GCC include path (e.g. /usr/local/include) and
libws2811.a in a GCC library path (e.g. /usr/local/lib).
See https://github.com/jgarff/rpi_ws281x for instructions
*/
Expand Down