gobot.io/x/gobot@v1.16.0/examples/square_fire.go (about) 1 // +build example 2 // 3 // Do not build by default. 4 5 package main 6 7 import ( 8 "time" 9 10 "gobot.io/x/gobot" 11 "gobot.io/x/gobot/api" 12 "gobot.io/x/gobot/drivers/gpio" 13 "gobot.io/x/gobot/platforms/intel-iot/edison" 14 ) 15 16 func main() { 17 master := gobot.NewMaster() 18 a := api.NewAPI(master) 19 a.Start() 20 21 board := edison.NewAdaptor() 22 red := gpio.NewLedDriver(board, "3") 23 green := gpio.NewLedDriver(board, "5") 24 blue := gpio.NewLedDriver(board, "6") 25 26 button := gpio.NewButtonDriver(board, "7") 27 28 enabled := true 29 work := func() { 30 red.Brightness(0xff) 31 green.Brightness(0x00) 32 blue.Brightness(0x00) 33 34 flash := false 35 on := true 36 37 gobot.Every(50*time.Millisecond, func() { 38 if enabled { 39 if flash { 40 if on { 41 red.Brightness(0x00) 42 green.Brightness(0xff) 43 blue.Brightness(0x00) 44 on = false 45 } else { 46 red.Brightness(0x00) 47 green.Brightness(0x00) 48 blue.Brightness(0xff) 49 on = true 50 } 51 } 52 } 53 }) 54 55 button.On(gpio.ButtonPush, func(data interface{}) { 56 flash = true 57 }) 58 59 button.On(gpio.ButtonRelease, func(data interface{}) { 60 flash = false 61 red.Brightness(0x00) 62 green.Brightness(0x00) 63 blue.Brightness(0xff) 64 }) 65 } 66 67 robot := gobot.NewRobot( 68 "square of fire", 69 []gobot.Connection{board}, 70 []gobot.Device{red, green, blue, button}, 71 work, 72 ) 73 74 robot.AddCommand("enable", func(params map[string]interface{}) interface{} { 75 enabled = !enabled 76 return enabled 77 }) 78 79 master.AddRobot(robot) 80 81 master.Start() 82 }