Skip to content

Commit

Permalink
Merge pull request #8 from pimoroni/feature/switch-counters
Browse files Browse the repository at this point in the history
Add method to clear a switch counter
  • Loading branch information
Gadgetoid committed Jul 8, 2021
2 parents 937db78 + af01d40 commit c577ca8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions library/ioexpander/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,17 @@ def read_switch_counter(self, pin):
# The most significant bit encodes the current GPIO state
return value & 0x7f, value & 0x80 == 0x80

def clear_switch_counter(self, pin):
"""Clear the switch count value on a pin to 0."""
io_pin = self.get_pin(pin)

if io_pin.port not in (0, 1):
raise ValueError("Pin {} does not support switch counting.".format(pin))

sw_reg = [REG_SWITCH_P00, REG_SWITCH_P10][io_pin.port] + io_pin.pin

self.i2c_write8(sw_reg, 0)

def setup_rotary_encoder(self, channel, pin_a, pin_b, pin_c=None, count_microsteps=False):
"""Set up a rotary encoder."""
channel -= 1
Expand Down
5 changes: 5 additions & 0 deletions library/tests/test_switch_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def test_read_switch_counter(smbus2, ioe):
assert ioe.read_switch_counter(1) == (2, False)


def test_clear_switch_counter(smbus2, ioe):
ioe.clear_switch_counter(1)
assert smbus2.SMBus(1).i2c_rdwr.called_once()


def test_switch_counter_invalid_pin(smbus2, ioe):
with pytest.raises(ValueError):
ioe.setup_switch_counter(10)
Expand Down

0 comments on commit c577ca8

Please sign in to comment.