Пример #1
0
func main() {
	d, err := gatt.NewDevice(option.DefaultClientOptions...)
	if err != nil {
		log.Fatalf("Failed to open device, err: %s\n", err)
		return
	}

	// Register handlers.
	d.Handle(gatt.PeripheralDiscovered(onPeriphDiscovered))
	d.Init(onStateChanged)
	select {}
}
Пример #2
0
func main() {
	flag.Parse()
	loadConfiguration()

	d, err := gatt.NewDevice(option.DefaultClientOptions...)
	if err != nil {
		log.Fatalf("Failed to open device, err: %s\n", err)
		return
	}

	// Register handlers.
	d.Handle(
		gatt.PeripheralDiscovered(onPeriphDiscovered),
		gatt.PeripheralConnected(onPeriphConnected),
		gatt.PeripheralDisconnected(onPeriphDisconnected),
	)

	d.Init(onStateChanged)
	<-done
	fmt.Println("Done")
}
Пример #3
0
func main() {
	flag.Parse()
	if len(flag.Args()) != 1 {
		log.Fatalf("usage: %s [options] peripheral-id\n", os.Args[0])
	}

	d, err := gatt.NewDevice(option.DefaultClientOptions...)
	if err != nil {
		log.Fatalf("Failed to open device, err: %s\n", err)
		return
	}

	// Register handlers.
	d.Handle(
		gatt.PeripheralDiscovered(onPeriphDiscovered),
		gatt.PeripheralConnected(onPeriphConnected),
		gatt.PeripheralDisconnected(onPeriphDisconnected),
	)

	d.Init(onStateChanged)
	<-done
	fmt.Println("Done")
}
Пример #4
0
func main() {
	err := readSettingsFile()
	if err != nil {
		log.Printf("WARNING: %s", err)
	}
	defer writeSettingsFile()

	// Sensor
	d, err := gatt.NewDevice(DefaultClientOptions...)
	if err != nil {
		log.Fatalf("Failed to open device, err: %s\n", err)
		return
	}

	d.Handle(
		gatt.PeripheralDiscovered(onPeriphDiscovered),
		gatt.PeripheralConnected(onPeriphConnected),
		gatt.PeripheralDisconnected(onPeriphDisconnected),
	)

	d.Init(onStateChanged)

	// API
	http.HandleFunc("/api", apiHandler)
	http.Handle("/", http.FileServer(http.Dir("./ui")))
	go http.ListenAndServe(":80", nil)

	// GPIO
	err = rpio.Open()
	if err != nil {
		log.Printf("Failed to open controller, err: %s\n", err)
		return
	}
	defer func() {
		for _, p := range []rpio.Pin{fan, cool, heat} {
			Stop(p)
		}
		rpio.Close()
	}()

	for _, p := range []rpio.Pin{fan, cool, heat} {
		p.Output()
		Stop(p)
	}

	go func() {
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt)
		<-c
		done <- true
	}()
	<-done

	exiting = true
	Stop(fan)
	Stop(cool)
	Stop(heat)
	if peripheral != nil {
		d.CancelConnection(peripheral)
		<-disconnected
		return
	}
	os.Exit(1)
}