|
79 | 79 | MPR121_SOFTRESET = const(0x80)
|
80 | 80 | # pylint: enable=bad-whitespace
|
81 | 81 |
|
| 82 | +class MPR121_Channel(): |
| 83 | + """Helper class to represent a touch channel on the MPR121. Not meant to |
| 84 | + be used directly.""" |
| 85 | + def __init__(self, mpr121, channel): |
| 86 | + self._mpr121 = mpr121 |
| 87 | + self._channel = channel |
| 88 | + |
| 89 | + @property |
| 90 | + def value(self): |
| 91 | + """Whether the touch pad is being touched or not.""" |
| 92 | + return self._mpr121.touched() & (1 << self._channel) != 0 |
| 93 | + |
| 94 | + @property |
| 95 | + def raw_value(self): |
| 96 | + """The raw touch measurement.""" |
| 97 | + return self._mpr121.filtered_data(self._channel) |
82 | 98 |
|
83 | 99 | class MPR121:
|
84 | 100 | """Driver for the MPR121 capacitive touch breakout board."""
|
85 | 101 |
|
86 | 102 | def __init__(self, i2c, address=MPR121_I2CADDR_DEFAULT):
|
87 | 103 | self._i2c = i2c_device.I2CDevice(i2c, address)
|
88 | 104 | self._buffer = bytearray(2)
|
| 105 | + self._channels = [None]*12 |
89 | 106 | self.reset()
|
90 | 107 |
|
| 108 | + def __getitem__(self, key): |
| 109 | + if key < 0 or key > 11: |
| 110 | + raise IndexError('Pin must be a value 0-11.') |
| 111 | + if self._channels[key] is None: |
| 112 | + self._channels[key] = MPR121_Channel(self, key) |
| 113 | + return self._channels[key] |
| 114 | + |
| 115 | + @property |
| 116 | + def touched_pins(self): |
| 117 | + """A tuple of touched state for all pins.""" |
| 118 | + touched = self.touched() |
| 119 | + return tuple([bool(touched >> i & 0x01) for i in range(12)]) |
| 120 | + |
91 | 121 | def _write_register_byte(self, register, value):
|
92 | 122 | # Write a byte value to the specifier register address.
|
93 | 123 | with self._i2c:
|
|
0 commit comments