func init() {
	relay, err := gpio.OpenPin(rpi.GPIO17, gpio.ModeOutput)
	if err != nil {
		panic(err)
	}
	relay.Clear()

	monitor, err := gpio.OpenPin(rpi.GPIO22, gpio.ModeInput)
	if err != nil {
		relay.Close()
		panic(err)
	}

	hardware = &hw{relay: relay, monitor: monitor}
}
Exemple #2
0
func main() {
	// set GPIO25 to output mode
	pin, err := gpio.OpenPin(rpi.GPIO25, gpio.ModeOutput)
	if err != nil {
		fmt.Printf("Error opening pin! %s\n", err)
		return
	}

	// turn the led off on exit
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for _ = range c {
			fmt.Printf("\nClearing and unexporting the pin.\n")
			pin.Clear()
			pin.Close()
			os.Exit(0)
		}
	}()

	for {
		pin.Set()
		time.Sleep(100 * time.Millisecond)
		pin.Clear()
		time.Sleep(100 * time.Millisecond)
	}
}
Exemple #3
0
func main() {
	// set GPIO25 to output mode
	pin, err := gpio.OpenPin(rpi.GPIO25, gpio.ModeOutput)
	if err != nil {
		fmt.Printf("Error opening pin! %s\n", err)
		return
	}

	// turn the led off on exit
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for _ = range c {
			fmt.Printf("\nClearing and unexporting the pin.\n")
			pin.Clear()
			pin.Close()
			os.Exit(0)
		}
	}()

	go buttonHandler(pin)

	// http listen
	//http.Handle("/button", websocket.Handler(EchoServer))
	http.HandleFunc("/button", func(w http.ResponseWriter, req *http.Request) {
		s := websocket.Server{Handler: websocket.Handler(EchoServer)}
		s.ServeHTTP(w, req)
	})
	http.ListenAndServe(":8080", nil)
}
Exemple #4
0
func main() {
	// set GPIO25 to output mode
	pin, err := gpio.OpenPin(rpi.GPIO25, gpio.ModeOutput)
	if err != nil {
		fmt.Printf("Error opening pin! %s\n", err)
		return
	}

	// turn the led off on exit
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for _ = range c {
			fmt.Printf("\nClearing and unexporting the pin.\n")
			pin.Clear()
			pin.Close()
			os.Exit(0)
		}
	}()

	go dance(pin, ctrlChan)
	ctrlChan <- true

	// http listen
	http.HandleFunc("/dance", danceHandler)
	http.ListenAndServe(":8080", nil)
}
Exemple #5
0
func main() {
	// set GPIO22 to input mode
	pin, err := gpio.OpenPin(gpio.GPIO22, gpio.ModeInput)
	if err != nil {
		fmt.Printf("Error opening pin! %s\n", err)
		return
	}
	// set GPIO17 to output mode
	power, err := gpio.OpenPin(gpio.GPIO17, gpio.ModeOutput)
	if err != nil {
		fmt.Printf("Error opening pin! %s\n", err)
		return
	}

	// clean up on exit
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for _ = range c {
			fmt.Println("Closing pin and terminating program.")
			power.Clear()
			pin.Close()
			power.Close()
			os.Exit(0)
		}
	}()

	err = pin.BeginWatch(gpio.EdgeFalling, func() {
		fmt.Printf("Callback for %d triggered!\n\n", gpio.GPIO22)
	})
	if err != nil {
		fmt.Printf("Unable to watch pin: %s\n", err.Error())
		os.Exit(1)
	}

	fmt.Println("Now watching pin 22 on a falling edge.")

	for {
		fmt.Println("Setting power high")
		power.Set()
		time.Sleep(2000 * time.Millisecond)
		fmt.Println("Setting power low")
		power.Clear()
		time.Sleep(2000 * time.Millisecond)
	}

}
Exemple #6
0
// Newtm16xx returns point of tm16xx which is initialized given gpio numbers
func newTm16xx(data, clk, strobe int,
	activeDisplay bool, intensity byte) (*tm16xx, error) {

	var d tm16xx
	var err error

	d.data, err = gpio.OpenPin(data, gpio.ModeOutput)
	if err != nil {
		return nil, err
	}
	d.clk, err = gpio.OpenPin(clk, gpio.ModeOutput)
	if err != nil {
		return nil, err
	}
	d.strobe, err = gpio.OpenPin(strobe, gpio.ModeOutput)
	if err != nil {
		return nil, err
	}

	d.data.SetMode(gpio.ModeOutput)
	d.clk.SetMode(gpio.ModeOutput)
	d.strobe.SetMode(gpio.ModeOutput)

	d.strobe.Set()
	d.clk.Set()

	d.sendCmd(0x40)
	v := min(7, intensity)
	if activeDisplay {
		v |= 8
	}
	d.sendCmd(0x80 | v)

	d.strobe.Clear()
	d.sendCmd(0xC0)
	for i := 0; i < 16; i++ {
		d.sendCmd(0x00)
	}

	d.strobe.Set()
	return &d, nil
}
Exemple #7
0
func main() {
	log.Print("Start")

	pin, err := gpio.OpenPin(irqPin, gpio.ModeInput)
	if err != nil {
		panic(err)
	}
	defer pin.Close()

	spiBus, err := spidev.NewSPIDevice("/dev/spidev0.0")
	if err != nil {
		panic(err)
	}
	defer spiBus.Close()

	rfm, err := NewDevice(spiBus, pin, 1, 10, false)
	if err != nil {
		log.Fatal(err)
	}
	log.Print(rfm)
}
Exemple #8
0
// Get returns the temperature data of the DHT11 IC
func Get(pin_num int) (t int, e error) {
	switch {
	case pin_num == 21:
		pin_num = rpi.GPIO21
	case pin_num == 22:
		pin_num = rpi.GPIO22
	case pin_num == 23:
		pin_num = rpi.GPIO23
	case pin_num == 24:
		pin_num = rpi.GPIO24
	case pin_num == 27:
		pin_num = rpi.GPIO27
	case pin_num == 17:
		pin_num = rpi.GPIO17
	default:
		return 0, fmt.Errorf("temp: invalid GPIO number %d", pin_num)
	}

	pin, e := gpio.OpenPin(pin_num, gpio.ModeOutput)
	if e != nil {
		return
	}
	// Reset the line
	pin.Set()
	time.Sleep(500 * time.Millisecond)
	pin.Clear()
	time.Sleep(20 * time.Millisecond)
	pin.Set()
	// Wait for start signal from the sensor
	pin.SetMode(gpio.ModeInput)
	t = 0
	pin.BeginWatch(gpio.EdgeFalling, func() {
		t++
	})
	time.Sleep(5 * time.Second)
	pin.Close()
	return
}
Exemple #9
0
func getPin() (gpio.Pin, error) {
	return gpio.OpenPin(irqPin, gpio.ModeInput)
}
Exemple #10
0
// OpenPin returns a gpio.Pin implementation specalised for the RPi.
func OpenPin(number int, mode gpio.Mode) (gpio.Pin, error) {
	initOnce.Do(initRPi)
	p, err := gpio.OpenPin(number, mode)
	return &pin{Pin: p, pin: uint8(number)}, err
}