Exemplo n.º 1
0
func newButtons(redN, greenN int) (*buttons, error) {
	var red, green gpio.Pin
	var err error
	if red, err = gpio.OpenPin(redN, gpio.ModeInput); err != nil {
		return nil, err
	}
	if green, err = gpio.OpenPin(greenN, gpio.ModeInput); err != nil {
		_ = red.Close()
		return nil, err
	}

	btns := &buttons{
		Presses: make(chan Button, 10),
		red:     util.Debounced(red, 20*time.Millisecond),
		green:   util.Debounced(green, 20*time.Millisecond),
	}

	btns.red.BeginWatch(gpio.EdgeFalling, func() {
		btns.Presses <- RedButton
	})

	btns.green.BeginWatch(gpio.EdgeFalling, func() {
		btns.Presses <- GreenButton
	})

	return btns, nil
}
Exemplo n.º 2
0
func Debounced(pin gpio.Pin, settleDuration time.Duration) *DebouncedInput {
	pin.SetMode(gpio.ModeInput)
	return &DebouncedInput{
		Pin:            pin,
		SettleDuration: settleDuration,
		lastTime:       time.Now(),
	}
}
Exemplo n.º 3
0
func loop(die <-chan int, t time.Duration, pin gpio.Pin) {
	tick := time.Tick(t)
	state := false
	fmt.Println("")
	for {
		select {
		case <-tick:
			if state {
				fmt.Print("\rclear        ")
				pin.Clear()
			} else {
				fmt.Print("\rset          ")
				pin.Set()
			}
			state = !state
		case <-die:
			fmt.Println("\ndying...")
			return
		}
	}
}
Exemplo n.º 4
0
func main() {
	flag.Parse()
	var i, n int
	var err error
	var v string
	pins := make([]int, 100) // should be good for anyone

	for _, v = range strings.Split(*pinNumbers, ",") {
		i, err = strconv.Atoi(v)
		if err == nil {
			pins[n] = i
			n++
		}
	}

	if n == 0 {
		log.Fatalf("No pins specified, got %s", *pinNumbers)
	} else {
		log.Printf("Watching %d pins: %#v", n, pins[:n])
	}

	toggle := make(chan PinEvent, 10)
	for _, i = range pins[:n] {
		var pin gpio.Pin
		var gpioNum = i
		pin, err = gpio.OpenPin(gpioNum, gpio.ModeInput)
		if err != nil {
			log.Fatal(err)
		}
		defer pin.Close()
		pin.BeginWatch(gpio.EdgeBoth, func() {
			toggle <- PinEvent{gpioNum, pin.Get()}
		})
	}

	// clean up on exit
	die := make(chan os.Signal)
	signal.Notify(die, os.Interrupt)
	for {
		select {
		case <-die:
			log.Println("Signal received, returning")
			return
		case e := <-toggle:
			log.Printf("Pin %d is now %t", e.Number, e.Value)
		}
	}
}