mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +08:00
Code cleanups suggested by gosimple
This commit is contained in:
parent
67ff7da9e3
commit
977c878a8b
@ -1019,10 +1019,8 @@ func RestoreAsset(dir, name string) error {
|
||||
return err
|
||||
}
|
||||
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// RestoreAssets restores an asset under the given directory recursively
|
||||
|
@ -31,7 +31,7 @@ func main() {
|
||||
var image *cv.IplImage
|
||||
gobot.On(camera.Event("frame"), func(data interface{}) {
|
||||
image = data.(*cv.IplImage)
|
||||
if detect == false {
|
||||
if !detect {
|
||||
window.ShowImage(image)
|
||||
}
|
||||
})
|
||||
|
@ -40,13 +40,13 @@ func main() {
|
||||
})
|
||||
|
||||
gobot.Every(3*time.Second, func() {
|
||||
if conway.alive == true {
|
||||
if conway.alive {
|
||||
conway.movement()
|
||||
}
|
||||
})
|
||||
|
||||
gobot.Every(10*time.Second, func() {
|
||||
if conway.alive == true {
|
||||
if conway.alive {
|
||||
conway.birthday()
|
||||
}
|
||||
})
|
||||
@ -107,8 +107,8 @@ func (c *conway) birthday() {
|
||||
|
||||
fmt.Println("Happy birthday", c.cell.Name, "you are", c.age, "and had", c.contacts, "contacts.")
|
||||
|
||||
if c.enoughContacts() == true {
|
||||
if c.alive == false {
|
||||
if c.enoughContacts() {
|
||||
if !c.alive {
|
||||
c.rebirth()
|
||||
}
|
||||
} else {
|
||||
@ -119,7 +119,7 @@ func (c *conway) birthday() {
|
||||
}
|
||||
|
||||
func (c *conway) movement() {
|
||||
if c.alive == true {
|
||||
if c.alive {
|
||||
c.cell.Roll(100, uint16(gobot.Rand(360)))
|
||||
}
|
||||
}
|
||||
|
2
gobot.go
2
gobot.go
@ -73,7 +73,7 @@ func (g *Gobot) Start() (errs []error) {
|
||||
}
|
||||
|
||||
// waiting for interrupt coming on the channel
|
||||
_ = <-c
|
||||
<-c
|
||||
|
||||
// Stop calls the Stop method on each robot in its collection of robots.
|
||||
g.Stop()
|
||||
|
@ -71,7 +71,7 @@ func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) (err error) {
|
||||
|
||||
// PwmWrite writes the 0-254 value to the specified pin
|
||||
func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) (err error) {
|
||||
if d.pwm == false {
|
||||
if !d.pwm {
|
||||
if err = d.littleWire.pwmInit(); err != nil {
|
||||
return
|
||||
}
|
||||
@ -87,7 +87,7 @@ func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) (err error) {
|
||||
|
||||
// ServoWrite writes the 0-180 degree val to the specified pin.
|
||||
func (d *DigisparkAdaptor) ServoWrite(pin string, angle uint8) (err error) {
|
||||
if d.servo == false {
|
||||
if !d.servo {
|
||||
if err = d.littleWire.servoInit(); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -469,7 +469,7 @@ func (b *Client) process() (err error) {
|
||||
b.FirmwareName = string(name[:])
|
||||
gobot.Publish(b.Event("FirmwareQuery"), b.FirmwareName)
|
||||
case StringData:
|
||||
str := currentBuffer[2:len(currentBuffer)]
|
||||
str := currentBuffer[2:]
|
||||
gobot.Publish(b.Event("StringData"), string(str[:len(str)-1]))
|
||||
}
|
||||
}
|
||||
|
@ -175,10 +175,7 @@ func (m *MotorDriver) Direction(direction string) (err error) {
|
||||
}
|
||||
|
||||
func (m *MotorDriver) isDigital() bool {
|
||||
if m.CurrentMode == "digital" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return m.CurrentMode == "digital"
|
||||
}
|
||||
|
||||
func (m *MotorDriver) changeState(state byte) (err error) {
|
||||
|
@ -218,10 +218,7 @@ func (h *JHD1313M1Driver) Scroll(leftToRight bool) error {
|
||||
func (h *JHD1313M1Driver) Halt() []error { return nil }
|
||||
|
||||
func (h *JHD1313M1Driver) setReg(command int, data int) error {
|
||||
if err := h.connection.I2cWrite(h.rgbAddress, []byte{byte(command), byte(data)}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return h.connection.I2cWrite(h.rgbAddress, []byte{byte(command), byte(data)})
|
||||
}
|
||||
|
||||
func (h *JHD1313M1Driver) command(buf []byte) error {
|
||||
|
@ -171,10 +171,7 @@ func (m *MCP23017Driver) ReadGPIO(pin uint8, portStr string) (val uint8, err err
|
||||
// val = 0 pull up disabled.
|
||||
func (m *MCP23017Driver) SetPullUp(pin uint8, val uint8, portStr string) error {
|
||||
selectedPort := m.getPort(portStr)
|
||||
if err := m.write(selectedPort.GPPU, pin, val); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return m.write(selectedPort.GPPU, pin, val)
|
||||
}
|
||||
|
||||
// SetGPIOPolarity will change a given pin's polarity based on the value:
|
||||
@ -182,10 +179,7 @@ func (m *MCP23017Driver) SetPullUp(pin uint8, val uint8, portStr string) error {
|
||||
// val = 0 same logic state of the input pin.
|
||||
func (m *MCP23017Driver) SetGPIOPolarity(pin uint8, val uint8, portStr string) (err error) {
|
||||
selectedPort := m.getPort(portStr)
|
||||
if err := m.write(selectedPort.IPOL, pin, val); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return m.write(selectedPort.IPOL, pin, val)
|
||||
}
|
||||
|
||||
// write gets the value of the passed in register, and then overwrites
|
||||
|
@ -174,7 +174,7 @@ func (s *SpheroDriver) Start() (errs []error) {
|
||||
go func() {
|
||||
for {
|
||||
header := s.readHeader()
|
||||
if header != nil && len(header) != 0 {
|
||||
if len(header) > 0 {
|
||||
body := s.readBody(header[4])
|
||||
data := append(header, body...)
|
||||
checksum := data[len(data)-1]
|
||||
@ -268,7 +268,7 @@ func (s *SpheroDriver) SetHeading(heading uint16) {
|
||||
// SetStabilization enables or disables the built-in auto stabilizing features of the Sphero
|
||||
func (s *SpheroDriver) SetStabilization(on bool) {
|
||||
b := uint8(0x01)
|
||||
if on == false {
|
||||
if !on {
|
||||
b = 0x00
|
||||
}
|
||||
s.packetChannel <- s.craftPacket([]uint8{b}, 0x02, 0x02)
|
||||
|
14
robot.go
14
robot.go
@ -152,18 +152,8 @@ func (r *Robot) Start() (errs []error) {
|
||||
// Stop stops a Robot's connections and Devices
|
||||
func (r *Robot) Stop() (errs []error) {
|
||||
log.Println("Stopping Robot", r.Name, "...")
|
||||
if heers := r.Devices().Halt(); len(heers) > 0 {
|
||||
for _, err := range heers {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if ceers := r.Connections().Finalize(); len(ceers) > 0 {
|
||||
for _, err := range ceers {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
errs = append(errs, r.Devices().Halt()...)
|
||||
errs = append(errs, r.Connections().Finalize()...)
|
||||
return errs
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user