本文整理汇总了Golang中github.com/hybridgroup/gobot.Publish函数的典型用法代码示例。如果您正苦于以下问题:Golang Publish函数的具体用法?Golang Publish怎么用?Golang Publish使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Publish函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Start
// Start starts the MakeyButtonDriver and polls the state of the button at the given interval.
//
// Emits the Events:
// Push int - On button push
// Release int - On button release
// Error error - On button error
func (b *MakeyButtonDriver) Start() (errs []error) {
state := 1
go func() {
for {
newValue, err := b.connection.DigitalRead(b.Pin())
if err != nil {
gobot.Publish(b.Event(Error), err)
} else if newValue != state && newValue != -1 {
state = newValue
if newValue == 0 {
b.Active = true
gobot.Publish(b.Event(Push), newValue)
} else {
b.Active = false
gobot.Publish(b.Event(Release), newValue)
}
}
select {
case <-time.After(b.interval):
case <-b.halt:
return
}
}
}()
return
}
示例2: Start
// Start starts the GroveTemperatureSensorDriver and reads the Sensor at the given interval.
// Emits the Events:
// Data int - Event is emitted on change and represents the current temperature in celsius from the sensor.
// Error error - Event is emitted on error reading from the sensor.
func (a *GroveTemperatureSensorDriver) Start() (errs []error) {
thermistor := 3975.0
a.temperature = 0
go func() {
for {
rawValue, err := a.Read()
resistance := float64(1023.0-rawValue) * 10000 / float64(rawValue)
newValue := 1/(math.Log(resistance/10000.0)/thermistor+1/298.15) - 273.15
if err != nil {
gobot.Publish(a.Event(Error), err)
} else if newValue != a.temperature && newValue != -1 {
a.temperature = newValue
gobot.Publish(a.Event(Data), a.temperature)
}
select {
case <-time.After(a.interval):
case <-a.halt:
return
}
}
}()
return
}
示例3: Start
// TODO better pause and toogle handling
func (gpsd *GpsdDriver) Start() (errs []error) {
gpsd.w.GpsdWrite(START)
go func() {
var tpv gpsdjson.TPV
for {
if line, err := gpsd.r.GpsdRead(); err == nil {
json.Unmarshal([]byte(line), &tpv)
if tpv.Class == TPV {
gobot.Publish(gpsd.Event(TPV), tpv)
}
} else {
log.Println("Error reading on gpsd socket", err.Error())
gobot.Publish(gpsd.Event(ERROR), err)
return
}
select {
case <-time.After(gpsd.interval):
case <-gpsd.halt:
return
}
}
}()
return nil
}
示例4: handleEvent
// HandleEvent publishes an specific event according to data received
func (j *JoystickDriver) handleEvent(event sdl.Event) error {
switch data := event.(type) {
case *sdl.JoyAxisEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
axis := j.findName(data.Axis, j.config.Axis)
if axis == "" {
return fmt.Errorf("Unknown Axis: %v", data.Axis)
}
gobot.Publish(j.Event(axis), data.Value)
}
case *sdl.JoyButtonEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
button := j.findName(data.Button, j.config.Buttons)
if button == "" {
return fmt.Errorf("Unknown Button: %v", data.Button)
}
if data.State == 1 {
gobot.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
}
gobot.Publish(j.Event(fmt.Sprintf("%s_release", button)), nil)
}
case *sdl.JoyHatEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" {
return fmt.Errorf("Unknown Hat: %v %v", data.Hat, data.Value)
}
gobot.Publish(j.Event(hat), true)
}
}
return nil
}
示例5: Init
func (b *BLEMinidroneDriver) Init() (err error) {
b.GenerateAllStates()
// subscribe to battery notifications
b.adaptor().Subscribe(DroneNotificationService, BatteryCharacteristic, func(data []byte, e error) {
gobot.Publish(b.Event(Battery), data[len(data)-1])
})
// subscribe to flying status notifications
b.adaptor().Subscribe(DroneNotificationService, FlightStatusCharacteristic, func(data []byte, e error) {
if len(data) < 7 || data[2] != 2 {
fmt.Println(data)
return
}
gobot.Publish(b.Event(Status), data[6])
if (data[6] == 1 || data[6] == 2) && !b.flying {
b.flying = true
gobot.Publish(b.Event(Flying), true)
} else if (data[6] == 0) && b.flying {
b.flying = false
gobot.Publish(b.Event(Landed), true)
}
})
return
}
示例6: Start
// Start inits leap motion driver by enabling gestures
// and listening from incoming messages.
//
// Publishes the following events:
// "message" - Emits Frame on new message received from Leap.
// "hand" - Emits Hand when detected in message from Leap.
// "gesture" - Emits Gesture when detected in message from Leap.
func (l *LeapMotionDriver) Start() (errs []error) {
enableGestures := map[string]bool{"enableGestures": true}
b, err := json.Marshal(enableGestures)
if err != nil {
return []error{err}
}
_, err = l.adaptor().ws.Write(b)
if err != nil {
return []error{err}
}
go func() {
var msg []byte
var frame Frame
for {
receive(l.adaptor().ws, &msg)
frame = l.ParseFrame(msg)
gobot.Publish(l.Event("message"), frame)
for _, hand := range frame.Hands {
gobot.Publish(l.Event("hand"), hand)
}
for _, gesture := range frame.Gestures {
gobot.Publish(l.Event("gesture"), gesture)
}
}
}()
return
}
示例7: Start
// Start writes initialization bytes and reads from adaptor
// using specified interval to accelerometer andtemperature data
func (h *MPU6050Driver) Start() (errs []error) {
if err := h.initialize(); err != nil {
return []error{err}
}
go func() {
for {
if err := h.connection.I2cWrite(mpu6050Address, []byte{MPU6050_RA_ACCEL_XOUT_H}); err != nil {
gobot.Publish(h.Event(Error), err)
continue
}
ret, err := h.connection.I2cRead(mpu6050Address, 14)
if err != nil {
gobot.Publish(h.Event(Error), err)
continue
}
buf := bytes.NewBuffer(ret)
binary.Read(buf, binary.BigEndian, &h.Accelerometer)
binary.Read(buf, binary.BigEndian, &h.Gyroscope)
binary.Read(buf, binary.BigEndian, &h.Temperature)
<-time.After(h.interval)
}
}()
return
}
示例8: Start
// Start initilizes i2c and reads from adaptor
// using specified interval to update with new value
func (w *WiichuckDriver) Start() (errs []error) {
if err := w.connection.I2cStart(0x52); err != nil {
return []error{err}
}
go func() {
for {
if err := w.connection.I2cWrite([]byte{0x40, 0x00}); err != nil {
gobot.Publish(w.Event(Error), err)
continue
}
if err := w.connection.I2cWrite([]byte{0x00}); err != nil {
gobot.Publish(w.Event(Error), err)
continue
}
newValue, err := w.connection.I2cRead(6)
if err != nil {
gobot.Publish(w.Event(Error), err)
continue
}
if len(newValue) == 6 {
if err = w.update(newValue); err != nil {
gobot.Publish(w.Event(Error), err)
continue
}
}
<-time.After(w.interval)
}
}()
return
}
示例9: ExampleOn
func ExampleOn() {
e := gobot.NewEvent()
gobot.On(e, func(s interface{}) {
fmt.Println(s)
})
gobot.Publish(e, 100)
gobot.Publish(e, 200)
}
示例10: updateButtons
// updateButtons publishes "c" and "x" events if present in data
func (w *WiichuckDriver) updateButtons() {
if w.data["c"] == 0 {
gobot.Publish(w.Event(C), true)
}
if w.data["z"] == 0 {
gobot.Publish(w.Event(Z), true)
}
}
示例11: update
func (b *ButtonDriver) update(newValue int) {
if newValue == 1 {
b.Active = true
gobot.Publish(b.Event(Push), newValue)
} else {
b.Active = false
gobot.Publish(b.Event(Release), newValue)
}
}
示例12: update
func (m *MakeyButtonDriver) update(newVal int) {
if newVal == 0 {
m.Active = true
gobot.Publish(m.Events["push"], newVal)
} else {
m.Active = false
gobot.Publish(m.Events["release"], newVal)
}
}
示例13: ExampleOnce
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)
}
示例14: update
func (b *ButtonDriver) update(newVal int) {
if newVal == 1 {
b.Active = true
gobot.Publish(b.Event("push"), newVal)
} else {
b.Active = false
gobot.Publish(b.Event("release"), newVal)
}
}
示例15: Start
// Start inits leap motion driver by enabling gestures
// and listening from incoming messages.
//
// Publishes the following events:
// "message" - Emits Frame on new message received from Leap.
func (l *LeapMotionDriver) Start() (errs []error) {
enableGestures := map[string]bool{
"enableGestures": true,
"background": true,
"optimizeHMD": true,
}
b, err := json.Marshal(enableGestures)
if err != nil {
return []error{err}
}
_, err = l.adaptor().ws.Write(b)
if err != nil {
return []error{err}
}
go func() {
var msg []byte
for {
receive(l.adaptor().ws, &msg)
gobot.Publish(l.Event("message"), l.ParseFrame(msg))
}
}()
return
}