Skip to content

Commit

Permalink
WIP switching linux sysfs io to shared package
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Oct 30, 2014
1 parent 04ea7b0 commit 72bd696
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 212 deletions.
98 changes: 0 additions & 98 deletions platforms/intel-iot/edison/digital_pin.go

This file was deleted.

123 changes: 64 additions & 59 deletions platforms/intel-iot/edison/edison_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package edison
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/sysfs"
)

type mux struct {
pin int
value int
value []byte
}
type sysfsPin struct {
pin int
Expand All @@ -24,10 +26,10 @@ type sysfsPin struct {

type EdisonAdaptor struct {
gobot.Adaptor
tristate *digitalPin
digitalPins map[int]*digitalPin
tristate *sysfs.DigitalPin
digitalPins map[int]*sysfs.DigitalPin
pwmPins map[int]*pwmPin
i2cDevice *i2cDevice
i2cDevice io.ReadWriteCloser
connect func(e *EdisonAdaptor)
}

Expand Down Expand Up @@ -109,8 +111,8 @@ var sysfsPinMap = map[string]sysfsPin{
levelShifter: 258,
pwmPin: 4,
mux: []mux{
mux{263, 1},
mux{240, 0},
mux{263, sysfs.HIGH},
mux{240, sysfs.LOW},
},
},
"11": sysfsPin{
Expand All @@ -119,8 +121,8 @@ var sysfsPinMap = map[string]sysfsPin{
levelShifter: 259,
pwmPin: 5,
mux: []mux{
mux{262, 1},
mux{241, 0},
mux{262, sysfs.HIGH},
mux{241, sysfs.LOW},
},
},
"12": sysfsPin{
Expand All @@ -129,7 +131,7 @@ var sysfsPinMap = map[string]sysfsPin{
levelShifter: 260,
pwmPin: -1,
mux: []mux{
mux{242, 0},
mux{242, sysfs.LOW},
},
},
"13": sysfsPin{
Expand All @@ -138,7 +140,7 @@ var sysfsPinMap = map[string]sysfsPin{
levelShifter: 261,
pwmPin: -1,
mux: []mux{
mux{243, 0},
mux{243, sysfs.LOW},
},
},
}
Expand Down Expand Up @@ -180,22 +182,22 @@ func NewEdisonAdaptor(name string) *EdisonAdaptor {
"EdisonAdaptor",
),
connect: func(e *EdisonAdaptor) {
e.tristate = newDigitalPin(214)
e.tristate.setDir("out")
e.tristate.digitalWrite("0")
e.tristate = sysfs.NewDigitalPin(214)
e.tristate.SetDirection(sysfs.OUT)
e.tristate.Write(sysfs.LOW)

for _, i := range []int{263, 262} {
io := newDigitalPin(i)
io.setDir("out")
io.digitalWrite("1")
io.close()
io := sysfs.NewDigitalPin(i)
io.SetDirection(sysfs.OUT)
io.Write(sysfs.HIGH)
io.Unexport()
}

for _, i := range []int{240, 241, 242, 243} {
io := newDigitalPin(i)
io.setDir("out")
io.digitalWrite("0")
io.close()
io := sysfs.NewDigitalPin(i)
io.SetDirection(sysfs.OUT)
io.Write(sysfs.LOW)
io.Unexport()
}

for _, i := range []string{"111", "115", "114", "109"} {
Expand All @@ -206,26 +208,26 @@ func NewEdisonAdaptor(name string) *EdisonAdaptor {
changePinMode(i, "0")
}

e.tristate.digitalWrite("1")
e.tristate.Write(sysfs.HIGH)
},
}
}

// Connect starts conection with board and creates
// digitalPins and pwmPins adaptor maps
func (e *EdisonAdaptor) Connect() bool {
e.digitalPins = make(map[int]*digitalPin)
e.digitalPins = make(map[int]*sysfs.DigitalPin)
e.pwmPins = make(map[int]*pwmPin)
e.connect(e)
return true
}

// Finalize closes connection to board and pins
func (e *EdisonAdaptor) Finalize() bool {
e.tristate.close()
e.tristate.Unexport()
for _, pin := range e.digitalPins {
if pin != nil {
pin.close()
pin.Unexport()
}
}
for _, pin := range e.pwmPins {
Expand All @@ -234,7 +236,7 @@ func (e *EdisonAdaptor) Finalize() bool {
}
}
if e.i2cDevice != nil {
e.i2cDevice.file.Close()
e.i2cDevice.Close()
}
return true
}
Expand All @@ -246,44 +248,46 @@ func (e *EdisonAdaptor) Reconnect() bool { return true }
func (e *EdisonAdaptor) Disconnect() bool { return true }

// digitalPin returns matched digitalPin for specified values
func (e *EdisonAdaptor) digitalPin(pin string, dir string) *digitalPin {
func (e *EdisonAdaptor) digitalPin(pin string, dir string) *sysfs.DigitalPin {
i := sysfsPinMap[pin]
if e.digitalPins[i.pin] == nil {
e.digitalPins[i.pin] = newDigitalPin(i.pin)
e.digitalPins[i.resistor] = newDigitalPin(i.resistor)
e.digitalPins[i.levelShifter] = newDigitalPin(i.levelShifter)
e.digitalPins[i.pin] = sysfs.NewDigitalPin(i.pin)
e.digitalPins[i.resistor] = sysfs.NewDigitalPin(i.resistor)
e.digitalPins[i.levelShifter] = sysfs.NewDigitalPin(i.levelShifter)
if len(i.mux) > 0 {
for _, mux := range i.mux {
e.digitalPins[mux.pin] = newDigitalPin(mux.pin)
e.digitalPins[mux.pin].setDir("out")
e.digitalPins[mux.pin].digitalWrite(strconv.Itoa(mux.value))
e.digitalPins[mux.pin] = sysfs.NewDigitalPin(mux.pin)
e.digitalPins[mux.pin].SetDirection(sysfs.OUT)
e.digitalPins[mux.pin].Write(mux.value)
}
}
}

if dir == "in" && e.digitalPins[i.pin].dir != "in" {
e.digitalPins[i.pin].setDir("in")
e.digitalPins[i.resistor].setDir("out")
e.digitalPins[i.resistor].digitalWrite("0")
e.digitalPins[i.levelShifter].setDir("out")
e.digitalPins[i.levelShifter].digitalWrite("0")
} else if dir == "out" && e.digitalPins[i.pin].dir != "out" {
e.digitalPins[i.pin].setDir("out")
e.digitalPins[i.resistor].setDir("in")
e.digitalPins[i.levelShifter].setDir("out")
e.digitalPins[i.levelShifter].digitalWrite("1")
if dir == "in" && e.digitalPins[i.pin].Direction() != "in" {
e.digitalPins[i.pin].SetDirection(sysfs.IN)
e.digitalPins[i.resistor].SetDirection(sysfs.OUT)
e.digitalPins[i.resistor].Write(sysfs.LOW)
e.digitalPins[i.levelShifter].SetDirection(sysfs.OUT)
e.digitalPins[i.levelShifter].Write(sysfs.LOW)
} else if dir == "out" && e.digitalPins[i.pin].Direction() != "out" {
e.digitalPins[i.pin].SetDirection(sysfs.OUT)
e.digitalPins[i.resistor].SetDirection(sysfs.IN)
e.digitalPins[i.levelShifter].SetDirection(sysfs.OUT)
e.digitalPins[i.levelShifter].Write(sysfs.HIGH)
}
return e.digitalPins[i.pin]
}

// DigitalRead reads digital value from pin
func (e *EdisonAdaptor) DigitalRead(pin string) int {
return e.digitalPin(pin, "in").digitalRead()
b := []byte{}
e.digitalPin(pin, "in").Read(b)
return int(b[0])
}

// DigitalWrite writes digital value to specified pin
func (e *EdisonAdaptor) DigitalWrite(pin string, val byte) {
e.digitalPin(pin, "out").digitalWrite(strconv.Itoa(int(val)))
e.digitalPin(pin, "out").Write([]byte(strconv.Itoa(int(val))))
}

// PwmWrite writes scaled pwm value to specified pin
Expand Down Expand Up @@ -323,37 +327,38 @@ func (e *EdisonAdaptor) AnalogRead(pin string) int {

// I2cStart initializes i2c device for addresss
func (e *EdisonAdaptor) I2cStart(address byte) {
e.tristate.digitalWrite("0")
e.tristate.Write(sysfs.LOW)

for _, i := range []int{14, 165, 212, 213} {
io := newDigitalPin(i)
io.setDir("in")
io.close()
io := sysfs.NewDigitalPin(i)
io.SetDirection(sysfs.IN)
io.Unexport()
}

for _, i := range []int{236, 237, 204, 205} {
io := newDigitalPin(i)
io.setDir("out")
io.digitalWrite("0")
io.close()
io := sysfs.NewDigitalPin(i)
io.SetDirection(sysfs.OUT)
io.Write(sysfs.LOW)
io.Unexport()
}

for _, i := range []string{"28", "27"} {
changePinMode(i, "1")
}

e.tristate.digitalWrite("1")
e.tristate.Write(sysfs.HIGH)

e.i2cDevice = newI2cDevice(address)
e.i2cDevice.start()
e.i2cDevice, _ = sysfs.NewI2cDevice("/dev/i2c-6", address)
}

// I2cWrite writes data to i2cDevice
func (e *EdisonAdaptor) I2cWrite(data []byte) {
e.i2cDevice.write(data)
e.i2cDevice.Write(data)
}

// I2cRead reads data from i2cDevice
func (e *EdisonAdaptor) I2cRead(size uint) []byte {
return e.i2cDevice.read(size)
b := make([]byte, size)
e.i2cDevice.Read(b)
return b
}
Loading

0 comments on commit 72bd696

Please sign in to comment.