示例#1
0
// NewDirectPinDriver return a new DirectPinDriver given a Connection and pin.
//
// Adds the following API Commands:
// 	"DigitalRead" - See DirectPinDriver.DigitalRead
// 	"DigitalWrite" - See DirectPinDriver.DigitalWrite
// 	"AnalogWrite" - See DirectPinDriver.AnalogWrite
// 	"PwmWrite" - See DirectPinDriver.PwmWrite
// 	"ServoWrite" - See DirectPinDriver.ServoWrite
func NewDirectPinDriver(a gobot.Connection, pin string) *DirectPinDriver {
	d := &DirectPinDriver{
		name:       "DirectPin",
		connection: a,
		pin:        pin,
		Commander:  gobot.NewCommander(),
	}

	d.AddCommand("DigitalRead", func(params map[string]interface{}) interface{} {
		val, err := d.DigitalRead()
		return map[string]interface{}{"val": val, "err": err}
	})
	d.AddCommand("DigitalWrite", func(params map[string]interface{}) interface{} {
		level, _ := strconv.Atoi(params["level"].(string))
		return d.DigitalWrite(byte(level))
	})
	d.AddCommand("PwmWrite", func(params map[string]interface{}) interface{} {
		level, _ := strconv.Atoi(params["level"].(string))
		return d.PwmWrite(byte(level))
	})
	d.AddCommand("ServoWrite", func(params map[string]interface{}) interface{} {
		level, _ := strconv.Atoi(params["level"].(string))
		return d.ServoWrite(byte(level))
	})

	return d
}
示例#2
0
// NewBlinkMDriver creates a new BlinkMDriver.
//
// Adds the following API commands:
//	Rgb - sets RGB color
//	Fade - fades the RGB color
//	FirmwareVersion - returns the version of the current Frimware
//	Color - returns the color of the LED.
func NewBlinkMDriver(a I2c) *BlinkMDriver {
	b := &BlinkMDriver{
		name:       "BlinkM",
		connection: a,
		Commander:  gobot.NewCommander(),
	}

	b.AddCommand("Rgb", func(params map[string]interface{}) interface{} {
		red := byte(params["red"].(float64))
		green := byte(params["green"].(float64))
		blue := byte(params["blue"].(float64))
		return b.Rgb(red, green, blue)
	})
	b.AddCommand("Fade", func(params map[string]interface{}) interface{} {
		red := byte(params["red"].(float64))
		green := byte(params["green"].(float64))
		blue := byte(params["blue"].(float64))
		return b.Fade(red, green, blue)
	})
	b.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
		version, err := b.FirmwareVersion()
		return map[string]interface{}{"version": version, "err": err}
	})
	b.AddCommand("Color", func(params map[string]interface{}) interface{} {
		color, err := b.Color()
		return map[string]interface{}{"color": color, "err": err}
	})

	return b
}
示例#3
0
// NewMCP23017Driver creates a new driver with specified i2c interface.
func NewMCP23017Driver(a I2c, conf MCP23017Config, deviceAddress int, v ...time.Duration) *MCP23017Driver {
	m := &MCP23017Driver{
		name:            "MCP23017",
		connection:      a,
		conf:            conf,
		mcp23017Address: deviceAddress,
		Commander:       gobot.NewCommander(),
		Eventer:         gobot.NewEventer(),
	}

	m.AddCommand("WriteGPIO", func(params map[string]interface{}) interface{} {
		pin := params["pin"].(uint8)
		val := params["val"].(uint8)
		port := params["port"].(string)
		err := m.WriteGPIO(pin, val, port)
		return map[string]interface{}{"err": err}
	})

	m.AddCommand("ReadGPIO", func(params map[string]interface{}) interface{} {
		pin := params["pin"].(uint8)
		port := params["port"].(string)
		val, err := m.ReadGPIO(pin, port)
		return map[string]interface{}{"val": val, "err": err}
	})

	return m
}
示例#4
0
// NewServoDriver returns a new ServoDriver given a ServoWriter and pin.
//
// Adds the following API Commands:
// 	"Move" - See ServoDriver.Move
//	"Min" - See ServoDriver.Min
//	"Center" - See ServoDriver.Center
//	"Max" - See ServoDriver.Max
func NewServoDriver(a ServoWriter, pin string) *ServoDriver {
	s := &ServoDriver{
		name:         "Servo",
		connection:   a,
		pin:          pin,
		Commander:    gobot.NewCommander(),
		CurrentAngle: 0,
	}

	s.AddCommand("Move", func(params map[string]interface{}) interface{} {
		angle := byte(params["angle"].(float64))
		return s.Move(angle)
	})
	s.AddCommand("Min", func(params map[string]interface{}) interface{} {
		return s.Min()
	})
	s.AddCommand("Center", func(params map[string]interface{}) interface{} {
		return s.Center()
	})
	s.AddCommand("Max", func(params map[string]interface{}) interface{} {
		return s.Max()
	})

	return s

}
示例#5
0
// NewAdafruitMotorHatDriver initializes the internal DCMotor and StepperMotor types.
// Again the Adafruit Motor Hat supports up to four DC motors and up to two stepper motors.
func NewAdafruitMotorHatDriver(a I2c) *AdafruitMotorHatDriver {
	var dc []adaFruitDCMotor
	var st []adaFruitStepperMotor
	for i := 0; i < 4; i++ {
		switch {
		case i == 0:
			dc = append(dc, adaFruitDCMotor{pwmPin: 8, in1Pin: 10, in2Pin: 9})
			st = append(st, adaFruitStepperMotor{pwmPinA: 8, pwmPinB: 13,
				ain1: 10, ain2: 9, bin1: 11, bin2: 12, revSteps: 200, secPerStep: 0.1})
		case i == 1:
			dc = append(dc, adaFruitDCMotor{pwmPin: 13, in1Pin: 11, in2Pin: 12})
			st = append(st, adaFruitStepperMotor{pwmPinA: 2, pwmPinB: 7,
				ain1: 4, ain2: 3, bin1: 5, bin2: 6, revSteps: 200, secPerStep: 0.1})
		case i == 2:
			dc = append(dc, adaFruitDCMotor{pwmPin: 2, in1Pin: 4, in2Pin: 3})
		case i == 3:
			dc = append(dc, adaFruitDCMotor{pwmPin: 7, in1Pin: 5, in2Pin: 6})
		}
	}
	driver := &AdafruitMotorHatDriver{
		name:          "AdafruitMotorHat",
		connection:    a,
		Commander:     gobot.NewCommander(),
		dcMotors:      dc,
		stepperMotors: st,
	}
	// TODO: add API funcs?
	return driver
}
示例#6
0
// NewLedDriver return a new LedDriver given a DigitalWriter and pin.
//
// Adds the following API Commands:
//	"Brightness" - See LedDriver.Brightness
//	"Toggle" - See LedDriver.Toggle
//	"On" - See LedDriver.On
//	"Off" - See LedDriver.Off
func NewLedDriver(a DigitalWriter, pin string) *LedDriver {
	l := &LedDriver{
		name:       "LED",
		pin:        pin,
		connection: a,
		high:       false,
		Commander:  gobot.NewCommander(),
	}

	l.AddCommand("Brightness", func(params map[string]interface{}) interface{} {
		level := byte(params["level"].(float64))
		return l.Brightness(level)
	})

	l.AddCommand("Toggle", func(params map[string]interface{}) interface{} {
		return l.Toggle()
	})

	l.AddCommand("On", func(params map[string]interface{}) interface{} {
		return l.On()
	})

	l.AddCommand("Off", func(params map[string]interface{}) interface{} {
		return l.Off()
	})

	return l
}
示例#7
0
// NewRgbLedDriver return a new RgbLedDriver given a DigitalWriter and
// 3 pins: redPin, greenPin, and bluePin
//
// Adds the following API Commands:
//	"SetRGB" - See RgbLedDriver.SetRGB
//	"Toggle" - See RgbLedDriver.Toggle
//	"On" - See RgbLedDriver.On
//	"Off" - See RgbLedDriver.Off
func NewRgbLedDriver(a DigitalWriter, redPin string, greenPin string, bluePin string) *RgbLedDriver {
	l := &RgbLedDriver{
		name:       "RGBLED",
		pinRed:     redPin,
		pinGreen:   greenPin,
		pinBlue:    bluePin,
		connection: a,
		high:       false,
		Commander:  gobot.NewCommander(),
	}

	l.AddCommand("SetRGB", func(params map[string]interface{}) interface{} {
		r := byte(params["r"].(int))
		g := byte(params["g"].(int))
		b := byte(params["b"].(int))
		return l.SetRGB(r, g, b)
	})

	l.AddCommand("Toggle", func(params map[string]interface{}) interface{} {
		return l.Toggle()
	})

	l.AddCommand("On", func(params map[string]interface{}) interface{} {
		return l.On()
	})

	l.AddCommand("Off", func(params map[string]interface{}) interface{} {
		return l.Off()
	})

	return l
}
示例#8
0
// NewDriver creates a new pebble driver
// Adds following events:
//		button - Sent when a pebble button is pressed
//		accel - Pebble watch acceleromenter data
//		tab - When a pebble watch tap event is detected
// And the following API commands:
//		"publish_event"
//		"send_notification"
//		"pending_message"
func NewDriver(adaptor *Adaptor) *Driver {
	p := &Driver{
		name:       "Pebble",
		connection: adaptor,
		Messages:   []string{},
		Eventer:    gobot.NewEventer(),
		Commander:  gobot.NewCommander(),
	}

	p.AddEvent("button")
	p.AddEvent("accel")
	p.AddEvent("tap")

	p.AddCommand("publish_event", func(params map[string]interface{}) interface{} {
		p.PublishEvent(params["name"].(string), params["data"].(string))
		return nil
	})

	p.AddCommand("send_notification", func(params map[string]interface{}) interface{} {
		p.SendNotification(params["message"].(string))
		return nil
	})

	p.AddCommand("pending_message", func(params map[string]interface{}) interface{} {
		return p.PendingMessage()
	})

	return p
}
示例#9
0
// NewAnalogSensorDriver returns a new AnalogSensorDriver with a polling interval of
// 10 Milliseconds given an AnalogReader and pin.
//
// Optionally accepts:
// 	time.Duration: Interval at which the AnalogSensor is polled for new information
//
// Adds the following API Commands:
// 	"Read" - See AnalogSensor.Read
func NewAnalogSensorDriver(a AnalogReader, pin string, v ...time.Duration) *AnalogSensorDriver {
	d := &AnalogSensorDriver{
		name:       "AnalogSensor",
		connection: a,
		pin:        pin,
		Eventer:    gobot.NewEventer(),
		Commander:  gobot.NewCommander(),
		interval:   10 * time.Millisecond,
		halt:       make(chan bool),
	}

	if len(v) > 0 {
		d.interval = v[0]
	}

	d.AddEvent(Data)
	d.AddEvent(Error)

	d.AddCommand("Read", func(params map[string]interface{}) interface{} {
		val, err := d.Read()
		return map[string]interface{}{"val": val, "err": err}
	})

	return d
}
示例#10
0
// NewDriver returns a new audio Driver. It accepts:
//
// *Adaptor: The audio adaptor to use for the driver
//  string: The filename of the audio to start playing
//
func NewDriver(a *Adaptor, filename string) *Driver {
	return &Driver{
		name:       "Audio",
		connection: a,
		interval:   500 * time.Millisecond,
		filename:   filename,
		halt:       make(chan bool, 0),
		Eventer:    gobot.NewEventer(),
		Commander:  gobot.NewCommander(),
	}
}
示例#11
0
func NewPingDriver(adaptor *loopbackAdaptor, pin string) *pingDriver {
	t := &pingDriver{
		name:       "Ping",
		connection: adaptor,
		pin:        pin,
		Eventer:    gobot.NewEventer(),
		Commander:  gobot.NewCommander(),
	}

	t.AddEvent("ping")

	t.AddCommand("ping", func(params map[string]interface{}) interface{} {
		return t.Ping()
	})

	return t
}
示例#12
0
// NewRelayDriver return a new RelayDriver given a DigitalWriter and pin.
//
// Adds the following API Commands:
//	"Toggle" - See RelayDriver.Toggle
//	"On" - See RelayDriver.On
//	"Off" - See RelayDriver.Off
func NewRelayDriver(a DigitalWriter, pin string) *RelayDriver {
	l := &RelayDriver{
		name:       "Relay",
		pin:        pin,
		connection: a,
		high:       false,
		Commander:  gobot.NewCommander(),
	}

	l.AddCommand("Toggle", func(params map[string]interface{}) interface{} {
		return l.Toggle()
	})

	l.AddCommand("On", func(params map[string]interface{}) interface{} {
		return l.On()
	})

	l.AddCommand("Off", func(params map[string]interface{}) interface{} {
		return l.Off()
	})

	return l
}
示例#13
0
func newTestDriver(adaptor *testAdaptor, name string, pin string) *testDriver {
	t := &testDriver{
		name:       name,
		connection: adaptor,
		pin:        pin,
		Eventer:    gobot.NewEventer(),
		Commander:  gobot.NewCommander(),
	}

	t.AddEvent("TestEvent")

	t.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {
		name := params["name"].(string)
		return fmt.Sprintf("hello %v", name)
	})

	t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} {
		name := params["name"].(string)
		return fmt.Sprintf("hello %v", name)
	})

	return t
}
示例#14
0
// NewSpheroDriver returns a new SpheroDriver given a Sphero Adaptor.
//
// Adds the following API Commands:
// 	"ConfigureLocator" - See SpheroDriver.ConfigureLocator
// 	"Roll" - See SpheroDriver.Roll
// 	"Stop" - See SpheroDriver.Stop
// 	"GetRGB" - See SpheroDriver.GetRGB
//	"ReadLocator" - See SpheroDriver.ReadLocator
// 	"SetBackLED" - See SpheroDriver.SetBackLED
// 	"SetHeading" - See SpheroDriver.SetHeading
// 	"SetStabilization" - See SpheroDriver.SetStabilization
//  "SetDataStreaming" - See SpheroDriver.SetDataStreaming
//  "SetRotationRate" - See SpheroDriver.SetRotationRate
func NewSpheroDriver(a *Adaptor) *SpheroDriver {
	s := &SpheroDriver{
		name:            "Sphero",
		connection:      a,
		Eventer:         gobot.NewEventer(),
		Commander:       gobot.NewCommander(),
		packetChannel:   make(chan *packet, 1024),
		responseChannel: make(chan []uint8, 1024),
	}

	s.AddEvent(Error)
	s.AddEvent(Collision)
	s.AddEvent(SensorData)

	s.AddCommand("SetRGB", func(params map[string]interface{}) interface{} {
		r := uint8(params["r"].(float64))
		g := uint8(params["g"].(float64))
		b := uint8(params["b"].(float64))
		s.SetRGB(r, g, b)
		return nil
	})

	s.AddCommand("Roll", func(params map[string]interface{}) interface{} {
		speed := uint8(params["speed"].(float64))
		heading := uint16(params["heading"].(float64))
		s.Roll(speed, heading)
		return nil
	})

	s.AddCommand("Stop", func(params map[string]interface{}) interface{} {
		s.Stop()
		return nil
	})

	s.AddCommand("GetRGB", func(params map[string]interface{}) interface{} {
		return s.GetRGB()
	})

	s.AddCommand("ReadLocator", func(params map[string]interface{}) interface{} {
		return s.ReadLocator()
	})

	s.AddCommand("SetBackLED", func(params map[string]interface{}) interface{} {
		level := uint8(params["level"].(float64))
		s.SetBackLED(level)
		return nil
	})

	s.AddCommand("SetRotationRate", func(params map[string]interface{}) interface{} {
		level := uint8(params["level"].(float64))
		s.SetRotationRate(level)
		return nil
	})

	s.AddCommand("SetHeading", func(params map[string]interface{}) interface{} {
		heading := uint16(params["heading"].(float64))
		s.SetHeading(heading)
		return nil
	})

	s.AddCommand("SetStabilization", func(params map[string]interface{}) interface{} {
		on := params["enable"].(bool)
		s.SetStabilization(on)
		return nil
	})

	s.AddCommand("SetDataStreaming", func(params map[string]interface{}) interface{} {
		N := uint16(params["N"].(float64))
		M := uint16(params["M"].(float64))
		Mask := uint32(params["Mask"].(float64))
		Pcnt := uint8(params["Pcnt"].(float64))
		Mask2 := uint32(params["Mask2"].(float64))

		s.SetDataStreaming(DataStreamingConfig{N: N, M: M, Mask2: Mask2, Pcnt: Pcnt, Mask: Mask})
		return nil
	})

	s.AddCommand("ConfigureLocator", func(params map[string]interface{}) interface{} {
		Flags := uint8(params["Flags"].(float64))
		X := int16(params["X"].(float64))
		Y := int16(params["Y"].(float64))
		YawTare := int16(params["YawTare"].(float64))

		s.ConfigureLocator(LocatorConfig{Flags: Flags, X: X, Y: Y, YawTare: YawTare})
		return nil
	})

	return s
}