From 2e1ab7b795ab573535fbb24332b0b2e6b5741efd Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Fri, 25 Oct 2013 15:41:02 -0700 Subject: [PATCH] Alter structure --- src/gobot/adaptor.go => adaptor.go | 0 src/gobot/connection.go => connection.go | 0 src/gobot/device.go => device.go | 0 src/gobot/driver.go => driver.go | 0 src/gobot/gobot.go => gobot.go | 0 src/gobot/port.go => port.go | 0 src/gobot/robot.go => robot.go | 0 src/gobot/beaglebone.go | 105 ----------------------- src/gobot/digital_pin.go | 84 ------------------ src/gobot/led.go | 44 ---------- 10 files changed, 233 deletions(-) rename src/gobot/adaptor.go => adaptor.go (100%) rename src/gobot/connection.go => connection.go (100%) rename src/gobot/device.go => device.go (100%) rename src/gobot/driver.go => driver.go (100%) rename src/gobot/gobot.go => gobot.go (100%) rename src/gobot/port.go => port.go (100%) rename src/gobot/robot.go => robot.go (100%) delete mode 100644 src/gobot/beaglebone.go delete mode 100644 src/gobot/digital_pin.go delete mode 100644 src/gobot/led.go diff --git a/src/gobot/adaptor.go b/adaptor.go similarity index 100% rename from src/gobot/adaptor.go rename to adaptor.go diff --git a/src/gobot/connection.go b/connection.go similarity index 100% rename from src/gobot/connection.go rename to connection.go diff --git a/src/gobot/device.go b/device.go similarity index 100% rename from src/gobot/device.go rename to device.go diff --git a/src/gobot/driver.go b/driver.go similarity index 100% rename from src/gobot/driver.go rename to driver.go diff --git a/src/gobot/gobot.go b/gobot.go similarity index 100% rename from src/gobot/gobot.go rename to gobot.go diff --git a/src/gobot/port.go b/port.go similarity index 100% rename from src/gobot/port.go rename to port.go diff --git a/src/gobot/robot.go b/robot.go similarity index 100% rename from src/gobot/robot.go rename to robot.go diff --git a/src/gobot/beaglebone.go b/src/gobot/beaglebone.go deleted file mode 100644 index 08cd609df..000000000 --- a/src/gobot/beaglebone.go +++ /dev/null @@ -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 -} diff --git a/src/gobot/digital_pin.go b/src/gobot/digital_pin.go deleted file mode 100644 index f07b0a91b..000000000 --- a/src/gobot/digital_pin.go +++ /dev/null @@ -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() -} diff --git a/src/gobot/led.go b/src/gobot/led.go deleted file mode 100644 index 6cf356e48..000000000 --- a/src/gobot/led.go +++ /dev/null @@ -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) -}