staceybellerose / CAP1203py

Python library for reading CAP1203 Touch Slider sensor from Sparkfun

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CAP1203py

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.

Installation

  1. Enable I2C on your computer
  2. Install the smbus or smbus2 library (via pip). The library is compatible with both.
  3. 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.

Usage

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>

Supporting Classes

Pad

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

Sensitivity

The Sensitivity class is used for setting the Sensitivity setting. Available settings are from x1-x128 (default x2)

PowerTime

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.

History

4/07/20 - Creation

Credits

Most of this code is being translated from the original Sparkfun library here: https://github.com/sparkfun/Qwiic_Capacitive_Touch_Slider_Arduino_Library

License

This is under the MIT License

About

Python library for reading CAP1203 Touch Slider sensor from Sparkfun

License:MIT License


Languages

Language:Python 100.0%