gobot.io/x/gobot@v1.16.0/examples/sphero_conways.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	"gobot.io/x/gobot"
    12  	"gobot.io/x/gobot/platforms/sphero"
    13  )
    14  
    15  type conway struct {
    16  	alive    bool
    17  	age      int
    18  	contacts int
    19  	cell     *sphero.SpheroDriver
    20  }
    21  
    22  func main() {
    23  	master := gobot.NewMaster()
    24  
    25  	spheros := []string{
    26  		"/dev/rfcomm0",
    27  		"/dev/rfcomm1",
    28  		"/dev/rfcomm2",
    29  	}
    30  
    31  	for _, port := range spheros {
    32  		spheroAdaptor := sphero.NewAdaptor(port)
    33  
    34  		cell := sphero.NewSpheroDriver(spheroAdaptor)
    35  		cell.SetName("Sphero" + port)
    36  
    37  		work := func() {
    38  			conway := new(conway)
    39  			conway.cell = cell
    40  
    41  			conway.birth()
    42  
    43  			cell.On(sphero.Collision, func(data interface{}) {
    44  				conway.contact()
    45  			})
    46  
    47  			gobot.Every(3*time.Second, func() {
    48  				if conway.alive {
    49  					conway.movement()
    50  				}
    51  			})
    52  
    53  			gobot.Every(10*time.Second, func() {
    54  				if conway.alive {
    55  					conway.birthday()
    56  				}
    57  			})
    58  		}
    59  
    60  		robot := gobot.NewRobot("conway",
    61  			[]gobot.Connection{spheroAdaptor},
    62  			[]gobot.Device{cell},
    63  			work,
    64  		)
    65  
    66  		master.AddRobot(robot)
    67  	}
    68  
    69  	master.Start()
    70  }
    71  
    72  func (c *conway) resetContacts() {
    73  	c.contacts = 0
    74  }
    75  
    76  func (c *conway) contact() {
    77  	c.contacts++
    78  }
    79  
    80  func (c *conway) rebirth() {
    81  	fmt.Println("Welcome back", c.cell.Name(), "!")
    82  	c.life()
    83  }
    84  
    85  func (c *conway) birth() {
    86  	c.resetContacts()
    87  	c.age = 0
    88  	c.life()
    89  	c.movement()
    90  }
    91  
    92  func (c *conway) life() {
    93  	c.cell.SetRGB(0, 255, 0)
    94  	c.alive = true
    95  }
    96  
    97  func (c *conway) death() {
    98  	fmt.Println(c.cell.Name(), "died :(")
    99  	c.alive = false
   100  	c.cell.SetRGB(255, 0, 0)
   101  	c.cell.Stop()
   102  }
   103  
   104  func (c *conway) enoughContacts() bool {
   105  	if c.contacts >= 2 && c.contacts < 7 {
   106  		return true
   107  	}
   108  	return false
   109  }
   110  
   111  func (c *conway) birthday() {
   112  	c.age++
   113  
   114  	fmt.Println("Happy birthday", c.cell.Name(), "you are", c.age, "and had", c.contacts, "contacts.")
   115  
   116  	if c.enoughContacts() {
   117  		if !c.alive {
   118  			c.rebirth()
   119  		}
   120  	} else {
   121  		c.death()
   122  	}
   123  
   124  	c.resetContacts()
   125  }
   126  
   127  func (c *conway) movement() {
   128  	if c.alive {
   129  		c.cell.Roll(100, uint16(gobot.Rand(360)))
   130  	}
   131  }