From c01d9a6384126b2af41a5f688f959016ddaf209e Mon Sep 17 00:00:00 2001 From: Marc-Antoine Ruel Date: Sun, 19 May 2019 12:19:23 -0400 Subject: [PATCH] Reduce the number of golint comments. Ran the following command: golint -min_confidence 0 ./... | grep -v ALL_CAPS | \ grep -v underscores | grep -v 'another file' and fixed the comments that made sense. --- .../bmxx80/bmx280smoketest/bmx280smoketest.go | 6 ++--- experimental/devices/ads1x15/ads1x15.go | 1 + experimental/devices/ccs811/ccs811.go | 25 ++++++------------ experimental/devices/ccs811/ccs811_test.go | 2 +- experimental/devices/epd/epd.go | 12 ++------- experimental/devices/hd44780/hd44780.go | 26 ++++--------------- experimental/devices/ht16k33/ht16k33.go | 11 +++----- experimental/devices/hx711/hx711.go | 1 + experimental/devices/inky/doc.go | 2 +- experimental/devices/inky/inky.go | 20 +++++++------- experimental/devices/mcp9808/mcp9808.go | 3 +++ .../devices/mfrc522/commands/low_level.go | 2 +- experimental/devices/mfrc522/mfrc522.go | 3 ++- .../mpu9250/accelerometer/accelerometer.go | 18 ++++++------- experimental/devices/mpu9250/mpu9250.go | 6 +---- experimental/devices/mpu9250/reg/reg.go | 3 +++ experimental/devices/mpu9250/spi.go | 7 ++--- experimental/devices/pca9548/pca9548.go | 2 +- experimental/devices/rainbowhat/rainbowhat.go | 6 +---- 19 files changed, 58 insertions(+), 98 deletions(-) diff --git a/devices/bmxx80/bmx280smoketest/bmx280smoketest.go b/devices/bmxx80/bmx280smoketest/bmx280smoketest.go index 51857b8..7e9a4b7 100644 --- a/devices/bmxx80/bmx280smoketest/bmx280smoketest.go +++ b/devices/bmxx80/bmx280smoketest/bmx280smoketest.go @@ -173,15 +173,15 @@ func run(i2cBus i2c.Bus, i2cAddr uint16, spiPort spi.PortCloser) (err error) { // 1°C if delta.Temperature > 1000 || delta.Temperature < -1000 { - return fmt.Errorf("Temperature delta higher than expected (%s): I²C got %s; SPI got %s", delta.Temperature, i2cEnv.Temperature, spiEnv.Temperature) + return fmt.Errorf("temperature delta higher than expected (%s): I²C got %s; SPI got %s", delta.Temperature, i2cEnv.Temperature, spiEnv.Temperature) } // 0.1kPa if delta.Pressure > 100 || delta.Pressure < -100 { - return fmt.Errorf("Pressure delta higher than expected (%s): I²C got %s; SPI got %s", delta.Pressure, i2cEnv.Pressure, spiEnv.Pressure) + return fmt.Errorf("pressure delta higher than expected (%s): I²C got %s; SPI got %s", delta.Pressure, i2cEnv.Pressure, spiEnv.Pressure) } // 4%rH if delta.Humidity > 400 || delta.Humidity < -400 { - return fmt.Errorf("Humidity delta higher than expected (%s): I²C got %s; SPI got %s", delta.Humidity, i2cEnv.Humidity, spiEnv.Humidity) + return fmt.Errorf("humidity delta higher than expected (%s): I²C got %s; SPI got %s", delta.Humidity, i2cEnv.Humidity, spiEnv.Humidity) } return nil } diff --git a/experimental/devices/ads1x15/ads1x15.go b/experimental/devices/ads1x15/ads1x15.go index ac7b38f..7efc98a 100644 --- a/experimental/devices/ads1x15/ads1x15.go +++ b/experimental/devices/ads1x15/ads1x15.go @@ -24,6 +24,7 @@ const I2CAddr uint16 = 0x48 // a differential reading between two pins. type Channel int +// Value channels. const ( // Absolute reading. Channel0 Channel = 4 diff --git a/experimental/devices/ccs811/ccs811.go b/experimental/devices/ccs811/ccs811.go index 3f5eb07..7cc76b5 100644 --- a/experimental/devices/ccs811/ccs811.go +++ b/experimental/devices/ccs811/ccs811.go @@ -86,7 +86,7 @@ func (d *Dev) errorCodeToError(errorCode SensorErrorID) error { default: errorText = fmt.Sprintf("Uknwown error, code: %d", errorCode) } - return fmt.Errorf("Sensor error: %s", errorText) + return fmt.Errorf("sensor error: %s", errorText) } // Opts holds the configuration options. The address must be 0x5A or 0x5B. @@ -108,11 +108,11 @@ var DefaultOpts = Opts{ // New creates a new driver for CCS811 VOC sensor. func New(bus i2c.Bus, opts *Opts) (*Dev, error) { if opts.Addr != 0x5A && opts.Addr != 0x5B { - return nil, fmt.Errorf("Invalid device address, only 0x5A or 0x5B are allowed") + return nil, fmt.Errorf("invalid device address, only 0x5A or 0x5B are allowed") } if opts.MeasurementMode > MeasurementModeConstant250 { - return nil, fmt.Errorf("Invalid measurement mode") + return nil, fmt.Errorf("invalid measurement mode") } dev := &Dev{ @@ -122,14 +122,14 @@ func New(bus i2c.Bus, opts *Opts) (*Dev, error) { // From boot mode to measurement mode. if err := dev.StartSensorApp(); err != nil { - return nil, fmt.Errorf("Error transitioning from boot do app mode: %v", err) + return nil, fmt.Errorf("error transitioning from boot do app mode: %v", err) } mmp := &MeasurementModeParams{MeasurementMode: opts.MeasurementMode, GenerateInterrupt: opts.InterruptWhenReady, UseThreshold: opts.UseThreshold} if err := dev.SetMeasurementModeRegister(*mmp); err != nil { - return nil, fmt.Errorf("Error setting measurement mode: %v", err) + return nil, fmt.Errorf("error setting measurement mode: %v", err) } return dev, nil @@ -197,10 +197,7 @@ func (d *Dev) GetMeasurementModeRegister() (MeasurementModeParams, error) { // Reset sets device into the BOOT mode. func (d *Dev) Reset() error { - if err := d.c.Tx([]byte{resetReg, 0x11, 0xE5, 0x72, 0x8A}, nil); err != nil { - return err - } - return nil + return d.c.Tx([]byte{resetReg, 0x11, 0xE5, 0x72, 0x8A}, nil) } // ReadStatus returns value of status register. @@ -234,10 +231,7 @@ func (d *Dev) SetEnvironmentData(temp, humidity float32) error { byte(rawTemp >> 8), byte(rawTemp)} - if err := d.c.Tx(w, nil); err != nil { - return err - } - return nil + return d.c.Tx(w, nil) } // GetBaseline provides current baseline used by internal measurement algorithm. @@ -288,10 +282,7 @@ func (d *Dev) GetBaseline() ([]byte, error) { // 2) The baseline must be written after the conditioning period func (d *Dev) SetBaseline(baseline []byte) error { w := []byte{baselineReg, baseline[0], baseline[1]} - if err := d.c.Tx(w, nil); err != nil { - return err - } - return nil + return d.c.Tx(w, nil) } // SensorValues represents data read from the sensor. diff --git a/experimental/devices/ccs811/ccs811_test.go b/experimental/devices/ccs811/ccs811_test.go index 63d7213..1d58146 100644 --- a/experimental/devices/ccs811/ccs811_test.go +++ b/experimental/devices/ccs811/ccs811_test.go @@ -40,7 +40,7 @@ func TestBasicInitialisationAndDataRead(t *testing.T) { if data.ECO2 != 0x102 && data.VOC != 0x203 && data.Status != 0xF && - data.Error != fmt.Errorf("Sensor error: %s", "HEATER_FAULT: The Heater current in the CCS811 is not in range.") && + data.Error != fmt.Errorf("sensor error: %s", "HEATER_FAULT: The Heater current in the CCS811 is not in range.") && data.RawDataCurrent != cExpected && data.RawDataVoltage != vExpected { t.Fatalf("Data parsed incorrectly, got %v", data) diff --git a/experimental/devices/epd/epd.go b/experimental/devices/epd/epd.go index da50127..1450e94 100644 --- a/experimental/devices/epd/epd.go +++ b/experimental/devices/epd/epd.go @@ -359,11 +359,7 @@ func (d *Dev) Init() error { return err } - if err := d.setLut(Full); err != nil { - return err - } - - return nil + return d.setLut(Full) } // Reset can be also used to awaken the device @@ -429,11 +425,7 @@ func (d *Dev) setMemoryArea(xStart, yStart, xEnd, yEnd int) error { if err := d.sendData([]byte{byte(yEnd & 0xFF)}); err != nil { return err } - if err := d.sendData([]byte{byte((yEnd >> 8) & 0xFF)}); err != nil { - return err - } - - return nil + return d.sendData([]byte{byte((yEnd >> 8) & 0xFF)}) } func (d *Dev) setLut(update PartialUpdate) error { diff --git a/experimental/devices/hd44780/hd44780.go b/experimental/devices/hd44780/hd44780.go index 2dcfc14..7b9072f 100644 --- a/experimental/devices/hd44780/hd44780.go +++ b/experimental/devices/hd44780/hd44780.go @@ -71,11 +71,7 @@ func (r *Dev) Reset() error { return err } - if err := r.bulkSendData(initSequence, r.writeInstruction); err != nil { - return err - } - - return nil + return r.bulkSendData(initSequence, r.writeInstruction) } func (r *Dev) String() string { @@ -95,10 +91,7 @@ func (r *Dev) Halt() error { // line - screen line, 0-based // column - column, 0-based func (r *Dev) SetCursor(line uint8, column uint8) error { - if err := r.writeInstruction(0x80 | (line*lineTwo + column)); err != nil { - return err - } - return nil + return r.writeInstruction(0x80 | (line*lineTwo + column)) } // Print the data string @@ -187,20 +180,14 @@ func (r *Dev) sendInstruction() error { if err := r.rsPin.Out(gpio.Low); err != nil { return err } - if err := r.enablePin.Out(gpio.Low); err != nil { - return err - } - return nil + return r.enablePin.Out(gpio.Low) } func (r *Dev) sendData() error { if err := r.rsPin.Out(gpio.High); err != nil { return err } - if err := r.enablePin.Out(gpio.Low); err != nil { - return err - } - return nil + return r.enablePin.Out(gpio.Low) } func (r *Dev) writeInstruction(data uint8) error { @@ -224,10 +211,7 @@ func (r *Dev) strobe() error { return err } delayUs(2) - if err := r.enablePin.Out(gpio.Low); err != nil { - return err - } - return nil + return r.enablePin.Out(gpio.Low) } func delayUs(ms uint) { diff --git a/experimental/devices/ht16k33/ht16k33.go b/experimental/devices/ht16k33/ht16k33.go index 48eedd3..8ab99a1 100644 --- a/experimental/devices/ht16k33/ht16k33.go +++ b/experimental/devices/ht16k33/ht16k33.go @@ -71,18 +71,13 @@ func (d *Dev) init() error { } // Set display to full brightness. - if err := d.SetBrightness(15); err != nil { - return err - } - return nil + return d.SetBrightness(15) } // SetBlink Blink display at specified frequency. func (d *Dev) SetBlink(freq BlinkFrequency) error { - if _, err := d.dev.Write([]byte{displaySetup | displayOn | byte(freq)}); err != nil { - return err - } - return nil + _, err := d.dev.Write([]byte{displaySetup | displayOn | byte(freq)}) + return err } // SetBrightness of entire display to specified value. diff --git a/experimental/devices/hx711/hx711.go b/experimental/devices/hx711/hx711.go index 4805318..a89a114 100644 --- a/experimental/devices/hx711/hx711.go +++ b/experimental/devices/hx711/hx711.go @@ -30,6 +30,7 @@ var ( // with a gain of 32. type InputMode int +// Valid InputMode. const ( CHANNEL_A_GAIN_128 InputMode = 1 CHANNEL_A_GAIN_64 InputMode = 3 diff --git a/experimental/devices/inky/doc.go b/experimental/devices/inky/doc.go index a964f5d..fd4ce18 100644 --- a/experimental/devices/inky/doc.go +++ b/experimental/devices/inky/doc.go @@ -3,7 +3,7 @@ // that can be found in the LICENSE file. // Package inky drives an Inky pHAT E ink display. - +// // Datasheet // // Inky lacks a true datasheet, so the code here is derived from the reference diff --git a/experimental/devices/inky/inky.go b/experimental/devices/inky/inky.go index 16a6df8..ab78512 100644 --- a/experimental/devices/inky/inky.go +++ b/experimental/devices/inky/inky.go @@ -28,11 +28,12 @@ const ( // setting the border color. type Color int +// Valid Color. const ( - Black = iota - Red = iota - Yellow = iota - White = iota + Black Color = iota + Red + Yellow + White ) var borderColor = map[Color]byte{ @@ -45,6 +46,7 @@ var borderColor = map[Color]byte{ // Model lists the supported e-ink display models. type Model int +// Supported Model. const ( PHAT Model = iota // TODO: Add wHAT here when supported. @@ -61,10 +63,10 @@ type Opts struct { BorderColor Color } -// NewpHAT opens a handle to an Inky pHAT. +// New opens a handle to an Inky pHAT. func New(p spi.Port, dc gpio.PinOut, reset gpio.PinOut, busy gpio.PinIn, o *Opts) (*Dev, error) { if o.ModelColor != Black && o.ModelColor != Red && o.ModelColor != Yellow { - return nil, fmt.Errorf("Unsupported color: %v", o.ModelColor) + return nil, fmt.Errorf("unsupported color: %v", o.ModelColor) } c, err := p.Connect(488*physic.KiloHertz, spi.Mode0, 8) @@ -153,11 +155,11 @@ func (d *Dev) Bounds() image.Rectangle { // Draw implements display.Drawer func (d *Dev) Draw(dstRect image.Rectangle, src image.Image, srcPtrs image.Point) error { if dstRect != d.Bounds() { - return fmt.Errorf("Partial update not supported") + return fmt.Errorf("partial update not supported") } if src.Bounds() != d.Bounds() { - return fmt.Errorf("Image must be the same size as bounds: %v", d.Bounds()) + return fmt.Errorf("image must be the same size as bounds: %v", d.Bounds()) } b := src.Bounds() @@ -331,7 +333,7 @@ func (d *Dev) sendCommand(command byte, data []byte) error { func (d *Dev) sendData(data []byte) error { if len(data) > 4096 { - return fmt.Errorf("Sending more data than chunk size: %d > 4096", len(data)) + return fmt.Errorf("sending more data than chunk size: %d > 4096", len(data)) } if err := d.dc.Out(gpio.High); err != nil { return err diff --git a/experimental/devices/mcp9808/mcp9808.go b/experimental/devices/mcp9808/mcp9808.go index 7c32201..6d03b51 100644 --- a/experimental/devices/mcp9808/mcp9808.go +++ b/experimental/devices/mcp9808/mcp9808.go @@ -139,6 +139,7 @@ func (d *Dev) SenseContinuous(interval time.Duration) (<-chan physic.Env, error) return env, nil } +// Precision implement SenseEnv. func (d *Dev) Precision(e *physic.Env) { switch d.res { case Maximum: @@ -339,6 +340,7 @@ func (d *Dev) setLowerAlert(t physic.Temperature) error { return nil } +// Alert represents an alert generated by the device. type Alert struct { AlertMode string AlertLevel physic.Temperature @@ -412,6 +414,7 @@ func alertTemperatureToBits(t physic.Temperature) (uint16, error) { type resolution uint8 +// Valid resolution values. const ( Maximum resolution = 0 Low resolution = 1 diff --git a/experimental/devices/mfrc522/commands/low_level.go b/experimental/devices/mfrc522/commands/low_level.go index 290c75a..d0d4915 100644 --- a/experimental/devices/mfrc522/commands/low_level.go +++ b/experimental/devices/mfrc522/commands/low_level.go @@ -96,7 +96,7 @@ func (r *LowLevel) Init() error { return r.SetAntenna(true) } -// setAntenna configures the antenna state, on/off. +// SetAntenna configures the antenna state, on/off. func (r *LowLevel) SetAntenna(state bool) error { if state { current, err := r.DevRead(TxControlReg) diff --git a/experimental/devices/mfrc522/mfrc522.go b/experimental/devices/mfrc522/mfrc522.go index de9e32d..e5b7dcc 100644 --- a/experimental/devices/mfrc522/mfrc522.go +++ b/experimental/devices/mfrc522/mfrc522.go @@ -35,6 +35,7 @@ type Key [6]byte // DefaultKey provides the default bytes for card authentication for method B. var DefaultKey = Key{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} +// TODO(maruel): Move to Opts pattern. Even golint complains about this. type config struct { defaultTimeout time.Duration beforeCall func() @@ -51,7 +52,7 @@ func WithTimeout(timeout time.Duration) configF { } } -// WithSyncsets the synchronization for the entire device, using internal mutex. +// WithSync sets the synchronization for the entire device, using internal mutex. func WithSync() configF { var mu sync.Mutex return func(c *config) *config { diff --git a/experimental/devices/mpu9250/accelerometer/accelerometer.go b/experimental/devices/mpu9250/accelerometer/accelerometer.go index 20df17f..ab0feee 100644 --- a/experimental/devices/mpu9250/accelerometer/accelerometer.go +++ b/experimental/devices/mpu9250/accelerometer/accelerometer.go @@ -1,27 +1,25 @@ package accelerometer +// Valid accelerator values. const ( ACCEL_FS_SEL_2G = 0 ACCEL_FS_SEL_4G = 8 ACCEL_FS_SEL_8G = 0x10 ACCEL_FS_SEL_16G = 0x18 - - ACCEL_FS_SENS_2G = 2.0 / 32768.0 - ACCEL_FS_SENS_4G = 4.0 / 32768.0 - ACCEL_FS_SENS_8G = 8.0 / 32768.0 - ACCEL_FS_SENS_16G = 16.0 / 32768.0 ) +// Sensitivity returns the sensitivity as float32. func Sensitivity(selector int) float32 { switch selector { + default: + fallthrough case ACCEL_FS_SEL_2G: - return ACCEL_FS_SENS_2G + return 2.0 / 32768.0 case ACCEL_FS_SEL_4G: - return ACCEL_FS_SENS_4G + return 4.0 / 32768.0 case ACCEL_FS_SEL_8G: - return ACCEL_FS_SENS_8G + return 8.0 / 32768.0 case ACCEL_FS_SEL_16G: - return ACCEL_FS_SENS_16G + return 16.0 / 32768.0 } - return ACCEL_FS_SENS_2G } diff --git a/experimental/devices/mpu9250/mpu9250.go b/experimental/devices/mpu9250/mpu9250.go index abdbc44..04fe1b5 100644 --- a/experimental/devices/mpu9250/mpu9250.go +++ b/experimental/devices/mpu9250/mpu9250.go @@ -82,15 +82,11 @@ func (m *MPU9250) Debug(f DebugF) { // Init initializes the device func (m *MPU9250) Init() error { - if err := m.transferBatch(initSequence, "error initializing %d: [%x:%x] => %v"); err != nil { - return err - } - return nil + return m.transferBatch(initSequence, "error initializing %d: [%x:%x] => %v") } // Calibrate Calibrates the device using maximum precision for both Gyroscope and Accelerometer. func (m *MPU9250) Calibrate() error { - if err := m.transferBatch(calibrateSequence, "error calibrating %d: [%x:%x] => %v"); err != nil { return err } diff --git a/experimental/devices/mpu9250/reg/reg.go b/experimental/devices/mpu9250/reg/reg.go index f4446ef..769f5b3 100644 --- a/experimental/devices/mpu9250/reg/reg.go +++ b/experimental/devices/mpu9250/reg/reg.go @@ -1,5 +1,8 @@ package reg +// Unsorted mix of register addresses, commands and constants. +// +// TODO(maruel): Needs to be redone. const ( MPU9250_DEFAULT_ADDRESS = 0xD1 MPU9250_ALT_DEFAULT_ADDRESS = 0xD2 diff --git a/experimental/devices/mpu9250/spi.go b/experimental/devices/mpu9250/spi.go index 15c7cdf..713e352 100644 --- a/experimental/devices/mpu9250/spi.go +++ b/experimental/devices/mpu9250/spi.go @@ -49,10 +49,7 @@ func (s *SpiTransport) writeByte(address byte, value byte) error { if err := s.device.Tx(buf[:], res[:]); err != nil { return err } - if err := s.cs.Out(gpio.High); err != nil { - return err - } - return nil + return s.cs.Out(gpio.High) } func (s *SpiTransport) writeMagReg(address byte, value byte) error { @@ -104,7 +101,7 @@ func (s *SpiTransport) readByte(address byte) (byte, error) { func (s *SpiTransport) readUint16(address ...byte) (uint16, error) { if len(address) != 2 { - return 0, fmt.Errorf("Only 2 bytes per read") + return 0, fmt.Errorf("only 2 bytes per read") } h, err := s.readByte(address[0]) if err != nil { diff --git a/experimental/devices/pca9548/pca9548.go b/experimental/devices/pca9548/pca9548.go index 98bb0d9..52df359 100644 --- a/experimental/devices/pca9548/pca9548.go +++ b/experimental/devices/pca9548/pca9548.go @@ -42,7 +42,7 @@ type Dev struct { // New creates a new handle to a pca9548 I²C multiplexer. func New(bus i2c.Bus, opts *Opts) (*Dev, error) { if opts.Addr < 0x70 || opts.Addr > 0x77 { - return nil, errors.New("Address outside valid range of 0x70-0x77") + return nil, errors.New("address outside valid range of 0x70-0x77") } d := &Dev{ c: bus, diff --git a/experimental/devices/rainbowhat/rainbowhat.go b/experimental/devices/rainbowhat/rainbowhat.go index 893ecf4..efbab13 100644 --- a/experimental/devices/rainbowhat/rainbowhat.go +++ b/experimental/devices/rainbowhat/rainbowhat.go @@ -176,9 +176,5 @@ func (d *Dev) Halt() error { return err } - if err := d.buttonC.Halt(); err != nil { - return err - } - - return nil + return d.buttonC.Halt() }