├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── blinkt.go └── sysfs ├── blinkt.go └── gpio └── gpio.go /.gitignore: -------------------------------------------------------------------------------- 1 | blinkt_go 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | ### Guidelines 4 | 5 | Guidelines for contributing. 6 | 7 | * Read the DCO below 8 | * Sign off your commits with `git commit -s` 9 | * Show how you have tested the changes 10 | * Provide context - why are these changes needed? 11 | 12 | ### License 13 | 14 | This project is licensed under the MIT License. 15 | 16 | #### Copyright notice 17 | 18 | Please add a Copyright notice to new files you add where this is not already present: 19 | 20 | ``` 21 | // Copyright (c) Alex Ellis 2018. All rights reserved. 22 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. 23 | ``` 24 | 25 | #### Sign your work 26 | 27 | > Note: all of the commits in your PR/Patch must be signed-off. 28 | 29 | The sign-off is a simple line at the end of the explanation for a patch. Your 30 | signature certifies that you wrote the patch or otherwise have the right to pass 31 | it on as an open-source patch. The rules are pretty simple: if you can certify 32 | the below (from [developercertificate.org](http://developercertificate.org/)): 33 | 34 | ``` 35 | Developer Certificate of Origin 36 | Version 1.1 37 | 38 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 39 | 1 Letterman Drive 40 | Suite D4700 41 | San Francisco, CA, 94129 42 | 43 | Everyone is permitted to copy and distribute verbatim copies of this 44 | license document, but changing it is not allowed. 45 | 46 | Developer's Certificate of Origin 1.1 47 | 48 | By making a contribution to this project, I certify that: 49 | 50 | (a) The contribution was created in whole or in part by me and I 51 | have the right to submit it under the open source license 52 | indicated in the file; or 53 | 54 | (b) The contribution is based upon previous work that, to the best 55 | of my knowledge, is covered under an appropriate open source 56 | license and I have the right under that license to submit that 57 | work with modifications, whether created in whole or in part 58 | by me, under the same open source license (unless I am 59 | permitted to submit under a different license), as indicated 60 | in the file; or 61 | 62 | (c) The contribution was provided directly to me by some other 63 | person who certified (a), (b) or (c) and I have not modified 64 | it. 65 | 66 | (d) I understand and agree that this project and the contribution 67 | are public and that a record of the contribution (including all 68 | personal information I submit with it, including my sign-off) is 69 | maintained indefinitely and may be redistributed consistent with 70 | this project or the open source license(s) involved. 71 | ``` 72 | 73 | Then you just add a line to every git commit message: 74 | 75 | Signed-off-by: Joe Smith 76 | 77 | Use your real name (sorry, no pseudonyms or anonymous contributions.) 78 | 79 | If you set your `user.name` and `user.email` git configs, you can sign your 80 | commit automatically with `git commit -s`. 81 | 82 | * Please sign your commits with `git commit -s` so that commits are traceable. 83 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Alex Ellis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | blinkt_go 2 | ========= 3 | 4 | > This library is work-in-progress and makes use of WiringPi and the Golang `rpi` library. 5 | 6 | ## Instructions: 7 | 8 | ### Install Go 9 | 10 | If you don't have Go on the Pi, download it from: https://golang.org/dl/ and pick the armv6l edition. 11 | 12 | ``` 13 | sudo tar -xvf go1.7.4.linux-armv6l.tar.gz -C /usr/local/ 14 | export GOPATH=$HOME/go 15 | ``` 16 | 17 | ### Install WiringPi 18 | ``` 19 | # sudo apt-get install -qy wiringpi 20 | ``` 21 | 22 | ### Install and build blinkt! library 23 | 24 | ``` 25 | # export GOPATH=$HOME/go/ 26 | # mkdir -p $GOPATH/src/github.com/alexellis/ 27 | # cd $GOPATH/src/github.com/alexellis/ 28 | 29 | # git clone https://github.com/alexellis/blinkt_go && cd blinkt_go 30 | 31 | # go get 32 | # go build 33 | # sudo ./blinkt_go 34 | ``` 35 | 36 | ## sysfs implementation and Docker Swarm 37 | 38 | Docker Swarm cannot run the main version of this library because WiringPi needs elevated privileges. 39 | 40 | To use this library with Docker Swarm please use the version of Blinkt in the sysfs package. 41 | 42 | > For more information on sysfs see: http://elinux.org/RPi_GPIO_Code_Samples#sysfs 43 | 44 | 45 | ## Related: 46 | 47 | * [Blinkt Golang examples programs](https://github.com/alexellis/blinkt_go_examples) 48 | * [Golang rpi library](https://github.com/alexellis/rpi/) 49 | -------------------------------------------------------------------------------- /blinkt.go: -------------------------------------------------------------------------------- 1 | package blinkt 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | "os/signal" 8 | "time" 9 | 10 | "github.com/alexellis/rpi" 11 | ) 12 | 13 | const DAT int = 23 14 | const CLK int = 24 15 | 16 | const redIndex int = 0 17 | const greenIndex int = 1 18 | const blueIndex int = 2 19 | const brightnessIndex int = 3 20 | 21 | // default raw brightness. Not to be used user-side 22 | const defaultBrightnessInt int = 15 23 | 24 | //upper and lower bounds for user specified brightness 25 | const minBrightness float64 = 0.0 26 | const maxBrightness float64 = 1.0 27 | 28 | // pulse sends a pulse through the DAT/CLK pins 29 | func pulse(pulses int) { 30 | rpi.DigitalWrite(rpi.GpioToPin(DAT), 0) 31 | for i := 0; i < pulses; i++ { 32 | rpi.DigitalWrite(rpi.GpioToPin(CLK), 1) 33 | rpi.DigitalWrite(rpi.GpioToPin(CLK), 0) 34 | } 35 | } 36 | 37 | // eof end of file or signal, from Python library 38 | func eof() { 39 | pulse(36) 40 | } 41 | 42 | // sof start of file (name from Python library) 43 | func sof() { 44 | pulse(32) 45 | } 46 | 47 | func writeByte(val int) { 48 | for i := 0; i < 8; i++ { 49 | // 0b10000000 = 128 50 | rpi.DigitalWrite(rpi.GpioToPin(DAT), val&128) 51 | rpi.DigitalWrite(rpi.GpioToPin(CLK), 1) 52 | val = val << 1 53 | rpi.DigitalWrite(rpi.GpioToPin(CLK), 0) 54 | } 55 | } 56 | 57 | func convertBrightnessToInt(brightness float64) int { 58 | 59 | if !inRangeFloat(minBrightness, brightness, maxBrightness) { 60 | log.Fatalf("Supplied brightness was %#v - value should be between: %#v and %#v", brightness, minBrightness, maxBrightness) 61 | } 62 | 63 | return int(brightness * 31.0) 64 | 65 | } 66 | 67 | func inRangeFloat(minVal float64, testVal float64, maxVal float64) bool { 68 | 69 | return (testVal >= minVal) && (testVal <= maxVal) 70 | } 71 | 72 | // SetClearOnExit turns all pixels off on Control + C / os.Interrupt signal. 73 | func (bl *Blinkt) SetClearOnExit(clearOnExit bool) { 74 | 75 | if clearOnExit { 76 | 77 | signalChan := make(chan os.Signal, 1) 78 | signal.Notify(signalChan, os.Interrupt) 79 | fmt.Println("Press Control + C to stop") 80 | 81 | go func() { 82 | for range signalChan { 83 | bl.Clear() 84 | bl.Show() 85 | os.Exit(1) 86 | } 87 | }() 88 | } 89 | } 90 | 91 | // Delay maps to time.Sleep, for ms milliseconds 92 | func Delay(ms int) { 93 | time.Sleep(time.Duration(ms) * time.Millisecond) 94 | } 95 | 96 | // Clear sets all the pixels to off, you still have to call Show. 97 | func (bl *Blinkt) Clear() { 98 | r := 0 99 | g := 0 100 | b := 0 101 | bl.SetAll(r, g, b) 102 | } 103 | 104 | // Show updates the LEDs with the values from SetPixel/Clear. 105 | func (bl *Blinkt) Show() { 106 | sof() 107 | for p, _ := range bl.pixels { 108 | brightness := bl.pixels[p][brightnessIndex] 109 | r := bl.pixels[p][redIndex] 110 | g := bl.pixels[p][greenIndex] 111 | b := bl.pixels[p][blueIndex] 112 | 113 | // 0b11100000 (224) 114 | bitwise := 224 115 | writeByte(bitwise | brightness) 116 | writeByte(b) 117 | writeByte(g) 118 | writeByte(r) 119 | } 120 | eof() 121 | } 122 | 123 | // SetAll sets all pixels to specified r, g, b colour. Show must be called to update the LEDs. 124 | func (bl *Blinkt) SetAll(r int, g int, b int) *Blinkt { 125 | 126 | for p, _ := range bl.pixels { 127 | bl.SetPixel(p, r, g, b) 128 | } 129 | 130 | return bl 131 | } 132 | 133 | // SetPixel sets an individual pixel to specified r, g, b colour. Show must be called to update the LEDs. 134 | func (bl *Blinkt) SetPixel(p int, r int, g int, b int) *Blinkt { 135 | 136 | bl.pixels[p][redIndex] = r 137 | bl.pixels[p][greenIndex] = g 138 | bl.pixels[p][blueIndex] = b 139 | 140 | return bl 141 | 142 | } 143 | 144 | // SetBrightness sets the brightness of all pixels. Brightness supplied should be between: 0.0 to 1.0 145 | func (bl *Blinkt) SetBrightness(brightness float64) *Blinkt { 146 | 147 | brightnessInt := convertBrightnessToInt(brightness) 148 | 149 | for p, _ := range bl.pixels { 150 | bl.pixels[p][brightnessIndex] = brightnessInt 151 | } 152 | 153 | return bl 154 | } 155 | 156 | // SetPixelBrightness sets the brightness of pixel p. Brightness supplied should be between: 0.0 to 1.0 157 | func (bl *Blinkt) SetPixelBrightness(p int, brightness float64) *Blinkt { 158 | 159 | brightnessInt := convertBrightnessToInt(brightness) 160 | bl.pixels[p][brightnessIndex] = brightnessInt 161 | return bl 162 | } 163 | 164 | func initPixels(brightness int) [8][4]int { 165 | var pixels [8][4]int 166 | for p, _ := range pixels { 167 | pixels[p][redIndex] = 0 168 | pixels[p][greenIndex] = 0 169 | pixels[p][blueIndex] = 0 170 | pixels[p][brightnessIndex] = brightness 171 | } 172 | return pixels 173 | } 174 | 175 | // Setup initializes GPIO via WiringPi base library. 176 | func (bl *Blinkt) Setup() { 177 | rpi.WiringPiSetup() 178 | rpi.PinMode(rpi.GpioToPin(DAT), rpi.OUTPUT) 179 | rpi.PinMode(rpi.GpioToPin(CLK), rpi.OUTPUT) 180 | } 181 | 182 | // NewBlinkt creates a Blinkt to interact with. You must call "Setup()" immediately afterwards. 183 | func NewBlinkt(brightness ...float64) Blinkt { 184 | 185 | //brightness is optional so set the default 186 | brightnessInt := defaultBrightnessInt 187 | 188 | //over-ride the default if the user has supplied a brightness value 189 | if len(brightness) > 0 { 190 | brightnessInt = convertBrightnessToInt(brightness[0]) 191 | } 192 | return Blinkt{ 193 | pixels: initPixels(brightnessInt), 194 | } 195 | } 196 | 197 | // Blinkt use the NewBlinkt function to initialize the pixels property. 198 | type Blinkt struct { 199 | pixels [8][4]int 200 | } 201 | 202 | func init() { 203 | 204 | } 205 | -------------------------------------------------------------------------------- /sysfs/blinkt.go: -------------------------------------------------------------------------------- 1 | package sysfs 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | "os/signal" 8 | "time" 9 | 10 | "github.com/alexellis/blinkt_go/sysfs/gpio" 11 | ) 12 | 13 | const DAT int = 23 14 | const CLK int = 24 15 | 16 | const redIndex int = 0 17 | const greenIndex int = 1 18 | const blueIndex int = 2 19 | const brightnessIndex int = 3 20 | 21 | // default raw brightness. Not to be used user-side 22 | const defaultBrightnessInt int = 15 23 | 24 | //upper and lower bounds for user specified brightness 25 | const minBrightness float64 = 0.0 26 | const maxBrightness float64 = 1.0 27 | 28 | func writeByte(val int) { 29 | for i := 0; i < 8; i++ { 30 | // 0b10000000 = 128 31 | gpio.DigitalWrite(gpio.GpioToPin(DAT), val&128) 32 | gpio.DigitalWrite(gpio.GpioToPin(CLK), 1) 33 | val = val << 1 34 | gpio.DigitalWrite(gpio.GpioToPin(CLK), 0) 35 | } 36 | } 37 | 38 | func convertBrightnessToInt(brightness float64) int { 39 | 40 | if !inRangeFloat(minBrightness, brightness, maxBrightness) { 41 | log.Fatalf("Supplied brightness was %#v - value should be between: %#v and %#v", brightness, minBrightness, maxBrightness) 42 | } 43 | 44 | return int(brightness * 31.0) 45 | 46 | } 47 | 48 | func inRangeFloat(minVal float64, testVal float64, maxVal float64) bool { 49 | 50 | return (testVal >= minVal) && (testVal <= maxVal) 51 | } 52 | 53 | // SetClearOnExit turns all pixels off on Control + C / os.Interrupt signal. 54 | func (bl *Blinkt) SetClearOnExit(clearOnExit bool) { 55 | 56 | if clearOnExit { 57 | 58 | signalChan := make(chan os.Signal, 1) 59 | signal.Notify(signalChan, os.Interrupt) 60 | fmt.Println("Press Control + C to stop") 61 | 62 | go func() { 63 | for range signalChan { 64 | bl.Clear() 65 | bl.Show() 66 | gpio.Cleanup() 67 | os.Exit(1) 68 | } 69 | }() 70 | } 71 | } 72 | 73 | // Delay maps to time.Sleep, for ms milliseconds 74 | func Delay(ms int) { 75 | time.Sleep(time.Duration(ms) * time.Millisecond) 76 | } 77 | 78 | // Clear sets all the pixels to off, you still have to call Show. 79 | func (bl *Blinkt) Clear() { 80 | r := 0 81 | g := 0 82 | b := 0 83 | bl.SetAll(r, g, b) 84 | } 85 | 86 | // Show updates the LEDs with the values from SetPixel/Clear. 87 | func (bl *Blinkt) Show() { 88 | for i := 0; i < 4; i++ { 89 | writeByte(0) 90 | } 91 | 92 | for p, _ := range bl.pixels { 93 | brightness := bl.pixels[p][brightnessIndex] 94 | r := bl.pixels[p][redIndex] 95 | g := bl.pixels[p][greenIndex] 96 | b := bl.pixels[p][blueIndex] 97 | 98 | // 0b11100000 (224) 99 | bitwise := 224 100 | writeByte(bitwise | brightness) 101 | writeByte(b) 102 | writeByte(g) 103 | writeByte(r) 104 | } 105 | writeByte(255) // 0xff = 255 106 | } 107 | 108 | // SetAll sets all pixels to specified r, g, b colour. Show must be called to update the LEDs. 109 | func (bl *Blinkt) SetAll(r int, g int, b int) *Blinkt { 110 | 111 | for p, _ := range bl.pixels { 112 | bl.SetPixel(p, r, g, b) 113 | } 114 | 115 | return bl 116 | } 117 | 118 | // SetPixel sets an individual pixel to specified r, g, b colour. Show must be called to update the LEDs. 119 | func (bl *Blinkt) SetPixel(p int, r int, g int, b int) *Blinkt { 120 | 121 | bl.pixels[p][redIndex] = r 122 | bl.pixels[p][greenIndex] = g 123 | bl.pixels[p][blueIndex] = b 124 | 125 | return bl 126 | 127 | } 128 | 129 | // SetBrightness sets the brightness of all pixels. Brightness supplied should be between: 0.0 to 1.0 130 | func (bl *Blinkt) SetBrightness(brightness float64) *Blinkt { 131 | 132 | brightnessInt := convertBrightnessToInt(brightness) 133 | 134 | for p, _ := range bl.pixels { 135 | bl.pixels[p][brightnessIndex] = brightnessInt 136 | } 137 | 138 | return bl 139 | } 140 | 141 | // SetPixelBrightness sets the brightness of pixel p. Brightness supplied should be between: 0.0 to 1.0 142 | func (bl *Blinkt) SetPixelBrightness(p int, brightness float64) *Blinkt { 143 | 144 | brightnessInt := convertBrightnessToInt(brightness) 145 | bl.pixels[p][brightnessIndex] = brightnessInt 146 | return bl 147 | } 148 | 149 | func initPixels(brightness int) [8][4]int { 150 | var pixels [8][4]int 151 | for p, _ := range pixels { 152 | 153 | pixels[p][redIndex] = 0 154 | pixels[p][greenIndex] = 0 155 | pixels[p][blueIndex] = 0 156 | pixels[p][brightnessIndex] = brightness 157 | 158 | } 159 | return pixels 160 | } 161 | 162 | // Setup initializes GPIO via WiringPi base library. 163 | func (bl *Blinkt) Setup() { 164 | gpio.Setup() 165 | gpio.PinMode(gpio.GpioToPin(DAT), gpio.OUTPUT) 166 | gpio.PinMode(gpio.GpioToPin(CLK), gpio.OUTPUT) 167 | } 168 | 169 | // NewBlinkt creates a Blinkt to interact with. You must call "Setup()" immediately afterwards. 170 | func NewBlinkt(brightness ...float64) Blinkt { 171 | 172 | //brightness is optional so set the default 173 | brightnessInt := defaultBrightnessInt 174 | 175 | //over-ride the default if the user has supplied a brightness value 176 | if len(brightness) > 0 { 177 | brightnessInt = convertBrightnessToInt(brightness[0]) 178 | } 179 | return Blinkt{ 180 | pixels: initPixels(brightnessInt), 181 | } 182 | } 183 | 184 | // Blinkt use the NewBlinkt function to initialize the pixels property. 185 | type Blinkt struct { 186 | pixels [8][4]int 187 | } 188 | 189 | func init() { 190 | 191 | } 192 | -------------------------------------------------------------------------------- /sysfs/gpio/gpio.go: -------------------------------------------------------------------------------- 1 | package gpio 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "log" 7 | "os" 8 | "strconv" 9 | ) 10 | 11 | const OUTPUT = 1 12 | 13 | type gpioPin struct { 14 | valueFd *os.File 15 | directionFd *os.File 16 | } 17 | 18 | var gpioPins map[string]gpioPin 19 | 20 | func Setup() { 21 | gpioPins = make(map[string]gpioPin) 22 | } 23 | 24 | func Cleanup() { 25 | for k, v := range gpioPins { 26 | val, _ := strconv.Atoi(k) 27 | fmt.Println("Cleaning up " + k) 28 | v.directionFd.Close() 29 | v.valueFd.Close() 30 | 31 | unexport(val) 32 | } 33 | } 34 | 35 | func export(pin int) { 36 | path := "/sys/class/gpio/export" 37 | bytesToWrite := []byte(strconv.Itoa(pin)) 38 | writeErr := ioutil.WriteFile(path, bytesToWrite, 0644) 39 | if writeErr != nil { 40 | log.Panic(writeErr) 41 | } 42 | } 43 | 44 | func unexport(pin int) { 45 | path := "/sys/class/gpio/unexport" 46 | bytesToWrite := []byte(strconv.Itoa(pin)) 47 | writeErr := ioutil.WriteFile(path, bytesToWrite, 0644) 48 | if writeErr != nil { 49 | log.Panic(writeErr) 50 | } 51 | } 52 | 53 | func pinExported(pin int) bool { 54 | pinPath := fmt.Sprintf("/sys/class/gpio/gpio%d", pin) 55 | if file, err := os.Stat(pinPath); err == nil && len(file.Name()) > 0 { 56 | return true 57 | } 58 | return false 59 | } 60 | 61 | func PinMode(pin int, val int) { 62 | pinName := strconv.Itoa(pin) 63 | 64 | exported := pinExported(pin) 65 | if val == OUTPUT { 66 | if exported == false { 67 | export(pin) 68 | } 69 | } else { 70 | if exported == true { 71 | unexport(pin) 72 | } 73 | } 74 | 75 | _, exists := gpioPins[pinName] 76 | if exists == false { 77 | pinPath := fmt.Sprintf("/sys/class/gpio/gpio%d", pin) 78 | valueFd, openErr := os.OpenFile(pinPath+"/value", os.O_WRONLY, 0640) 79 | if openErr != nil { 80 | log.Panic(openErr, pinPath) 81 | } 82 | directionFd, openErr := os.OpenFile(pinPath+"/direction", os.O_WRONLY, 0640) 83 | if openErr != nil { 84 | log.Panic(openErr, pinPath) 85 | } 86 | gpioPins[pinName] = gpioPin{ 87 | valueFd: valueFd, 88 | directionFd: directionFd, 89 | } 90 | if val == OUTPUT { 91 | pinDigitalWrite(pin, "out", "direction") 92 | } 93 | } 94 | } 95 | 96 | func GpioToPin(pin int) int { 97 | return pin 98 | } 99 | 100 | func DigitalWrite(pin int, val int) { 101 | pinDigitalWrite(pin, strconv.Itoa(val), "value") 102 | } 103 | 104 | func pinDigitalWrite(pin int, val string, mode string) { 105 | pinName := strconv.Itoa(pin) 106 | var err error 107 | if mode == "direction" { 108 | _, err = gpioPins[pinName].directionFd.Write([]byte(val)) 109 | } else { 110 | _, err = gpioPins[pinName].valueFd.Write([]byte(val)) 111 | } 112 | 113 | if err != nil { 114 | log.Panic(err, fmt.Sprintf("Pin: %s Mode: %s Value: %s ", pinName, val, mode)) 115 | } 116 | } 117 | 118 | --------------------------------------------------------------------------------