Skip to content

Commit

Permalink
Add Init function to DriverInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Mar 31, 2014
1 parent 0418ca2 commit bc014f7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type device struct {
}

type Device interface {
Init() bool
Start() bool
Halt() bool
}
Expand All @@ -27,6 +28,11 @@ func NewDevice(driver DriverInterface, r *Robot) *device {
return d
}

func (d *device) Init() bool {
log.Println("Device " + d.Name + " initialized")
return d.Driver.Init()
}

func (d *device) Start() bool {
log.Println("Device " + d.Name + " started")
return d.Driver.Start()
Expand Down
1 change: 1 addition & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Driver struct {
}

type DriverInterface interface {
Init() bool
Start() bool
Halt() bool
}
16 changes: 13 additions & 3 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ func (r *Robot) startRobot() {
r.initName()
r.initCommands()
r.initConnections()
r.initDevices()
if r.startConnections() != true {
panic("Could not start connections")
}
if r.initDevices() != true {
panic("Could not initialize devices")
}
if r.startDevices() != true {
panic("Could not start devices")
}
Expand Down Expand Up @@ -63,13 +65,21 @@ func (r *Robot) initConnections() {
}
}

func (r *Robot) initDevices() {
func (r *Robot) initDevices() bool {
r.devices = make([]*device, len(r.Devices))
log.Println("Initializing devices...")
for i, device := range r.Devices {
log.Println("Initializing device ", FieldByNamePtr(device, "Name"), "...")
r.devices[i] = NewDevice(device, r)
}
success := true
for _, device := range r.devices {
log.Println("Initializing device " + device.Name + "...")
if device.Init() == false {
success = false
break
}
}
return success
}

func (r *Robot) startConnections() bool {
Expand Down
1 change: 1 addition & 0 deletions test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type testDriver struct {
Driver
}

func (me *testDriver) Init() bool { return true }
func (me *testDriver) Start() bool { return true }
func (me *testDriver) Halt() bool { return true }

Expand Down

0 comments on commit bc014f7

Please sign in to comment.