Skip to content

Commit

Permalink
Add serialport support
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 3, 2013
1 parent 45ada00 commit 60abbf8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/sphero_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func main() {
master := gobot.GobotMaster()

spheros := map[string]string{
"Sphero-BPO": "127.0.0.1:4560",
"Sphero-BPO": "/dev/rfcomm0",
}

for name, port := range spheros {
Expand Down
13 changes: 0 additions & 13 deletions port.go

This file was deleted.

21 changes: 20 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package gobot

import (
"encoding/json"
"github.com/tarm/goserial"
"io"
"math/rand"
"net"
"reflect"
"regexp"
"time"
)

type Port io.ReadWriteCloser

func Every(t string, f func()) {
dur := parseDuration(t)
go func() {
Expand Down Expand Up @@ -46,14 +51,28 @@ func On(cs chan interface{}) interface{} {
return nil
}

func ConnectTo(port string) net.Conn {
func ConnectToTcp(port string) io.ReadWriteCloser {
tcpPort, err := net.Dial("tcp", port)
if err != nil {
panic(err)
}
return tcpPort
}

func ConnectToSerial(port string, baud int) io.ReadWriteCloser {
c := &serial.Config{Name: port, Baud: baud}
s, err := serial.OpenPort(c)
if err != nil {
panic(err)
}
return s
}

func IsUrl(url string) bool {
rp := regexp.MustCompile("([^A-Za-z0-9]+).([^A-Za-z0-9]+).([^A-Za-z0-9]+)")
return rp.MatchString(url)
}

func Call(thing interface{}, method string, params ...interface{}) []reflect.Value {
in := make([]reflect.Value, len(params))
for k, param := range params {
Expand Down

0 comments on commit 60abbf8

Please sign in to comment.