Skip to content

Commit

Permalink
Update gpio package
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Apr 29, 2014

Verified

This commit was signed with the committer’s verified signature.
eitoball Eito Katagiri
1 parent 6c9815d commit 9f746b4
Showing 25 changed files with 606 additions and 598 deletions.
32 changes: 0 additions & 32 deletions gpio/analog_sensor.go

This file was deleted.

29 changes: 29 additions & 0 deletions gpio/analog_sensor_driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gpio

import (
"github.com/hybridgroup/gobot"
)

type AnalogSensorDriver struct {
gobot.Driver
Adaptor AnalogReader
}

func NewAnalogSensor(a AnalogReader) *AnalogSensorDriver {
return &AnalogSensorDriver{
Driver: gobot.Driver{
Commands: []string{
"ReadC",
},
},
Adaptor: a,
}
}

func (a *AnalogSensorDriver) Start() bool { return true }
func (a *AnalogSensorDriver) Init() bool { return true }
func (a *AnalogSensorDriver) Halt() bool { return true }

func (a *AnalogSensorDriver) Read() int {
return a.Adaptor.AnalogRead(a.Pin)
}
18 changes: 9 additions & 9 deletions gpio/analog_sensor_test.go → gpio/analog_sensor_driver_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gobotGPIO
package gpio

