Exemplo n.º 1
0
func main() {
	flag.Parse()

	// Only one context should be needed for an application.  It should always be closed.
	ctx := usb.NewContext()
	defer ctx.Close()

	// Debugging can be turned on; this shows some of the inner workings of the libusb package.
	ctx.Debug(*debug)

	// ListDevices is used to find the devices to open.
	devs, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
		// The usbid package can be used to print out human readable information.
		fmt.Printf("%03d.%03d %s:%s %s\n", desc.Bus, desc.Address, desc.Vendor, desc.Product, usbid.Describe(desc))
		fmt.Printf("  Protocol: %s\n", usbid.Classify(desc))

		// The configurations can be examined from the Descriptor, though they can only
		// be set once the device is opened.  All configuration references must be closed,
		// to free up the memory in libusb.
		for _, cfg := range desc.Configs {
			// This loop just uses more of the built-in and usbid pretty printing to list
			// the USB devices.
			fmt.Printf("  %s:\n", cfg)
			for _, alt := range cfg.Interfaces {
				fmt.Printf("    --------------\n")
				for _, iface := range alt.Setups {
					fmt.Printf("    %s\n", iface)
					fmt.Printf("      %s\n", usbid.Classify(iface))
					for _, end := range iface.Endpoints {
						fmt.Printf("      %s\n", end)
					}
				}
			}
			fmt.Printf("    --------------\n")
		}

		// After inspecting the descriptor, return true or false depending on whether
		// the device is "interesting" or not.  Any descriptor for which true is returned
		// opens a Device which is retuned in a slice (and must be subsequently closed).
		return false
	})

	// All Devices returned from ListDevices must be closed.
	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()

	// ListDevices can occaionally fail, so be sure to check its return value.
	if err != nil {
		log.Fatalf("list: %s", err)
	}

	for _, dev := range devs {
		// Once the device has been selected from ListDevices, it is opened
		// and can be interacted with.
		_ = dev
	}
}
Exemplo n.º 2
0
func InitUSB() {
	var err error
	flag.Parse()

	// Only one context should be needed for an application.  It should always be closed.
	ctx = usb.NewContext()

	ctx.Debug(*debug)

	log.Printf("Scanning for device %q...", *device)

	dev, _ := ctx.GetDeviceWithVidPid(*device)

	// Open up two ep for read and write
	ep_bulk_read, err = dev.OpenEndpoint(
		uint8(*config),
		uint8(*iface),
		uint8(*setup),
		uint8(*endpoint_bulk_read)|uint8(usb.ENDPOINT_DIR_IN))

	ep_bulk_write, err = dev.OpenEndpoint(
		uint8(*config),
		uint8(*iface),
		uint8(*setup),
		uint8(*endpoint_bulk_write)|uint8(usb.ENDPOINT_DIR_OUT))

	if err != nil {
		log.Fatalf("open: %s", err)
	}

	log.Printf("Init Done\n")
}
Exemplo n.º 3
0
func Do(arm byte, base byte, led byte) (int, error) {
	command := []byte{arm, base, led}
	context := usb.NewContext()
	defer context.Close()
	var robotDesc *usb.Descriptor
	devs, err := context.ListDevices(func(desc *usb.Descriptor) bool {
		if desc.Vendor == 0x1267 {
			robotDesc = desc
			return true
		}
		return false
	})
	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()
	if robotDesc == nil {
		fmt.Println("Could not find Maplin Robot Arm")
		var reterr error
		return 0, reterr
	}

	if err != nil {
		fmt.Println("Some devices had an error: %s", err)
		var reterr error
		return 0, reterr
	}
	devs[0].Control(0x40, 6, 0x100, 0, command)
	time.Sleep(150 * time.Millisecond)
	return devs[0].Control(0x40, 6, 0x100, 0, []byte{0, 0, led})
}
Exemplo n.º 4
0
func (s *Display) OpenDevice() error {
	devices, err := usb.NewContext().ListDevices(func(desc *usb.Descriptor) bool {
		return desc.Vendor == usb.ID(vendorID) && desc.Device == usb.BCD(deviceID)
	})

	if err != nil {
		return err
	}

	if len(devices) == 0 {
		return fmt.Errorf("no lcd detected")
	}

	s.device = devices[0]

	return nil
}
Exemplo n.º 5
0
func flash(buf []byte) {
	ctx := usb.NewContext()
	defer ctx.Close()
	ctx.Debug(usbDebug)
	devices, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
		return desc.Vendor == VENDOR_ID && desc.Product == PRODUCT_ID
	})

	if err != nil {
		log.Fatal(err)
	}

	for _, d := range devices {
		fmt.Printf("Found device %s:%s\n", d.Descriptor.Vendor, d.Descriptor.Product)
		defer d.Close()
	}
	if len(devices) == 0 {
		log.Println("Could not find device")
		return
	}

	d := devices[0]
	d.WriteTimeout = 3 * time.Second

	//Config 1, Interface 0, setup 0, Endpoint 2 as deduced from lsusb
	e, err := d.OpenEndpoint(1, 0, 0, 2)

	if err != nil {
		log.Fatal(err)
	}

	maxSize := int(e.Info().MaxPacketSize)

	for i := 0; i < len(buf); {
		top := i + maxSize
		if top > len(buf) {
			top = len(buf)
		}
		s, err := e.Write(buf[i:top])
		if err != nil {
			log.Printf("Written %d of %d bytes\n", i+s, len(buf))
			log.Fatal(err)
		}
		i += s
	}
}
Exemplo n.º 6
0
func temperature() (float64, error) {
	ctx := usb.NewContext()
	defer ctx.Close()

	devs, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
		return desc.Vendor == 0x0c45 && desc.Product == 0x7401
	})

	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()

	if err != nil {
		return 0.0, err
	}

	if len(devs) == 0 {
		return 0.0, fmt.Errorf("No thermometers found.")
	}

	dev := devs[0]
	if err = dev.SetConfig(1); err != nil {
		return 0.0, err
	}

	ep, err := dev.OpenEndpoint(1, 1, 0, 0x82)
	if err != nil {
		return 0.0, err
	}
	if _, err = dev.Control(0x21, 0x09, 0x0200, 0x01, []byte{0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00}); err != nil {
		return 0.0, err
	}
	buf := make([]byte, 8)
	if _, err = ep.Read(buf); err != nil {
		return 0.0, err
	}
	return float64(buf[4]) + float64(buf[5])/256, nil
}
Exemplo n.º 7
0
// Open an LCDSysInfo device at index. If you have one device connected,
// then choose index 0. If you have more than one device connected, you will
// have to play around with index until you get the device you want.
func (l *LCDSysInfo) Open(index int) error {
	l.ctx = usb.NewContext()
	l.ctx.Debug(0)

	// Find our LCDSysInfo device.
	devs, err := l.ctx.ListDevices(func(desc *usb.Descriptor) bool {
		if desc.Vendor == VENDOR_ID && desc.Product == PRODUCT_ID {
			return true
		}

		return false
	})
	// I don't test for an error because I've noticed it returns a
	// device and throws a NOT_FOUND error. Ok?...
	if len(devs) == 0 {
		return fmt.Errorf("Could not find LCDSysInfo device: %v", err)
	}

	// Pick the nth index of the device. If we only have one device
	// connected, this will always be 0.
	if len(devs) > index {
		l.dev = devs[index]
	}

	// gousb will open all the devices it returns. So we must close
	// devices we are not using.
	for i, v := range devs {
		if i == index {
			continue
		}

		v.Close()
	}

	return nil
}
Exemplo n.º 8
0
Arquivo: main.go Projeto: ebfe/gousb
func main() {
	flag.Parse()

	// Only one context should be needed for an application.  It should always be closed.
	ctx := usb.NewContext()
	defer ctx.Close()

	ctx.Debug(*debug)

	log.Printf("Scanning for device %q...", *device)

	// ListDevices is used to find the devices to open.
	devs, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
		if fmt.Sprintf("%s:%s", desc.Vendor, desc.Product) != *device {
			return false
		}

		// The usbid package can be used to print out human readable information.
		fmt.Printf("  Protocol: %s\n", usbid.Classify(desc))

		// The configurations can be examined from the Descriptor, though they can only
		// be set once the device is opened.  All configuration references must be closed,
		// to free up the memory in libusb.
		for _, cfg := range desc.Configs {
			// This loop just uses more of the built-in and usbid pretty printing to list
			// the USB devices.
			fmt.Printf("  %s:\n", cfg)
			for _, alt := range cfg.Interfaces {
				fmt.Printf("    --------------\n")
				for _, iface := range alt.Setups {
					fmt.Printf("    %s\n", iface)
					fmt.Printf("      %s\n", usbid.Classify(iface))
					for _, end := range iface.Endpoints {
						fmt.Printf("      %s\n", end)
					}
				}
			}
			fmt.Printf("    --------------\n")
		}

		return true
	})

	// All Devices returned from ListDevices must be closed.
	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()

	// ListDevices can occaionally fail, so be sure to check its return value.
	if err != nil {
		log.Fatalf("list: %s", err)
	}

	if len(devs) == 0 {
		log.Fatalf("no devices found")
	}

	dev := devs[0]

	log.Printf("Connecting to endpoint...")
	log.Printf("- %#v", dev.Descriptor)
	ep, err := dev.OpenEndpoint(uint8(*config), uint8(*iface), uint8(*setup), uint8(*endpoint)|uint8(usb.ENDPOINT_DIR_IN))
	if err != nil {
		log.Fatalf("open: %s", err)
	}
	_ = ep
}
Exemplo n.º 9
0
func init() {
	ctx = usb.NewContext()
}
Exemplo n.º 10
0
Arquivo: xbox.go Projeto: krasin/gousb
func main() {
	ctx := usb.NewContext()
	defer ctx.Close()

	//ctx.Debug(10)

	type modelInfo struct {
		config, iface, setup, endIn, endOut uint8
	}
	var model modelInfo

	devs, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
		switch {
		case desc.Vendor == 0x045e && desc.Product == 0x028e:
			log.Printf("Found standard Microsoft controller")
			/*
			   250.006 045e:028e Xbox360 Controller (Microsoft Corp.)
			     Protocol: Vendor Specific Class (Vendor Specific Subclass) Vendor Specific Protocol
			     Config 01:
			       --------------
			       Interface 00 Setup 00
			         Vendor Specific Class
			         Endpoint 1 IN  interrupt - unsynchronized data [32 0]
			         Endpoint 1 OUT interrupt - unsynchronized data [32 0]
			       --------------
			       Interface 01 Setup 00
			         Vendor Specific Class
			         Endpoint 2 IN  interrupt - unsynchronized data [32 0]
			         Endpoint 2 OUT interrupt - unsynchronized data [32 0]
			         Endpoint 3 IN  interrupt - unsynchronized data [32 0]
			         Endpoint 3 OUT interrupt - unsynchronized data [32 0]
			       --------------
			       Interface 02 Setup 00
			         Vendor Specific Class
			         Endpoint 0 IN  interrupt - unsynchronized data [32 0]
			       --------------
			       Interface 03 Setup 00
			         Vendor Specific Class
			       --------------
			*/
			model = modelInfo{1, 0, 0, 1, 1}
		case desc.Vendor == 0x1689 && desc.Product == 0xfd00:
			log.Printf("Found Razer Onza Tournament controller")
			/*
				250.006 1689:fd00 Unknown 1689:fd00
				  Protocol: Vendor Specific Class (Vendor Specific Subclass) Vendor Specific Protocol
				  Config 01:
				    --------------
				    Interface 00 Setup 00
				      Vendor Specific Class
				      Endpoint 1 IN  interrupt - unsynchronized data [32 0]
				      Endpoint 2 OUT interrupt - unsynchronized data [32 0]
				    --------------
				    Interface 01 Setup 00
				      Vendor Specific Class
				      Endpoint 3 IN  interrupt - unsynchronized data [32 0]
				      Endpoint 0 OUT interrupt - unsynchronized data [32 0]
				      Endpoint 1 IN  interrupt - unsynchronized data [32 0]
				      Endpoint 1 OUT interrupt - unsynchronized data [32 0]
				    --------------
				    Interface 02 Setup 00
				      Vendor Specific Class
				      Endpoint 2 IN  interrupt - unsynchronized data [32 0]
				    --------------
				    Interface 03 Setup 00
				      Vendor Specific Class
				    --------------
			*/
			model = modelInfo{1, 0, 0, 1, 2}
		default:
			return false
		}
		return true
	})
	if err != nil {
		log.Fatalf("listdevices: %s", err)
	}
	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()
	if len(devs) != 1 {
		log.Fatalf("found %d devices, want 1", len(devs))
	}
	controller := devs[0]

	if err := controller.Reset(); err != nil {
		log.Fatalf("reset: %s", err)
	}

	in, err := controller.OpenEndpoint(
		model.config,
		model.iface,
		model.setup,
		model.endIn|uint8(usb.ENDPOINT_DIR_IN))
	if err != nil {
		log.Fatalf("in: openendpoint: %s", err)
	}

	out, err := controller.OpenEndpoint(
		model.config,
		model.iface,
		model.setup,
		model.endOut|uint8(usb.ENDPOINT_DIR_OUT))
	if err != nil {
		log.Fatalf("out: openendpoint: %s", err)
	}

	// https://github.com/Grumbel/xboxdrv/blob/master/PROTOCOL

	var b [32]byte
	for {
		n, err := in.Read(b[:])
		log.Printf("read %d bytes: % x [err: %v]", n, b[:n], err)
		if err != nil {
			break
		}
	}

	const (
		Empty      byte = iota // 00000000 ( 0) no LEDs
		WarnAll                // 00000001 ( 1) flash all briefly
		NewPlayer1             // 00000010 ( 2) p1 flash then solid
		NewPlayer2             // 00000011
		NewPlayer3             // 00000100
		NewPlayer4             // 00000101
		Player1                // 00000110 ( 6) p1 solid
		Player2                // 00000111
		Player3                // 00001000
		Player4                // 00001001
		Waiting                // 00001010 (10) empty w/ loops
		WarnPlayer             // 00001011 (11) flash active
		_                      // 00001100 (12) empty
		Battery                // 00001101 (13) squiggle
		Searching              // 00001110 (14) slow flash
		Booting                // 00001111 (15) solid then flash
	)

	led := func(b byte) {
		out.Write([]byte{0x01, 0x03, b})
	}

	setPlayer := func(player byte) {
		spin := []byte{
			Player1, Player2, Player4, Player3,
		}
		spinIdx := 0
		spinDelay := 100 * time.Millisecond

		led(Booting)
		time.Sleep(100 * time.Millisecond)
		for spinDelay > 20*time.Millisecond {
			led(spin[spinIdx])
			time.Sleep(spinDelay)
			spinIdx = (spinIdx + 1) % len(spin)
			spinDelay -= 5 * time.Millisecond
		}
		for i := 0; i < 40; i++ { // just for safety
			cur := spin[spinIdx]
			led(cur)
			time.Sleep(spinDelay)
			spinIdx = (spinIdx + 1) % len(spin)
			if cur == player {
				break
			}
		}
	}

	led(Empty)
	time.Sleep(1 * time.Second)
	setPlayer(Player1)

	/*
		time.Sleep(1 * time.Second)
		setPlayer(Player2)
		time.Sleep(1 * time.Second)
		setPlayer(Player3)
		time.Sleep(1 * time.Second)
		setPlayer(Player4)
		time.Sleep(5 * time.Second)
		led(Waiting)
	*/

	var last, cur [32]byte
	decode := func() {
		n, err := in.Read(cur[:])
		if err != nil || n != 20 {
			log.Printf("ignoring read: %d bytes, err = %v", n, err)
			return
		}

		// 1-bit values
		for _, v := range []struct {
			idx  int
			bit  uint
			name string
		}{
			{2, 0, "DPAD U"},
			{2, 1, "DPAD D"},
			{2, 2, "DPAD L"},
			{2, 3, "DPAD R"},
			{2, 4, "START"},
			{2, 5, "BACK"},
			{2, 6, "THUMB L"},
			{2, 7, "THUMB R"},
			{3, 0, "LB"},
			{3, 1, "RB"},
			{3, 2, "GUIDE"},
			{3, 4, "A"},
			{3, 5, "B"},
			{3, 6, "X"},
			{3, 7, "Y"},
		} {
			c := cur[v.idx] & (1 << v.bit)
			l := last[v.idx] & (1 << v.bit)
			if c == l {
				continue
			}
			switch {
			case c != 0:
				log.Printf("Button %q pressed", v.name)
			case l != 0:
				log.Printf("Button %q released", v.name)
			}
		}

		// 8-bit values
		for _, v := range []struct {
			idx  int
			name string
		}{
			{4, "LT"},
			{5, "RT"},
		} {
			c := cur[v.idx]
			l := last[v.idx]
			if c == l {
				continue
			}
			log.Printf("Trigger %q = %v", v.name, c)
		}

		dword := func(hi, lo byte) int16 {
			return int16(hi)<<8 | int16(lo)
		}

		//     +y
		//      N
		// -x W-|-E +x
		//      S
		//     -y
		dirs := [...]string{
			"W", "SW", "S", "SE", "E", "NE", "N", "NW", "W",
		}
		dir := func(x, y int16) (string, int32) {
			// Direction
			rad := math.Atan2(float64(y), float64(x))
			dir := 4 * rad / math.Pi
			card := int(dir + math.Copysign(0.5, dir))

			// Magnitude
			mag := math.Sqrt(float64(x)*float64(x) + float64(y)*float64(y))
			return dirs[card+4], int32(mag)
		}

		// 16-bit values
		for _, v := range []struct {
			hiX, loX int
			hiY, loY int
			name     string
		}{
			{7, 6, 9, 8, "LS"},
			{11, 10, 13, 12, "RS"},
		} {
			c, cmag := dir(
				dword(cur[v.hiX], cur[v.loX]),
				dword(cur[v.hiY], cur[v.loY]),
			)
			l, lmag := dir(
				dword(last[v.hiX], last[v.loX]),
				dword(last[v.hiY], last[v.loY]),
			)
			ccenter := cmag < 10240
			lcenter := lmag < 10240
			if ccenter && lcenter {
				continue
			}
			if c == l && cmag == lmag {
				continue
			}
			if cmag > 10240 {
				log.Printf("Stick %q = %v x %v", v.name, c, cmag)
			} else {
				log.Printf("Stick %q centered", v.name)
			}
		}

		last, cur = cur, last
	}

	controller.ReadTimeout = 60 * time.Second
	for {
		decode()
	}
}
Exemplo n.º 11
0
func main() {
	flag.Parse()

	ctx := usb.NewContext()
	defer ctx.Close()

	if *debug != 0 {
		ctx.Debug(*debug)
	}

	var model modelInfo

	devs, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
		switch {
		case desc.Vendor == 0x045e && desc.Product == 0x028e:
			log.Printf("Found standard Microsoft controller")
			/*
			   250.006 045e:028e Xbox360 Controller (Microsoft Corp.)
			     Protocol: Vendor Specific Class (Vendor Specific Subclass) Vendor Specific Protocol
			     Config 01:
			       --------------
			       Interface 00 Setup 00
			         Vendor Specific Class
			         Endpoint 1 IN  interrupt - unsynchronized data [32 0]
			         Endpoint 1 OUT interrupt - unsynchronized data [32 0]
			       --------------
			       Interface 01 Setup 00
			         Vendor Specific Class
			         Endpoint 2 IN  interrupt - unsynchronized data [32 0]
			         Endpoint 2 OUT interrupt - unsynchronized data [32 0]
			         Endpoint 3 IN  interrupt - unsynchronized data [32 0]
			         Endpoint 3 OUT interrupt - unsynchronized data [32 0]
			       --------------
			       Interface 02 Setup 00
			         Vendor Specific Class
			         Endpoint 0 IN  interrupt - unsynchronized data [32 0]
			       --------------
			       Interface 03 Setup 00
			         Vendor Specific Class
			       --------------
			*/
			model = modelInfo{1, 0, 0, 1, 1, "360"}

		case desc.Vendor == 0x045e && desc.Product == 0x02d1:
			log.Printf("Found Microsoft Xbox One controller")
			/*
			   250.006 045e:02d1 Unknown (Microsoft Corp.)
			     Protocol: Vendor Specific Class
			     Config 01:
			       --------------
			       Interface 00 Setup 00
			         Vendor Specific Class
			         Endpoint 1 OUT interrupt - unsynchronized data [64 0]
			         Endpoint 1 IN  interrupt - unsynchronized data [64 0]
			       --------------
			       Interface 01 Setup 00
			         Vendor Specific Class
			       Interface 01 Setup 01
			         Vendor Specific Class
			         Endpoint 2 OUT isochronous - unsynchronized data [228 0]
			         Endpoint 2 IN  isochronous - unsynchronized data [228 0]
			       --------------
			       Interface 02 Setup 00
			         Vendor Specific Class
			       Interface 02 Setup 01
			         Vendor Specific Class
			         Endpoint 3 OUT bulk - unsynchronized data [64 0]
			         Endpoint 3 IN  bulk - unsynchronized data [64 0]
			       --------------
			*/
			model = modelInfo{1, 0, 0, 1, 1, "one"}

		case desc.Vendor == 0x1689 && desc.Product == 0xfd00:
			log.Printf("Found Razer Onza Tournament controller")
			/*
				250.006 1689:fd00 Unknown 1689:fd00
				  Protocol: Vendor Specific Class (Vendor Specific Subclass) Vendor Specific Protocol
				  Config 01:
				    --------------
				    Interface 00 Setup 00
				      Vendor Specific Class
				      Endpoint 1 IN  interrupt - unsynchronized data [32 0]
				      Endpoint 2 OUT interrupt - unsynchronized data [32 0]
				    --------------
				    Interface 01 Setup 00
				      Vendor Specific Class
				      Endpoint 3 IN  interrupt - unsynchronized data [32 0]
				      Endpoint 0 OUT interrupt - unsynchronized data [32 0]
				      Endpoint 1 IN  interrupt - unsynchronized data [32 0]
				      Endpoint 1 OUT interrupt - unsynchronized data [32 0]
				    --------------
				    Interface 02 Setup 00
				      Vendor Specific Class
				      Endpoint 2 IN  interrupt - unsynchronized data [32 0]
				    --------------
				    Interface 03 Setup 00
				      Vendor Specific Class
				    --------------
			*/
			model = modelInfo{1, 0, 0, 1, 2, "360"}
		default:
			return false
		}
		return true
	})
	if err != nil {
		log.Fatalf("listdevices: %s", err)
	}
	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()
	if len(devs) != 1 {
		log.Fatalf("found %d devices, want 1", len(devs))
	}
	controller := devs[0]

	if err := controller.Reset(); err != nil {
		log.Fatalf("reset: %s", err)
	}

	in, err := controller.OpenEndpoint(
		model.config,
		model.iface,
		model.setup,
		model.endIn|uint8(usb.ENDPOINT_DIR_IN))
	if err != nil {
		log.Fatalf("in: openendpoint: %s", err)
	}

	out, err := controller.OpenEndpoint(
		model.config,
		model.iface,
		model.setup,
		model.endOut|uint8(usb.ENDPOINT_DIR_OUT))
	if err != nil {
		log.Fatalf("out: openendpoint: %s", err)
	}

	switch {
	case *readonly:
		var b [512]byte
		for {
			n, err := in.Read(b[:])
			log.Printf("read %d bytes: % x [err: %v]", n, b[:n], err)
			if err != nil {
				break
			}
		}
	case model.kind == "360":
		XBox360(controller, in, out)
	case model.kind == "one":
		XBoxOne(controller, in, out)
	}
}
Exemplo n.º 12
0
func New() (_k *Kinect, _e error) {
	ctx := usb.NewContext()
	defer func() {
		// Be sure to close the context if we return an error
		if _e != nil {
			ctx.Close()
		}
	}()

	dev, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
		if desc.Vendor != 0x045e { // Microsoft Corp.
			return false
		}
		switch desc.Product {
		case 0x02ae, 0x02ad, 0x02b0:
			return true
		}
		return false
	})
	if err != nil {
		return nil, err
	}
	k := &Kinect{
		ctx: ctx,
	}

	for _, d := range dev {
		switch d.Product {
		case 0x02ae: // XBOX NUI Camera
			k.cam = d
		case 0x02ad: // XBOX NUI Audio
			k.mic = d
		case 0x02b0: // XBOX NUI Motor
			k.mot = d
		default:
			d.Close()
		}
	}

	switch {
	case k.cam == nil:
		return nil, fmt.Errorf("kinect: failed to detect camera")
	case k.mic == nil:
		return nil, fmt.Errorf("kinect: failed to detect mic")
	case k.mot == nil:
		return nil, fmt.Errorf("kinect: failed to detect motor")
	}

	// Perform initialization
	resp := []byte{0xFF}
	if _, err := k.mot.Control(0xC0, 0x10, 0, 0, resp); err != nil {
		return nil, fmt.Errorf("kinect: motor init: %s", err)
	} else if resp[0] != 0x22 {
		return nil, fmt.Errorf("kinect: motor init failed (code %#x)", resp[0])
	}

	//if err := k.SetAngle(+20); err != nil { return nil, err }
	//if err := k.SetAngle(-20); err != nil { return nil, err }
	if err := k.SetAngle(0); err != nil {
		return nil, err
	}

	fmt.Println("Kinect: ", k.cam.Configs)
	if k.depth, err = k.cam.OpenEndpoint(1, 0, 0, 0x82); err != nil {
		return nil, fmt.Errorf("kinect: open depth: %s", err)
	}

	k.stop = make(chan struct{}, 1)
	k.depthDone = make(chan struct{}, 1)
	k.depthFrame = make(chan bool, 1000)

	go k.streamDepth()

	if err := k.initDepth(); err != nil {
		return nil, fmt.Errorf("kinect: init depth: %s", err)
	}

	go func() {
		for {
			select {
			case <-time.After(1 * time.Hour):
			case <-k.stop:
				return
			}
		}
	}()

	return k, nil
}
Exemplo n.º 13
0
	SET_CONT_CARRIER  Request = 0x20
	CHANNEL_SCANN     Request = 0x21
	LAUNCH_BOOTLOADER Request = 0xFF

	RADIO_POWER_M18dBm = 0
	RADIO_POWER_M12dBm = 1
	RADIO_POWER_M6dBm  = 2
	RADIO_POWER_0dBm   = 3

	DefaultChannel  = 10
	DefaultDataRate = cflie.DATA_RATE_250K
)

var DefaultRadioAddress = [5]byte{0xE7, 0xE7, 0xE7, 0xE7, 0xE7}

var defaultContext = usb.NewContext()

var ErrDeviceNotFound = fmt.Errorf("Device not found")
var ErrTooManyDevicesMatch = fmt.Errorf("Too many devices match (> 1)")

// Open opens a CrazyRadio USB dongle
func Open(info cflie.DeviceInfo) (dev cflie.Device, err error) {
	d, err := defaultContext.ListDevices(func(desc *usb.Descriptor) bool {
		if desc.Vendor == Vendor && desc.Product == Product &&
			int(desc.Bus) == info.Bus() && int(desc.Address) == info.Address() &&
			uint16(desc.Device) == uint16(((info.MajorVer()&0xFF)<<8)+(info.MinorVer()&0xFF)) {
			return true
		}
		return false
	})
	if err != nil {