SPI Device with cgo

master
Friedl Ulrich 10 years ago
parent 3475675001
commit 67daba17ef

@ -85,28 +85,20 @@ int spi_xfer(int fd, char* tx, char* rx, int length) {
*/ */
import "C" import "C"
import "unsafe" import "unsafe"
import "errors"
// SPIDevice device // SPIDevice device
type SPIDevice struct { type SPIDevice struct {
fd int fd C.int
}
// SPIError is the error
type SPIError struct {
s string
}
func (e SPIError) Error() string {
return e.s
} }
// NewSPIDevice creates a new device // NewSPIDevice creates a new device
func NewSPIDevice() (*SPIDevice, error) { func NewSPIDevice() (*SPIDevice, error) {
name := C.CString("/dev/spidev0.0") name := C.CString("/dev/spidev0.0")
defer C.free(unsafe.Pointer(name))
i := C.spi_open(name) i := C.spi_open(name)
C.free(unsafe.Pointer(name))
if i < 0 { if i < 0 {
return nil, SPIError{"could not open"} return nil, errors.New("could not open")
} }
return &SPIDevice{i}, nil return &SPIDevice{i}, nil
} }
@ -115,9 +107,9 @@ func NewSPIDevice() (*SPIDevice, error) {
func (d *SPIDevice) Xfer(tx []byte) ([]byte, error) { func (d *SPIDevice) Xfer(tx []byte) ([]byte, error) {
length := len(tx) length := len(tx)
rx := make([]byte, length) rx := make([]byte, length)
ret := C.spi_xfer(d.fd, unsafe.Pointer(&tx[0]), unsafe.Pointer(&rx[0]), length) ret := C.spi_xfer(d.fd, unsafe.Pointer(&tx[0]), unsafe.Pointer(&rx[0]), C.int(length))
if ret < 0 { if ret < 0 {
return nil, SPIError{"could not xfer"} return nil, errors.New("could not xfer")
} }
return rx, nil return rx, nil
} }

Loading…
Cancel
Save