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