From 3b84dcbc5c5ef8279c46e122729939a307b62bb2 Mon Sep 17 00:00:00 2001 From: David Rebbe Date: Tue, 17 Dec 2024 00:55:40 -0500 Subject: [PATCH] simple go example --- examples/icsneo/go/simple/go.mod | 3 ++ examples/icsneo/go/simple/simple.go | 49 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 examples/icsneo/go/simple/go.mod create mode 100644 examples/icsneo/go/simple/simple.go diff --git a/examples/icsneo/go/simple/go.mod b/examples/icsneo/go/simple/go.mod new file mode 100644 index 0000000..3fecc93 --- /dev/null +++ b/examples/icsneo/go/simple/go.mod @@ -0,0 +1,3 @@ +module simple + +go 1.23.4 diff --git a/examples/icsneo/go/simple/simple.go b/examples/icsneo/go/simple/simple.go new file mode 100644 index 0000000..7fd37d6 --- /dev/null +++ b/examples/icsneo/go/simple/simple.go @@ -0,0 +1,49 @@ +package main + +// #cgo CFLAGS: -I../../../../include +// #cgo LDFLAGS: -L../../../../build -licsneo -lstdc++ +// #include "icsneo/icsneo.h" +import "C" +import ( + "fmt" + "unsafe" +) + +func main() { + findDevices() +} + +func findDevices() { + // Get all devices attached to host + devices := [255]*C.icsneo_device_t{nil} + devicesCount := 255 + res := C.icsneo_device_find_all(&devices[0], (*C.uint)(unsafe.Pointer(&devicesCount)), nil) + if res != C.icsneo_error_success { + printError(res) + return + } + println("Found", devicesCount, "device(s):") + // Iterate through all the devices + for i, device := range devices[:devicesCount] { + description := make([]byte, 255) + descriptionLength := 255 + res := C.icsneo_device_get_description(device, (*C.char)(unsafe.Pointer(&description[0])), (*C.uint)(unsafe.Pointer(&descriptionLength))) + if res != C.icsneo_error_success { + printError(res) + continue + } + fmt.Printf("\t%d. %s\n", i+1, string(description[:descriptionLength])) + } +} + +func printError(err C.icsneo_error_t) C.icsneo_error_t { + buffer := make([]byte, 255) + bufferLength := 255 + res := C.icsneo_get_error_code(err, (*C.char)(unsafe.Pointer(&buffer[0])), (*C.uint)(unsafe.Pointer(&bufferLength))) + if res != C.icsneo_error_success { + println("icsneo_get_error_code failed, original error:", err) + return res + } + println("Error:", string(buffer[:bufferLength])) + return res +}