This library offers implementations of Bela's Trill touch sensors for MicroPython.
Table of Contents
This library offers implementations of Bela 's Trill touch sensors for MicroPython. The library has been tested using an ESP32 development board (TinyPICO).
To get a local copy simply clone this repository.
git clone https://github.com/heerkog/MicroPythonTrill.gitThe repository is structured as followed:
LICENSEthe license.readme.mdtouch.pytouch helper classes.trill.pythe library classes.
Example implementation:
from machine import SoftI2C, Pin
from trill import Square
from touch import Touches2D
i2c = SoftI2C(scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=400000)
square = Square(i2c)
data = square.read()
touches = Touches2D(data)
for touch in touches.getTouches():
print(touch)
The library consists of two python files with the following classes:
The file trill.py consists of the following six classes with functions:
TrillSensorThe generic Trill sensor representation, with functions:__init__(i2c, address, mode, sleep=10)Initializes a generic Trill sensor using the I2C busi2c, the I2C addressaddress, the modemode, and a sleep time between I2C commands of10ms.identify()Ask the sensor to identify itself and read its type and firmware version.get_type()Get the sensor type.get_firmware_version()Get the sensor firmware version.get_size()Get the size of the sensor as a tuple (x, y).get_num_channels()Get the number of channels of the sensor.read()Read the latest scan value from the sensor.set_mode(mode)Set the sensor mode, as eithertrill.MODE_CENTROID,trill.MODE_RAW,trill.MODE_BASELINE, ortrill.MODE_DIFF.get_mode()Get the sensor mode. ReturnsNoneif mode hasn't been set.set_scan_settings(speed=0, resolution=12)Set the scan speed and resolution (numBits) of the sensor, with speed being a value from 0 to 3, and resolution being a value from 9 to 16.bodyupdate_baseline()Update the baseline capacitance values of the sensor.set_prescaler(prescaler=8)Set the prescaler of the sensor, with prescaler being a value from 1 to 8.set_noise_threshold(threshold)Set the noise threshold for thetrill.MODE_CENTROIDandtrill.MODE_DIFFmodes, with threshold being a value from 0 to 255.set_IDAC_value(value)Set the IDAC value of the sensor, with value being a value from 0 to 255.set_minimum_touch_size(self, minSize)Set the minimum registered touch size.set_auto_scan_interval(interval=1)Set the automatic scan interval (used with the EVT pin).is_1D()ReturnsTrueif the sensor is one-directional.is_2D()ReturnsTrueif the sensor is two-directional.
BarSubclass ofTrillSensor, implements a Trill Bar sensor.__init__(i2c, address=0x20, mode=MODE_CENTROID, sleep=10)Initializes a Trill Bar sensor using the I2C busi2c, the I2C address0x20, the modetrill.MODE_CENTROID, and a sleep time between I2C commands of10ms.
SquareSubclass ofTrillSensor, implements a Trill Square sensor.__init__(i2c, address=0x28, mode=MODE_CENTROID, sleep=10)Initializes a Trill Square sensor using the I2C busi2c, the I2C address0x28, the modetrill.MODE_CENTROID, and a sleep time between I2C commands of10ms.
CraftSubclass ofTrillSensor, implements a Trill Craft sensor.__init__(i2c, address=0x30, mode=MODE_CENTROID, sleep=10)Initializes a Trill Bar sensor using the I2C busi2c, the I2C address0x30, the modetrill.MODE_CENTROID, and a sleep time between I2C commands of10ms.
RingSubclass ofTrillSensor, implements a Trill Ring sensor.__init__(i2c, address=0x38, mode=MODE_CENTROID, sleep=10)Initializes a Trill Bar sensor using the I2C busi2c, the I2C address0x38, the modetrill.MODE_CENTROID, and a sleep time between I2C commands of10ms.
HexSubclass ofTrillSensor, implements a Trill Hex sensor.__init__(i2c, address=0x40, mode=MODE_CENTROID, sleep=10)Initializes a Trill Bar sensor using the I2C busi2c, the I2C address0x40, the modetrill.MODE_CENTROID, and a sleep time between I2C commands of10ms.
The file touch.py consists of the following three classes with functions:
TouchesImplements a helper class to obtain Trill sensor touches fromtrill.MODE_CENTROIDdata.__init__(data)Converts data read usingtrill.MODE_CENTROIDto a list of touchesget_touches()Returns a list of touches as tuples.get_num_touches()Returns the number of touches registered.get_touch(index)Returns the touch atindexas a tuple.is_empty()ReturnsTrueif no touches are registered.
Touches1DSubclass ofTouches, interpretstrill.MODE_CENTROIDdata from one-directional sensors.__init__(data)Converts one-directional data read using MODE_CENTROID to a list of touchesget_touches()Returns a list of touches as tuples[(vertical position, touch size), ...].get_touch(index)Returns the touch atindexas a tuple(vertical position, touch size).
Touches2DSubclass ofTouches, interpretstrill.MODE_CENTROIDdata from two-directional sensors.__init__(data)Converts two-directional data read using MODE_CENTROID to a list of touchesget_touches()Returns a list of touches as tuples[(horizontal position, vertical position, horizontal size, vertical size), ...].get_touch(index)Returns the touch atindexas a tuple(horizontal position, vertical position, horizontal size, vertical size).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the GNU General Public License. See LICENSE for more information.
Heerko Groefsema - @HeerkoG - hgroefsema.nl
Project Link: https://github.com/heerkog/MicroPythonTrill
The following resources were of interest during development: