Ejemplo n.º 1
0
func NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver {
	p := &PebbleDriver{
		Driver: gobot.Driver{
			Name: name,
			Events: map[string]*gobot.Event{
				"button": gobot.NewEvent(),
				"accel":  gobot.NewEvent(),
				"tap":    gobot.NewEvent(),
			},
			Commands: make(map[string]func(map[string]interface{}) interface{}),
			Adaptor:  adaptor,
		},
		Messages: []string{},
	}

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

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

	p.Driver.AddCommand("PendingMessage", func(params map[string]interface{}) interface{} {
		m := make(map[string]string)
		m["result"] = p.PendingMessage()
		return m
	})

	return p
}
Ejemplo n.º 2
0
func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *JoystickDriver {
	d := &JoystickDriver{
		Driver: gobot.Driver{
			Events:  make(map[string]*gobot.Event),
			Adaptor: a,
		},
	}

	file, e := ioutil.ReadFile(config)
	if e != nil {
		panic(fmt.Sprintf("File error: %v\n", e))
	}
	var jsontype joystickConfig
	json.Unmarshal(file, &jsontype)
	d.config = jsontype
	for _, value := range d.config.Buttons {
		d.Events[fmt.Sprintf("%s_press", value.Name)] = gobot.NewEvent()
		d.Events[fmt.Sprintf("%s_release", value.Name)] = gobot.NewEvent()
	}
	for _, value := range d.config.Axis {
		d.Events[value.Name] = gobot.NewEvent()
	}
	for _, value := range d.config.Hats {
		d.Events[value.Name] = gobot.NewEvent()
	}
	return d
}
Ejemplo n.º 3
0
func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButtonDriver {
	return &MakeyButtonDriver{
		Driver: gobot.Driver{
			Name: name,
			Pin:  pin,
			Events: map[string]*gobot.Event{
				"push":    gobot.NewEvent(),
				"release": gobot.NewEvent(),
			},
		},
		Active:  false,
		Adaptor: a,
	}
}
Ejemplo n.º 4
0
func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
	return &ButtonDriver{
		Driver: gobot.Driver{
			Name: name,
			Pin:  pin,
			Events: map[string]*gobot.Event{
				"push":    gobot.NewEvent(),
				"release": gobot.NewEvent(),
			},
			Adaptor: a.(gobot.AdaptorInterface),
		},
		Active: false,
	}
}
Ejemplo n.º 5
0
// newBoard creates a new board connected in specified serial port.
// Adds following events: "firmware_query", "capability_query",
// "analog_mapping_query", "report_version", "i2c_reply",
// "string_data", "firmware_query"
func newBoard(sp io.ReadWriteCloser) *board {
	board := &board{
		majorVersion:     0,
		minorVersion:     0,
		serial:           sp,
		firmwareName:     "",
		pins:             []pin{},
		analogPins:       []byte{},
		connected:        false,
		events:           make(map[string]*gobot.Event),
		initTimeInterval: defaultInitTimeInterval,
	}

	for _, s := range []string{
		"firmware_query",
		"capability_query",
		"analog_mapping_query",
		"report_version",
		"i2c_reply",
		"string_data",
		"firmware_query",
	} {
		board.events[s] = gobot.NewEvent()
	}

	return board
}
Ejemplo n.º 6
0
func ExampleOn() {
	e := gobot.NewEvent()
	gobot.On(e, func(s interface{}) {
		fmt.Println(s)
	})
	gobot.Publish(e, 100)
	gobot.Publish(e, 200)
}
Ejemplo n.º 7
0
func ExampleOnce() {
	e := gobot.NewEvent()
	gobot.Once(e, func(s interface{}) {
		fmt.Println(s)
		fmt.Println("I will no longer respond to events")
	})
	gobot.Publish(e, 100)
	gobot.Publish(e, 200)
}
Ejemplo n.º 8
0
func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
	s := &SpheroDriver{
		Driver: gobot.Driver{
			Name: name,
			Events: map[string]*gobot.Event{
				"Collision": gobot.NewEvent(),
			},
			Commands: make(map[string]func(map[string]interface{}) interface{}),
			Adaptor:  a,
		},
		packetChannel:   make(chan *packet, 1024),
		responseChannel: make(chan []uint8, 1024),
	}

	s.Driver.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.Driver.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.Driver.AddCommand("Stop", func(params map[string]interface{}) interface{} {
		s.Stop()
		return nil
	})

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

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

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

	return s
}
Ejemplo n.º 9
0
func NewArdroneDriver(adaptor *ArdroneAdaptor, name string) *ArdroneDriver {
	return &ArdroneDriver{
		Driver: gobot.Driver{
			Name: name,
			Events: map[string]*gobot.Event{
				"Flying": gobot.NewEvent(),
			},
			Adaptor: adaptor,
		},
	}
}
Ejemplo n.º 10
0
func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
	return &LeapMotionDriver{
		Driver: gobot.Driver{
			Name: name,
			Events: map[string]*gobot.Event{
				"Message": gobot.NewEvent(),
			},
			Adaptor: a,
		},
	}
}
Ejemplo n.º 11
0
func NewCameraDriver(name string, source interface{}) *CameraDriver {
	return &CameraDriver{
		Driver: gobot.Driver{
			Name: name,
			Events: map[string]*gobot.Event{
				"Frame": gobot.NewEvent(),
			},
		},
		Source: source,
	}
}
Ejemplo n.º 12
0
func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
	return &WiichuckDriver{
		Driver: gobot.Driver{
			Name: name,
			Events: map[string]*gobot.Event{
				"z_button": gobot.NewEvent(),
				"c_button": gobot.NewEvent(),
				"joystick": gobot.NewEvent(),
			},
			Adaptor: a.(gobot.AdaptorInterface),
		},
		joystick: map[string]float64{
			"sy_origin": -1,
			"sx_origin": -1,
		},
		data: map[string]float64{
			"sx": 0,
			"sy": 0,
			"z":  0,
			"c":  0,
		},
	}
}
Ejemplo n.º 13
0
func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {
	return &NeuroskyDriver{
		Driver: gobot.Driver{
			Name: name,
			Events: map[string]*gobot.Event{
				"Extended":   gobot.NewEvent(),
				"Signal":     gobot.NewEvent(),
				"Attention":  gobot.NewEvent(),
				"Meditation": gobot.NewEvent(),
				"Blink":      gobot.NewEvent(),
				"Wave":       gobot.NewEvent(),
				"EEG":        gobot.NewEvent(),
			},
			Adaptor: a,
		},
	}
}
Ejemplo n.º 14
0
// EventStream returns a gobot.Event based on the following params:
//
// * source - "all"/"devices"/"device" (More info at: http://docs.spark.io/api/#reading-data-from-a-core-events)
// * name  - Event name to subscribe for, leave blank to subscribe to all events.
//
// A new event is emitted as a spark.Event struct
func (s *SparkCoreAdaptor) EventStream(source string, name string) (event *gobot.Event, err error) {
	var url string

	switch source {
	case "all":
		url = fmt.Sprintf("%s/v1/events/%s?access_token=%s", s.APIServer, name, s.AccessToken)
	case "devices":
		url = fmt.Sprintf("%s/v1/devices/events/%s?access_token=%s", s.APIServer, name, s.AccessToken)
	case "device":
		url = fmt.Sprintf("%s/events/%s?access_token=%s", s.deviceURL(), name, s.AccessToken)
	default:
		err = errors.New("source param should be: all, devices or device")
		return
	}

	events, errors, err := eventSource(url)
	if err != nil {
		return
	}

	event = gobot.NewEvent()

	go func() {
		for {
			select {
			case ev := <-events:
				if ev.Event() != "" && ev.Data() != "" {
					gobot.Publish(event, Event{Name: ev.Event(), Data: ev.Data()})
				}
			case ev := <-errors:
				gobot.Publish(event, Event{Error: ev})
			}
		}
	}()
	return
}
Ejemplo n.º 15
0
func ExamplePublish() {
	e := gobot.NewEvent()
	gobot.Publish(e, 100)
}
Ejemplo n.º 16
0
// process uses incoming data and executes actions depending on what is received.
// The following messages are processed: reportVersion, AnalogMessageRangeStart,
// digitalMessageRangeStart.
// And the following responses: capability, analog mapping, pin state,
// i2c, firmwareQuery, string data.
// If neither of those messages is received, then data is treated as "bad_byte"
func (b *board) process(data []byte) (err error) {
	buf := bytes.NewBuffer(data)
	for {
		messageType, err := buf.ReadByte()
		if err != nil {
			// we ran out of bytes so we break out of the process loop
			break
		}
		switch {
		case reportVersion == messageType:
			if b.majorVersion, err = buf.ReadByte(); err != nil {
				return err
			}
			if b.minorVersion, err = buf.ReadByte(); err != nil {
				return err
			}
			gobot.Publish(b.events["report_version"], b.version())
		case analogMessageRangeStart <= messageType &&
			analogMessageRangeEnd >= messageType:

			leastSignificantByte, err := buf.ReadByte()
			if err != nil {
				return err
			}
			mostSignificantByte, err := buf.ReadByte()
			if err != nil {
				return err
			}

			value := uint(leastSignificantByte) | uint(mostSignificantByte)<<7
			pin := (messageType & 0x0F)

			b.pins[b.analogPins[pin]].value = int(value)
			gobot.Publish(b.events[fmt.Sprintf("analog_read_%v", pin)],
				[]byte{
					byte(value >> 24),
					byte(value >> 16),
					byte(value >> 8),
					byte(value & 0xff),
				},
			)
		case digitalMessageRangeStart <= messageType &&
			digitalMessageRangeEnd >= messageType:

			port := messageType & 0x0F
			firstBitmask, err := buf.ReadByte()
			if err != nil {
				return err
			}
			secondBitmask, err := buf.ReadByte()
			if err != nil {
				return err
			}
			portValue := firstBitmask | (secondBitmask << 7)

			for i := 0; i < 8; i++ {
				pinNumber := (8*byte(port) + byte(i))
				pin := b.pins[pinNumber]
				if byte(pin.mode) == input {
					pin.value = int((portValue >> (byte(i) & 0x07)) & 0x01)
					gobot.Publish(b.events[fmt.Sprintf("digital_read_%v", pinNumber)],
						[]byte{byte(pin.value & 0xff)})
				}
			}
		case startSysex == messageType:
			currentBuffer := []byte{messageType}
			for {
				b, err := buf.ReadByte()
				if err != nil {
					// we ran out of bytes before we reached the endSysex so we break
					break
				}
				currentBuffer = append(currentBuffer, b)
				if currentBuffer[len(currentBuffer)-1] == endSysex {
					break
				}
			}
			command := currentBuffer[1]
			switch command {
			case capabilityResponse:
				supportedModes := 0
				n := 0

				for _, val := range currentBuffer[2:(len(currentBuffer) - 5)] {
					if val == 127 {
						modes := []byte{}
						for _, mode := range []byte{input, output, analog, pwm, servo} {
							if (supportedModes & (1 << mode)) != 0 {
								modes = append(modes, mode)
							}
						}
						b.pins = append(b.pins, pin{modes, output, 0, 0})
						b.events[fmt.Sprintf("digital_read_%v", len(b.pins)-1)] = gobot.NewEvent()
						b.events[fmt.Sprintf("pin_%v_state", len(b.pins)-1)] = gobot.NewEvent()
						supportedModes = 0
						n = 0
						continue
					}

					if n == 0 {
						supportedModes = supportedModes | (1 << val)
					}
					n ^= 1
				}
				gobot.Publish(b.events["capability_query"], nil)
			case analogMappingResponse:
				pinIndex := byte(0)

				for _, val := range currentBuffer[2 : len(b.pins)-1] {

					b.pins[pinIndex].analogChannel = val

					if val != 127 {
						b.analogPins = append(b.analogPins, pinIndex)
					}
					b.events[fmt.Sprintf("analog_read_%v", pinIndex)] = gobot.NewEvent()
					pinIndex++
				}

				gobot.Publish(b.events["analog_mapping_query"], nil)
			case pinStateResponse:
				pin := b.pins[currentBuffer[2]]
				pin.mode = currentBuffer[3]
				pin.value = int(currentBuffer[4])

				if len(currentBuffer) > 6 {
					pin.value = int(uint(pin.value) | uint(currentBuffer[5])<<7)
				}
				if len(currentBuffer) > 7 {
					pin.value = int(uint(pin.value) | uint(currentBuffer[6])<<14)
				}

				gobot.Publish(b.events[fmt.Sprintf("pin_%v_state", currentBuffer[2])],
					map[string]int{
						"pin":   int(currentBuffer[2]),
						"mode":  int(pin.mode),
						"value": int(pin.value),
					},
				)
			case i2CReply:
				i2cReply := map[string][]byte{
					"slave_address": []byte{byte(currentBuffer[2]) | byte(currentBuffer[3])<<7},
					"register":      []byte{byte(currentBuffer[4]) | byte(currentBuffer[5])<<7},
					"data":          []byte{byte(currentBuffer[6]) | byte(currentBuffer[7])<<7},
				}
				for i := 8; i < len(currentBuffer); i = i + 2 {
					if currentBuffer[i] == byte(0xF7) {
						break
					}
					if i+2 > len(currentBuffer) {
						break
					}
					i2cReply["data"] = append(i2cReply["data"],
						byte(currentBuffer[i])|byte(currentBuffer[i+1])<<7,
					)
				}
				gobot.Publish(b.events["i2c_reply"], i2cReply)
			case firmwareQuery:
				name := []byte{}
				for _, val := range currentBuffer[4:(len(currentBuffer) - 1)] {
					if val != 0 {
						name = append(name, val)
					}
				}
				b.firmwareName = string(name[:])
				gobot.Publish(b.events["firmware_query"], b.firmwareName)
			case stringData:
				str := currentBuffer[2:len(currentBuffer)]
				gobot.Publish(b.events["string_data"], string(str[:len(str)]))
			default:
				return fmt.Errorf("bad byte: 0x%x", command)
			}
		}
	}
	return
}