From e22682db0094881577a58ce8412d74e01135e55e Mon Sep 17 00:00:00 2001 From: The_Spider Date: Sat, 30 Oct 2021 10:34:00 -0500 Subject: [PATCH] unknown stack error --- cmd/go-temper/main.go | 116 ++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++ go.sum | 2 + internal/temper/temper.go | 71 +++++++++++++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 cmd/go-temper/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/temper/temper.go diff --git a/cmd/go-temper/main.go b/cmd/go-temper/main.go new file mode 100644 index 0000000..ece56a1 --- /dev/null +++ b/cmd/go-temper/main.go @@ -0,0 +1,116 @@ +package main + +import ( + "log" + "time" + + "gitea.smoothnet.org/nhyatt/go-temper/internal/temper" +) + +func main() { + /* + temp, err := GetTemperature() + + if err == nil { + log.Printf( + "Temperature: %.2f°K %.2f°F %.2f°C\n", + temp.Temperature+273.15, + 9.0/5.0*temp.Temperature+32, + temp.Temperature, + ) + } else { + log.Fatalf("Failed: %s", err) + } + */ + dev, err := temper.GetDevice() + if err != nil { + log.Fatalf("[FATAL] Unable to get TEMPer Device ID: %v\n", err) + } + + defer dev.Device.Close() + + for { + temp, err := dev.GetTemperature() + if err != nil { + log.Printf("[WARNING] Unable to get temperature from device: %v\n", err) + } else { + log.Printf( + "Temperature: %.2f°K %.2f°F %.2f°C\n", + temp+273.15, + 9.0/5.0*temp+32, + temp, + ) + } + + time.Sleep(time.Duration(5 * time.Second)) + } +} + +/* +type DeviceTemperature struct { + Device *gousb.Device + Temperature float64 +} + +func GetTemperature() (DeviceTemperature, 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 DeviceTemperature{}, err + } + + if len(devs) == 0 { + return DeviceTemperature{}, fmt.Errorf("no devices found matching VID %s and PID %s", vid, pid) + } + + // Pick the first device found. + dev := devs[0] + devs[0].SetAutoDetach(true) + + for _, d := range devs { + defer d.Close() + } + + temp, err := getDeviceTemperature(dev) + return temp, err +} + +func getDeviceTemperature(dev *gousb.Device) (DeviceTemperature, error) { + cfg, err := dev.Config(1) + if err != nil { + return DeviceTemperature{}, err + } + defer cfg.Close() + + intf, err := cfg.Interface(1, 0) + if err != nil { + return DeviceTemperature{}, err + } + defer intf.Close() + + epIn, err := intf.InEndpoint(0x82) + if err != nil { + return DeviceTemperature{}, err + } + + _, err = dev.Control( + 0x21, 0x09, 0x0200, 0x01, []byte{0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00}, + ) + if err != nil { + return DeviceTemperature{}, err + } + + buf := make([]byte, 8) + if _, err = epIn.Read(buf); err != nil { + return DeviceTemperature{}, err + } + + return DeviceTemperature{Device: dev, Temperature: float64(buf[2]) + float64(buf[3])/256}, nil +} +*/ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c1f7db6 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module gitea.smoothnet.org/nhyatt/go-temper + +go 1.17 + +require github.com/google/gousb v1.1.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1e79240 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/google/gousb v1.1.1 h1:2sjwXlc0PIBgDnXtNxUrHcD/RRFOmAtRq4QgnFBE6xc= +github.com/google/gousb v1.1.1/go.mod h1:b3uU8itc6dHElt063KJobuVtcKHWEfFOysOqBNzHhLY= diff --git a/internal/temper/temper.go b/internal/temper/temper.go new file mode 100644 index 0000000..d7bf941 --- /dev/null +++ b/internal/temper/temper.go @@ -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 +}