Skip to content

Commit

Permalink
First pass at adding some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Aug 13, 2014
1 parent 30dc0f6 commit 11ff06b
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.swp
profile.cov
count.out
*.swp
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
PACKAGES := gobot gobot/api $(shell ls ./platforms | sed -e 's/^/gobot\/platforms\//')

.PHONY: test cover robeaux
.PHONY: test cover robeaux

test:
for package in $(PACKAGES) ; do \
go test github.com/hybridgroup/$$package ; \
done ; \

cover:
echo "mode: count" > profile.cov ; \
for package in $(PACKAGES) ; do \
Expand All @@ -32,4 +32,4 @@ endif
cd .. ; \
rm -rf robeaux-tmp/ ; \
go fmt ./robeaux/robeaux.go ; \

2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *connections) Start() error {
return err
}

// Filanize() finalizes all the connections.
// Finalize() finalizes all the connections.
func (c *connections) Finalize() {
for _, connection := range *c {
connection.Finalize()
Expand Down
99 changes: 97 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,104 @@
package gobot
// Copyright 2014 The Gobot Authors, HybridGroup. All rights reserved.

/*
Package gobot is the main point of entry in your Gobot application. A Gobot
is typically composed of one or more robots that makes up a project.
Commands are a way to expose your robots functionality with the external world.
A Gobot can be configured to expose a restful HTTP interface using the api
package. You can define custom commands on your Gobot and interact with your
application as a web service.
Basic Setup
package main
import (
"fmt"
"time"
"github.com/hybridgroup/gobot"
)
func main() {
gbot := gobot.NewGobot()
robot := gobot.NewRobot("Eve", func() {
gobot.Every(500*time.Millisecond, func() {
fmt.Println("Greeting Human")
})
})
gbot.AddRobot(robot)
gbot.Start()
}
Web Enabled? You bet!
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/api"
)
overall doc for my package
func main() {
gbot := gobot.NewGobot()
// Starts the API server on default port 3000
api.NewAPI(gbot).Start()
// Accessible via http://localhost:3000/api/commands/say_hello
gbot.AddCommand("say_hello", func(params map[string]interface{}) interface{} {
return "Master says hello!"
})
hello := gbot.AddRobot(gobot.NewRobot("Eve"))
// Accessible via http://localhost:3000/robots/Eve/commands/say_hello
hello.AddCommand("say_hello", func(params map[string]interface{}) interface{} {
return fmt.Sprintf("%v says hello!", hello.Name)
})
gbot.Start()
}
Blinking teh LED (Hello Eve!)
package main
import (
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
)
func main() {
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "led", "13")
work := func() {
gobot.Every(1*time.Second, func() {
led.Toggle()
})
}
robot := gobot.NewRobot("Eve",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{led},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}
*/
package gobot
21 changes: 21 additions & 0 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import (
"os/signal"
)

// JSONGobot holds a JSON representation of a Gobot.
type JSONGobot struct {
Robots []*JSONRobot `json:"robots"`
Commands []string `json:"commands"`
}

// Gobot is a container composed of one or more robots
type Gobot struct {
robots *robots
commands map[string]func(map[string]interface{}) interface{}
trap func(chan os.Signal)
}

// NewGobot instantiates a new Gobot
func NewGobot() *Gobot {
return &Gobot{
robots: &robots{},
Expand All @@ -27,18 +30,32 @@ func NewGobot() *Gobot {
}
}

/*
AddCommand creates a new command and adds it to the Gobot. This command
will be available via HTTP using '/commands/name'
Example:
gbot.AddCommand( 'rollover', func( params map[string]interface{}) interface{} {
fmt.Println( "Rolling over - Stand by...")
})
With the api package setup, you can now get your Gobot to rollover using: http://localhost:3000/commands/rollover
*/
func (g *Gobot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
g.commands[name] = f
}

// Commands lists all available commands on this Gobot instance.
func (g *Gobot) Commands() map[string]func(map[string]interface{}) interface{} {
return g.commands
}

// Command fetch the associated command using the given command name
func (g *Gobot) Command(name string) func(map[string]interface{}) interface{} {
return g.commands[name]
}

// Start runs the main Gobot event loop
func (g *Gobot) Start() {
g.robots.Start()

Expand All @@ -54,15 +71,18 @@ func (g *Gobot) Start() {
})
}

// Robots fetch all robots associated with this Gobot instance.
func (g *Gobot) Robots() *robots {
return g.robots
}

// AddRobot adds a new robot to our Gobot instance.
func (g *Gobot) AddRobot(r *Robot) *Robot {
*g.robots = append(*g.robots, r)
return r
}

// Robot find a robot with a given name.
func (g *Gobot) Robot(name string) *Robot {
for _, robot := range *g.Robots() {
if robot.Name == name {
Expand All @@ -72,6 +92,7 @@ func (g *Gobot) Robot(name string) *Robot {
return nil
}

// ToJSON retrieves a JSON representation of this Gobot.
func (g *Gobot) ToJSON() *JSONGobot {
jsonGobot := &JSONGobot{
Robots: []*JSONRobot{},
Expand Down
8 changes: 5 additions & 3 deletions platforms/beaglebone/beaglebone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"strings"
)

const Slots = "/sys/devices/bone_capemgr.*"
const Ocp = "/sys/devices/ocp.*"
const I2CLocation = "/dev/i2c-1"
const (
Slots = "/sys/devices/bone_capemgr.*"
Ocp = "/sys/devices/ocp.*"
I2CLocation = "/dev/i2c-1"
)

var pins = map[string]int{
"P8_3": 38,
Expand Down
23 changes: 23 additions & 0 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
"log"
)

// JSONRobot a JSON representation of a robot.
type JSONRobot struct {
Name string `json:"name"`
Commands []string `json:"commands"`
Connections []*JSONConnection `json:"connections"`
Devices []*JSONDevice `json:"devices"`
}

// Robot software representation of a physical board. A robot is a named
// entitity that manages multiple IO devices using a set of adaptors. Additionally
// a user can specificy custom commands to control a robot remotely.
type Robot struct {
Name string
commands map[string]func(map[string]interface{}) interface{}
Expand All @@ -22,22 +26,28 @@ type Robot struct {

type robots []*Robot

// Len counts the robots associated with this instance.
func (r *robots) Len() int {
return len(*r)
}

// Start initialises the event loop. All robots that were added will
// be automtically started as a result of this call.
func (r *robots) Start() {
for _, robot := range *r {
robot.Start()
}
}

// Each enumerates thru the robts and calls specified function
func (r *robots) Each(f func(*Robot)) {
for _, robot := range *r {
f(robot)
}
}

// NewRobot constructs a new named robot. Though a robot's name will be generated,
// we recommend that user take care of naming a robot for later access.
func NewRobot(name string, v ...interface{}) *Robot {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
Expand Down Expand Up @@ -77,18 +87,24 @@ func NewRobot(name string, v ...interface{}) *Robot {
return r
}

// AddCommand setup a new command that we be made available via the REST api.
func (r *Robot) AddCommand(name string, f func(map[string]interface{}) interface{}) {
r.commands[name] = f
}

// Commands lists out all available commands on this robot.
func (r *Robot) Commands() map[string]func(map[string]interface{}) interface{} {
return r.commands
}

// Command fetch a named command on this robot.
func (r *Robot) Command(name string) func(map[string]interface{}) interface{} {
return r.commands[name]
}

// Start a robot instance and runs it's work function if any. You should not
// need to manually start a robot if already part of a Gobot application as the
// robot will be automatically started for you.
func (r *Robot) Start() {
log.Println("Starting Robot", r.Name, "...")
if err := r.Connections().Start(); err != nil {
Expand All @@ -103,15 +119,18 @@ func (r *Robot) Start() {
}
}

// Devices retrieves all devices associated with this robot.
func (r *Robot) Devices() *devices {
return r.devices
}

// AddDevice adds a new device on this robot.
func (r *Robot) AddDevice(d Device) Device {
*r.devices = append(*r.Devices(), d)
return d
}

// Device finds a device by name.
func (r *Robot) Device(name string) Device {
if r == nil {
return nil
Expand All @@ -124,15 +143,18 @@ func (r *Robot) Device(name string) Device {
return nil
}

// Connections retrieves all connections on this robot.
func (r *Robot) Connections() *connections {
return r.connections
}

// AddConnection add a new connection on this robot.
func (r *Robot) AddConnection(c Connection) Connection {
*r.connections = append(*r.Connections(), c)
return c
}

// Connection finds a connection by name.
func (r *Robot) Connection(name string) Connection {
if r == nil {
return nil
Expand All @@ -145,6 +167,7 @@ func (r *Robot) Connection(name string) Connection {
return nil
}

// ToJSON returns a JSON representation of the master robot.
func (r *Robot) ToJSON() *JSONRobot {
jsonRobot := &JSONRobot{
Name: r.Name,
Expand Down

0 comments on commit 11ff06b

Please sign in to comment.