import (
. "github.com/onsi/ginkgo"
@@ -7,25 +7,25 @@ import (

var _ = Describe("Analog-Sensor", func() {
var (
someAdaptor TestAdaptor
someDriver *AnalogSensor
t TestAdaptor
a *AnalogSensorDriver
)

BeforeEach(func() {
someDriver = NewAnalogSensor(someAdaptor)
someDriver.Pin = "1"
a = NewAnalogSensor(t)
a.Pin = "1"
})

It("Must be able to Read", func() {
Expect(someDriver.Read()).To(Equal(99))
Expect(a.Read()).To(Equal(99))
})
It("Must be able to Start", func() {
Expect(someDriver.Start()).To(Equal(true))
Expect(a.Start()).To(Equal(true))
})
It("Must be able to Halt", func() {
Expect(someDriver.Halt()).To(Equal(true))
Expect(a.Halt()).To(Equal(true))
})
It("Must be able to Init", func() {
Expect(someDriver.Init()).To(Equal(true))
Expect(a.Init()).To(Equal(true))
})
})
56 changes: 0 additions & 56 deletions gpio/button.go

This file was deleted.

54 changes: 54 additions & 0 deletions gpio/button_driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package gpio

import (
"github.com/hybridgroup/gobot"
)

type ButtonDriver struct {
gobot.Driver
Adaptor DigitalReader
Active bool
}

func NewButtonDriver(a DigitalReader) *ButtonDriver {
return &ButtonDriver{
Driver: gobot.Driver{
Events: map[string]chan interface{}{
"push": make(chan interface{}),
"release": make(chan interface{}),
},
},
Active: false,
Adaptor: a,
}
}

func (b *ButtonDriver) Start() bool {
state := 0
go func() {
for {
new_value := b.readState()
if new_value != state && new_value != -1 {
state = new_value
b.update(new_value)
}
}
}()
return true
}
func (b *ButtonDriver) Halt() bool { return true }
func (b *ButtonDriver) Init() bool { return true }

func (b *ButtonDriver) readState() int {
return b.Adaptor.DigitalRead(b.Pin)
}

func (b *ButtonDriver) update(new_val int) {
if new_val == 1 {
b.Active = true
gobot.Publish(b.Events["push"], new_val)
} else {
b.Active = false
gobot.Publish(b.Events["release"], new_val)
}
}
26 changes: 13 additions & 13 deletions gpio/button_test.go → gpio/button_driver_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gobotGPIO
package gpio

import (
. "github.com/onsi/ginkgo"
@@ -7,36 +7,36 @@ import (

var _ = Describe("Button", func() {
var (
someAdaptor TestAdaptor
someDriver *Button
t TestAdaptor
b *ButtonDriver
)

BeforeEach(func() {
someDriver = NewButton(someAdaptor)
someDriver.Pin = "1"
b = NewButtonDriver(t)
b.Pin = "1"
})

It("Must be able to readState", func() {
Expect(someDriver.readState()).To(Equal(1))
Expect(b.readState()).To(Equal(1))
})

It("Must update on button state change to on", func() {
someDriver.update(1)
Expect(someDriver.Active).To(Equal(true))
b.update(1)
Expect(b.Active).To(Equal(true))
})

It("Must update on button state change to off", func() {
someDriver.update(0)
Expect(someDriver.Active).To(Equal(false))
b.update(0)
Expect(b.Active).To(Equal(false))
})

It("Must be able to Start", func() {
Expect(someDriver.Start()).To(Equal(true))
Expect(b.Start()).To(Equal(true))
})
It("Must be able to Init", func() {
Expect(someDriver.Init()).To(Equal(true))
Expect(b.Init()).To(Equal(true))
})
It("Must be able to Halt", func() {
Expect(someDriver.Halt()).To(Equal(true))
Expect(b.Halt()).To(Equal(true))
})
})
32 changes: 16 additions & 16 deletions gpio/commands.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
package gobotGPIO
package gpio

import "strconv"

// Led
func (l *Led) ToggleC(params map[string]interface{}) {
func (l *LedDriver) ToggleC(params map[string]interface{}) {
l.Toggle()
}
func (l *Led) OnC(params map[string]interface{}) {
func (l *LedDriver) OnC(params map[string]interface{}) {
l.On()
}
func (l *Led) OffC(params map[string]interface{}) {
func (l *LedDriver) OffC(params map[string]interface{}) {
l.Off()
}
func (l *Led) BrightnessC(params map[string]interface{}) {
func (l *LedDriver) BrightnessC(params map[string]interface{}) {
level := byte(params["level"].(float64))
l.Brightness(level)
}

// Servo
func (l *Servo) MoveC(params map[string]interface{}) {
func (l *ServoDriver) MoveC(params map[string]interface{}) {
angle := byte(params["angle"].(float64))
l.Move(angle)
}
func (l *Servo) MinC(params map[string]interface{}) {
func (l *ServoDriver) MinC(params map[string]interface{}) {
l.Min()
}
func (l *Servo) CenterC(params map[string]interface{}) {
func (l *ServoDriver) CenterC(params map[string]interface{}) {
l.Center()
}
func (l *Servo) MaxC(params map[string]interface{}) {
func (l *ServoDriver) MaxC(params map[string]interface{}) {
l.Max()
}

// Direct Pin
func (d *DirectPin) DigitalReadC(params map[string]interface{}) int {
func (d *DirectPinDriver) DigitalReadC(params map[string]interface{}) int {
return d.DigitalRead()
}
func (d *DirectPin) DigitalWriteC(params map[string]interface{}) {
func (d *DirectPinDriver) DigitalWriteC(params map[string]interface{}) {
level, _ := strconv.Atoi(params["level"].(string))
d.DigitalWrite(byte(level))
}
func (d *DirectPin) AnalogReadC(params map[string]interface{}) int {
func (d *DirectPinDriver) AnalogReadC(params map[string]interface{}) int {
return d.AnalogRead()
}
func (d *DirectPin) AnalogWriteC(params map[string]interface{}) {
func (d *DirectPinDriver) AnalogWriteC(params map[string]interface{}) {
level, _ := strconv.Atoi(params["level"].(string))
d.AnalogWrite(byte(level))
}
func (d *DirectPin) PwmWriteC(params map[string]interface{}) {
func (d *DirectPinDriver) PwmWriteC(params map[string]interface{}) {
level, _ := strconv.Atoi(params["level"].(string))
d.PwmWrite(byte(level))
}
func (d *DirectPin) ServoWriteC(params map[string]interface{}) {
func (d *DirectPinDriver) ServoWriteC(params map[string]interface{}) {
level, _ := strconv.Atoi(params["level"].(string))
d.ServoWrite(byte(level))
}

// Analog Sensor
func (d *AnalogSensor) ReadC(params map[string]interface{}) int {
func (d *AnalogSensorDriver) ReadC(params map[string]interface{}) int {
return d.Read()
}
Loading
Oops, something went wrong.

0 comments on commit 9f746b4

Please sign in to comment.