From 347567500115cc2d5d6c5a9877662a80e59fa20d Mon Sep 17 00:00:00 2001 From: Friedl Ulrich Date: Thu, 5 Feb 2015 21:49:36 +0100 Subject: [PATCH] SPI Device with cgo --- rfm69spi.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/rfm69spi.go b/rfm69spi.go index ae2ab50..05ec4ab 100644 --- a/rfm69spi.go +++ b/rfm69spi.go @@ -91,13 +91,22 @@ type SPIDevice struct { fd int } +// SPIError is the error +type SPIError struct { + s string +} + +func (e SPIError) Error() string { + return e.s +} + // NewSPIDevice creates a new device func NewSPIDevice() (*SPIDevice, error) { name := C.CString("/dev/spidev0.0") i := C.spi_open(name) - C.free(name) + C.free(unsafe.Pointer(name)) if i < 0 { - return nil, error("") + return nil, SPIError{"could not open"} } return &SPIDevice{i}, nil } @@ -108,7 +117,7 @@ func (d *SPIDevice) Xfer(tx []byte) ([]byte, error) { rx := make([]byte, length) ret := C.spi_xfer(d.fd, unsafe.Pointer(&tx[0]), unsafe.Pointer(&rx[0]), length) if ret < 0 { - return nil, error("could not xfer") + return nil, SPIError{"could not xfer"} } return rx, nil }