unknown stack error
This commit is contained in:
71
internal/temper/temper.go
Normal file
71
internal/temper/temper.go
Normal file
@ -0,0 +1,71 @@
|
||||
package temper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/gousb"
|
||||
)
|
||||
|
||||
type TEMPer struct {
|
||||
Device *gousb.Device
|
||||
}
|
||||
|
||||
// GetDevice will return the first temper device found.
|
||||
func GetDevice() (*TEMPer, error) {
|
||||
ctx := gousb.NewContext()
|
||||
defer ctx.Close()
|
||||
|
||||
vid, pid := gousb.ID(0x0c45), gousb.ID(0x7401)
|
||||
devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
|
||||
return desc.Vendor == vid && desc.Product == pid
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return &TEMPer{}, err
|
||||
}
|
||||
|
||||
if len(devs) == 0 {
|
||||
return &TEMPer{}, fmt.Errorf("no devices found matching VID %s and PID %s", vid, pid)
|
||||
}
|
||||
|
||||
devs[0].SetAutoDetach(true)
|
||||
|
||||
for i := 1; i <= len(devs)-1; i++ {
|
||||
defer devs[i].Close()
|
||||
}
|
||||
|
||||
return &TEMPer{Device: devs[0]}, nil
|
||||
}
|
||||
|
||||
func (d *TEMPer) GetTemperature() (float64, error) {
|
||||
cfg, err := d.Device.Config(1)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer cfg.Close()
|
||||
|
||||
intf, err := cfg.Interface(1, 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer intf.Close()
|
||||
|
||||
epIn, err := intf.InEndpoint(0x82)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
_, err = d.Device.Control(
|
||||
0x21, 0x09, 0x0200, 0x01, []byte{0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00},
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
buf := make([]byte, 8)
|
||||
if _, err = epIn.Read(buf); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return float64(buf[2]) + float64(buf[3])/256, nil
|
||||
}
|
Reference in New Issue
Block a user