Skip to content

jsgoecke/gpio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 

Repository files navigation

gpio

An experimental Go library for the Raspberry Pi GPIO ports.

Example

package main

import (
	"github.com/jsgoecke/gpio"
	"log"
	"sync"
	"time"
)

const (
	// Times is the times to iterate between on/off
	Times = 3
	// Duration is the duration, in milliseconds to pause between on/off
	Duration = 100
)

var wg sync.WaitGroup

func main() {
	ports := []int{17, 18, 23, 24, 25}
	wg.Add(len(ports))
	for _, port := range ports {
		pin, err := gpio.New(port)
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("GPIO %d - Initialized\n", pin.Port)
		go blinkPort(pin)
	}
	wg.Wait()
}

func blinkPort(pin *gpio.Pin) {
	pin.Direction("out")

	for i := 0; i < Times; i++ {
		pin.High()
		event := <-pin.EventChannel
		if event.State == gpio.PinHigh {
			log.Printf("GPIO %d - State: HIGH\n", pin.Port)
		}
		time.Sleep(Duration * time.Millisecond)

		pin.Low()
		event = <-pin.EventChannel
		if event.State == gpio.PinLow {
			log.Printf("GPIO %d - State: LOW\n", pin.Port)
		}
		time.Sleep(Duration * time.Millisecond)
	}

	pin.Close()
	log.Printf("GPIO %d - Closed\n", pin.Port)
	wg.Done()
}

About

An experimental Go library for the Raspberry Pi GPIO ports.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages