├── .github └── workflows │ ├── ci.yml │ └── close-inactive-issues.yml ├── .gitignore ├── .gitmodules ├── .pre-commit-config.yaml ├── AUTHORS ├── LICENSE ├── README.md ├── basic_test.go ├── build-examples.sh ├── clean-example.sh ├── docker └── app-builder │ └── Dockerfile ├── examples ├── color_wipe │ ├── color_wipe.go │ ├── go.mod │ └── go.sum ├── hw │ ├── go.mod │ ├── go.sum │ └── hw.go ├── invader8x8 │ ├── go.mod │ ├── go.sum │ ├── invader0.png │ ├── invader1.png │ └── invader8x8.go └── swiss │ ├── go.mod │ ├── go.sum │ └── swiss.go ├── go.mod ├── go.sum ├── ws2811.go ├── ws2811_gamma8.go ├── ws2811_hw.go ├── ws2811_sim.go └── ws2811_stripe_type.go /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push, pull_request] 3 | 4 | jobs: 5 | build: 6 | name: Build 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: Set up Go 1.20.6 10 | uses: actions/setup-go@v4 11 | with: 12 | go-version: '1.20.6' 13 | 14 | - name: Check out code 15 | uses: actions/checkout@v3 16 | 17 | - name: pre-commit 18 | uses: pre-commit/action@v3.0.0 19 | 20 | - name: Set up QEMU 21 | uses: docker/setup-qemu-action@v2 22 | with: 23 | platforms: all 24 | 25 | - name: Set up Docker Buildx 26 | id: buildx 27 | uses: docker/setup-buildx-action@v2 28 | with: 29 | version: latest 30 | 31 | - name: Builder instance name 32 | run: echo ${{ steps.buildx.outputs.name }} 33 | 34 | - name: Available platforms 35 | run: echo ${{ steps.buildx.outputs.platforms }} 36 | 37 | - name: Fix example to use local module 38 | run: | 39 | for i in examples/*; do \ 40 | echo "replace github.com/rpi-ws281x/rpi-ws281x-go => /go/src/rpi-ws281x-go/" >> $i/go.mod; 41 | done 42 | 43 | - name: Build ARM-V7 builder 44 | run: | 45 | docker buildx build \ 46 | --platform linux/arm/v7 \ 47 | --tag rpi-ws281x-builder-armv7 \ 48 | --load \ 49 | --file ./docker/app-builder/Dockerfile . 50 | 51 | - name: Build examples (arm-v7) 52 | run: | 53 | for i in examples/*; do \ 54 | docker run --rm \ 55 | --platform linux/arm/v7 \ 56 | -v "$(pwd)/$i":/usr/src/$(basename $i) \ 57 | -w /usr/src/$(basename $i) \ 58 | rpi-ws281x-builder-armv7 \ 59 | go build -o $(basename $i)-armv7 -v . 60 | done 61 | 62 | - name: Build ARM64 builder 63 | run: | 64 | docker buildx build \ 65 | --platform linux/arm64 \ 66 | --tag rpi-ws281x-builder-arm64 \ 67 | --load \ 68 | --file ./docker/app-builder/Dockerfile . 69 | 70 | - name: Build examples (arm64) 71 | run: | 72 | for i in examples/*; do \ 73 | docker run --rm \ 74 | --platform linux/arm64 \ 75 | -v "$(pwd)/$i":/usr/src/$(basename $i) \ 76 | -w /usr/src/$(basename $i) \ 77 | rpi-ws281x-builder-arm64 \ 78 | go build -o $(basename $i)-arm64 -v . 79 | done 80 | 81 | - name: Upload example binary 82 | uses: actions/upload-artifact@v3 83 | with: 84 | name: examples 85 | path: | 86 | examples/*/*-armv7 87 | examples/*/*-arm64 88 | -------------------------------------------------------------------------------- /.github/workflows/close-inactive-issues.yml: -------------------------------------------------------------------------------- 1 | name: Close inactive issues 2 | on: 3 | schedule: 4 | - cron: '30 1 * * *' 5 | 6 | jobs: 7 | close-issues: 8 | runs-on: ubuntu-latest 9 | permissions: 10 | issues: write 11 | pull-requests: write 12 | steps: 13 | - uses: actions/stale@v5 14 | with: 15 | days-before-issue-stale: 30 16 | days-before-issue-close: 14 17 | stale-issue-label: 'stale' 18 | stale-issue-message: 'This issue is stale because it has been open for 30 days with no activity.' 19 | close-issue-message: 'This issue was closed because it has been inactive for 14 days since being marked as stale.' 20 | days-before-pr-stale: -1 21 | days-before-pr-close: -1 22 | repo-token: ${{ secrets.GITHUB_TOKEN }} 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | .idea 26 | 27 | examples/*/*-armv7 28 | examples/*/*-arm64 29 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rpi-ws281x/rpi-ws281x-go/247b076ddb3cdee0c9c7cb8c8f99fe6b86831603/.gitmodules -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pre-commit/pre-commit-hooks 3 | rev: v4.4.0 4 | hooks: 5 | - id: check-yaml 6 | - id: end-of-file-fixer 7 | - id: trailing-whitespace 8 | - repo: https://github.com/golangci/golangci-lint 9 | rev: v1.53.3 10 | hooks: 11 | - id: golangci-lint 12 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Jacques Supcik 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![GoDoc](https://godoc.org/github.com/rpi-ws281x/rpi-ws281x-go?status.svg)](http://godoc.org/github.com/rpi-ws281x/rpi-ws281x-go) 2 | [![Go Report Card](https://goreportcard.com/badge/github.com/rpi-ws281x/rpi-ws281x-go)](https://goreportcard.com/report/github.com/rpi-ws281x/rpi-ws281x-go) 3 | [![Actions](https://github.com/rpi-ws281x/rpi-ws281x-go/workflows/CI/badge.svg)](https://github.com/rpi-ws281x/rpi-ws281x-go/actions) 4 | [![license](https://img.shields.io/github/license/rpi-ws281x/rpi-ws281x-go.svg)](https://github.com/rpi-ws281x/rpi-ws281x-go) 5 | 6 | # rpi-ws281x-go 7 | 8 | ## Summary 9 | 10 | Go (golang) binding for the rpi_ws281x userspace Raspberry Pi library for controlling WS281X LEDs by Jeremy Garff ([https://github.com/jgarff/rpi_ws281x](https://github.com/jgarff/rpi_ws281x)). The goal for this library is to offer all the features of the C library and to make is as efficiently as possible. 11 | 12 | ## Installing 13 | 14 | This module is a wrapper around the [rpi_ws281x](https://github.com/jgarff/rpi_ws281x) C library and you need to have this C library installed on your machine before installing this module. 15 | 16 | ### Compiling directly on the Raspberry Pi 17 | 18 | **This is not the recommended way**, but if you want to compile everything on the Raspbery Pi itself, start by building 19 | the pi_ws281x C library according to the [documentation](https://github.com/jgarff/rpi_ws281x#build), 20 | copy the `*.a` files to `/usr/local/lib` and the `*.h` files to `/usr/local/include`. 21 | 22 | Then you can compile the go code as usual. 23 | 24 | ### Cross compiling 25 | 26 | The recommended way for building software for embedded systems is to use cross compilation. Cross compilation is very 27 | easy in go... unless you use cgo. And because this module is a wrapper around a C library, we have to use cgo and the 28 | cross compilation is not so easy. 29 | 30 | The solution proposed here uses a docker container to cross-compile the go code and should work on GNU/Linux, macos 31 | and Windows. 32 | 33 | First check that you have a recent version of docker desktop. Run the following command: 34 | 35 | ``` 36 | docker buildx ls 37 | ``` 38 | 39 | You should see something like this : 40 | 41 | ``` 42 | NAME/NODE DRIVER/ENDPOINT STATUS PLATFORMS 43 | default * docker 44 | default default running linux/amd64, linux/arm64, ..., linux/arm/v7, linux/arm/v6 45 | ``` 46 | 47 | If you see `linux/arm/v6` and `linux/arm/v7` you can cross-compile for arm 32 bits. If you see `linux/arm64` you 48 | can compile code for arm 64 bits. 49 | 50 | Now you need to build a docker image with the required toolchain and libraries: 51 | 52 | ``` 53 | docker buildx build --platform linux/arm/v7 --tag ws2811-builder --file docker/app-builder/Dockerfile . 54 | ``` 55 | 56 | You can replace `linux/arm/v7` by `linux/arm64` if you want to build for arm64. 57 | 58 | You can now use this Docker image to build your app. For example, you can build the "swiss" example using this command: 59 | 60 | ``` 61 | > cd examples/swiss 62 | > APP="swiss" 63 | > docker run --rm -v "$PWD":/usr/src/$APP --platform linux/arm/v7 \ 64 | -w /usr/src/$APP ws2811-builder:latest go build -o "$APP-armv7" -v 65 | ``` 66 | 67 | On GNU/Linux or macos, you can check the built binary with the `file` command: 68 | 69 | ``` 70 | > file swiss-armv7 71 | 72 | swiss-armv7: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), 73 | dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, 74 | Go BuildID=..., BuildID[sha1]=..., not stripped``` 75 | ``` 76 | 77 | As you can see, the resulting binary is an executable file for the ARM processor. 78 | 79 | Note that the example will use the `rpi-ws2811x-go` module from the github repository. If you want to use the module 80 | from the Docker image, you can add the following line to the `go.mod` file of the example: 81 | 82 | ```text 83 | replace github.com/rpi-ws281x/rpi-ws281x-go => /go/src/rpi-ws281x-go/ 84 | ``` 85 | 86 | ## Using the module 87 | 88 | In order to use this module, you have to understand the options of the underlying C library. Read [documentation of the C library](https://github.com/jgarff/rpi_ws281x) for more information. 89 | 90 | The mapping of these options from go to C should be obvious. The [documentation of this module](https://godoc.org/github.com/rpi-ws281x/rpi-ws281x-go) and particularly the section about the [channel options](https://godoc.org/github.com/rpi-ws281x/rpi-ws281x-go#ChannelOption) provide further information. 91 | 92 | ## Testing 93 | 94 | This library is tested using the following hardware setup: 95 | 96 |

97 | 98 |

99 | 100 | In this circuit, the 4050 is a driver that converts the 3.3V of the Raspberry Pi to the 5V needed by the ws2811 chip. The LED matrix is connected by an external power supply that provides the required current. 101 | 102 | Here is the result of the "Swiss" example: 103 | 104 |

105 | 106 |

107 | 108 | ## Special Thanks 109 | 110 | * Thank you to [Jeremy Garff](https://github.com/jgarff) for writing and maintaining the C library. 111 | * Thank you to all contributors (alphabetically): 112 | - [Alexandr Pavlyuk](https://github.com/pav5000) 113 | - [Allen Flickinger](https://github.com/FuzzyStatic) 114 | - [Ben Watkins](https://github.com/OutdatedVersion) 115 | - [Chris C.](https://github.com/TwinProduction) 116 | - [Elie Grenon](https://github.com/DrunkenPoney) 117 | - [Herman](https://github.com/hermanbanken) 118 | - [Ivaylo Stoyanov](https://github.com/ivkos) 119 | - [Stephen Onnen](https://github.com/onnenon) 120 | 121 | ## Projects using this module 122 | 123 | * [Rainbow and Random demo](https://github.com/FuzzyStatic/rpi-ws281x-examples-go) 124 | -------------------------------------------------------------------------------- /basic_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package ws2811 16 | 17 | import ( 18 | "flag" 19 | "fmt" 20 | "os/user" 21 | "runtime" 22 | "testing" 23 | 24 | "github.com/stretchr/testify/assert" 25 | ) 26 | 27 | const ( 28 | pixelColor = 255 << 16 // red 29 | ) 30 | 31 | func TestSnake(t *testing.T) { 32 | gpioPin := flag.Int("gpio-pin", 18, "GPIO pin") 33 | width := flag.Int("width", 32, "LED matrix width") 34 | height := flag.Int("height", 8, "LED matrix height") 35 | brightness := flag.Int("brightness", 64, "Brightness (0-255)") 36 | 37 | flag.Parse() 38 | 39 | user, err := user.Current() 40 | if err != nil { 41 | t.Fatal(err) 42 | } 43 | 44 | if runtime.GOARCH == "arm" && user.Uid != "0" { 45 | fmt.Println("This test requires root privilege") 46 | fmt.Println("Please try \"sudo go test -v\"") 47 | t.SkipNow() 48 | } 49 | 50 | size := *width * *height 51 | opt := DefaultOptions 52 | opt.Channels[0].Brightness = *brightness 53 | opt.Channels[0].LedCount = size 54 | opt.Channels[0].GpioPin = *gpioPin 55 | 56 | ws, err := MakeWS2811(&opt) 57 | if err != nil { 58 | t.Fatal(err) 59 | } 60 | 61 | err = ws.Init() 62 | if err != nil { 63 | t.Fatal(err) 64 | } 65 | 66 | bitmap := make([]uint32, size) 67 | 68 | for i := 0; i < size; i++ { 69 | if i > 0 { 70 | bitmap[i-1] = 0 71 | } 72 | 73 | bitmap[i] = pixelColor 74 | copy(ws.Leds(0), bitmap) 75 | assert.Nil(t, ws.Render()) 76 | assert.Nil(t, ws.Wait()) 77 | } 78 | 79 | for i := 0; i < len(ws.Leds(0)); i++ { 80 | ws.Leds(0)[i] = 0 81 | } 82 | assert.Nil(t, ws.Render()) 83 | assert.Nil(t, ws.Wait()) 84 | ws.Fini() 85 | } 86 | -------------------------------------------------------------------------------- /build-examples.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | docker buildx build --no-cache --platform linux/arm/v7 -t rpi-ws281x-builder-armv7 --load --file docker/app-builder/Dockerfile . 4 | docker buildx build --no-cache --platform linux/arm64 -t rpi-ws281x-builder-arm64 --load --file docker/app-builder/Dockerfile . 5 | 6 | for i in examples/*; do 7 | app=$(basename $i) 8 | docker run --platform linux/arm/v7 --rm -v "$(pwd)/$i":"/usr/src/$app" -w /usr/src/$app rpi-ws281x-builder-armv7 go build -o "$app-armv7" -v 9 | docker run --platform linux/arm64 --rm -v "$(pwd)/$i":"/usr/src/$app" -w /usr/src/$app rpi-ws281x-builder-arm64 go build -o "$app-arm64" -v 10 | done 11 | 12 | file examples/*/*-armv7 13 | file examples/*/*-arm64 14 | -------------------------------------------------------------------------------- /clean-example.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | docker rmi rpi-ws281x-builder-armv7 4 | docker rmi rpi-ws281x-builder-arm64 5 | 6 | rm examples/*/*-armv7 7 | rm examples/*/*-arm64 8 | -------------------------------------------------------------------------------- /docker/app-builder/Dockerfile: -------------------------------------------------------------------------------- 1 | # Stage 0 : Build the C library 2 | 3 | FROM debian AS lib_builder 4 | 5 | WORKDIR /foundry 6 | 7 | RUN apt-get update -y && apt-get install -y \ 8 | build-essential \ 9 | cmake \ 10 | git 11 | 12 | RUN git clone https://github.com/jgarff/rpi_ws281x.git \ 13 | && cd rpi_ws281x \ 14 | && mkdir build \ 15 | && cd build \ 16 | && cmake -D BUILD_SHARED=OFF -D BUILD_TEST=OFF .. \ 17 | && cmake --build . \ 18 | && make install 19 | 20 | # Stage 1 : Build a go image with the rpi_ws281x C library 21 | 22 | FROM golang:latest 23 | COPY --from=lib_builder /usr/local/lib/libws2811.a /usr/local/lib/ 24 | COPY --from=lib_builder /usr/local/include/ws2811 /usr/local/include/ws2811 25 | 26 | WORKDIR /go/src/rpi-ws281x-go 27 | COPY go.mod . 28 | COPY go.sum . 29 | COPY *.go ./ 30 | -------------------------------------------------------------------------------- /examples/color_wipe/color_wipe.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "time" 19 | 20 | ws2811 "github.com/rpi-ws281x/rpi-ws281x-go" 21 | ) 22 | 23 | const ( 24 | brightness = 90 25 | ledCounts = 64 26 | sleepTime = 50 27 | ) 28 | 29 | type wsEngine interface { 30 | Init() error 31 | Render() error 32 | Wait() error 33 | Fini() 34 | Leds(channel int) []uint32 35 | } 36 | 37 | func checkError(err error) { 38 | if err != nil { 39 | panic(err) 40 | } 41 | } 42 | 43 | type colorWipe struct { 44 | ws wsEngine 45 | } 46 | 47 | func (cw *colorWipe) setup() error { 48 | return cw.ws.Init() 49 | } 50 | 51 | func (cw *colorWipe) display(color uint32) error { 52 | for i := 0; i < len(cw.ws.Leds(0)); i++ { 53 | cw.ws.Leds(0)[i] = color 54 | if err := cw.ws.Render(); err != nil { 55 | return err 56 | } 57 | time.Sleep(sleepTime * time.Millisecond) 58 | } 59 | return nil 60 | } 61 | 62 | func main() { 63 | opt := ws2811.DefaultOptions 64 | opt.Channels[0].Brightness = brightness 65 | opt.Channels[0].LedCount = ledCounts 66 | 67 | dev, err := ws2811.MakeWS2811(&opt) 68 | checkError(err) 69 | 70 | cw := &colorWipe{ 71 | ws: dev, 72 | } 73 | checkError(cw.setup()) 74 | defer dev.Fini() 75 | 76 | cw.display(uint32(0x0000ff)) 77 | cw.display(uint32(0x00ff00)) 78 | cw.display(uint32(0xff0000)) 79 | cw.display(uint32(0x000000)) 80 | 81 | } 82 | -------------------------------------------------------------------------------- /examples/color_wipe/go.mod: -------------------------------------------------------------------------------- 1 | module color_wipe 2 | 3 | go 1.20 4 | 5 | require github.com/rpi-ws281x/rpi-ws281x-go v1.0.8 6 | 7 | require github.com/pkg/errors v0.9.1 // indirect 8 | -------------------------------------------------------------------------------- /examples/color_wipe/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 5 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 6 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 7 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 8 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 9 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 10 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 11 | github.com/rpi-ws281x/rpi-ws281x-go v1.0.8 h1:fyyjhZTi2vq5XR20dvM0VHAIrYePE5R7SssUAgZoHH8= 12 | github.com/rpi-ws281x/rpi-ws281x-go v1.0.8/go.mod h1:bYxseeVKIJy6hmt/UFJ7ty2ZbpZ5UMHNSTWT5rrmxjg= 13 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 14 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 15 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 16 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 17 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 18 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 19 | gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= 20 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 21 | -------------------------------------------------------------------------------- /examples/hw/go.mod: -------------------------------------------------------------------------------- 1 | module hw 2 | 3 | go 1.20 4 | 5 | require github.com/rpi-ws281x/rpi-ws281x-go v1.0.8 6 | 7 | require github.com/pkg/errors v0.9.1 // indirect 8 | -------------------------------------------------------------------------------- /examples/hw/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 5 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 6 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 7 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 8 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 9 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 10 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 11 | github.com/rpi-ws281x/rpi-ws281x-go v1.0.8 h1:fyyjhZTi2vq5XR20dvM0VHAIrYePE5R7SssUAgZoHH8= 12 | github.com/rpi-ws281x/rpi-ws281x-go v1.0.8/go.mod h1:bYxseeVKIJy6hmt/UFJ7ty2ZbpZ5UMHNSTWT5rrmxjg= 13 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 14 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 15 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 16 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 17 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 18 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 19 | gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= 20 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 21 | -------------------------------------------------------------------------------- /examples/hw/hw.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "fmt" 19 | 20 | ws2811 "github.com/rpi-ws281x/rpi-ws281x-go" 21 | ) 22 | 23 | func main() { 24 | fmt.Println("*****************************") 25 | fmt.Println("* rpi_ws281x Hardware Check *") 26 | fmt.Println("*****************************") 27 | hw := ws2811.HwDetect() 28 | fmt.Printf("Hardware Type : %d\n", hw.Type) 29 | fmt.Printf("Hardware Version : 0x%08X\n", hw.Version) 30 | fmt.Printf("Periph base : 0x%08X\n", hw.PeriphBase) 31 | fmt.Printf("Video core base : 0x%08X\n", hw.VideocoreBase) 32 | fmt.Printf("Description : %v\n", hw.Desc) 33 | } 34 | -------------------------------------------------------------------------------- /examples/invader8x8/go.mod: -------------------------------------------------------------------------------- 1 | module invader8x8 2 | 3 | go 1.20 4 | 5 | require github.com/rpi-ws281x/rpi-ws281x-go v1.0.8 6 | 7 | require github.com/pkg/errors v0.9.1 // indirect 8 | -------------------------------------------------------------------------------- /examples/invader8x8/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 5 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 6 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 7 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 8 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 9 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 10 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 11 | github.com/rpi-ws281x/rpi-ws281x-go v1.0.8 h1:fyyjhZTi2vq5XR20dvM0VHAIrYePE5R7SssUAgZoHH8= 12 | github.com/rpi-ws281x/rpi-ws281x-go v1.0.8/go.mod h1:bYxseeVKIJy6hmt/UFJ7ty2ZbpZ5UMHNSTWT5rrmxjg= 13 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 14 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 15 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 16 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 17 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 18 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 19 | gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= 20 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 21 | -------------------------------------------------------------------------------- /examples/invader8x8/invader0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rpi-ws281x/rpi-ws281x-go/247b076ddb3cdee0c9c7cb8c8f99fe6b86831603/examples/invader8x8/invader0.png -------------------------------------------------------------------------------- /examples/invader8x8/invader1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rpi-ws281x/rpi-ws281x-go/247b076ddb3cdee0c9c7cb8c8f99fe6b86831603/examples/invader8x8/invader1.png -------------------------------------------------------------------------------- /examples/invader8x8/invader8x8.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "encoding/base64" 19 | "image" 20 | _ "image/png" 21 | "strings" 22 | "time" 23 | 24 | ws2811 "github.com/rpi-ws281x/rpi-ws281x-go" 25 | ) 26 | 27 | const ( 28 | brightness = 90 29 | width = 8 30 | height = 8 31 | ledCounts = width * height 32 | maxCount = 50 33 | sleepTime = 200 34 | ) 35 | 36 | type wsEngine interface { 37 | Init() error 38 | Render() error 39 | Wait() error 40 | Fini() 41 | Leds(channel int) []uint32 42 | } 43 | 44 | func check(err error) { 45 | if err != nil { 46 | panic(err) 47 | } 48 | } 49 | 50 | type invader struct { 51 | img []image.Image 52 | current int 53 | ws wsEngine 54 | } 55 | 56 | func (inv *invader) setup(images ...string) error { 57 | inv.img = make([]image.Image, len(images)) 58 | for i, data := range images { 59 | var err error 60 | r := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data)) 61 | inv.img[i], _, err = image.Decode(r) 62 | if err != nil { 63 | return err 64 | } 65 | } 66 | inv.current = 0 67 | return inv.ws.Init() 68 | } 69 | 70 | func coordinatesToIndex(bounds image.Rectangle, x int, y int) int { 71 | if x%2 == 0 { 72 | return (x-bounds.Min.X)*height + (y - bounds.Min.Y) 73 | } 74 | return (x-bounds.Min.X)*height + (height - 1) - (y - bounds.Min.Y) 75 | } 76 | 77 | func rgbToColor(r uint32, g uint32, b uint32) uint32 { 78 | return ((r>>8)&0xff)<<16 + ((g>>8)&0xff)<<8 + ((b >> 8) & 0xff) 79 | } 80 | 81 | func (inv *invader) display() error { 82 | bounds := inv.img[inv.current].Bounds() 83 | for y := bounds.Min.Y; y < bounds.Max.Y; y++ { 84 | for x := bounds.Min.X; x < bounds.Max.X; x++ { 85 | r, g, b, _ := inv.img[inv.current].At(x, y).RGBA() 86 | inv.ws.Leds(0)[coordinatesToIndex(bounds, x, y)] = rgbToColor(r, g, b) 87 | } 88 | } 89 | return inv.ws.Render() 90 | } 91 | 92 | func (inv *invader) next() { 93 | inv.current = (inv.current + 1) % len(inv.img) 94 | } 95 | 96 | func main() { 97 | opt := ws2811.DefaultOptions 98 | opt.Channels[0].Brightness = brightness 99 | opt.Channels[0].LedCount = ledCounts 100 | 101 | dev, err := ws2811.MakeWS2811(&opt) 102 | check(err) 103 | 104 | inv := &invader{ 105 | ws: dev, 106 | } 107 | check(inv.setup(invader0Png, invader1Png)) 108 | defer dev.Fini() 109 | 110 | for count := 0; count < maxCount; count++ { 111 | inv.display() 112 | inv.next() 113 | time.Sleep(sleepTime * time.Millisecond) 114 | } 115 | } 116 | 117 | const invader0Png = ` 118 | iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAACXBIWXMAABLqAAAS6gEWyM/fAAAAB3RJ 119 | TUUH4gEfAAwq1Uz4igAAAExJREFUCNdtjLENwDAIBM9IP0T2yEBu2CsDuWEKNwyRwg1CpuGOFz8kAcCa 120 | G3i/56jVawWrUjNrDQfW3CM8uY4kSeHZgCo146z2EZ4/za8lhsTPr+cAAAAASUVORK5CYII= 121 | ` 122 | 123 | const invader1Png = ` 124 | iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMA 125 | ABLqAAAS6gEWyM/fAAAAB3RJTUUH4gEfCgAfIt32cwAAAFZJREFUCNdjZGVlZWBgYGBgOJPwnIGBwWSB 126 | JITLhCyKzGBC5iDLMUEovVnCEFEI40zCc8aLqW8YsAJWVlZWVtaLqW/QGAwQV0GEIKIQLgNcFFkHAwMD 127 | AIRcIQCYxqiMAAAAAElFTkSuQmCC 128 | ` 129 | -------------------------------------------------------------------------------- /examples/swiss/go.mod: -------------------------------------------------------------------------------- 1 | module swiss 2 | 3 | go 1.20 4 | 5 | require github.com/rpi-ws281x/rpi-ws281x-go v1.0.8 6 | 7 | require github.com/pkg/errors v0.9.1 // indirect 8 | -------------------------------------------------------------------------------- /examples/swiss/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 5 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 6 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 7 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 8 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 9 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 10 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 11 | github.com/rpi-ws281x/rpi-ws281x-go v1.0.8 h1:fyyjhZTi2vq5XR20dvM0VHAIrYePE5R7SssUAgZoHH8= 12 | github.com/rpi-ws281x/rpi-ws281x-go v1.0.8/go.mod h1:bYxseeVKIJy6hmt/UFJ7ty2ZbpZ5UMHNSTWT5rrmxjg= 13 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 14 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 15 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 16 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 17 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 18 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 19 | gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= 20 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 21 | -------------------------------------------------------------------------------- /examples/swiss/swiss.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | ws2811 "github.com/rpi-ws281x/rpi-ws281x-go" 19 | ) 20 | 21 | const ( 22 | brightness = 128 23 | width = 8 24 | height = 8 25 | ledCounts = width * height 26 | ) 27 | 28 | func checkError(err error) { 29 | if err != nil { 30 | panic(err) 31 | } 32 | } 33 | 34 | func main() { 35 | opt := ws2811.DefaultOptions 36 | opt.Channels[0].Brightness = brightness 37 | opt.Channels[0].LedCount = ledCounts 38 | 39 | dev, err := ws2811.MakeWS2811(&opt) 40 | checkError(err) 41 | 42 | checkError(dev.Init()) 43 | defer dev.Fini() 44 | 45 | for x := 0; x < width; x++ { 46 | for y := 0; y < height; y++ { 47 | color := uint32(0xff0000) 48 | if x > 2 && x < 5 && y > 0 && y < 7 { 49 | color = 0xffffff 50 | } 51 | if x > 0 && x < 7 && y > 2 && y < 5 { 52 | color = 0xffffff 53 | } 54 | dev.Leds(0)[x*height+y] = color 55 | } 56 | } 57 | checkError(dev.Render()) 58 | 59 | } 60 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | go 1.20 2 | 3 | module github.com/rpi-ws281x/rpi-ws281x-go 4 | 5 | require ( 6 | github.com/pkg/errors v0.9.1 7 | github.com/stretchr/testify v1.4.0 8 | ) 9 | 10 | require ( 11 | github.com/davecgh/go-spew v1.1.1 // indirect 12 | github.com/kr/pretty v0.1.0 // indirect 13 | github.com/pmezard/go-difflib v1.0.0 // indirect 14 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect 15 | gopkg.in/yaml.v2 v2.2.4 // indirect 16 | ) 17 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 5 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 6 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 7 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 8 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 9 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 10 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 11 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 12 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 13 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 14 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 15 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 16 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 17 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= 18 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 19 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 20 | gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= 21 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 22 | -------------------------------------------------------------------------------- /ws2811.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Interface to ws2811 chip (neopixel driver). Make sure that you have 16 | // ws2811.h and pwm.h in a GCC include path (e.g. /usr/local/include) and 17 | // libws2811.a in a GCC library path (e.g. /usr/local/lib). 18 | // See https://github.com/jgarff/rpi_ws281x for instructions 19 | 20 | package ws2811 21 | 22 | import "github.com/pkg/errors" 23 | 24 | const ( 25 | // DefaultDmaNum is the default DMA number. 26 | DefaultDmaNum = 10 27 | // RpiPwmChannels is the number of PWM leds in the Raspberry Pi 28 | RpiPwmChannels = 2 29 | // TargetFreq is the target frequency. It is usually 800kHz (800000), and an go as low as 400000 30 | TargetFreq = 800000 31 | // DefaultGpioPin is the default pin on the Raspberry Pi where the signal will be available. Note 32 | // that it is the BCM (Broadcom Pin Number) and the "Pin" 18 is actually the physical pin 12 of the 33 | // Raspberry Pi. 34 | DefaultGpioPin = 18 35 | // DefaultLedCount is the default number of LEDs on the stripe. 36 | DefaultLedCount = 16 37 | // DefaultBrightness is the default maximum brightness of the LEDs. The brightness value can be between 0 and 255. 38 | // If the brightness is too low, the LEDs remain dark. If the brightness is too high, the system needs too much 39 | // current. 40 | DefaultBrightness = 64 // Safe value between 0 and 255. 41 | ) 42 | 43 | const ( 44 | // HwVerTypeUnknown represents unknown hardware 45 | HwVerTypeUnknown = 0 46 | // HwVerTypePi1 represents the Raspberry Pi 1 47 | HwVerTypePi1 = 1 48 | // HwVerTypePi2 represents the Raspberry Pi 2 49 | HwVerTypePi2 = 2 50 | ) 51 | 52 | // StateDesc is a map from a return state to its string description. 53 | //nolint: gochecknoglobals 54 | var StateDesc = map[int]string{ 55 | 0: "Success", 56 | -1: "Generic failure", 57 | -2: "Out of memory", 58 | -3: "Hardware revision is not supported", 59 | -4: "Memory lock failed", 60 | -5: "mmap() failed", 61 | -6: "Unable to map registers into userspace", 62 | -7: "Unable to initialize GPIO", 63 | -8: "Unable to initialize PWM", 64 | -9: "Failed to create mailbox device", 65 | -10: "DMA error", 66 | -11: "Selected GPIO not possible", 67 | -12: "Unable to initialize PCM", 68 | -13: "Unable to initialize SPI", 69 | -14: "SPI transfer error", 70 | } 71 | 72 | // HwDesc is the Hardware Description 73 | type HwDesc struct { 74 | Type uint32 75 | Version uint32 76 | PeriphBase uint32 77 | VideocoreBase uint32 78 | Desc string 79 | } 80 | 81 | // ChannelOption is the list of channel options 82 | type ChannelOption struct { 83 | // GpioPin is the GPIO Pin with PWM alternate function, 0 if unused 84 | GpioPin int 85 | // Invert inverts output signal 86 | Invert bool 87 | // LedCount is the number of LEDs, 0 if channel is unused 88 | LedCount int 89 | // StripeType is the strip color layout -- one of WS2811StripXXX constants 90 | StripeType int 91 | // Brightness is the maximum brightness of the LEDs. Value between 0 and 255 92 | Brightness int 93 | // WShift is the white shift value 94 | WShift int 95 | // RShift is the red shift value 96 | RShift int 97 | // GShift is the green shift value 98 | GShift int 99 | // BShift is blue shift value 100 | BShift int 101 | // Gamma is the gamma correction table 102 | Gamma []byte 103 | } 104 | 105 | // Option is the list of device options 106 | type Option struct { 107 | // RenderWaitTime is the time in µs before the next render can run 108 | RenderWaitTime int 109 | // Frequency is the required output frequency 110 | Frequency int 111 | // DmaNum is the number of a DMA _not_ already in use 112 | DmaNum int 113 | // Channels are channel options 114 | Channels []ChannelOption 115 | } 116 | 117 | // DefaultOptions defines sensible default options for MakeWS2811 118 | //nolint: gochecknoglobals 119 | var DefaultOptions = Option{ 120 | Frequency: TargetFreq, 121 | DmaNum: DefaultDmaNum, 122 | Channels: []ChannelOption{ 123 | { 124 | GpioPin: DefaultGpioPin, 125 | LedCount: DefaultLedCount, 126 | Brightness: DefaultBrightness, 127 | StripeType: WS2812Strip, 128 | Invert: false, 129 | Gamma: gamma8, 130 | }, 131 | }, 132 | } 133 | 134 | // Leds returns the LEDs array of a given channel 135 | func (ws2811 *WS2811) Leds(channel int) []uint32 { 136 | return ws2811.leds[channel] 137 | } 138 | 139 | // SetLedsSync wait for the frame to finish and replace all the LEDs 140 | func (ws2811 *WS2811) SetLedsSync(channel int, leds []uint32) error { 141 | if err := ws2811.Wait(); err != nil { 142 | return errors.WithMessage(err, "Error setting LEDs") 143 | } 144 | 145 | l := len(leds) 146 | 147 | if l > len(ws2811.leds[channel]) { 148 | return errors.New("Error: Too many LEDs") 149 | } 150 | 151 | for i := 0; i < l; i++ { 152 | ws2811.leds[channel][i] = leds[i] 153 | } 154 | 155 | return nil 156 | } 157 | 158 | // StatusDesc returns the description of a status code 159 | func StatusDesc(code int) string { 160 | desc, ok := StateDesc[code] 161 | if ok { 162 | return desc 163 | } 164 | 165 | return "Unknown" 166 | } 167 | -------------------------------------------------------------------------------- /ws2811_gamma8.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package ws2811 16 | 17 | //nolint: gochecknoglobals 18 | var gamma8 = []byte{ 19 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 21 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 22 | 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 23 | 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 24 | 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 25 | 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, 26 | 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, 27 | 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, 28 | 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, 29 | 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, 30 | 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114, 31 | 115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142, 32 | 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175, 33 | 177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213, 34 | 215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255, 35 | } 36 | -------------------------------------------------------------------------------- /ws2811_hw.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Interface to ws2811 chip (neopixel driver). Make sure that you have 16 | // ws2811.h and pwm.h in a GCC include path (e.g. /usr/local/include) and 17 | // libws2811.a in a GCC library path (e.g. /usr/local/lib). 18 | // See https://github.com/jgarff/rpi_ws281x for instructions 19 | 20 | // +build arm arm64 21 | 22 | package ws2811 23 | 24 | // #cgo CFLAGS: -std=c99 -I /usr/local/include/ws2811 -I /usr/include/ws2811 25 | // #cgo LDFLAGS: -lws2811 -lm 26 | // #include 27 | // #include 28 | // #include 29 | // #include 30 | import "C" 31 | 32 | import ( 33 | "errors" 34 | "fmt" 35 | "unsafe" 36 | ) 37 | 38 | // WS2811 represent the ws2811 device 39 | type WS2811 struct { 40 | dev *C.ws2811_t 41 | initialized bool 42 | leds [][]uint32 43 | } 44 | 45 | // HwDetect gives information about the hardware 46 | func HwDetect() HwDesc { 47 | hw := unsafe.Pointer(C.rpi_hw_detect()) // nolint: gas 48 | return HwDesc{ 49 | Type: uint32((*C.rpi_hw_t)(hw)._type), 50 | Version: uint32((*C.rpi_hw_t)(hw).hwver), 51 | PeriphBase: uint32((*C.rpi_hw_t)(hw).periph_base), 52 | VideocoreBase: uint32((*C.rpi_hw_t)(hw).videocore_base), 53 | Desc: C.GoString((*C.rpi_hw_t)(hw).desc), 54 | } 55 | } 56 | 57 | // MakeWS2811 create an instance of WS2811. 58 | func MakeWS2811(opt *Option) (ws2811 *WS2811, err error) { 59 | ws2811 = &WS2811{ 60 | initialized: false, 61 | } 62 | if ws2811 == nil { 63 | err = errors.New("unable to allocate memory") 64 | return nil, err 65 | } 66 | // Allocate and reset structure 67 | ws2811.dev = (*C.ws2811_t)(C.malloc(C.sizeof_ws2811_t)) 68 | C.memset(unsafe.Pointer(ws2811.dev), 0, C.sizeof_ws2811_t) // nolint: gas 69 | 70 | ws2811.dev.freq = C.uint32_t(opt.Frequency) 71 | ws2811.dev.dmanum = C.int(opt.DmaNum) 72 | 73 | for i, cOpt := range opt.Channels { // nolint: gotype 74 | ws2811.dev.channel[i].gpionum = C.int(cOpt.GpioPin) 75 | ws2811.dev.channel[i].count = C.int(cOpt.LedCount) 76 | ws2811.dev.channel[i].brightness = C.uint8_t(cOpt.Brightness) 77 | ws2811.dev.channel[i].strip_type = C.int(cOpt.StripeType) 78 | ws2811.dev.channel[i].wshift = C.uint8_t(cOpt.WShift) 79 | ws2811.dev.channel[i].rshift = C.uint8_t(cOpt.RShift) 80 | ws2811.dev.channel[i].gshift = C.uint8_t(cOpt.GShift) 81 | ws2811.dev.channel[i].bshift = C.uint8_t(cOpt.BShift) 82 | 83 | if cOpt.Invert { 84 | ws2811.dev.channel[i].invert = C.int(1) 85 | } else { 86 | ws2811.dev.channel[i].invert = C.int(0) 87 | } 88 | if cOpt.Gamma != nil { 89 | // allocate and copy gamma table. The memory will be freed by C.ws2811_fini(). 90 | m := (*C.uint8_t)(C.malloc(C.size_t(256))) 91 | ws2811.dev.channel[i].gamma = m 92 | C.memcpy(unsafe.Pointer(m), unsafe.Pointer(&cOpt.Gamma[0]), C.size_t(256)) // nolint: gas 93 | } 94 | } 95 | return ws2811, err 96 | } 97 | 98 | // Init initialize the device. It should be called only once before any other method. 99 | func (ws2811 *WS2811) Init() error { 100 | if ws2811.initialized { 101 | return errors.New("device already initialized") 102 | } 103 | res := int(C.ws2811_init(ws2811.dev)) 104 | if res != 0 { 105 | return fmt.Errorf("error ws2811.init: %d (%v)", res, StatusDesc(res)) 106 | } 107 | ws2811.initialized = true 108 | ws2811.leds = make([][]uint32, RpiPwmChannels) 109 | for i := 0; i < RpiPwmChannels; i++ { 110 | // var ledsArray *C.ws2811_led_t = C.ws2811_leds(ws2811.dev, C.int(i)) 111 | ledsArray := ws2811.dev.channel[i].leds // nolint: gotype 112 | length := int(ws2811.dev.channel[i].count) // nolint: gotype 113 | if length <= 0 { 114 | continue // spi_init does not initialize channel[1] 115 | } 116 | // convert the led C array into a golang slice: 117 | // https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices 118 | // 1 << 28 is the largest pseudo-size that we can use. If we try a larger number, 119 | // then we get a compile error: "type [N]uint32 too large". 120 | ws2811.leds[i] = (*[1 << 28]uint32)(unsafe.Pointer(ledsArray))[:length:length] // nolint: gas 121 | } 122 | return nil 123 | } 124 | 125 | // SetBrightness changes the brightness of a given channel. Value between 0 and 255 126 | func (ws2811 *WS2811) SetBrightness(channel int, brightness int) { 127 | ws2811.dev.channel[channel].brightness = C.uint8_t(brightness) 128 | } 129 | 130 | // SetCustomGammaFactor sets a custom Gamma correction array based on a gamma correction factor 131 | func (ws2811 *WS2811) SetCustomGammaFactor(gammaFactor float64) { 132 | C.ws2811_set_custom_gamma_factor(ws2811.dev, C.double(gammaFactor)) 133 | } 134 | 135 | // Render sends a complete frame to the LED Matrix 136 | func (ws2811 *WS2811) Render() error { 137 | res := int(C.ws2811_render(ws2811.dev)) 138 | if res != 0 { 139 | return fmt.Errorf("error ws2811.render: %d (%v)", res, StatusDesc(res)) 140 | } 141 | return nil 142 | } 143 | 144 | // Wait waits for render to finish. The time needed for render is given by: 145 | // time = 1/frequency * 8 * 3 * LedCount + 0.05 146 | // (8 is the color depth and 3 is the number of colors (LEDs) per pixel). 147 | // See https://cdn-shop.adafruit.com/datasheets/WS2811.pdf for more details. 148 | func (ws2811 *WS2811) Wait() error { 149 | res := int(C.ws2811_wait(ws2811.dev)) 150 | if res != 0 { 151 | return fmt.Errorf("error ws2811.wait: %d (%v)", res, StatusDesc(res)) 152 | 153 | } 154 | return nil 155 | } 156 | 157 | // Fini shuts down the device and frees memory. 158 | func (ws2811 *WS2811) Fini() { 159 | C.ws2811_fini(ws2811.dev) 160 | // release the memory allocated by MakeWS2811. Note that we should not release 161 | // ws2811.dev.channel[i].gamma (also allocated by MakeWS2811) because C.ws2811_fini 162 | // already releases this data. 163 | C.free(unsafe.Pointer(ws2811.dev)) // nolint: gas 164 | ws2811.initialized = false 165 | } 166 | -------------------------------------------------------------------------------- /ws2811_sim.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Interface to ws2811 chip (neopixel driver). Make sure that you have 16 | // ws2811.h and pwm.h in a GCC include path (e.g. /usr/local/include) and 17 | // libws2811.a in a GCC library path (e.g. /usr/local/lib). 18 | // See https://github.com/jgarff/rpi_ws281x for instructions 19 | 20 | // +build !arm,!arm64 21 | 22 | package ws2811 23 | 24 | import ( 25 | "errors" 26 | ) 27 | 28 | // WS2811 represent the ws2811 device 29 | type WS2811 struct { 30 | initialized bool 31 | leds [][]uint32 32 | opt *Option 33 | } 34 | 35 | // HwDetect gives information about the hardware 36 | func HwDetect() HwDesc { 37 | return HwDesc{ 38 | Type: 0, 39 | Version: 0, 40 | PeriphBase: 0, 41 | VideocoreBase: 0, 42 | Desc: "DUMMY", 43 | } 44 | } 45 | 46 | // MakeWS2811 create an instance of WS2811. 47 | func MakeWS2811(opt *Option) (*WS2811, error) { 48 | ws2811 := &WS2811{ 49 | initialized: false, 50 | opt: opt, 51 | } 52 | 53 | return ws2811, nil 54 | } 55 | 56 | // Init initialize the device. It should be called only once before any other method. 57 | func (ws2811 *WS2811) Init() error { 58 | if ws2811.initialized { 59 | return errors.New("device already initialized") 60 | } 61 | 62 | ws2811.leds = make([][]uint32, RpiPwmChannels) 63 | 64 | for i := 0; i < RpiPwmChannels; i++ { 65 | var ledCount int 66 | if i < len(ws2811.opt.Channels) { 67 | ledCount = ws2811.opt.Channels[i].LedCount 68 | } else { 69 | ledCount = 0 70 | } 71 | 72 | ws2811.leds[i] = make([]uint32, ledCount) 73 | } 74 | 75 | return nil 76 | } 77 | 78 | // SetBrightness changes the brightness of a given channel. Value between 0 and 255 79 | func (ws2811 *WS2811) SetBrightness(channel int, brightness int) {} 80 | 81 | // Render sends a complete frame to the LED Matrix 82 | func (ws2811 *WS2811) Render() error { 83 | return nil 84 | } 85 | 86 | // Wait waits for render to finish. The time needed for render is given by: 87 | // time = 1/frequency * 8 * 3 * LedCount + 0.05 88 | // (8 is the color depth and 3 is the number of colors (LEDs) per pixel). 89 | // See https://cdn-shop.adafruit.com/datasheets/WS2811.pdf for more details. 90 | func (ws2811 *WS2811) Wait() error { 91 | return nil 92 | } 93 | 94 | // Fini shuts down the device and frees memory. 95 | func (ws2811 *WS2811) Fini() { 96 | } 97 | -------------------------------------------------------------------------------- /ws2811_stripe_type.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Jacques Supcik / HEIA-FR 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // This file contains the constants for all ws281x stripe types. 16 | 17 | package ws2811 18 | 19 | // 4 color R, G, B and W ordering 20 | const ( 21 | // SK6812StripRGBW is the RGBW Mode 22 | SK6812StripRGBW = 0x18100800 23 | // SK6812StripRBGW is the StripRBGW Mode 24 | SK6812StripRBGW = 0x18100008 25 | // SK6812StripGRBW is the StripGRBW Mode 26 | SK6812StripGRBW = 0x18081000 27 | // SK6812StrioGBRW is the StrioGBRW Mode 28 | SK6812StrioGBRW = 0x18080010 29 | // SK6812StrioBRGW is the StrioBRGW Mode 30 | SK6812StrioBRGW = 0x18001008 31 | // SK6812StripBGRW is the StripBGRW Mode 32 | SK6812StripBGRW = 0x18000810 33 | // SK6812ShiftWMask is the Shift White Mask 34 | SK6812ShiftWMask = 0xf0000000 35 | ) 36 | 37 | // 3 color R, G and B ordering 38 | const ( 39 | // WS2811StripRGB is the RGB Mode 40 | WS2811StripRGB = 0x100800 41 | // WS2811StripRBG is the RBG Mode 42 | WS2811StripRBG = 0x100008 43 | // WS2811StripGRB is the GRB Mode 44 | WS2811StripGRB = 0x081000 45 | // WS2811StripGBR is the GBR Mode 46 | WS2811StripGBR = 0x080010 47 | // WS2811StripBRG is the BRG Mode 48 | WS2811StripBRG = 0x001008 49 | // WS2811StripBGR is the BGR Mode 50 | WS2811StripBGR = 0x000810 51 | ) 52 | 53 | // Predefined fixed LED types 54 | const ( 55 | // WS2812Strip is the WS2812 Mode 56 | WS2812Strip = WS2811StripGRB 57 | // SK6812Strip is the SK6812 Mode 58 | SK6812Strip = WS2811StripGRB 59 | // SK6812WStrip is the SK6812W Mode 60 | SK6812WStrip = SK6812StripGRBW 61 | ) 62 | --------------------------------------------------------------------------------