Add pwm and analog test coverage

This commit is contained in:
Adrian Zankich 2014-11-04 12:21:18 -08:00
parent 00f4c637a1
commit e888b78035
3 changed files with 63 additions and 26 deletions

View File

@ -14,6 +14,26 @@ import (
var i2cLocation = "/dev/i2c-6"
// writeFile validates file existence and writes data into it
var writeFile = func(name, data string) error {
if _, err := os.Stat(name); err == nil {
err := ioutil.WriteFile(
name,
[]byte(data),
0644,
)
if err != nil {
return err
}
} else {
return errors.New("File doesn't exist: " + name)
}
return nil
}
var readFile = func(path string) ([]byte, error) {
return ioutil.ReadFile(path)
}
type mux struct {
pin int
value int
@ -147,23 +167,6 @@ var sysfsPinMap = map[string]sysfsPin{
},
}
// writeFile validates file existence and writes data into it
var writeFile = func(name, data string) error {
if _, err := os.Stat(name); err == nil {
err := ioutil.WriteFile(
name,
[]byte(data),
0644,
)
if err != nil {
return err
}
} else {
return errors.New("File doesn't exist: " + name)
}
return nil
}
// changePinMode writes pin mode to current_pinmux file
func changePinMode(pin, mode string) {
err := writeFile(
@ -314,7 +317,7 @@ func (e *EdisonAdaptor) PwmWrite(pin string, val byte) {
// AnalogRead returns value from analog reading of specified pin
func (e *EdisonAdaptor) AnalogRead(pin string) int {
buf, err := ioutil.ReadFile(
buf, err := readFile(
"/sys/bus/iio/devices/iio:device1/in_voltage" + pin + "_raw",
)
if err != nil {

View File

@ -17,15 +17,20 @@ func initTestEdisonAdaptor() *EdisonAdaptor {
writeFile = func(name, data string) error {
return nil
}
readFile = func(name string) ([]byte, error) {
return []byte("11"), nil
}
a := NewEdisonAdaptor("myAdaptor")
a.Connect()
a.DigitalWrite("3", 1)
a.i2cDevice = new(gobot.NullReadWriteCloser)
return a
}
func TestEdisonAdaptorFinalize(t *testing.T) {
gobot.Assert(t, initTestEdisonAdaptor().Finalize(), true)
a := initTestEdisonAdaptor()
a.DigitalWrite("3", 1)
a.PwmWrite("5", 100)
a.i2cDevice = new(gobot.NullReadWriteCloser)
gobot.Assert(t, a.Finalize(), true)
}
func TestEdisonAdaptorDigitalIO(t *testing.T) {
@ -62,3 +67,35 @@ func TestEdisonAdaptorI2c(t *testing.T) {
a.I2cWrite([]byte{0x00, 0x01})
gobot.Assert(t, a.I2cRead(2), make([]byte, 2))
}
func TestEdisonAdaptorPwm(t *testing.T) {
a := initTestEdisonAdaptor()
lastWritePath := ""
lastReadPath := ""
lastWriteData := ""
writeFile = func(path, data string) (err error) {
lastWritePath = path
lastWriteData = data
return
}
readFile = func(path string) (b []byte, err error) {
lastReadPath = path
return []byte("100\n"), nil
}
a.PwmWrite("5", 100)
gobot.Assert(t, lastWritePath, "/sys/class/pwm/pwmchip0/pwm1/duty_cycle")
}
func TestEdisonAdaptorAnalog(t *testing.T) {
a := initTestEdisonAdaptor()
lastReadPath := ""
readFile = func(path string) (b []byte, err error) {
lastReadPath = path
return []byte("100\n"), nil
}
i := a.AnalogRead("0")
gobot.Assert(t, lastReadPath, "/sys/bus/iio/devices/iio:device1/in_voltage0_raw")
gobot.Assert(t, i, 100)
}

View File

@ -1,9 +1,6 @@
package edison
import (
"io/ioutil"
"strconv"
)
import "strconv"
// pwmPath returns pwm base path
func pwmPath() string {
@ -57,7 +54,7 @@ func (p *pwmPin) enable(val string) {
// period reads from pwm period path and returns value
func (p *pwmPin) period() string {
buf, err := ioutil.ReadFile(pwmPeriodPath(p.pin))
buf, err := readFile(pwmPeriodPath(p.pin))
if err != nil {
panic(err)
}