Created
December 29, 2017 06:55
-
-
Save six519/51d088b2b8e3c3631fafa815b88d3569 to your computer and use it in GitHub Desktop.
(Go Programming Language) Write To Raspberry Pi GPIO Pins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Install the library | |
------------------- | |
go get github.com/six519/gpio | |
Sample usage | |
------------ | |
Go to https://github.com/six519/gpio | |
*/ | |
package gpio | |
import ( | |
"fmt" | |
"os" | |
"strconv" | |
) | |
const OUTPUT string = "out" | |
const LOW string = "0" | |
const HIGH string = "1" | |
var export_path string = "/sys/class/gpio/export" | |
var gpio_path string = "/sys/class/gpio/gpio" | |
var unexport_path string = "/sys/class/gpio/unexport" | |
func writeInt(path string, val int) { | |
f, err := os.Create(path) | |
if err != nil { | |
fmt.Println("Can't open file " + path) | |
} else { | |
defer f.Close() | |
f.WriteString(strconv.Itoa(val)) | |
} | |
} | |
func writeStr(path string, val string) { | |
f, err := os.Create(path) | |
if err != nil { | |
fmt.Println("Can't open file " + path) | |
} else { | |
defer f.Close() | |
f.WriteString(val) | |
} | |
} | |
func PinMode(pin int, mode string) { | |
writeInt(export_path, pin) | |
direction := gpio_path + strconv.Itoa(pin) + "/direction" | |
writeStr(direction, mode) | |
} | |
func DigitalWrite(pin int, val string) { | |
value := gpio_path + strconv.Itoa(pin) + "/value" | |
writeStr(value, val) | |
} | |
func CleanUp(pin int) { | |
writeInt(unexport_path, pin) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment