Reduce number of errcheck lint messages from 117 to 2.

- Ran github.com/kisielk/errcheck on the code base, and either trapped errors or
  marked them as ignored explicitly. The stats was calculated via:
    errcheck ./... | grep -v defer | wc -l
- Stop registering gpio.INVALID, it can't be registered.
- Fix a go vet issue in cap1188.
pull/1/head
Marc-Antoine Ruel 9 years ago
parent 68f531c751
commit 089b8dccab

@ -42,7 +42,9 @@ func (s *SmokeTest) Run(f *flag.FlagSet, args []string) (err error) {
i2cAddr := f.Uint("ia", 0x76, "I²C bus address to use; either 0x76 (BMx280, the default) or 0x77 (BMP180)")
spiID := f.String("spi", "", "SPI port to use")
record := f.Bool("r", false, "record operation (for playback unit testing)")
f.Parse(args)
if err := f.Parse(args); err != nil {
return err
}
if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments")

@ -31,15 +31,16 @@ import (
"periph.io/x/periph/devices"
)
// New returns an object that communicates over 1-wire to the DS18B20 sensor with the
// specified 64-bit address.
// New returns an object that communicates over 1-wire to the DS18B20 sensor
// with the specified 64-bit address.
//
// resolutionBits must be in the range 9..12 and determines how many bits of precision
// the readings have. The resolution affects the conversion time: 9bits:94ms, 10bits:188ms,
// 11bits:375ms, 12bits:750ms.
// resolutionBits must be in the range 9..12 and determines how many bits of
// precision the readings have. The resolution affects the conversion time:
// 9bits:94ms, 10bits:188ms, 11bits:375ms, 12bits:750ms.
//
// A resolution of 10 bits corresponds to 0.25C and tends to be a good compromise between
// conversion time and the device's inherent accuracy of +/-0.5C.
// A resolution of 10 bits corresponds to 0.25C and tends to be a good
// compromise between conversion time and the device's inherent accuracy of
// +/-0.5C.
func New(o onewire.Bus, addr onewire.Address, resolutionBits int) (*Dev, error) {
if resolutionBits < 9 || resolutionBits > 12 {
return nil, errors.New("ds18b20: invalid resolutionBits")
@ -47,8 +48,8 @@ func New(o onewire.Bus, addr onewire.Address, resolutionBits int) (*Dev, error)
d := &Dev{onewire: onewire.Dev{Bus: o, Addr: addr}, resolution: resolutionBits}
// Start by reading the scratchpad memory, this will tell us whether we can talk to the
// device correctly and also how it's configured.
// Start by reading the scratchpad memory, this will tell us whether we can
// talk to the device correctly and also how it's configured.
spad, err := d.readScratchpad()
if err != nil {
return nil, err
@ -57,10 +58,14 @@ func New(o onewire.Bus, addr onewire.Address, resolutionBits int) (*Dev, error)
// Change the resolution, if necessary (datasheet p.6).
if int(spad[4]>>5) != resolutionBits-9 {
// Set the value in the configuration register.
d.onewire.Tx([]byte{0x4e, 0, 0, byte((resolutionBits-9)<<5) | 0x1f}, nil)
if err := d.onewire.Tx([]byte{0x4e, 0, 0, byte((resolutionBits-9)<<5) | 0x1f}, nil); err != nil {
return nil, err
}
// Copy the scratchpad to EEPROM to save the values.
d.onewire.TxPower([]byte{0x48}, nil)
// Wait for the write to complete
if err := d.onewire.TxPower([]byte{0x48}, nil); err != nil {
return nil, err
}
// Wait for the write to complete.
time.Sleep(10 * time.Millisecond)
}
@ -69,13 +74,13 @@ func New(o onewire.Bus, addr onewire.Address, resolutionBits int) (*Dev, error)
// ConvertAll performs a conversion on all DS18B20 devices on the bus.
//
// During the conversion it places the bus in strong pull-up mode to
// power parasitic devices and returns when the conversions have
// completed. This time period is determined by the maximum
// resolution of all devices on the bus and must be provided.
// During the conversion it places the bus in strong pull-up mode to power
// parasitic devices and returns when the conversions have completed. This time
// period is determined by the maximum resolution of all devices on the bus and
// must be provided.
//
// ConvertAll uses time.Sleep to wait for the conversion to finish,
// which takes from 94ms to 752ms.
// ConvertAll uses time.Sleep to wait for the conversion to finish, which takes
// from 94ms to 752ms.
func ConvertAll(o onewire.Bus, maxResolutionBits int) error {
if maxResolutionBits < 9 || maxResolutionBits > 12 {
return errors.New("ds18b20: invalid maxResolutionBits")
@ -89,7 +94,8 @@ func ConvertAll(o onewire.Bus, maxResolutionBits int) error {
//===== Dev
// Dev is a handle to a Dallas Semi / Maxim DS18B20 temperature sensor on a 1-wire bus.
// Dev is a handle to a Dallas Semi / Maxim DS18B20 temperature sensor on a
// 1-wire bus.
type Dev struct {
onewire onewire.Dev // device on 1-wire bus
resolution int // resolution in bits (9..12)
@ -113,7 +119,9 @@ func (d *Dev) Temperature() (devices.Celsius, error) {
return d.LastTemp()
}
// LastTemp reads the temperature resulting from the last conversion from the device.
// LastTemp reads the temperature resulting from the last conversion from the
// device.
//
// It is useful in combination with ConvertAll.
func (d *Dev) LastTemp() (devices.Celsius, error) {
// Read the scratchpad memory.
@ -122,15 +130,15 @@ func (d *Dev) LastTemp() (devices.Celsius, error) {
return 0, err
}
// spad[1] is MSB, spad[0] is LSB and has 4 fractional bits. Need to do sign extension
// multiply by 1000 to get devices.Millis, divide by 16 due to 4 fractional bits.
// Datasheet p.4.
// spad[1] is MSB, spad[0] is LSB and has 4 fractional bits. Need to do sign
// extension multiply by 1000 to get devices.Millis, divide by 16 due to 4
// fractional bits. Datasheet p.4.
c := (devices.Celsius(int8(spad[1]))<<8 + devices.Celsius(spad[0])) * 1000 / 16
// The device powers up with a value of 85°C, so if we read that odds are very high
// that either no conversion was performed or that the conversion failed due to lack of
// power. This prevents reading a temp of exactly 85°C, but that seems like the right
// tradeoff.
// The device powers up with a value of 85°C, so if we read that odds are
// very high that either no conversion was performed or that the conversion
// failed due to lack of power. This prevents reading a temp of exactly 85°C,
// but that seems like the right tradeoff.
if c == 85000 {
return 0, busError("ds18b20: has not performed a temperature conversion (insufficient pull-up?)")
}

@ -37,7 +37,7 @@ func New() (*Conn, error) {
c := &Conn{w: w, c: make(chan ir.Message), list: map[string][]string{}}
// Unconditionally retrieve the list of all known keys at start.
if _, err := w.Write([]byte("LIST\n")); err != nil {
w.Close()
_ = w.Close()
return nil, err
}
go c.loop(bufio.NewReader(w))

@ -62,7 +62,9 @@ func (s *SmokeTest) Run(f *flag.FlagSet, args []string) (err error) {
rotated := f.Bool("rotated", false, "Rotate the displays by 180°")
record := f.Bool("record", false, "record operation (for playback unit testing)")
f.Parse(args)
if err := f.Parse(args); err != nil {
return err
}
if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments")

@ -79,7 +79,9 @@ func (d *Dev) SetBrightness(b Brightness) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
d.start()
d.writeByte(byte(b))
if _, err := d.writeByte(byte(b)); err != nil {
return err
}
d.stop()
return nil
}
@ -104,15 +106,23 @@ func (d *Dev) Write(seg []byte) (int, error) {
// Use auto-incrementing address. It is possible to write to a single
// segment but there isn't much point.
d.start()
d.writeByte(0x40)
if _, err := d.writeByte(0x40); err != nil {
return 0, err
}
d.stop()
d.start()
d.writeByte(0xC0)
if _, err := d.writeByte(0xC0); err != nil {
return 0, err
}
for i := 0; i < 6; i++ {
if len(seg) <= i {
d.writeByte(0)
if _, err := d.writeByte(0); err != nil {
return i, err
}
} else {
d.writeByte(seg[i])
if _, err := d.writeByte(seg[i]); err != nil {
return i, err
}
}
}
d.stop()
@ -153,16 +163,16 @@ var digitToSegment = []byte{
}
func (d *Dev) start() {
d.data.Out(gpio.Low)
_ = d.data.Out(gpio.Low)
d.sleepHalfCycle()
d.clk.Out(gpio.Low)
_ = d.clk.Out(gpio.Low)
}
func (d *Dev) stop() {
d.sleepHalfCycle()
d.clk.Out(gpio.High)
_ = d.clk.Out(gpio.High)
d.sleepHalfCycle()
d.data.Out(gpio.High)
_ = d.data.Out(gpio.High)
d.sleepHalfCycle()
}
@ -171,27 +181,27 @@ func (d *Dev) stop() {
func (d *Dev) writeByte(b byte) (bool, error) {
for i := 0; i < 8; i++ {
// LSB (!)
d.data.Out(b&(1<<byte(i)) != 0)
_ = d.data.Out(b&(1<<byte(i)) != 0)
d.sleepHalfCycle()
d.clk.Out(gpio.High)
_ = d.clk.Out(gpio.High)
d.sleepHalfCycle()
d.clk.Out(gpio.Low)
_ = d.clk.Out(gpio.Low)
}
// 9th clock is ACK.
d.data.Out(gpio.Low)
_ = d.data.Out(gpio.Low)
time.Sleep(clockHalfCycle)
// TODO(maruel): Add.
//if err := d.data.In(gpio.PullUp, gpio.NoEdge); err != nil {
// return false, err
//}
d.clk.Out(gpio.High)
_ = d.clk.Out(gpio.High)
d.sleepHalfCycle()
//ack := d.data.Read() == gpio.Low
//d.sleepHalfCycle()
//if err := d.data.Out(); err != nil {
// return false, err
//}
d.clk.Out(gpio.Low)
_ = d.clk.Out(gpio.Low)
return true, nil
}

@ -150,9 +150,9 @@ func New(clk gpio.PinIO, data gpio.PinIO, speedHz int) (*I2C, error) {
func (i *I2C) start() {
// Page 9, section 3.1.4 START and STOP conditions
// In multi-master mode, it would have to sense SDA first and after the sleep.
i.sda.Out(gpio.Low)
_ = i.sda.Out(gpio.Low)
i.sleepHalfCycle()
i.scl.Out(gpio.Low)
_ = i.scl.Out(gpio.Low)
}
// "When CLK is a high level and DIO changes from low level to high level, data
@ -161,11 +161,11 @@ func (i *I2C) start() {
// Lasts 3/2 cycle.
func (i *I2C) stop() {
// Page 9, section 3.1.4 START and STOP conditions
i.scl.Out(gpio.Low)
_ = i.scl.Out(gpio.Low)
i.sleepHalfCycle()
i.scl.Out(gpio.High)
_ = i.scl.Out(gpio.High)
i.sleepHalfCycle()
i.sda.Out(gpio.High)
_ = i.sda.Out(gpio.High)
// TODO(maruel): This sleep could be skipped, assuming we wait for the next
// transfer if too quick to happen.
i.sleepHalfCycle()
@ -184,13 +184,13 @@ func (i *I2C) writeByte(b byte) (bool, error) {
// clock."
// Page 10, section 3.1.5 Byte format
for x := 0; x < 8; x++ {
i.sda.Out(b&byte(1<<byte(7-x)) != 0)
_ = i.sda.Out(b&byte(1<<byte(7-x)) != 0)
i.sleepHalfCycle()
// Let the device read SDA.
// TODO(maruel): Support clock stretching, the device may keep the line low.
i.scl.Out(gpio.High)
_ = i.scl.Out(gpio.High)
i.sleepHalfCycle()
i.scl.Out(gpio.Low)
_ = i.scl.Out(gpio.Low)
}
// Page 10, section 3.1.6 ACK and NACK
// 9th clock is ACK.
@ -233,18 +233,18 @@ func (i *I2C) readByte() (byte, error) {
for x := 0; x < 8; x++ {
i.sleepHalfCycle()
// TODO(maruel): Support clock stretching, the device may keep the line low.
i.scl.Out(gpio.High)
_ = i.scl.Out(gpio.High)
i.sleepHalfCycle()
if i.sda.Read() == gpio.High {
b |= byte(1) << byte(7-x)
}
i.scl.Out(gpio.Low)
_ = i.scl.Out(gpio.Low)
}
if err := i.sda.Out(gpio.Low); err != nil {
return 0, err
}
i.sleepHalfCycle()
i.scl.Out(gpio.High)
_ = i.scl.Out(gpio.High)
i.sleepHalfCycle()
return b, nil
}

@ -98,23 +98,23 @@ func (s *SPI) Tx(w, r []byte) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.csn != nil {
s.csn.Out(gpio.Low)
_ = s.csn.Out(gpio.Low)
s.sleepHalfCycle()
}
for i := uint(0); i < uint(len(w)*8); i++ {
s.sdo.Out(w[i/8]&(1<<(i%8)) != 0)
_ = s.sdo.Out(w[i/8]&(1<<(i%8)) != 0)
s.sleepHalfCycle()
s.sck.Out(gpio.Low)
_ = s.sck.Out(gpio.Low)
s.sleepHalfCycle()
if len(r) != 0 {
if s.sdi.Read() == gpio.High {
r[i/8] |= 1 << (i % 8)
}
}
s.sck.Out(gpio.Low)
_ = s.sck.Out(gpio.Low)
}
if s.csn != nil {
s.csn.Out(gpio.High)
_ = s.csn.Out(gpio.High)
}
return nil
}

@ -66,15 +66,15 @@ const (
)
const (
// reg_LEDLinking - The Sensor Input LED Linking register controls whether a
// regLEDLinking - The Sensor Input LED Linking register controls whether a
// capacitive touch sensor input is linked to an LED output. If the
// corresponding bit is set, then the appropriate LED output will change
// states defined by the LED Behavior controls in response to the capacitive
// touch sensor input.
reg_LEDLinking = 0x72
// reg_LEDOutputControl - The LED Output Control Register controls the output
regLEDLinking = 0x72
// regLEDOutputControl - The LED Output Control Register controls the output
// state of the LED pins that are not linked to sensor inputs.
reg_LEDOutputControl = 0x74
regLEDOutputControl = 0x74
)
// Dev is a handle to a cap1188.
@ -156,11 +156,11 @@ func (d *Dev) InputStatus() ([]TouchStatus, error) {
// Doing so, disabled the option for the host to set specific LEDs on/off.
func (d *Dev) LinkLEDs(on bool) error {
if on {
if err := d.regWrapper.WriteUint8(reg_LEDLinking, 0xff); err != nil {
if err := d.regWrapper.WriteUint8(regLEDLinking, 0xff); err != nil {
return wrap(fmt.Errorf("failed to link LEDs - %s", err))
}
} else {
if err := d.regWrapper.WriteUint8(reg_LEDLinking, 0x00); err != nil {
if err := d.regWrapper.WriteUint8(regLEDLinking, 0x00); err != nil {
return wrap(fmt.Errorf("failed to unlink LEDs - %s", err))
}
}
@ -176,10 +176,10 @@ func (d *Dev) AllLEDs(on bool) error {
return wrap(fmt.Errorf("can't manually set LEDs when they are linked to sensors"))
}
if on {
return d.regWrapper.WriteUint8(reg_LEDOutputControl, 0xff)
return d.regWrapper.WriteUint8(regLEDOutputControl, 0xff)
}
return d.regWrapper.WriteUint8(reg_LEDOutputControl, 0x00)
return d.regWrapper.WriteUint8(regLEDOutputControl, 0x00)
}
// SetLED sets the state of a LED as on or off
@ -195,33 +195,35 @@ func (d *Dev) SetLED(idx int, state bool) error {
log.Printf("Set LED state %d - %t\n", idx, state)
}
if state {
return d.setBit(reg_LEDOutputControl, idx)
return d.setBit(regLEDOutputControl, idx)
}
return d.clearBit(reg_LEDOutputControl, idx)
return d.clearBit(regLEDOutputControl, idx)
}
// Reset issues a soft reset to the device using the reset pin
// if available.
func (d *Dev) Reset() (err error) {
d.ClearInterrupt()
func (d *Dev) Reset() error {
if err := d.ClearInterrupt(); err != nil {
return err
}
if d != nil && d.ResetPin != nil {
if d.Debug {
log.Println("cap1188: Resetting the device using the reset pin")
}
if err = d.ResetPin.Out(gpio.Low); err != nil {
if err := d.ResetPin.Out(gpio.Low); err != nil {
return err
}
time.Sleep(1 * time.Microsecond)
if err = d.ResetPin.Out(gpio.High); err != nil {
if err := d.ResetPin.Out(gpio.High); err != nil {
return err
}
time.Sleep(10 * time.Millisecond)
if err = d.ResetPin.Out(gpio.Low); err != nil {
if err := d.ResetPin.Out(gpio.Low); err != nil {
return err
}
}
// track the reset time since the device won't be ready for up to 15ms
// and won't be ready for first conversion for up to 200ms
// Track the reset time since the device won't be ready for up to 15ms
// and won't be ready for first conversion for up to 200ms.
d.resetAt = time.Now()
return nil

Loading…
Cancel
Save