示例#1
0
func main() {
	gbot := gobot.NewGobot()

	e := edison.NewEdisonAdaptor("edison")
	m := mqtt.NewMqttAdaptor("mqtt", "tcp://192.168.0.90:1883", "pump")

	lever := gpio.NewButtonDriver(e, "lever", "2")
	fault := gpio.NewButtonDriver(e, "fault", "4")
	pump := gpio.NewDirectPinDriver(e, "pump", "13")

	work := func() {
		dgram := url.Values{
			"name":         {"Four"},
			"dispenser_id": {"4"},
			"drink_id":     {"0"},
			"event":        {"online"},
			"details":      {"dispenser"},
		}
		pumping := false
		served := byte(0)

		m.On("startPump", func(data []byte) {
			if !pumping {
				pumping = true
				pump.DigitalWrite(1)
				served++
				dgram.Set("event", "online")
				dgram.Set("drink_id", fmt.Sprintf("%v", served))
				m.Publish("pumped", []byte(dgram.Encode()))
				gobot.After(2*time.Second, func() {
					pump.DigitalWrite(0)
					pumping = false
				})
			}
		})

		gobot.On(lever.Event("push"), func(data interface{}) {
			m.Publish("pump", []byte{})
		})

		m.On("startFault", func(data []byte) {
			dgram.Set("event", "error")
			m.Publish("fault", []byte(dgram.Encode()))
		})

		gobot.On(fault.Event("push"), func(data interface{}) {
			m.Publish("startFault", []byte{})
		})
	}

	gbot.AddRobot(gobot.NewRobot("brewmachine",
		[]gobot.Connection{e, m},
		[]gobot.Device{lever, fault, pump},
		work,
	))

	gbot.Start()
}
示例#2
0
func main() {
	gbot := gobot.NewGobot()

	a := api.NewAPI(gbot)
	a.Start()

	board := firmata.NewFirmataAdaptor("arduino", os.Args[1])

	// digital devices
	button = gpio.NewButtonDriver(board, "button", "2")
	blue = gpio.NewLedDriver(board, "blue", "3")
	green = gpio.NewLedDriver(board, "green", "4")
	buzzer = gpio.NewGroveBuzzerDriver(board, "buzzer", "7")
	touch = gpio.NewButtonDriver(board, "touch", "8")

	// analog devices
	light = gpio.NewAnalogSensorDriver(board, "light", "0")

	work := func() {
		Reset()

		gobot.On(button.Event(gpio.Push), func(data interface{}) {
			TurnOff()
			fmt.Println("On!")
			blue.On()
		})

		gobot.On(button.Event(gpio.Release), func(data interface{}) {
			Reset()
		})

		gobot.On(touch.Event(gpio.Push), func(data interface{}) {
			Doorbell()
		})

		gobot.On(light.Event("data"), func(data interface{}) {
			fmt.Println("light", data)
		})
	}

	robot := gobot.NewRobot("airlock",
		[]gobot.Connection{board},
		[]gobot.Device{button, blue, green, buzzer, touch, light},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
示例#3
0
func main() {
	gbot := gobot.NewGobot()

	e := edison.NewEdisonAdaptor("edison")

	button := gpio.NewButtonDriver(e, "myButton", "2")
	led := gpio.NewLedDriver(e, "myLed", "4")

	work := func() {
		gobot.On(button.Event("push"), func(data interface{}) {
			led.On()
		})
		gobot.On(button.Event("release"), func(data interface{}) {
			led.Off()
		})
	}

	robot := gobot.NewRobot("buttonBot",
		[]gobot.Connection{e},
		[]gobot.Device{led, button},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
示例#4
0
func main() {
	gbot := gobot.NewGobot()

	sparkCore := spark.NewSparkCoreAdaptor("spark", "device_id", "access_token")
	led := gpio.NewLedDriver(sparkCore, "led", "D7")
	button := gpio.NewButtonDriver(sparkCore, "button", "D5")

	work := func() {
		gobot.On(button.Event("push"), func(data interface{}) {
			led.On()
		})

		gobot.On(button.Event("release"), func(data interface{}) {
			led.Off()
		})
	}

	robot := gobot.NewRobot("spark",
		[]gobot.Connection{sparkCore},
		[]gobot.Device{button, led},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
示例#5
0
func main() {
	gbot := gobot.NewGobot()

	firmataAdaptor := firmata.NewFirmataAdaptor("myFirmata", "/dev/ttyACM0")

	button := gpio.NewButtonDriver(firmataAdaptor, "myButton", "2")
	led := gpio.NewLedDriver(firmataAdaptor, "myLed", "13")

	work := func() {
		button.On(gpio.ButtonPush, func(data interface{}) {
			led.On()
		})
		button.On(gpio.ButtonRelease, func(data interface{}) {
			led.Off()
		})
	}

	robot := gobot.NewRobot("buttonBot",
		[]gobot.Connection{firmataAdaptor},
		[]gobot.Device{button, led},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
示例#6
0
func main() {
	gbot := gobot.NewGobot()

	chipAdaptor := chip.NewChipAdaptor("chip")
	button := gpio.NewButtonDriver(chipAdaptor, "button", "XIO-P6")
	led := gpio.NewLedDriver(chipAdaptor, "led", "XIO-P7")

	work := func() {
		button.On(gpio.ButtonPush, func(data interface{}) {
			led.On()
		})

		button.On(gpio.ButtonRelease, func(data interface{}) {
			led.Off()
		})
	}

	robot := gobot.NewRobot("buttonBot",
		[]gobot.Connection{chipAdaptor},
		[]gobot.Device{button, led},
		work,
	)
	gbot.AddRobot(robot)
	gbot.Start()
}
示例#7
0
func main() {
	gbot := gobot.NewGobot()

	r := raspi.NewRaspiAdaptor("raspi")
	button := gpio.NewButtonDriver(r, "button", "11")
	led := gpio.NewLedDriver(r, "led", "7")

	work := func() {
		button.On(gpio.ButtonPush, func(data interface{}) {
			fmt.Println("button pressed")
			led.On()
		})

		button.On(gpio.ButtonRelease, func(data interface{}) {
			fmt.Println("button released")
			led.Off()
		})
	}

	robot := gobot.NewRobot("buttonBot",
		[]gobot.Connection{r},
		[]gobot.Device{button, led},
		work,
	)
	gbot.AddRobot(robot)
	gbot.Start()
}
示例#8
0
func main() {
	gbot := gobot.NewGobot()

	e := edison.NewEdisonAdaptor("edison")

	button := gpio.NewButtonDriver(e, "myButton", "5")
	led := gpio.NewLedDriver(e, "myLed", "13")

	work := func() {
		button.On(gpio.ButtonPush, func(data interface{}) {
			led.On()
		})
		button.On(gpio.ButtonRelease, func(data interface{}) {
			led.Off()
		})
	}

	robot := gobot.NewRobot("buttonBot",
		[]gobot.Connection{e},
		[]gobot.Device{led, button},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
示例#9
0
func main() {
	gbot := gobot.NewGobot()

	board := firmata.NewFirmataAdaptor("arduino", os.Args[1])

	// digital devices
	button := gpio.NewButtonDriver(board, "button", "2")
	blue := gpio.NewLedDriver(board, "blue", "3")

	work := func() {
		gobot.On(button.Event(gpio.Push), func(data interface{}) {
			fmt.Println("On!")
			blue.On()
		})

		gobot.On(button.Event(gpio.Release), func(data interface{}) {
			fmt.Println("Off!")
			blue.Off()
		})
	}

	robot := gobot.NewRobot("airlock",
		[]gobot.Connection{board},
		[]gobot.Device{button, blue},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
示例#10
0
func main() {
	e := edison.NewEdisonAdaptor("edison")
	led := gpio.NewLedDriver(e, "led", "13")
	button := gpio.NewButtonDriver(e, "button", "5")

	e.Connect()
	led.Start()
	button.Start()

	led.Off()

	buttonEvents := button.Subscribe()
	for {
		select {
		case event := <-buttonEvents:
			fmt.Println("Event:", event.Name, event.Data)
			if event.Name == gpio.ButtonPush {
				led.Toggle()
			}
		}
	}
}
示例#11
0
func main() {
	master := gobot.NewGobot()

	sparkCore := spark.NewSparkCoreAdaptor("spark", "device_id", "access_token")
	led := gpio.NewLedDriver(sparkCore, "led", "D7")
	button := gpio.NewButtonDriver(sparkCore, "button", "D5")

	work := func() {
		gobot.On(button.Events["push"], func(data interface{}) {
			led.On()
		})

		gobot.On(button.Events["release"], func(data interface{}) {
			led.Off()
		})
	}

	master.Robots = append(master.Robots,
		gobot.NewRobot("spark", []gobot.Connection{sparkCore}, []gobot.Device{button, led}, work))

	master.Start()
}
示例#12
0
func main() {
	gbot := gobot.NewGobot()

	firmataAdaptor := firmata.NewFirmataAdaptor("myFirmata", "/dev/ttyACM0")

	button := gpio.NewButtonDriver(firmataAdaptor, "myButton", "2")
	led := gpio.NewLedDriver(firmataAdaptor, "myLed", "13")

	work := func() {
		gobot.On(button.Events["push"], func(data interface{}) {
			led.On()
		})
		gobot.On(button.Events["release"], func(data interface{}) {
			led.Off()
		})
	}

	gbot.Robots = append(gbot.Robots,
		gobot.NewRobot("buttonBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{button, led}, work),
	)

	gbot.Start()
}
示例#13
0
func main() {
	gbot := gobot.NewGobot()

	firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
	led1 := gpio.NewLedDriver(firmataAdaptor, "led1", "3")
	led2 := gpio.NewLedDriver(firmataAdaptor, "led2", "4")
	button := gpio.NewButtonDriver(firmataAdaptor, "button", "2")
	sensor := gpio.NewAnalogSensorDriver(firmataAdaptor, "sensor", "0")

	work := func() {
		gobot.Every(1*time.Second, func() {
			led1.Toggle()
		})
		gobot.Every(2*time.Second, func() {
			led2.Toggle()
		})
		button.On(gpio.ButtonPush, func(data interface{}) {
			led2.On()
		})
		button.On(gpio.ButtonRelease, func(data interface{}) {
			led2.Off()
		})
		sensor.On(gpio.Data, func(data interface{}) {
			fmt.Println("sensor", data)
		})
	}

	robot := gobot.NewRobot("bot",
		[]gobot.Connection{firmataAdaptor},
		[]gobot.Device{led1, led2, button, sensor},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
示例#14
0
func main() {
	gbot := gobot.NewGobot()

	chipAdaptor := chip.NewChipAdaptor("chip")
	button := gpio.NewButtonDriver(chipAdaptor, "button", "XIO-P0")

	work := func() {
		gobot.On(button.Event("push"), func(data interface{}) {
			fmt.Println("button pressed")
		})

		gobot.On(button.Event("release"), func(data interface{}) {
			fmt.Println("button released")
		})
	}

	robot := gobot.NewRobot("buttonBot",
		[]gobot.Connection{chipAdaptor},
		[]gobot.Device{button},
		work,
	)
	gbot.AddRobot(robot)
	gbot.Start()
}
示例#15
0
func main() {
	gbot := gobot.NewGobot()

	beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone")
	button := gpio.NewButtonDriver(beagleboneAdaptor, "button", "P8_9")

	work := func() {
		gobot.On(button.Event("push"), func(data interface{}) {
			fmt.Println("button pressed")
		})

		gobot.On(button.Event("release"), func(data interface{}) {
			fmt.Println("button released")
		})
	}

	robot := gobot.NewRobot("buttonBot",
		[]gobot.Connection{beagleboneAdaptor},
		[]gobot.Device{button},
		work,
	)
	gbot.AddRobot(robot)
	gbot.Start()
}
示例#16
0
文件: square.go 项目: ympons/gobot
func main() {
	gbot := gobot.NewGobot()
	a := api.NewAPI(gbot)
	a.Start()

	board := edison.NewEdisonAdaptor("edison")
	red := gpio.NewLedDriver(board, "red", "3")
	green := gpio.NewLedDriver(board, "green", "5")
	blue := gpio.NewLedDriver(board, "blue", "6")

	button := gpio.NewButtonDriver(board, "button", "7")

	enabled := true
	work := func() {
		red.Brightness(0xff)
		green.Brightness(0x00)
		blue.Brightness(0x00)

		flash := false
		on := true

		gobot.Every(250*time.Millisecond, func() {
			if enabled {
				if flash {
					if on {
						red.Brightness(0x00)
						green.Brightness(0xff)
						blue.Brightness(0x00)
						on = false
					} else {
						red.Brightness(0xff)
						green.Brightness(0x00)
						blue.Brightness(0x00)
						on = true
					}
				}
			}
		})

		button.On(gpio.ButtonPush, func(data interface{}) {
			flash = true
		})

		button.On(gpio.ButtonRelease, func(data interface{}) {
			flash = false
			red.Brightness(0x00)
			green.Brightness(0x00)
			blue.Brightness(0xff)
		})
	}

	robot := gobot.NewRobot(
		"square of fire",
		[]gobot.Connection{board},
		[]gobot.Device{red, green, blue, button},
		work,
	)

	robot.AddCommand("enable", func(params map[string]interface{}) interface{} {
		enabled = !enabled
		return enabled
	})

	gbot.AddRobot(robot)

	gbot.Start()
}
示例#17
0
func main() {

	// initialize our base configuration for the system
	initConfiguration()

	// initialize a new gobot
	gbot := gobot.NewGobot()

	// initialize a raspberry pi adaptor
	raspberry := raspi.NewRaspiAdaptor("raspi")

	// start/stop button for a woman
	buttonWoman := gpio.NewButtonDriver(raspberry, "buttonWoman", "11") // GPIO #17 (Low)
	ledWoman := gpio.NewLedDriver(raspberry, "ledWoman", "36")          // GPIO #16 (Low)

	// start/stop buttom for a man
	buttonMan := gpio.NewButtonDriver(raspberry, "buttonMan", "12") // GPIO #18 (Low)
	ledMan := gpio.NewLedDriver(raspberry, "ledMan", "40")          // GPIO #21 (Low)

	// contact with the wire (start- and finish-area)
	contactStart := gpio.NewButtonDriver(raspberry, "contactStart", "13")   // GPIO #27 (Low)
	contactFinish := gpio.NewButtonDriver(raspberry, "contactFinish", "22") // GPIO #25 (Low)

	// user made contact with the wire (use buzzer to indicate audible)
	contactWire := gpio.NewButtonDriver(raspberry, "contactWire", "16") // GPIO #23 (Low)
	buzzer := gpio.NewLedDriver(raspberry, "buzzer", "32")              // GPIO #12 (Low)

	// create a channel for game events
	GameEvents = make(chan string)

	// simulate events with keyboard interaction
	go simulate(GameEvents)

	// define the work to be done by the robot (i.e. react to events)
	work := func() {

		// user pushed the start/stop button
		gobot.On(buttonWoman.Event("push"), func(data interface{}) {
			handleButtonPress(FEMALE)
		})

		gobot.On(buttonMan.Event("push"), func(data interface{}) {
			handleButtonPress(MALE)
		})

		// user made contact with wire
		gobot.On(contactWire.Event("push"), handleWireContact)

		// user is starting the game (must touch the starting area)
		gobot.On(contactStart.Event("push"), handleStartContact)

		// user finished the game (touched finish area)
		gobot.On(contactFinish.Event("push"), handleFinishContact)

		go func() {

			for event := range GameEvents {

				switch event {
				// sound the buzzer
				case "soundBuzzer":
					go func() {
						buzzer.On()
						<-time.After(300 * time.Millisecond)
						buzzer.Off()
					}()

				// enable/disable the led for the woman button
				case "enableLedWoman":
					ledWoman.On()
				case "disableLedWoman":
					ledWoman.Off()

				// enable/disable the lef for the man button
				case "enableLedMan":
					ledMan.On()
				case "disableLedMan":
					ledMan.Off()

				// disable all leds
				case "ledOff":
					ledWoman.Off()
					ledMan.Off()

				// simulated events
				case "button":
					handleButtonPress(FEMALE)
				case "contact":
					handleWireContact(nil)
				case "start":
					handleStartContact(nil)
				case "finish":
					handleFinishContact(nil)

				case "off":
					ledMan.Off()
					ledWoman.Off()
					buzzer.Off()
				}
			}
		}()

	}

	// we need to define a robot to be used with gobot
	var robot *gobot.Robot

	// switch cases depending on the mode
	if Mode == MODE_DEBUG {
		// debug mode is run on the mac without physical connections
		fmt.Println("RUNNING IN DEBUG-MODE")

		robot = gobot.NewRobot("buzzwire",
			[]gobot.Connection{},
			[]gobot.Device{},
			work)
	} else {
		// all other modes are run on the pi with physical connections
		robot = gobot.NewRobot("buzzwire",
			[]gobot.Connection{raspberry},
			[]gobot.Device{buttonWoman, ledWoman, buttonMan, ledMan, contactStart, contactWire, buzzer, contactFinish},
			work)
	}

	// add the robot to the fleet
	gbot.AddRobot(robot)

	// start the webserver in a separate go routine
	go startServer("localhost:8484")

	// open the default webseite
	go openInDefaultBrowser("localhost:8484")

	// start the robot (blocking)
	gbot.Start()
}