// NewAnalogSensorDriver returns a new AnalogSensorDriver with a polling interval of // 10 Milliseconds given an AnalogReader, name and pin. // // Optinally 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, name string, pin string, v ...time.Duration) *AnalogSensorDriver { d := &AnalogSensorDriver{ name: name, 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 }
// NewWiichuckDriver creates a WiichuckDriver with specified i2c interface and name. // // It adds the following events: // "z"- Gets triggered every interval amount of time if the z button is pressed // "c" - Gets triggered every interval amount of time if the c button is pressed // "joystick" - Gets triggered every "interval" amount of time if a joystick event occurred, you can access values x, y // "error" - Gets triggered whenever the WiichuckDriver encounters an error func NewWiichuckDriver(a I2c, name string, v ...time.Duration) *WiichuckDriver { w := &WiichuckDriver{ name: name, connection: a, interval: 10 * time.Millisecond, pauseTime: 1 * time.Millisecond, Eventer: gobot.NewEventer(), joystick: map[string]float64{ "sy_origin": -1, "sx_origin": -1, }, data: map[string]float64{ "sx": 0, "sy": 0, "z": 0, "c": 0, }, } if len(v) > 0 { w.interval = v[0] } w.AddEvent(Z) w.AddEvent(C) w.AddEvent(Joystick) w.AddEvent(Error) return w }
// NewCameraDriver creates a new driver with specified name and source. // It also creates a start function to either set camera as a File or Camera capture. func NewCameraDriver(name string, source interface{}, v ...time.Duration) *CameraDriver { c := &CameraDriver{ name: name, Eventer: gobot.NewEventer(), Source: source, interval: 10 * time.Millisecond, start: func(c *CameraDriver) (err error) { switch v := c.Source.(type) { case string: c.camera = cv.NewFileCapture(v) case int: c.camera = cv.NewCameraCapture(v) default: return errors.New("Unknown camera source") } return }, } if len(v) > 0 { c.interval = v[0] } c.AddEvent(Frame) return c }
// NewPebbleDriver creates a new pebble driver with specified name // 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 NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver { p := &PebbleDriver{ name: name, 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 }
// New returns a new Client func New() *Client { c := &Client{ ProtocolVersion: "", FirmwareName: "", connection: nil, pins: []Pin{}, analogPins: []int{}, connected: false, Eventer: gobot.NewEventer(), } for _, s := range []string{ "FirmwareQuery", "CapabilityQuery", "AnalogMappingQuery", "ProtocolVersion", "I2cReply", "StringData", "Error", } { c.AddEvent(s) } return c }
// NewMCP23017Driver creates a new driver with specified name and i2c interface. func NewMCP23017Driver(a I2c, name string, conf MCP23017Config, deviceAddress int, v ...time.Duration) *MCP23017Driver { m := &MCP23017Driver{ name: name, connection: a, conf: conf, mcp23017Address: deviceAddress, Commander: gobot.NewCommander(), Eventer: gobot.NewEventer(), } m.AddCommand("WriteGPIO", func(params map[string]interface{}) interface{} { pin := params["pin"].(float64) val := params["val"].(float64) port := params["port"].(string) return m.WriteGPIO(pin, val, port) }) m.AddCommand("ReadGPIO", func(params map[string]interface{}) interface{} { pin := params["pin"].(float64) port := params["port"].(string) val, err := m.ReadGPIO(pin, port) return map[string]interface{}{"val": val, "err": err} }) return m }
func NewJenkinsconnectDriver(a *JenkinsconnectAdaptor, name string) *JenkinsconnectDriver { j := &JenkinsconnectDriver{ name: name, connection: a, Eventer: gobot.NewEventer(), Commander: gobot.NewCommander(), } j.AddEvent(JobResult) j.AddCommand("ParseResults", func(params map[string]interface{}) interface{} { var snapshot JobOutcomeSnapshot result := ParseJobState(params) if (JobOutcome{}) != result { fmt.Printf("Result: %v\n", result) lastOutcome, ok := jobStates[result.Name] if ok { fmt.Printf("Last outcome: %v\n", lastOutcome) } snapshot.Outcome = result snapshot.RanAt = time.Now() jobStates[result.Name] = snapshot gobot.Publish(j.Event(JobResult), result) } return result }) return j }
// NewBebopDriver creates an BebopDriver with specified name. func NewBebopDriver(connection *BebopAdaptor, name string) *BebopDriver { d := &BebopDriver{ name: name, connection: connection, Eventer: gobot.NewEventer(), } d.AddEvent("flying") return d }
// NewArdroneDriver creates an ArdroneDriver with specified name. // // It add the following events: // 'flying' - Sent when the device has taken off. func NewArdroneDriver(connection *ArdroneAdaptor, name string) *ArdroneDriver { d := &ArdroneDriver{ name: name, connection: connection, Eventer: gobot.NewEventer(), } d.AddEvent("flying") return d }
// NewBLEDeviceInformationDriver creates a BLEDeviceInformationDriver // by name func NewBLEDeviceInformationDriver(a *BLEClientAdaptor, name string) *BLEDeviceInformationDriver { n := &BLEDeviceInformationDriver{ name: name, connection: a, Eventer: gobot.NewEventer(), } return n }
// NewSparkCoreAdaptor creates new spark core adaptor with deviceId and accessToken // using api.spark.io server as default func NewSparkCoreAdaptor(name string, deviceID string, accessToken string) *SparkCoreAdaptor { return &SparkCoreAdaptor{ name: name, DeviceID: deviceID, AccessToken: accessToken, APIServer: "https://api.spark.io", Eventer: gobot.NewEventer(), } }
// NewBLEBatteryDriver creates a BLEBatteryDriver by name func NewBLEBatteryDriver(a *BLEClientAdaptor, name string) *BLEBatteryDriver { n := &BLEBatteryDriver{ name: name, connection: a, Eventer: gobot.NewEventer(), } return n }
// NewSpheroOllieDriver creates a SpheroOllieDriver by name func NewSpheroOllieDriver(a *BLEClientAdaptor, name string) *SpheroOllieDriver { n := &SpheroOllieDriver{ name: name, connection: a, Eventer: gobot.NewEventer(), packetChannel: make(chan *packet, 1024), } return n }
// NewLeapMotionDriver creates a new leap motion driver with specified name // // Adds the following events: // "message" - Gets triggered when receiving a message from leap motion func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver { l := &LeapMotionDriver{ name: name, connection: a, Eventer: gobot.NewEventer(), } l.AddEvent("message") return l }
func NewAudioDriver(a *AudioAdaptor, name string, filename string) *AudioDriver { d := &AudioDriver{ name: name, connection: a, interval: 500 * time.Millisecond, filename: filename, halt: make(chan bool, 0), Eventer: gobot.NewEventer(), Commander: gobot.NewCommander(), } return d }
func newMockFirmataBoard() *mockFirmataBoard { m := &mockFirmataBoard{ Eventer: gobot.NewEventer(), disconnectError: nil, pins: make([]client.Pin, 100), } m.pins[1].Value = 1 m.pins[15].Value = 133 m.AddEvent("I2cReply") return m }
// NewBME280Driver creates a new driver with specified name and i2c interface func NewBME280Driver(a i2c.I2c, name string, v ...time.Duration) *BME280Driver { m := &BME280Driver{ name: name, connection: a, Eventer: gobot.NewEventer(), interval: 10 * time.Millisecond, } if len(v) > 0 { m.interval = v[0] } m.AddEvent(i2c.Error) return m }
// NewMPL115A2Driver creates a new driver with specified name and i2c interface func NewMPL115A2Driver(a I2c, name string, v ...time.Duration) *MPL115A2Driver { m := &MPL115A2Driver{ name: name, connection: a, Eventer: gobot.NewEventer(), interval: 10 * time.Millisecond, } if len(v) > 0 { m.interval = v[0] } m.AddEvent(Error) return m }
func NewPingDriver(adaptor *loopbackAdaptor, name string, pin string) *pingDriver { t := &pingDriver{ name: name, 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 }
// NewNeuroskyDriver creates a NeuroskyDriver by name // and adds the following events: // // extended - user's current extended level // signal - shows signal strength // attention - user's current attention level // meditation - user's current meditation level // blink - user's current blink level // wave - shows wave data // eeg - showing eeg data func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver { n := &NeuroskyDriver{ name: name, connection: a, Eventer: gobot.NewEventer(), } n.AddEvent("extended") n.AddEvent("signal") n.AddEvent("attention") n.AddEvent("meditation") n.AddEvent("blink") n.AddEvent("wave") n.AddEvent("eeg") n.AddEvent("error") return n }
// NewMavlinkDriver creates a new mavlink driver with specified name. // // It add the following events: // "packet" - triggered when a new packet is read // "message" - triggered when a new valid message is processed func NewMavlinkDriver(a *MavlinkAdaptor, name string, v ...time.Duration) *MavlinkDriver { m := &MavlinkDriver{ name: name, connection: a, Eventer: gobot.NewEventer(), interval: 10 * time.Millisecond, } if len(v) > 0 { m.interval = v[0] } m.AddEvent("packet") m.AddEvent("message") m.AddEvent("errorIO") m.AddEvent("errorMAVLink") return m }
func NewGpsdDriver(adaptor *GpsdAdaptor, name string, t ...time.Duration) *GpsdDriver { gpsd := &GpsdDriver{ name: name, halt: make(chan bool), pause: make(chan bool), interval: time.Second, r: adaptor, w: adaptor, Eventer: gobot.NewEventer(), Commander: gobot.NewCommander(), } if len(t) > 0 { gpsd.interval = t[0] } gpsd.AddEvent(TPV) gpsd.AddEvent(ERROR) return gpsd }
// NewGroveTemperatureSensorDriver returns a new GroveTemperatureSensorDriver with a polling interval of // 10 Milliseconds given an AnalogReader, name and pin. // // Optionally accepts: // time.Duration: Interval at which the TemperatureSensor is polled for new information // // Adds the following API Commands: // "Read" - See AnalogSensor.Read func NewGroveTemperatureSensorDriver(a AnalogReader, name string, pin string, v ...time.Duration) *GroveTemperatureSensorDriver { d := &GroveTemperatureSensorDriver{ name: name, connection: a, pin: pin, Eventer: gobot.NewEventer(), interval: 10 * time.Millisecond, halt: make(chan bool), } if len(v) > 0 { d.interval = v[0] } d.AddEvent(Data) d.AddEvent(Error) return d }
// NewJoystickDriver returns a new JoystickDriver with a polling interval of // 10 Milliseconds given a JoystickAdaptor, name and json button configuration // file location. // // Optinally accepts: // time.Duration: Interval at which the JoystickDriver is polled for new information func NewJoystickDriver(a *JoystickAdaptor, name string, config string, v ...time.Duration) *JoystickDriver { d := &JoystickDriver{ name: name, connection: a, Eventer: gobot.NewEventer(), configPath: config, poll: func() sdl.Event { return sdl.PollEvent() }, interval: 10 * time.Millisecond, halt: make(chan bool, 0), } if len(v) > 0 { d.interval = v[0] } d.AddEvent("error") return d }
// NewButtonDriver returns a new ButtonDriver with a polling interval of // 10 Milliseconds given a DigitalReader, name and pin. // // Optinally accepts: // time.Duration: Interval at which the ButtonDriver is polled for new information func NewButtonDriver(a DigitalReader, name string, pin string, v ...time.Duration) *ButtonDriver { b := &ButtonDriver{ name: name, connection: a, pin: pin, Active: false, Eventer: gobot.NewEventer(), interval: 10 * time.Millisecond, halt: make(chan bool), } if len(v) > 0 { b.interval = v[0] } b.AddEvent(Push) b.AddEvent(Release) b.AddEvent(Error) return b }
func NewKeyboardDriver(name string) *KeyboardDriver { k := &KeyboardDriver{ name: name, connect: func(k *KeyboardDriver) (err error) { if err := configure(); err != nil { return err } k.stdin = os.Stdin return }, listen: func(k *KeyboardDriver) { ctrlc := bytes{3} for { var keybuf bytes k.stdin.Read(keybuf[0:3]) if keybuf == ctrlc { proc, err := os.FindProcess(os.Getpid()) if err != nil { log.Fatal(err) } proc.Signal(os.Interrupt) break } gobot.Publish(k.Event("key"), Parse(keybuf)) } }, Eventer: gobot.NewEventer(), } k.AddEvent("key") return k }
// NewBLEMinidroneDriver creates a BLEMinidroneDriver by name func NewBLEMinidroneDriver(a *BLEClientAdaptor, name string) *BLEMinidroneDriver { n := &BLEMinidroneDriver{ name: name, connection: a, Pcmd: Pcmd{ Flag: 0, Roll: 0, Pitch: 0, Yaw: 0, Gaz: 0, Psi: 0, }, Eventer: gobot.NewEventer(), } n.AddEvent(Battery) n.AddEvent(Status) n.AddEvent(Flying) n.AddEvent(Landed) return n }
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 }
// NewFirmataAdaptor returns a new FirmataAdaptor with specified name and optionally accepts: // // string: port the FirmataAdaptor uses to connect to a serial port with a baude rate of 57600 // io.ReadWriteCloser: connection the FirmataAdaptor uses to communication with the hardware // // If an io.ReadWriteCloser is not supplied, the FirmataAdaptor will open a connection // to a serial port with a baude rate of 57600. If an io.ReadWriteCloser // is supplied, then the FirmataAdaptor will use the provided io.ReadWriteCloser and use the // string port as a label to be displayed in the log and api. func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor { f := &FirmataAdaptor{ name: name, port: "", conn: nil, board: client.New(), openSP: func(port string) (io.ReadWriteCloser, error) { return serial.OpenPort(&serial.Config{Name: port, Baud: 57600}) }, Eventer: gobot.NewEventer(), } for _, arg := range args { switch arg.(type) { case string: f.port = arg.(string) case io.ReadWriteCloser: f.conn = arg.(io.ReadWriteCloser) } } return f }
// NewSpheroDriver returns a new SpheroDriver given a SpheroAdaptor and name. // // 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 *SpheroAdaptor, name string) *SpheroDriver { s := &SpheroDriver{ name: name, 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 }