spi: more refactoring

This commit is contained in:
kunalpowar 2014-05-22 02:31:42 +05:30
parent 67d316851a
commit 2d3dee8764
5 changed files with 11 additions and 9 deletions

View File

@ -37,8 +37,6 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Printf("analog value is: %v\n", val) fmt.Printf("analog value is: %v\n", val)
} }
} }

View File

@ -18,9 +18,9 @@ func main() {
spiBus := embd.NewSPIBus(embd.SPIMode0, 0, 1000000, 8, 0) spiBus := embd.NewSPIBus(embd.SPIMode0, 0, 1000000, 8, 0)
defer spiBus.Close() defer spiBus.Close()
dataBuf := []uint8{1, 2, 3} dataBuf := [3]uint8{1, 2, 3}
if err := spiBus.TransferAndRecieveData(dataBuf); err != nil { if err := spiBus.TransferAndRecieveData(dataBuf[:]); err != nil {
panic(err) panic(err)
} }

View File

@ -36,12 +36,10 @@ func main() {
} }
func getSensorValue(bus embd.SPIBus) (uint16, error) { func getSensorValue(bus embd.SPIBus) (uint16, error) {
data := make([]uint8, 3) data := [3]uint8{1, 128, 0}
data[0] = 1
data[1] = 128
data[2] = 0
var err error var err error
err = bus.TransferAndRecieveData(data) err = bus.TransferAndRecieveData(data[:])
if err != nil { if err != nil {
return uint16(0), err return uint16(0), err
} }

2
spi.go
View File

@ -40,8 +40,10 @@ type SPIBus interface {
// SPIDriver interface interacts with the host descriptors to allow us // SPIDriver interface interacts with the host descriptors to allow us
// control of SPI communication. // control of SPI communication.
type SPIDriver interface { type SPIDriver interface {
// Bus returns a SPIBus interface which allows us to use spi functionalities
Bus(byte, byte, int, int, int) SPIBus Bus(byte, byte, int, int, int) SPIBus
// Close cleans up all the initialized SPIbus
Close() error Close() error
} }

View File

@ -14,6 +14,8 @@ type spiDriver struct {
sbf spiBusFactory sbf spiBusFactory
} }
// NewSPIDriver returns a SPIDriver interface which allows control
// over the SPI bus.
func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory, i func() error) SPIDriver { func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory, i func() error) SPIDriver {
return &spiDriver{ return &spiDriver{
spiDevMinor: spiDevMinor, spiDevMinor: spiDevMinor,
@ -22,6 +24,7 @@ func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory, i func() error) SPIDriver
} }
} }
// Bus returns a SPIBus interface which allows us to use spi functionalities
func (s *spiDriver) Bus(mode, channel byte, speed, bpw, delay int) SPIBus { func (s *spiDriver) Bus(mode, channel byte, speed, bpw, delay int) SPIBus {
s.busMapLock.Lock() s.busMapLock.Lock()
defer s.busMapLock.Unlock() defer s.busMapLock.Unlock()
@ -32,6 +35,7 @@ func (s *spiDriver) Bus(mode, channel byte, speed, bpw, delay int) SPIBus {
return b return b
} }
// Close cleans up all the initialized SPIbus
func (s *spiDriver) Close() error { func (s *spiDriver) Close() error {
for _, b := range s.busMap { for _, b := range s.busMap {
b.Close() b.Close()