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