Skip to content

Commit 8cd8bbb

Browse files
committed
i2c(mcp23017): fix missing code for halt is idempotent
1 parent 888810e commit 8cd8bbb

File tree

3 files changed

+62
-62
lines changed

3 files changed

+62
-62
lines changed

drivers/i2c/adafruit2327_driver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ func NewAdafruit2327Driver(c Connector, options ...func(Config)) *Adafruit2327Dr
3434
}
3535

3636
// SetServoMotorFreq sets the frequency for the currently addressed PWM Servo HAT.
37-
func (a *Adafruit2327Driver) SetServoMotorFreq(freq float64) error {
38-
return a.SetPWMFreq(float32(freq))
37+
func (d *Adafruit2327Driver) SetServoMotorFreq(freq float64) error {
38+
return d.SetPWMFreq(float32(freq))
3939
}
4040

4141
// SetServoMotorPulse is a convenience function to specify the 'tick' value,
4242
// between 0-4095, when the signal will turn on, and when it will turn off.
43-
func (a *Adafruit2327Driver) SetServoMotorPulse(channel byte, on, off int32) error {
44-
return a.SetPWM(int(channel), uint16(on), uint16(off)) //nolint:gosec // TODO: fix later
43+
func (d *Adafruit2327Driver) SetServoMotorPulse(channel byte, on, off int32) error {
44+
return d.SetPWM(int(channel), uint16(on), uint16(off)) //nolint:gosec // TODO: fix later
4545
}

drivers/i2c/mcp23017_driver.go

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -234,68 +234,68 @@ func WithMCP23017AutoIODirOff(val uint8) func(Config) {
234234
// SetPinMode set pin mode of a given pin immediately, based on the value:
235235
// val = 0 output
236236
// val = 1 input
237-
func (m *MCP23017Driver) SetPinMode(pin uint8, portStr string, val uint8) error {
238-
m.mutex.Lock()
239-
defer m.mutex.Unlock()
237+
func (d *MCP23017Driver) SetPinMode(pin uint8, portStr string, val uint8) error {
238+
d.mutex.Lock()
239+
defer d.mutex.Unlock()
240240

241-
selectedPort := m.getPort(portStr)
241+
selectedPort := d.getPort(portStr)
242242
// Set IODIR register bit for given pin to an output/input.
243-
return m.write(selectedPort.IODIR, pin, bitState(val))
243+
return d.writePin(selectedPort.IODIR, pin, bitState(val))
244244
}
245245

246246
// SetPullUp sets the pull up state of a given pin immediately, based on the value:
247247
// val = 1 pull up enabled.
248248
// val = 0 pull up disabled.
249-
func (m *MCP23017Driver) SetPullUp(pin uint8, portStr string, val uint8) error {
250-
m.mutex.Lock()
251-
defer m.mutex.Unlock()
249+
func (d *MCP23017Driver) SetPullUp(pin uint8, portStr string, val uint8) error {
250+
d.mutex.Lock()
251+
defer d.mutex.Unlock()
252252

253-
selectedPort := m.getPort(portStr)
254-
return m.write(selectedPort.GPPU, pin, bitState(val))
253+
selectedPort := d.getPort(portStr)
254+
return d.writePin(selectedPort.GPPU, pin, bitState(val))
255255
}
256256

257257
// SetGPIOPolarity will change a given pin's polarity immediately, based on the value:
258258
// val = 1 opposite logic state of the input pin.
259259
// val = 0 same logic state of the input pin.
260-
func (m *MCP23017Driver) SetGPIOPolarity(pin uint8, portStr string, val uint8) error {
261-
m.mutex.Lock()
262-
defer m.mutex.Unlock()
260+
func (d *MCP23017Driver) SetGPIOPolarity(pin uint8, portStr string, val uint8) error {
261+
d.mutex.Lock()
262+
defer d.mutex.Unlock()
263263

264-
selectedPort := m.getPort(portStr)
265-
return m.write(selectedPort.IPOL, pin, bitState(val))
264+
selectedPort := d.getPort(portStr)
265+
return d.writePin(selectedPort.IPOL, pin, bitState(val))
266266
}
267267

268268
// WriteGPIO writes a value to a gpio pin (0-7) and a port (A or B).
269-
func (m *MCP23017Driver) WriteGPIO(pin uint8, portStr string, val uint8) error {
270-
m.mutex.Lock()
271-
defer m.mutex.Unlock()
269+
func (d *MCP23017Driver) WriteGPIO(pin uint8, portStr string, val uint8) error {
270+
d.mutex.Lock()
271+
defer d.mutex.Unlock()
272272

273-
selectedPort := m.getPort(portStr)
274-
if !m.mcpBehav.autoIODirOff {
273+
selectedPort := d.getPort(portStr)
274+
if !d.mcpBehav.autoIODirOff {
275275
// Set IODIR register bit for given pin to an output by clearing bit.
276276
// can't call SetPinMode() because mutex will cause deadlock
277-
if err := m.write(selectedPort.IODIR, pin, clearBit); err != nil {
277+
if err := d.writePin(selectedPort.IODIR, pin, clearBit); err != nil {
278278
return err
279279
}
280280
}
281281
// write value to OLAT register bit
282-
return m.write(selectedPort.OLAT, pin, bitState(val))
282+
return d.writePin(selectedPort.OLAT, pin, bitState(val))
283283
}
284284

285285
// ReadGPIO reads a value from a given gpio pin (0-7) and a port (A or B).
286-
func (m *MCP23017Driver) ReadGPIO(pin uint8, portStr string) (uint8, error) {
287-
m.mutex.Lock()
288-
defer m.mutex.Unlock()
286+
func (d *MCP23017Driver) ReadGPIO(pin uint8, portStr string) (uint8, error) {
287+
d.mutex.Lock()
288+
defer d.mutex.Unlock()
289289

290-
selectedPort := m.getPort(portStr)
291-
if !m.mcpBehav.autoIODirOff {
290+
selectedPort := d.getPort(portStr)
291+
if !d.mcpBehav.autoIODirOff {
292292
// Set IODIR register bit for given pin to an input by set bit.
293293
// can't call SetPinMode() because mutex will cause deadlock
294-
if err := m.write(selectedPort.IODIR, pin, setBit); err != nil {
294+
if err := d.writePin(selectedPort.IODIR, pin, setBit); err != nil {
295295
return 0, err
296296
}
297297
}
298-
val, err := m.read(selectedPort.GPIO)
298+
val, err := d.readReg(selectedPort.GPIO)
299299
if err != nil {
300300
return val, err
301301
}
@@ -306,19 +306,19 @@ func (m *MCP23017Driver) ReadGPIO(pin uint8, portStr string) (uint8, error) {
306306
return val, nil
307307
}
308308

309-
func (m *MCP23017Driver) initialize() error {
309+
func (d *MCP23017Driver) initialize() error {
310310
// Set IOCON register with MCP23017 configuration.
311-
ioconReg := m.getPort("A").IOCON // IOCON address is the same for Port A or B.
312-
ioconVal := m.mcpConf.getUint8Value()
311+
ioconReg := d.getPort("A").IOCON // IOCON address is the same for Port A or B.
312+
ioconVal := d.mcpConf.getUint8Value()
313313

314-
_, err := m.connection.Write([]uint8{ioconReg, ioconVal})
314+
_, err := d.write([]uint8{ioconReg, ioconVal})
315315
return err
316316
}
317317

318318
// write gets the value of the passed in register, and then sets the bit specified
319319
// by the pin to the given state.
320-
func (m *MCP23017Driver) write(reg uint8, pin uint8, state bitState) error {
321-
valOrg, err := m.read(reg)
320+
func (d *MCP23017Driver) writePin(reg uint8, pin uint8, state bitState) error {
321+
valOrg, err := d.readReg(reg)
322322
if err != nil {
323323
return fmt.Errorf("MCP write-read: %v", err)
324324
}
@@ -330,58 +330,58 @@ func (m *MCP23017Driver) write(reg uint8, pin uint8, state bitState) error {
330330
val = uint8(bit.Set(int(valOrg), pin)) //nolint:gosec // TODO: fix later
331331
}
332332

333-
if val != valOrg || m.mcpBehav.forceRefresh {
333+
if val != valOrg || d.mcpBehav.forceRefresh {
334334
if mcp23017Debug {
335335
log.Printf("write done: MCP forceRefresh: %t, address: 0x%X, register: 0x%X, name: %s, value: 0x%X\n",
336-
m.mcpBehav.forceRefresh, m.GetAddressOrDefault(mcp23017DefaultAddress), reg, m.getRegName(reg), val)
336+
d.mcpBehav.forceRefresh, d.GetAddressOrDefault(mcp23017DefaultAddress), reg, d.getRegName(reg), val)
337337
}
338-
if err := m.connection.WriteByteData(reg, val); err != nil {
338+
if err := d.writeByteData(reg, val); err != nil {
339339
return fmt.Errorf("MCP write-WriteByteData(reg=%d,val=%d): %v", reg, val, err)
340340
}
341341
} else if mcp23017Debug {
342342
log.Printf("write skipped: MCP forceRefresh: %t, address: 0x%X, register: 0x%X, name: %s, value: 0x%X\n",
343-
m.mcpBehav.forceRefresh, m.GetAddressOrDefault(mcp23017DefaultAddress), reg, m.getRegName(reg), val)
343+
d.mcpBehav.forceRefresh, d.GetAddressOrDefault(mcp23017DefaultAddress), reg, d.getRegName(reg), val)
344344
}
345345
return nil
346346
}
347347

348348
// read get the data from a given register
349349
// it is mainly a wrapper to create additional debug messages, when activated
350-
func (m *MCP23017Driver) read(reg uint8) (uint8, error) {
351-
val, err := m.connection.ReadByteData(reg)
350+
func (d *MCP23017Driver) readReg(reg uint8) (uint8, error) {
351+
val, err := d.readByteData(reg)
352352
if err != nil {
353353
return val, fmt.Errorf("MCP write-ReadByteData(reg=%d): %v", reg, err)
354354
}
355355
if mcp23017Debug {
356356
log.Printf("reading done: MCP autoIODirOff: %t, address: 0x%X, register:0x%X, name: %s, value: 0x%X\n",
357-
m.mcpBehav.autoIODirOff, m.GetAddressOrDefault(mcp23017DefaultAddress), reg, m.getRegName(reg), val)
357+
d.mcpBehav.autoIODirOff, d.GetAddressOrDefault(mcp23017DefaultAddress), reg, d.getRegName(reg), val)
358358
}
359359
return val, nil
360360
}
361361

362362
// getPort return the port (A or B) given a string and the bank.
363363
// Port A is the default if an incorrect or no port is specified.
364-
func (m *MCP23017Driver) getPort(portStr string) port {
364+
func (d *MCP23017Driver) getPort(portStr string) port {
365365
portStr = strings.ToUpper(portStr)
366366
switch portStr {
367367
case "A":
368-
return mcp23017GetBank(m.mcpConf.bank).portA
368+
return mcp23017GetBank(d.mcpConf.bank).portA
369369
case "B":
370-
return mcp23017GetBank(m.mcpConf.bank).portB
370+
return mcp23017GetBank(d.mcpConf.bank).portB
371371
default:
372-
return mcp23017GetBank(m.mcpConf.bank).portA
372+
return mcp23017GetBank(d.mcpConf.bank).portA
373373
}
374374
}
375375

376376
// getUint8Value returns the configuration data as a packed value.
377-
func (mc *mcp23017Config) getUint8Value() uint8 {
378-
return mc.bank<<7 | mc.mirror<<6 | mc.seqop<<5 | mc.disslw<<4 | mc.haen<<3 | mc.odr<<2 | mc.intpol<<1
377+
func (dc *mcp23017Config) getUint8Value() uint8 {
378+
return dc.bank<<7 | dc.mirror<<6 | dc.seqop<<5 | dc.disslw<<4 | dc.haen<<3 | dc.odr<<2 | dc.intpol<<1
379379
}
380380

381381
// getRegName returns the name of the given register related to the configured bank
382382
// and can be used to write nice debug messages
383-
func (m *MCP23017Driver) getRegName(reg uint8) string {
384-
b := mcp23017GetBank(m.mcpConf.bank)
383+
func (d *MCP23017Driver) getRegName(reg uint8) string {
384+
b := mcp23017GetBank(d.mcpConf.bank)
385385
portStr := "A"
386386
regStr := "unknown"
387387

drivers/i2c/mcp23017_driver_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,34 +550,34 @@ func TestMCP23017_write(t *testing.T) {
550550
// clear bit
551551
d, _ := initTestMCP23017WithStubbedAdaptor(0)
552552
port := d.getPort("A")
553-
err := d.write(port.IODIR, uint8(7), 0)
553+
err := d.writePin(port.IODIR, uint8(7), 0)
554554
require.NoError(t, err)
555555

556556
// set bit
557557
d, _ = initTestMCP23017WithStubbedAdaptor(0)
558558
port = d.getPort("B")
559-
err = d.write(port.IODIR, uint8(7), 1)
559+
err = d.writePin(port.IODIR, uint8(7), 1)
560560
require.NoError(t, err)
561561

562562
// write error
563563
d, a := initTestMCP23017WithStubbedAdaptor(0)
564564
a.i2cWriteImpl = func([]byte) (int, error) {
565565
return 0, errors.New("write error")
566566
}
567-
err = d.write(port.IODIR, uint8(7), 0)
567+
err = d.writePin(port.IODIR, uint8(7), 0)
568568
require.ErrorContains(t, err, "MCP write-read: MCP write-ReadByteData(reg=1): write error")
569569

570570
// read error
571571
d, a = initTestMCP23017WithStubbedAdaptor(0)
572572
a.i2cReadImpl = func(b []byte) (int, error) {
573573
return len(b), errors.New("read error")
574574
}
575-
err = d.write(port.IODIR, uint8(7), 0)
575+
err = d.writePin(port.IODIR, uint8(7), 0)
576576
require.ErrorContains(t, err, "MCP write-read: MCP write-ReadByteData(reg=1): read error")
577577
a.i2cReadImpl = func(b []byte) (int, error) {
578578
return len(b), nil
579579
}
580-
err = d.write(port.IODIR, uint8(7), 1)
580+
err = d.writePin(port.IODIR, uint8(7), 1)
581581
require.NoError(t, err)
582582
}
583583

@@ -589,7 +589,7 @@ func TestMCP23017_read(t *testing.T) {
589589
copy(b, []byte{255})
590590
return 1, nil
591591
}
592-
val, _ := d.read(port.IODIR)
592+
val, _ := d.readReg(port.IODIR)
593593
assert.Equal(t, uint8(255), val)
594594

595595
// read error
@@ -598,7 +598,7 @@ func TestMCP23017_read(t *testing.T) {
598598
return len(b), errors.New("read error")
599599
}
600600

601-
val, err := d.read(port.IODIR)
601+
val, err := d.readReg(port.IODIR)
602602
assert.Equal(t, uint8(0), val)
603603
require.ErrorContains(t, err, "MCP write-ReadByteData(reg=0): read error")
604604

@@ -609,7 +609,7 @@ func TestMCP23017_read(t *testing.T) {
609609
copy(b, []byte{255})
610610
return 1, nil
611611
}
612-
val, _ = d.read(port.IODIR)
612+
val, _ = d.readReg(port.IODIR)
613613
assert.Equal(t, uint8(255), val)
614614
}
615615

0 commit comments

Comments
 (0)