Python library for reading CAP1203 Touch Slider sensor from Sparkfun. https://www.sparkfun.com/products/15344
Written for the Raspberry Pi, but should work on other Linux based computers so long as they have an i2c bus you can connect to.
- Enable I2C on your computer
- Install the smbus or smbus2 library (via pip). The library is compatible with both.
- Add this library to your project. You can either...
-
Clone this repo to your project via:
git clone https://github.com/n8many/CAP1203py.git
-
Or download "cap1203.py" to have the code in your project base directory.
-
If you cloned the repo to your project, add the CAP1203 object via:
from CAP1203py.cap1203 import CAP1203
If you added the file directly to your project, then use:
from cap1203 import CAP1203
Initializing a sensor can be as easy as:
import smbus
bus = smbus.SMBus(1) # For Raspberry Pi
cap = CAP1203(bus)
There are a few options for reading the sensor.
To see if any pads are currently being touched, you can use the is_touched()
res = cap.is_touched()
print(res)
# True
To see if any pads are currently being touched, you can use the get_touched()
function, which will return a
Pad
Enum.
res = cap.get_touched()
print(res)
# <Pad.Right: 4>
res = cap.get_touched()
print(res)
# <Pad.0: 0>
If you just want to check the status of the touch sensor without clearing the interrupt, you can use the
check_touched()
function
res = cap.get_touched()
print(res)
# <Pad.Right: 4>
res = cap.get_touched()
print(res)
# <Pad.Right: 4>
The Pad
class is used in various functions when setting or returning sets of Pads. It is an IntFlag
class,
so it can be used to store one, multiple, or no pads.
For example:
from cap1203 import CAP1203, Pad
interrupt_pads = Pad.Left | Pad.Right
print(interrupt_pads)
# <Pad.Left|Right: 5>
cap.set_interrupt_setting(interrupt_pads)
# Center pad no longer triggers interrupts
The Sensitivity
class is used for setting the Sensitivity setting. Available settings are from x1-x128 (default x2)
The PowerTime
enum is used for setting the time on the Power Button function. This is for the "Power Button"
function on the CAP1203. You can read more about it on page 16 of the datasheet.
4/07/20 - Creation
Most of this code is being translated from the original Sparkfun library here: https://github.com/sparkfun/Qwiic_Capacitive_Touch_Slider_Arduino_Library
This is under the MIT License