mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
Starting support for Intel Joule with the built-in LEDs and more
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
6f2f56e0f8
commit
2a6615424b
32
examples/joule_blink.go
Normal file
32
examples/joule_blink.go
Normal file
@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
"github.com/hybridgroup/gobot/platforms/intel-iot/joule"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
e := joule.NewJouleAdaptor("joule")
|
||||
led := gpio.NewLedDriver(e, "led", "100")
|
||||
|
||||
work := func() {
|
||||
gobot.Every(1*time.Second, func() {
|
||||
led.Toggle()
|
||||
})
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("blinkBot",
|
||||
[]gobot.Connection{e},
|
||||
[]gobot.Device{led},
|
||||
work,
|
||||
)
|
||||
|
||||
gbot.AddRobot(robot)
|
||||
|
||||
gbot.Start()
|
||||
}
|
49
examples/joule_leds.go
Normal file
49
examples/joule_leds.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
"github.com/hybridgroup/gobot/platforms/intel-iot/joule"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
e := joule.NewJouleAdaptor("joule")
|
||||
led0 := gpio.NewLedDriver(e, "led", "100")
|
||||
led1 := gpio.NewLedDriver(e, "led", "101")
|
||||
led2 := gpio.NewLedDriver(e, "led", "102")
|
||||
led3 := gpio.NewLedDriver(e, "led", "103")
|
||||
|
||||
work := func() {
|
||||
led0.Off()
|
||||
led1.Off()
|
||||
led2.Off()
|
||||
led3.Off()
|
||||
|
||||
gobot.Every(1*time.Second, func() {
|
||||
led0.Toggle()
|
||||
})
|
||||
gobot.Every(2*time.Second, func() {
|
||||
led1.Toggle()
|
||||
})
|
||||
gobot.Every(2*time.Second, func() {
|
||||
led2.Toggle()
|
||||
})
|
||||
gobot.Every(3*time.Second, func() {
|
||||
led3.Toggle()
|
||||
})
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("blinkBot",
|
||||
[]gobot.Connection{e},
|
||||
[]gobot.Device{led0, led1, led2, led3},
|
||||
work,
|
||||
)
|
||||
|
||||
gbot.AddRobot(robot)
|
||||
|
||||
gbot.Start()
|
||||
}
|
84
platforms/intel-iot/joule/README.md
Normal file
84
platforms/intel-iot/joule/README.md
Normal file
@ -0,0 +1,84 @@
|
||||
# Edison
|
||||
|
||||
The Intel Joule is a wifi and Bluetooth® enabled development platform for the Internet of Things.
|
||||
|
||||
For more info about the Intel Joule platform go to:
|
||||
|
||||
http://www.intel.com/joule
|
||||
|
||||
## Setting up your Intel Joule
|
||||
|
||||
Everything you need to get started with the Joule is in the Intel Getting Started Guide located at:
|
||||
|
||||
https://intel.com/joule/getstarted
|
||||
|
||||
Don't forget to configure your Joule's wifi connection and update your Joule to the latest firmware image!
|
||||
|
||||
## Example program
|
||||
|
||||
Save the following code into a file called `main.go`.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
"github.com/hybridgroup/gobot/platforms/intel-iot/joule"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
e := joule.NewJouleAdaptor("edison")
|
||||
led := gpio.NewLedDriver(e, "led", "103")
|
||||
|
||||
work := func() {
|
||||
gobot.Every(1*time.Second, func() {
|
||||
led.Toggle()
|
||||
})
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("blinkBot",
|
||||
[]gobot.Connection{e},
|
||||
[]gobot.Device{led},
|
||||
work,
|
||||
)
|
||||
|
||||
gbot.AddRobot(robot)
|
||||
|
||||
gbot.Start()
|
||||
}
|
||||
```
|
||||
|
||||
You can read the [full API documentation online](http://godoc.org/github.com/hybridgroup/gobot).
|
||||
|
||||
#### Cross compiling for the Intel Joule
|
||||
|
||||
Compile your Gobot program run the following command using the command
|
||||
line from the directory where you have your `main.go` file:
|
||||
|
||||
```bash
|
||||
$ GOARCH=386 GOOS=linux go build .
|
||||
```
|
||||
|
||||
Then you can simply upload your program over the network from your host computer to the Joule
|
||||
|
||||
```bash
|
||||
$ scp main root@<IP of your device>:/home/root/blink
|
||||
```
|
||||
|
||||
and then execute it on your Joule (use screen to connect, see the Intel
|
||||
setup steps if you don't recall how to connect)
|
||||
|
||||
```bash
|
||||
$ ./blink
|
||||
```
|
||||
|
||||
At this point you should see one of the onboard LEDs blinking. Press control + c
|
||||
to exit.
|
||||
|
||||
To update the program after you made a change, you will need to scp it
|
||||
over once again and start it from the command line (via screen).
|
7
platforms/intel-iot/joule/doc.go
Normal file
7
platforms/intel-iot/joule/doc.go
Normal file
@ -0,0 +1,7 @@
|
||||
/*
|
||||
Package joule contains the Gobot adaptor for the Intel Joule.
|
||||
|
||||
For further information refer to intel-iot README:
|
||||
https://github.com/hybridgroup/gobot/blob/master/platforms/intel-iot/joule/README.md
|
||||
*/
|
||||
package joule
|
410
platforms/intel-iot/joule/joule_adaptor.go
Normal file
410
platforms/intel-iot/joule/joule_adaptor.go
Normal file
@ -0,0 +1,410 @@
|
||||
package joule
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
"github.com/hybridgroup/gobot/platforms/i2c"
|
||||
"github.com/hybridgroup/gobot/sysfs"
|
||||
)
|
||||
|
||||
var _ gobot.Adaptor = (*JouleAdaptor)(nil)
|
||||
|
||||
var _ gpio.DigitalReader = (*JouleAdaptor)(nil)
|
||||
var _ gpio.DigitalWriter = (*JouleAdaptor)(nil)
|
||||
var _ gpio.PwmWriter = (*JouleAdaptor)(nil)
|
||||
|
||||
var _ i2c.I2c = (*JouleAdaptor)(nil)
|
||||
|
||||
func writeFile(path string, data []byte) (i int, err error) {
|
||||
file, err := sysfs.OpenFile(path, os.O_WRONLY, 0644)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return file.Write(data)
|
||||
}
|
||||
|
||||
func readFile(path string) ([]byte, error) {
|
||||
file, err := sysfs.OpenFile(path, os.O_RDONLY, 0644)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return make([]byte, 0), err
|
||||
}
|
||||
|
||||
buf := make([]byte, 200)
|
||||
var i = 0
|
||||
i, err = file.Read(buf)
|
||||
if i == 0 {
|
||||
return buf, err
|
||||
}
|
||||
return buf[:i], err
|
||||
}
|
||||
|
||||
type sysfsPin struct {
|
||||
pin int
|
||||
pwmPin int
|
||||
}
|
||||
|
||||
// JouleAdaptor represents an Intel Joule
|
||||
type JouleAdaptor struct {
|
||||
name string
|
||||
digitalPins map[int]sysfs.DigitalPin
|
||||
pwmPins map[int]*pwmPin
|
||||
i2cDevice sysfs.I2cDevice
|
||||
connect func(e *JouleAdaptor) (err error)
|
||||
}
|
||||
|
||||
var sysfsPinMap = map[string]sysfsPin{
|
||||
// disabled
|
||||
"0": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"1": sysfsPin{
|
||||
pin: 446,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"2": sysfsPin{
|
||||
pin: 421,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// disabled
|
||||
"3": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"4": sysfsPin{
|
||||
pin: 422,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"5": sysfsPin{
|
||||
pin: 356,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"6": sysfsPin{
|
||||
pin: 417,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// UART
|
||||
"7": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"8": sysfsPin{
|
||||
pin: 419,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// disabled
|
||||
"9": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"10": sysfsPin{
|
||||
pin: 416,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"11": sysfsPin{
|
||||
pin: 381,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"13": sysfsPin{
|
||||
pin: 382,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"15": sysfsPin{
|
||||
pin: 380,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"17": sysfsPin{
|
||||
pin: 379,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"19": sysfsPin{
|
||||
pin: 378,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// UART
|
||||
"21": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// disabled
|
||||
"22": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// UART
|
||||
"23": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// disabled
|
||||
"24": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"25": sysfsPin{
|
||||
pin: 463,
|
||||
pwmPin: 0,
|
||||
},
|
||||
// low voltage should not use
|
||||
"26": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"27": sysfsPin{
|
||||
pin: 464,
|
||||
pwmPin: 1,
|
||||
},
|
||||
// disabled
|
||||
"28": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"29": sysfsPin{
|
||||
pin: 465,
|
||||
pwmPin: 2,
|
||||
},
|
||||
// disabled?
|
||||
"30": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"31": sysfsPin{
|
||||
pin: 466,
|
||||
pwmPin: 3,
|
||||
},
|
||||
// disabled?
|
||||
"32": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// disabled
|
||||
"33": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
"34": sysfsPin{
|
||||
pin: 393,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// GND
|
||||
"35": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// GND
|
||||
"36": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// GND
|
||||
"37": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// GND
|
||||
"38": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// disabled
|
||||
"39": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// GND
|
||||
"40": sysfsPin{
|
||||
pin: -1,
|
||||
pwmPin: -1,
|
||||
},
|
||||
|
||||
// TODO: Second header
|
||||
|
||||
// LED100
|
||||
"100": sysfsPin{
|
||||
pin: 337,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// LED101
|
||||
"101": sysfsPin{
|
||||
pin: 338,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// LED102
|
||||
"102": sysfsPin{
|
||||
pin: 339,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// LED103
|
||||
"103": sysfsPin{
|
||||
pin: 340,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// LEDWIFI
|
||||
"104": sysfsPin{
|
||||
pin: 438,
|
||||
pwmPin: -1,
|
||||
},
|
||||
// LEDBT
|
||||
"105": sysfsPin{
|
||||
pin: 439,
|
||||
pwmPin: -1,
|
||||
},
|
||||
}
|
||||
|
||||
// NewJouleAdaptor returns a new JouleAdaptor with specified name
|
||||
func NewJouleAdaptor(name string) *JouleAdaptor {
|
||||
return &JouleAdaptor{
|
||||
name: name,
|
||||
connect: func(e *JouleAdaptor) (err error) {
|
||||
return
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Name returns the JouleAdaptors name
|
||||
func (e *JouleAdaptor) Name() string { return e.name }
|
||||
|
||||
// Connect initializes the Joule for use with the Arduino beakout board
|
||||
func (e *JouleAdaptor) Connect() (errs []error) {
|
||||
e.digitalPins = make(map[int]sysfs.DigitalPin)
|
||||
e.pwmPins = make(map[int]*pwmPin)
|
||||
if err := e.connect(e); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize releases all i2c devices and exported digital and pwm pins.
|
||||
func (e *JouleAdaptor) Finalize() (errs []error) {
|
||||
for _, pin := range e.digitalPins {
|
||||
if pin != nil {
|
||||
if err := pin.Unexport(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, pin := range e.pwmPins {
|
||||
if pin != nil {
|
||||
if err := pin.enable("0"); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if err := pin.unexport(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if e.i2cDevice != nil {
|
||||
if err := e.i2cDevice.Close(); errs != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
// digitalPin returns matched digitalPin for specified values
|
||||
func (e *JouleAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
|
||||
i := sysfsPinMap[pin]
|
||||
if e.digitalPins[i.pin] == nil {
|
||||
e.digitalPins[i.pin] = sysfs.NewDigitalPin(i.pin)
|
||||
if err = e.digitalPins[i.pin].Export(); err != nil {
|
||||
// TODO: log error
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if dir == "in" {
|
||||
if err = e.digitalPins[i.pin].Direction(sysfs.IN); err != nil {
|
||||
return
|
||||
}
|
||||
} else if dir == "out" {
|
||||
if err = e.digitalPins[i.pin].Direction(sysfs.OUT); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return e.digitalPins[i.pin], nil
|
||||
}
|
||||
|
||||
// DigitalRead reads digital value from pin
|
||||
func (e *JouleAdaptor) DigitalRead(pin string) (i int, err error) {
|
||||
sysfsPin, err := e.digitalPin(pin, "in")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return sysfsPin.Read()
|
||||
}
|
||||
|
||||
// DigitalWrite writes a value to the pin. Acceptable values are 1 or 0.
|
||||
func (e *JouleAdaptor) DigitalWrite(pin string, val byte) (err error) {
|
||||
sysfsPin, err := e.digitalPin(pin, "out")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return sysfsPin.Write(int(val))
|
||||
}
|
||||
|
||||
// PwmWrite writes the 0-254 value to the specified pin
|
||||
func (e *JouleAdaptor) PwmWrite(pin string, val byte) (err error) {
|
||||
sysPin := sysfsPinMap[pin]
|
||||
if sysPin.pwmPin != -1 {
|
||||
if e.pwmPins[sysPin.pwmPin] == nil {
|
||||
if err = e.DigitalWrite(pin, 1); err != nil {
|
||||
return
|
||||
}
|
||||
e.pwmPins[sysPin.pwmPin] = newPwmPin(sysPin.pwmPin)
|
||||
if err = e.pwmPins[sysPin.pwmPin].export(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = e.pwmPins[sysPin.pwmPin].enable("1"); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
p, err := e.pwmPins[sysPin.pwmPin].period()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
period, err := strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
duty := gobot.FromScale(float64(val), 0, 255.0)
|
||||
return e.pwmPins[sysPin.pwmPin].writeDuty(strconv.Itoa(int(float64(period) * duty)))
|
||||
}
|
||||
return errors.New("Not a PWM pin")
|
||||
}
|
||||
|
||||
// I2cStart initializes i2c device for addresss
|
||||
func (e *JouleAdaptor) I2cStart(address int) (err error) {
|
||||
if e.i2cDevice != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: handle the additional I2C buses
|
||||
e.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-0", address)
|
||||
return
|
||||
}
|
||||
|
||||
// I2cWrite writes data to i2c device
|
||||
func (e *JouleAdaptor) I2cWrite(address int, data []byte) (err error) {
|
||||
if err = e.i2cDevice.SetAddress(address); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = e.i2cDevice.Write(data)
|
||||
return
|
||||
}
|
||||
|
||||
// I2cRead returns size bytes from the i2c device
|
||||
func (e *JouleAdaptor) I2cRead(address int, size int) (data []byte, err error) {
|
||||
data = make([]byte, size)
|
||||
if err = e.i2cDevice.SetAddress(address); err != nil {
|
||||
return
|
||||
}
|
||||
_, err = e.i2cDevice.Read(data)
|
||||
return
|
||||
}
|
177
platforms/intel-iot/joule/joule_adaptor_test.go
Normal file
177
platforms/intel-iot/joule/joule_adaptor_test.go
Normal file
@ -0,0 +1,177 @@
|
||||
package joule
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/hybridgroup/gobot/gobottest"
|
||||
"github.com/hybridgroup/gobot/sysfs"
|
||||
)
|
||||
|
||||
type NullReadWriteCloser struct {
|
||||
contents []byte
|
||||
}
|
||||
|
||||
func (n *NullReadWriteCloser) SetAddress(int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NullReadWriteCloser) Write(b []byte) (int, error) {
|
||||
n.contents = make([]byte, len(b))
|
||||
copy(n.contents[:], b[:])
|
||||
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (n *NullReadWriteCloser) Read(b []byte) (int, error) {
|
||||
copy(b, n.contents)
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
var closeErr error = nil
|
||||
|
||||
func (n *NullReadWriteCloser) Close() error {
|
||||
return closeErr
|
||||
}
|
||||
|
||||
func initTestJouleAdaptor() (*JouleAdaptor, *sysfs.MockFilesystem) {
|
||||
a := NewJouleAdaptor("myAdaptor")
|
||||
fs := sysfs.NewMockFilesystem([]string{
|
||||
"/sys/bus/iio/devices/iio:device1/in_voltage0_raw",
|
||||
"/sys/kernel/debug/gpio_debug/gpio111/current_pinmux",
|
||||
"/sys/kernel/debug/gpio_debug/gpio115/current_pinmux",
|
||||
"/sys/kernel/debug/gpio_debug/gpio114/current_pinmux",
|
||||
"/sys/kernel/debug/gpio_debug/gpio109/current_pinmux",
|
||||
"/sys/kernel/debug/gpio_debug/gpio131/current_pinmux",
|
||||
"/sys/kernel/debug/gpio_debug/gpio129/current_pinmux",
|
||||
"/sys/kernel/debug/gpio_debug/gpio40/current_pinmux",
|
||||
"/sys/kernel/debug/gpio_debug/gpio13/current_pinmux",
|
||||
"/sys/kernel/debug/gpio_debug/gpio28/current_pinmux",
|
||||
"/sys/kernel/debug/gpio_debug/gpio27/current_pinmux",
|
||||
"/sys/class/pwm/pwmchip0/export",
|
||||
"/sys/class/pwm/pwmchip0/unexport",
|
||||
"/sys/class/pwm/pwmchip0/pwm0/duty_cycle",
|
||||
"/sys/class/pwm/pwmchip0/pwm0/period",
|
||||
"/sys/class/pwm/pwmchip0/pwm0/enable",
|
||||
"/sys/class/gpio/export",
|
||||
"/sys/class/gpio/unexport",
|
||||
"/sys/class/gpio/gpio13/value",
|
||||
"/sys/class/gpio/gpio13/direction",
|
||||
"/sys/class/gpio/gpio40/value",
|
||||
"/sys/class/gpio/gpio40/direction",
|
||||
"/sys/class/gpio/gpio446/value",
|
||||
"/sys/class/gpio/gpio446/direction",
|
||||
"/sys/class/gpio/gpio463/value",
|
||||
"/sys/class/gpio/gpio463/direction",
|
||||
"/sys/class/gpio/gpio421/value",
|
||||
"/sys/class/gpio/gpio421/direction",
|
||||
"/sys/class/gpio/gpio221/value",
|
||||
"/sys/class/gpio/gpio221/direction",
|
||||
"/sys/class/gpio/gpio243/value",
|
||||
"/sys/class/gpio/gpio243/direction",
|
||||
"/sys/class/gpio/gpio229/value",
|
||||
"/sys/class/gpio/gpio229/direction",
|
||||
"/sys/class/gpio/gpio253/value",
|
||||
"/sys/class/gpio/gpio253/direction",
|
||||
"/sys/class/gpio/gpio261/value",
|
||||
"/sys/class/gpio/gpio261/direction",
|
||||
"/sys/class/gpio/gpio214/value",
|
||||
"/sys/class/gpio/gpio214/direction",
|
||||
"/sys/class/gpio/gpio14/direction",
|
||||
"/sys/class/gpio/gpio14/value",
|
||||
"/sys/class/gpio/gpio165/direction",
|
||||
"/sys/class/gpio/gpio165/value",
|
||||
"/sys/class/gpio/gpio212/direction",
|
||||
"/sys/class/gpio/gpio212/value",
|
||||
"/sys/class/gpio/gpio213/direction",
|
||||
"/sys/class/gpio/gpio213/value",
|
||||
"/sys/class/gpio/gpio236/direction",
|
||||
"/sys/class/gpio/gpio236/value",
|
||||
"/sys/class/gpio/gpio237/direction",
|
||||
"/sys/class/gpio/gpio237/value",
|
||||
"/sys/class/gpio/gpio204/direction",
|
||||
"/sys/class/gpio/gpio204/value",
|
||||
"/sys/class/gpio/gpio205/direction",
|
||||
"/sys/class/gpio/gpio205/value",
|
||||
"/sys/class/gpio/gpio263/direction",
|
||||
"/sys/class/gpio/gpio263/value",
|
||||
"/sys/class/gpio/gpio262/direction",
|
||||
"/sys/class/gpio/gpio262/value",
|
||||
"/sys/class/gpio/gpio240/direction",
|
||||
"/sys/class/gpio/gpio240/value",
|
||||
"/sys/class/gpio/gpio241/direction",
|
||||
"/sys/class/gpio/gpio241/value",
|
||||
"/sys/class/gpio/gpio242/direction",
|
||||
"/sys/class/gpio/gpio242/value",
|
||||
"/sys/class/gpio/gpio218/direction",
|
||||
"/sys/class/gpio/gpio218/value",
|
||||
"/sys/class/gpio/gpio250/direction",
|
||||
"/sys/class/gpio/gpio250/value",
|
||||
"/dev/i2c-0",
|
||||
})
|
||||
sysfs.SetFilesystem(fs)
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents = "5000\n"
|
||||
a.Connect()
|
||||
return a, fs
|
||||
}
|
||||
|
||||
func TestJouleAdaptor(t *testing.T) {
|
||||
a, _ := initTestJouleAdaptor()
|
||||
gobottest.Assert(t, a.Name(), "myAdaptor")
|
||||
}
|
||||
|
||||
func TestJouleAdaptorConnect(t *testing.T) {
|
||||
a, _ := initTestJouleAdaptor()
|
||||
gobottest.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
||||
func TestJouleAdaptorFinalize(t *testing.T) {
|
||||
a, _ := initTestJouleAdaptor()
|
||||
a.DigitalWrite("1", 1)
|
||||
a.PwmWrite("25", 100)
|
||||
|
||||
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
||||
a.I2cStart(0xff)
|
||||
|
||||
gobottest.Assert(t, len(a.Finalize()), 0)
|
||||
|
||||
closeErr = errors.New("close error")
|
||||
sysfs.SetFilesystem(sysfs.NewMockFilesystem([]string{}))
|
||||
gobottest.Refute(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
||||
func TestJouleAdaptorDigitalIO(t *testing.T) {
|
||||
a, fs := initTestJouleAdaptor()
|
||||
|
||||
a.DigitalWrite("1", 1)
|
||||
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio446/value"].Contents, "1")
|
||||
|
||||
a.DigitalWrite("2", 0)
|
||||
i, err := a.DigitalRead("2")
|
||||
gobottest.Assert(t, err, nil)
|
||||
gobottest.Assert(t, i, 0)
|
||||
}
|
||||
|
||||
func TestJouleAdaptorI2c(t *testing.T) {
|
||||
a, _ := initTestJouleAdaptor()
|
||||
|
||||
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
||||
a.I2cStart(0xff)
|
||||
|
||||
a.i2cDevice = &NullReadWriteCloser{}
|
||||
a.I2cWrite(0xff, []byte{0x00, 0x01})
|
||||
|
||||
data, _ := a.I2cRead(0xff, 2)
|
||||
gobottest.Assert(t, data, []byte{0x00, 0x01})
|
||||
}
|
||||
|
||||
func TestJouleAdaptorPwm(t *testing.T) {
|
||||
a, fs := initTestJouleAdaptor()
|
||||
|
||||
err := a.PwmWrite("25", 100)
|
||||
gobottest.Assert(t, err, nil)
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "1960")
|
||||
|
||||
err = a.PwmWrite("4", 100)
|
||||
gobottest.Assert(t, err, errors.New("Not a PWM pin"))
|
||||
}
|
76
platforms/intel-iot/joule/pwm_pin.go
Normal file
76
platforms/intel-iot/joule/pwm_pin.go
Normal file
@ -0,0 +1,76 @@
|
||||
// TODO: move this into shared PWM package
|
||||
package joule
|
||||
|
||||
import "strconv"
|
||||
|
||||
// pwmPath returns pwm base path
|
||||
func pwmPath() string {
|
||||
return "/sys/class/pwm/pwmchip0"
|
||||
}
|
||||
|
||||
// pwmExportPath returns export path
|
||||
func pwmExportPath() string {
|
||||
return pwmPath() + "/export"
|
||||
}
|
||||
|
||||
// pwmUnExportPath returns unexport path
|
||||
func pwmUnExportPath() string {
|
||||
return pwmPath() + "/unexport"
|
||||
}
|
||||
|
||||
// pwmDutyCyclePath returns duty_cycle path for specified pin
|
||||
func pwmDutyCyclePath(pin string) string {
|
||||
return pwmPath() + "/pwm" + pin + "/duty_cycle"
|
||||
}
|
||||
|
||||
// pwmPeriodPath returns period path for specified pin
|
||||
func pwmPeriodPath(pin string) string {
|
||||
return pwmPath() + "/pwm" + pin + "/period"
|
||||
}
|
||||
|
||||
// pwmEnablePath returns enable path for specified pin
|
||||
func pwmEnablePath(pin string) string {
|
||||
return pwmPath() + "/pwm" + pin + "/enable"
|
||||
}
|
||||
|
||||
type pwmPin struct {
|
||||
pin string
|
||||
}
|
||||
|
||||
// newPwmPin returns an exported and enabled pwmPin
|
||||
func newPwmPin(pin int) *pwmPin {
|
||||
return &pwmPin{pin: strconv.Itoa(pin)}
|
||||
}
|
||||
|
||||
// enable writes value to pwm enable path
|
||||
func (p *pwmPin) enable(val string) (err error) {
|
||||
_, err = writeFile(pwmEnablePath(p.pin), []byte(val))
|
||||
return
|
||||
}
|
||||
|
||||
// period reads from pwm period path and returns value
|
||||
func (p *pwmPin) period() (period string, err error) {
|
||||
buf, err := readFile(pwmPeriodPath(p.pin))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return string(buf[0 : len(buf)-1]), nil
|
||||
}
|
||||
|
||||
// writeDuty writes value to pwm duty cycle path
|
||||
func (p *pwmPin) writeDuty(duty string) (err error) {
|
||||
_, err = writeFile(pwmDutyCyclePath(p.pin), []byte(duty))
|
||||
return
|
||||
}
|
||||
|
||||
// export writes pin to pwm export path
|
||||
func (p *pwmPin) export() (err error) {
|
||||
_, err = writeFile(pwmExportPath(), []byte(p.pin))
|
||||
return
|
||||
}
|
||||
|
||||
// export writes pin to pwm unexport path
|
||||
func (p *pwmPin) unexport() (err error) {
|
||||
_, err = writeFile(pwmUnExportPath(), []byte(p.pin))
|
||||
return
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user