Actual code

This commit is contained in:
Skylten
2020-06-14 20:40:31 +02:00
parent 6ad76786b7
commit 205ccbce1d
53 changed files with 6897 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
## Deprecated
This Go code is being phased out and replaced with https://github.com/rpi-ws281x/rpi-ws281x-go
For issues and bugs with (or contributions to) the Go library, please see: https://github.com/rpi-ws281x/rpi-ws281x-go/issues
----
## 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
View File
@@ -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
}
+91
View File
@@ -0,0 +1,91 @@
// Copyright (c) 2015, Jacques Supcik, HEIA-FR
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the <organization> nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*
Interface to ws2811 chip (neopixel driver). Make sure that you have
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
*/
package ws2811
/*
#cgo CFLAGS: -std=c99
#cgo LDFLAGS: -lws2811
#include "ws2811.go.h"
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
func Init(gpioPin int, ledCount int, brightness int) error {
C.ledstring.channel[0].gpionum = C.int(gpioPin)
C.ledstring.channel[0].count = C.int(ledCount)
C.ledstring.channel[0].brightness = C.uint8_t(brightness)
res := int(C.ws2811_init(&C.ledstring))
if res == 0 {
return nil
} else {
return errors.New(fmt.Sprintf("Error ws2811.init.%d", res))
}
}
func Fini() {
C.ws2811_fini(&C.ledstring)
}
func Render() error {
res := int(C.ws2811_render(&C.ledstring))
if res == 0 {
return nil
} else {
return errors.New(fmt.Sprintf("Error ws2811.render.%d", res))
}
}
func Wait() error {
res := int(C.ws2811_wait(&C.ledstring))
if res == 0 {
return nil
} else {
return errors.New(fmt.Sprintf("Error ws2811.wait.%d", res))
}
}
func SetLed(index int, value uint32) {
C.ws2811_set_led(&C.ledstring, C.int(index), C.uint32_t(value))
}
func Clear() {
C.ws2811_clear(&C.ledstring)
}
func SetBitmap(a []uint32) {
C.ws2811_set_bitmap(&C.ledstring, unsafe.Pointer(&a[0]), C.int(len(a)*4))
}
+64
View File
@@ -0,0 +1,64 @@
/***********************************************************************************
* Copyright (c) 2015, Jacques Supcik, HEIA-FR
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***********************************************************************************/
#include <stdint.h>
#include <string.h>
#include <ws2811.h>
ws2811_t ledstring = {
.freq = 800000,
.dmanum = 10,
.channel = {
[0] = {
.gpionum = 18,
.count = 256,
.invert = 0,
.brightness = 32,
},
[1] = {
.gpionum = 0,
.count = 0,
.invert = 0,
.brightness = 0,
},
},
};
void ws2811_set_led(ws2811_t *ws2811, int index, uint32_t value) {
ws2811->channel[0].leds[index] = value;
}
void ws2811_clear(ws2811_t *ws2811) {
for (int chan = 0; chan < RPI_PWM_CHANNELS; chan++) {
ws2811_channel_t *channel = &ws2811->channel[chan];
memset(channel->leds, 0, sizeof(ws2811_led_t) * channel->count);
}
}
void ws2811_set_bitmap(ws2811_t *ws2811, void* a, int len) {
memcpy(ws2811->channel[0].leds, a, len);
}