Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slice write support #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
_obj
_test

# intellij idea files
.idea

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
Expand Down
7 changes: 4 additions & 3 deletions MCP23S17/MCP23S17.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package MCP23S17

import (
"fmt"

"github.com/luismesas/goPi/spi"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ type MCP23S17 struct {

func NewMCP23S17(hardwareAddress uint8, bus int, chip_select int) *MCP23S17 {
mcp := new(MCP23S17)
mcp.Device = spi.NewSPIDevice(bus, chip_select)
mcp.Device = spi.NewSPIDevice(bus, chip_select, spi.SPI_DELAY)
mcp.HardwareAddress = hardwareAddress

mcp.IODIRa = NewMCP23S17Register(IODIRA, mcp)
Expand Down Expand Up @@ -141,7 +142,7 @@ func (mcp *MCP23S17) getSPIControlByte(read_write_cmd uint8) byte {
// Returns the value of the address specified.
func (mcp *MCP23S17) Read(address byte) byte {
ctrl_byte := mcp.getSPIControlByte(READ_CMD)
data, err := mcp.Device.Send([3]byte{ctrl_byte, address, 0})
data, err := mcp.Device.Send([]byte{ctrl_byte, address, 0})
if err != nil {
panic(fmt.Sprintf("Error reading from MCP23S17: %s\n", err))
return 0x00
Expand All @@ -155,7 +156,7 @@ func (mcp *MCP23S17) Read(address byte) byte {
// Writes data to the address specified.
func (mcp *MCP23S17) Write(data byte, address byte) {
ctrl_byte := mcp.getSPIControlByte(WRITE_CMD)
_, err := mcp.Device.Send([3]byte{ctrl_byte, address, data})
_, err := mcp.Device.Send([]byte{ctrl_byte, address, data})
if err != nil {
panic(fmt.Sprintf("Error writing on MCP23S17: %s\n", err))
}
Expand Down
5 changes: 3 additions & 2 deletions examples/blink_MCP23S17/blink_MCP23S17.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"github.com/luismesas/goPi/MCP23S17"
"github.com/luismesas/goPi/spi"
"log"
"time"

"github.com/luismesas/goPi/MCP23S17"
"github.com/luismesas/goPi/spi"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion examples/blink_piface/blink_piface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"time"

"github.com/luismesas/goPi/piface"
"github.com/luismesas/goPi/spi"
"time"
)

func main() {
Expand Down
9 changes: 4 additions & 5 deletions piface/piface.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
/*
Package piface is for interfacing with a PiFace Digital IO board.
/*
Package piface is for interfacing with a PiFace Digital IO board.

- More Info at http://www.piface.org.uk/products/piface_digital/
- Guides at http://prod.www.piface.org.uk/guides/
- goPi blink example at https://github.com/luismesas/goPi/blob/master/examples/blink_piface/blink_piface.go

Its possible to connect up to four PiFace boards to a Raspberry Pi, however some jumper
settings are required to set the hardware address.
settings are required to set the hardware address.

- See http://prod.www.piface.org.uk/guides/howto/PiFace_Digital_Jumper_Settings/

*/
package piface



import (
"fmt"

"github.com/luismesas/goPi/MCP23S17"
)

Expand Down
5 changes: 2 additions & 3 deletions rpi.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

// Package rpi is a library for interfacing with Raspberry Pi IO
package rpi

// This is the wrong url here ???
// This is the wrong url here ???
import (
_ "github.com/luismesas/goPi/MCP23S17"
_ "github.com/luismesas/goPi/ioctl"
_ "github.com/luismesas/goPi/spi"
_ "github.com/luismesas/goPi/piface"
_ "github.com/luismesas/goPi/spi"
)
29 changes: 13 additions & 16 deletions spi/SPIDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package spi

import (
"fmt"
"github.com/luismesas/goPi/ioctl"
"os"
"unsafe"

"github.com/luismesas/goPi/ioctl"
)

const SPIDEV = "/dev/spidev"
Expand All @@ -26,13 +27,15 @@ type SPIDevice struct {
mode uint8
bpw uint8
speed uint32
delay uint16
}

// An SPI Device at /dev/spi<bus>.<chip_select>.
func NewSPIDevice(bus int, chipSelect int) *SPIDevice {
func NewSPIDevice(bus int, chipSelect int, delayUsec uint16) *SPIDevice {
spi := new(SPIDevice)
spi.Bus = bus
spi.Chip = chipSelect
spi.delay = delayUsec

return spi
}
Expand Down Expand Up @@ -61,16 +64,16 @@ func (spi *SPIDevice) Close() error {
}

// Sends bytes over SPI channel and returns []byte response
func (spi *SPIDevice) Send(bytes_to_send [3]byte) ([]byte, error) {
wBuffer := bytes_to_send
rBuffer := [3]byte{}
func (spi *SPIDevice) Send(wBuffer []byte) ([]byte, error) {
length := len(wBuffer)
rBuffer := make([]byte, length)

// generates message
transfer := SPI_IOC_TRANSFER{}
transfer.txBuf = uint64(uintptr(unsafe.Pointer(&wBuffer)))
transfer.rxBuf = uint64(uintptr(unsafe.Pointer(&rBuffer)))
transfer.length = uint32(unsafe.Sizeof(wBuffer))
transfer.delayUsecs = SPI_DELAY
transfer.txBuf = uint64(uintptr(unsafe.Pointer(&wBuffer[0])))
transfer.rxBuf = uint64(uintptr(unsafe.Pointer(&rBuffer[0])))
transfer.length = uint32(length)
transfer.delayUsecs = spi.delay
transfer.bitsPerWord = spi.bpw
transfer.speedHz = spi.speed

Expand All @@ -80,13 +83,7 @@ func (spi *SPIDevice) Send(bytes_to_send [3]byte) ([]byte, error) {
return nil, fmt.Errorf("Error on sending: %s\n", err)
}

// generates a valid response
ret := make([]byte, unsafe.Sizeof(rBuffer))
for i := range ret {
ret[i] = rBuffer[i]
}

return ret, nil
return rBuffer, nil
}

func (spi *SPIDevice) SetMode(mode uint8) error {
Expand Down
3 changes: 2 additions & 1 deletion spi/spi.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package spi

import (
"github.com/luismesas/goPi/ioctl"
"unsafe"

"github.com/luismesas/goPi/ioctl"
)

const SPI_IOC_MAGIC = 107
Expand Down