mirror of
https://github.com/kidoman/embd
synced 2024-12-22 04:40:04 +01:00
spi: more refactoring
This commit is contained in:
parent
67d316851a
commit
2d3dee8764
@ -37,8 +37,6 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("analog value is: %v\n", val)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,9 +18,9 @@ func main() {
|
||||
spiBus := embd.NewSPIBus(embd.SPIMode0, 0, 1000000, 8, 0)
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -36,12 +36,10 @@ func main() {
|
||||
}
|
||||
|
||||
func getSensorValue(bus embd.SPIBus) (uint16, error) {
|
||||
data := make([]uint8, 3)
|
||||
data[0] = 1
|
||||
data[1] = 128
|
||||
data[2] = 0
|
||||
data := [3]uint8{1, 128, 0}
|
||||
|
||||
var err error
|
||||
err = bus.TransferAndRecieveData(data)
|
||||
err = bus.TransferAndRecieveData(data[:])
|
||||
if err != nil {
|
||||
return uint16(0), err
|
||||
}
|
||||
|
2
spi.go
2
spi.go
@ -40,8 +40,10 @@ type SPIBus interface {
|
||||
// SPIDriver interface interacts with the host descriptors to allow us
|
||||
// control of SPI communication.
|
||||
type SPIDriver interface {
|
||||
// Bus returns a SPIBus interface which allows us to use spi functionalities
|
||||
Bus(byte, byte, int, int, int) SPIBus
|
||||
|
||||
// Close cleans up all the initialized SPIbus
|
||||
Close() error
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,8 @@ type spiDriver struct {
|
||||
sbf spiBusFactory
|
||||
}
|
||||
|
||||
// NewSPIDriver returns a SPIDriver interface which allows control
|
||||
// over the SPI bus.
|
||||
func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory, i func() error) SPIDriver {
|
||||
return &spiDriver{
|
||||
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 {
|
||||
s.busMapLock.Lock()
|
||||
defer s.busMapLock.Unlock()
|
||||
@ -32,6 +35,7 @@ func (s *spiDriver) Bus(mode, channel byte, speed, bpw, delay int) SPIBus {
|
||||
return b
|
||||
}
|
||||
|
||||
// Close cleans up all the initialized SPIbus
|
||||
func (s *spiDriver) Close() error {
|
||||
for _, b := range s.busMap {
|
||||
b.Close()
|
||||
|
Loading…
x
Reference in New Issue
Block a user