Merge pull request #132 from hybridgroup/godocs-intel-iot
Adding godocs documentation to intel-iot package
This commit is contained in:
commit
b4d16f01cc
|
@ -5,18 +5,27 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
// gpioPath returns base gpio path
|
||||
func gpioPath() string {
|
||||
return "/sys/class/gpio"
|
||||
}
|
||||
|
||||
// gpioExportPath returns export path
|
||||
func gpioExportPath() string {
|
||||
return gpioPath() + "/export"
|
||||
}
|
||||
|
||||
// gpioUnExportPath returns unexport path
|
||||
func gpioUnExportPath() string {
|
||||
return gpioPath() + "/unexport"
|
||||
}
|
||||
|
||||
// gpioDirectionPath returns direction path for specified pin
|
||||
func gpioDirectionPath(pin string) string {
|
||||
return gpioPath() + "/gpio" + pin + "/direction"
|
||||
}
|
||||
|
||||
// gpioValuePath returns value path for specified pin
|
||||
func gpioValuePath(pin string) string {
|
||||
return gpioPath() + "/gpio" + pin + "/value"
|
||||
}
|
||||
|
@ -26,12 +35,15 @@ type digitalPin struct {
|
|||
dir string
|
||||
}
|
||||
|
||||
// newDigitalPin returns an exported digital pin
|
||||
func newDigitalPin(pin int) *digitalPin {
|
||||
d := &digitalPin{pin: strconv.Itoa(pin)}
|
||||
d.export()
|
||||
return d
|
||||
}
|
||||
|
||||
// setDir sets writes a directory using direction path for specified pin.
|
||||
// It panics on error
|
||||
func (d *digitalPin) setDir(dir string) {
|
||||
d.dir = dir
|
||||
err := writeFile(gpioDirectionPath(d.pin), dir)
|
||||
|
@ -40,6 +52,8 @@ func (d *digitalPin) setDir(dir string) {
|
|||
}
|
||||
}
|
||||
|
||||
// digitalWrite writes specified value to gpio value path
|
||||
// It panics on error
|
||||
func (d *digitalPin) digitalWrite(value string) {
|
||||
if d.dir != "out" {
|
||||
d.setDir("out")
|
||||
|
@ -50,6 +64,7 @@ func (d *digitalPin) digitalWrite(value string) {
|
|||
}
|
||||
}
|
||||
|
||||
// digitalRead reads from gpio value path
|
||||
func (d *digitalPin) digitalRead() int {
|
||||
if d.dir != "in" {
|
||||
d.setDir("in")
|
||||
|
@ -67,14 +82,17 @@ func (d *digitalPin) digitalRead() int {
|
|||
return i
|
||||
}
|
||||
|
||||
// export writes directory for gpio export path
|
||||
func (d *digitalPin) export() {
|
||||
writeFile(gpioExportPath(), d.pin)
|
||||
}
|
||||
|
||||
// unexport writes directory for gpio unexport path
|
||||
func (d *digitalPin) unexport() {
|
||||
writeFile(gpioUnExportPath(), d.pin)
|
||||
}
|
||||
|
||||
// close unexports digital pin
|
||||
func (d *digitalPin) close() {
|
||||
d.unexport()
|
||||
}
|
||||
|
|
|
@ -143,6 +143,7 @@ var sysfsPinMap = map[string]sysfsPin{
|
|||
},
|
||||
}
|
||||
|
||||
// writeFile validates file existence and writes data into it
|
||||
func writeFile(name, data string) error {
|
||||
if _, err := os.Stat(name); err == nil {
|
||||
err := ioutil.WriteFile(
|
||||
|
@ -159,6 +160,7 @@ func writeFile(name, data string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// changePinMode writes pin mode to current_pinmux file
|
||||
func changePinMode(pin, mode string) {
|
||||
err := writeFile(
|
||||
"/sys/kernel/debug/gpio_debug/gpio"+pin+"/current_pinmux",
|
||||
|
@ -169,6 +171,8 @@ func changePinMode(pin, mode string) {
|
|||
}
|
||||
}
|
||||
|
||||
// NewEditionAdaptor creates a EdisonAdaptor with specified name and
|
||||
// creates connect function
|
||||
func NewEdisonAdaptor(name string) *EdisonAdaptor {
|
||||
return &EdisonAdaptor{
|
||||
Adaptor: *gobot.NewAdaptor(
|
||||
|
@ -207,6 +211,8 @@ func NewEdisonAdaptor(name string) *EdisonAdaptor {
|
|||
}
|
||||
}
|
||||
|
||||
// Connect starts conection with board and creates
|
||||
// digitalPins and pwmPins adaptor maps
|
||||
func (e *EdisonAdaptor) Connect() bool {
|
||||
e.digitalPins = make(map[int]*digitalPin)
|
||||
e.pwmPins = make(map[int]*pwmPin)
|
||||
|
@ -214,6 +220,7 @@ func (e *EdisonAdaptor) Connect() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// Finalize closes connection to board and pins
|
||||
func (e *EdisonAdaptor) Finalize() bool {
|
||||
e.tristate.close()
|
||||
for _, pin := range e.digitalPins {
|
||||
|
@ -231,9 +238,14 @@ func (e *EdisonAdaptor) Finalize() bool {
|
|||
}
|
||||
return true
|
||||
}
|
||||
func (e *EdisonAdaptor) Reconnect() bool { return true }
|
||||
|
||||
// Reconnect retries connection to edison board
|
||||
func (e *EdisonAdaptor) Reconnect() bool { return true }
|
||||
|
||||
// Disconnect returns true if connection to edison board is finished successfully
|
||||
func (e *EdisonAdaptor) Disconnect() bool { return true }
|
||||
|
||||
// digitalPin returns matched digitalPin for specified values
|
||||
func (e *EdisonAdaptor) digitalPin(pin string, dir string) *digitalPin {
|
||||
i := sysfsPinMap[pin]
|
||||
if e.digitalPins[i.pin] == nil {
|
||||
|
@ -264,14 +276,17 @@ func (e *EdisonAdaptor) digitalPin(pin string, dir string) *digitalPin {
|
|||
return e.digitalPins[i.pin]
|
||||
}
|
||||
|
||||
// DigitalRead reads digital value from pin
|
||||
func (e *EdisonAdaptor) DigitalRead(pin string) int {
|
||||
return e.digitalPin(pin, "in").digitalRead()
|
||||
}
|
||||
|
||||
// DigitalWrite writes digital value to specified pin
|
||||
func (e *EdisonAdaptor) DigitalWrite(pin string, val byte) {
|
||||
e.digitalPin(pin, "out").digitalWrite(strconv.Itoa(int(val)))
|
||||
}
|
||||
|
||||
// PwmWrite writes scaled pwm value to specified pin
|
||||
func (e *EdisonAdaptor) PwmWrite(pin string, val byte) {
|
||||
sysPin := sysfsPinMap[pin]
|
||||
if sysPin.pwmPin != -1 {
|
||||
|
@ -291,6 +306,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(
|
||||
"/sys/bus/iio/devices/iio:device1/in_voltage" + pin + "_raw",
|
||||
|
@ -305,6 +321,7 @@ func (e *EdisonAdaptor) AnalogRead(pin string) int {
|
|||
return val
|
||||
}
|
||||
|
||||
// I2cStart initializes i2c device for addresss
|
||||
func (e *EdisonAdaptor) I2cStart(address byte) {
|
||||
e.tristate.digitalWrite("0")
|
||||
|
||||
|
@ -331,10 +348,12 @@ func (e *EdisonAdaptor) I2cStart(address byte) {
|
|||
e.i2cDevice.start()
|
||||
}
|
||||
|
||||
// I2cWrite writes data to i2cDevice
|
||||
func (e *EdisonAdaptor) I2cWrite(data []byte) {
|
||||
e.i2cDevice.write(data)
|
||||
}
|
||||
|
||||
// I2cRead reads data from i2cDevice
|
||||
func (e *EdisonAdaptor) I2cRead(size uint) []byte {
|
||||
return e.i2cDevice.read(size)
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ type i2cDevice struct {
|
|||
i2cLocation string
|
||||
}
|
||||
|
||||
/// newI2cDevice creates a new i2c device for address
|
||||
func newI2cDevice(address byte) *i2cDevice {
|
||||
return &i2cDevice{
|
||||
i2cLocation: I2CLocation,
|
||||
|
@ -21,6 +22,8 @@ func newI2cDevice(address byte) *i2cDevice {
|
|||
}
|
||||
}
|
||||
|
||||
// start initializes i2c device.
|
||||
// Panics on error.
|
||||
func (i *i2cDevice) start() {
|
||||
var err error
|
||||
i.file, err = os.OpenFile(i.i2cLocation, os.O_RDWR, os.ModeExclusive)
|
||||
|
@ -39,10 +42,12 @@ func (i *i2cDevice) start() {
|
|||
i.write([]byte{0})
|
||||
}
|
||||
|
||||
// write writes data to i2c file
|
||||
func (i *i2cDevice) write(data []byte) {
|
||||
i.file.Write(data)
|
||||
}
|
||||
|
||||
// read gets data from i2c file
|
||||
func (i *i2cDevice) read(len uint) []byte {
|
||||
buf := make([]byte, len)
|
||||
i.file.Read(buf)
|
||||
|
|
|
@ -5,21 +5,32 @@ 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"
|
||||
}
|
||||
|
@ -28,6 +39,7 @@ type pwmPin struct {
|
|||
pin string
|
||||
}
|
||||
|
||||
// newPwmPin returns an exported and enabled pwmPin
|
||||
func newPwmPin(pin int) *pwmPin {
|
||||
p := &pwmPin{pin: strconv.Itoa(pin)}
|
||||
p.export()
|
||||
|
@ -35,6 +47,7 @@ func newPwmPin(pin int) *pwmPin {
|
|||
return p
|
||||
}
|
||||
|
||||
// enable writes value to pwm enable path
|
||||
func (p *pwmPin) enable(val string) {
|
||||
err := writeFile(pwmEnablePath(p.pin), val)
|
||||
if err != nil {
|
||||
|
@ -42,6 +55,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))
|
||||
if err != nil {
|
||||
|
@ -50,6 +64,7 @@ func (p *pwmPin) period() string {
|
|||
return string(buf[0 : len(buf)-1])
|
||||
}
|
||||
|
||||
// writeDuty writes value to pwm duty cycle path
|
||||
func (p *pwmPin) writeDuty(duty string) {
|
||||
err := writeFile(pwmDutyCyclePath(p.pin), duty)
|
||||
if err != nil {
|
||||
|
@ -57,14 +72,17 @@ func (p *pwmPin) writeDuty(duty string) {
|
|||
}
|
||||
}
|
||||
|
||||
// export writes pin to pwm export path
|
||||
func (p *pwmPin) export() {
|
||||
writeFile(pwmExportPath(), p.pin)
|
||||
}
|
||||
|
||||
// export writes pin to pwm unexport path
|
||||
func (p *pwmPin) unexport() {
|
||||
writeFile(pwmUnExportPath(), p.pin)
|
||||
}
|
||||
|
||||
// close disables and unexports pin
|
||||
func (p *pwmPin) close() {
|
||||
p.enable("0")
|
||||
p.unexport()
|
||||
|
|
|
@ -1 +1,10 @@
|
|||
/*
|
||||
This package contains the Gobot adaptor for the Intel Edison (http://www.intel.com/content/www/us/en/do-it-yourself/edison.html) IoT platform.
|
||||
|
||||
This package currently supports the following Intel IoT hardware:
|
||||
- Intel Edison with the Arduino breakout board
|
||||
|
||||
For further information refer to intel-iot README:
|
||||
https://github.com/hybridgroup/gobot/blob/master/platforms/intel-iot/README.md
|
||||
*/
|
||||
package inteliot
|
||||
|
|
Loading…
Reference in New Issue