From cb681cfebee2f3aa514b0dccdc272a4ef9041001 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Sun, 3 Mar 2019 17:21:26 -0800 Subject: [PATCH] MPR121 touch detection --- python/samples/touch.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 python/samples/touch.py diff --git a/python/samples/touch.py b/python/samples/touch.py new file mode 100644 index 0000000..cc9f7fb --- /dev/null +++ b/python/samples/touch.py @@ -0,0 +1,33 @@ +""" +Example for MPR121 Capacitive Touch Sensor. +Available from multiple vendors. +""" + +import sys +import serial +import time +import struct +import random + +from i2cdriver import I2CDriver + +class MPR121: + def __init__(self, i2, a = 0x5a): + self.i2 = i2 + self.a = a + self.i2.regwr(self.a, 0x5e, 0x0c) + + def read(self): + """ Return 12 touch detection flags """ + (tb,) = struct.unpack("> i) & 1) for i in range(12)] + +if __name__ == '__main__': + i2 = I2CDriver(sys.argv[1]) + + i2.reset() + + d = MPR121(i2) + while 1: + print(d.read()) + time.sleep(.1)