mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +08:00
Alter structure
This commit is contained in:
parent
bbf80e4f0e
commit
2e1ab7b795
@ -1,105 +0,0 @@
|
||||
package gobot
|
||||
|
||||
type Beaglebone struct {
|
||||
Adaptor
|
||||
Pins []*DigitalPin
|
||||
Translations map[string]int
|
||||
}
|
||||
|
||||
func (b *Beaglebone) Connect() {
|
||||
b.Pins = make([]*DigitalPin,120)
|
||||
b.Translations = map[string]int {
|
||||
"P8_3": 38,
|
||||
"P8_4": 39,
|
||||
"P8_5": 34,
|
||||
"P8_6": 35,
|
||||
"P8_7": 66,
|
||||
"P8_8": 67,
|
||||
"P8_9": 69,
|
||||
"P8_10": 68,
|
||||
"P8_11": 45,
|
||||
"P8_12": 44,
|
||||
"P8_13": 23,
|
||||
"P8_14": 26,
|
||||
"P8_15": 47,
|
||||
"P8_16": 46,
|
||||
"P8_17": 27,
|
||||
"P8_18": 65,
|
||||
"P8_19": 22,
|
||||
"P8_20": 63,
|
||||
"P8_21": 62,
|
||||
"P8_22": 37,
|
||||
"P8_23": 36,
|
||||
"P8_24": 33,
|
||||
"P8_25": 32,
|
||||
"P8_26": 61,
|
||||
"P8_27": 86,
|
||||
"P8_28": 88,
|
||||
"P8_29": 87,
|
||||
"P8_30": 89,
|
||||
"P8_31": 10,
|
||||
"P8_32": 11,
|
||||
"P8_33": 9,
|
||||
"P8_34": 81,
|
||||
"P8_35": 8,
|
||||
"P8_36": 80,
|
||||
"P8_37": 78,
|
||||
"P8_38": 79,
|
||||
"P8_39": 76,
|
||||
"P8_40": 77,
|
||||
"P8_41": 74,
|
||||
"P8_42": 75,
|
||||
"P8_43": 72,
|
||||
"P8_44": 73,
|
||||
"P8_45": 70,
|
||||
"P8_46": 71,
|
||||
"P9_11": 30,
|
||||
"P9_12": 60,
|
||||
"P9_13": 31,
|
||||
"P9_14": 50,
|
||||
"P9_15": 48,
|
||||
"P9_16": 51,
|
||||
"P9_17": 5,
|
||||
"P9_18": 4,
|
||||
"P9_19": 13,
|
||||
"P9_20": 12,
|
||||
"P9_21": 3,
|
||||
"P9_22": 2,
|
||||
"P9_23": 49,
|
||||
"P9_24": 15,
|
||||
"P9_25": 117,
|
||||
"P9_26": 14,
|
||||
"P9_27": 115,
|
||||
"P9_28": 113,
|
||||
"P9_29": 111,
|
||||
"P9_30": 112,
|
||||
"P9_31": 110,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Beaglebone) Disconnect(){}
|
||||
func (b *Beaglebone) IsConnected() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *Beaglebone) DigitalWrite(pin string, val string) {
|
||||
i := b.BeaglebonePin(pin, "w")
|
||||
b.Pins[i].DigitalWrite(val)
|
||||
}
|
||||
|
||||
func (b *Beaglebone) TranslatePin(pin string) int{
|
||||
for key, value := range b.Translations {
|
||||
if key == pin {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (b *Beaglebone) BeaglebonePin(pin string, mode string) int {
|
||||
i := b.TranslatePin(pin)
|
||||
if b.Pins[i] == nil || b.Pins[i].Mode != mode {
|
||||
b.Pins[i] = NewDigitalPin(i, mode)
|
||||
}
|
||||
return i
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
package gobot
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DigitalPin struct {
|
||||
PinNum string
|
||||
Mode string
|
||||
PinFile *os.File
|
||||
Status string
|
||||
}
|
||||
|
||||
const GPIO_PATH = "/sys/class/gpio"
|
||||
const GPIO_DIRECTION_READ = "in"
|
||||
const GPIO_DIRECTION_WRITE = "out"
|
||||
const HIGH = 1
|
||||
const LOW = 0
|
||||
|
||||
func NewDigitalPin(pinNum int, mode string) *DigitalPin {
|
||||
d := DigitalPin{PinNum: strconv.Itoa(pinNum)}
|
||||
|
||||
fi, _ := os.OpenFile(GPIO_PATH + "/export", os.O_WRONLY | os.O_APPEND, 0666)
|
||||
fi.WriteString(d.PinNum)
|
||||
fi.Close()
|
||||
|
||||
d.SetMode(mode)
|
||||
|
||||
return &d
|
||||
}
|
||||
|
||||
func (d *DigitalPin) SetMode(mode string) {
|
||||
d.Mode = mode
|
||||
|
||||
if mode == "w" {
|
||||
fi, _ := os.OpenFile(GPIO_PATH + "/gpio" + d.PinNum + "/direction", os.O_WRONLY, 0666)
|
||||
fi.WriteString(GPIO_DIRECTION_WRITE)
|
||||
fi.Close()
|
||||
d.PinFile, _ = os.OpenFile(GPIO_PATH + "/gpio" + d.PinNum + "/value", os.O_WRONLY, 0666)
|
||||
} else if mode =="r" {
|
||||
fi, _ := os.OpenFile(GPIO_PATH + "/gpio" + d.PinNum + "/direction", os.O_WRONLY, 0666)
|
||||
fi.WriteString(GPIO_DIRECTION_READ)
|
||||
fi.Close()
|
||||
d.PinFile, _ = os.OpenFile(GPIO_PATH + "/gpio" + d.PinNum + "/value", os.O_RDONLY, 0666)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DigitalPin) DigitalWrite(value string) {
|
||||
|
||||
if d.Mode != "w" {
|
||||
d.SetMode("w")
|
||||
}
|
||||
|
||||
d.PinFile.WriteString(value)
|
||||
d.PinFile.Sync()
|
||||
}
|
||||
|
||||
func (d *DigitalPin) IsOn() bool {
|
||||
if d.Status == "1" {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DigitalPin) IsOff() bool {
|
||||
return !d.IsOn()
|
||||
}
|
||||
|
||||
func (d *DigitalPin) On() {
|
||||
d.DigitalWrite("1")
|
||||
}
|
||||
|
||||
func (d *DigitalPin) Off() {
|
||||
d.DigitalWrite("0")
|
||||
}
|
||||
|
||||
func (d *DigitalPin) Close() {
|
||||
fi, _ := os.OpenFile(GPIO_PATH + "/unexport", os.O_WRONLY | os.O_APPEND, 0666)
|
||||
fi.WriteString(d.PinNum)
|
||||
fi.Close()
|
||||
d.PinFile.Close()
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package gobot
|
||||
|
||||
type Led struct {
|
||||
Driver
|
||||
Beaglebone *Beaglebone
|
||||
High bool
|
||||
}
|
||||
|
||||
func NewLed(b *Beaglebone) *Led{
|
||||
l := Led{High: false, Beaglebone: b}
|
||||
return &l
|
||||
}
|
||||
|
||||
func (l *Led) IsOn() bool {
|
||||
return l.High
|
||||
}
|
||||
|
||||
func (l *Led) IsOff() bool {
|
||||
return !l.IsOn()
|
||||
}
|
||||
|
||||
func (l *Led) On() bool {
|
||||
l.changeState(l.Pin, "1")
|
||||
l.High = true
|
||||
return true
|
||||
}
|
||||
|
||||
func (l *Led) Off() bool {
|
||||
l.changeState(l.Pin, "0")
|
||||
l.High = false
|
||||
return true
|
||||
}
|
||||
|
||||
func (l *Led) Toggle() {
|
||||
if l.IsOn() {
|
||||
l.Off()
|
||||
} else {
|
||||
l.On()
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Led) changeState(pin string, level string) {
|
||||
l.Beaglebone.DigitalWrite(pin, level)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user