simple go example

David Rebbe 2024-12-17 00:55:40 -05:00
parent 7b1b3bfe18
commit 3b84dcbc5c
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,3 @@
module simple
go 1.23.4

View File

@ -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
}