<s> import time <EOL> MPR121_I2CADDR_DEFAULT = <NUM_LIT> <EOL> MPR121_TOUCHSTATUS_L = <NUM_LIT> <EOL> MPR121_TOUCHSTATUS_H = <NUM_LIT> <EOL> MPR121_FILTDATA_0L = <NUM_LIT> <EOL> MPR121_FILTDATA_0H = <NUM_LIT> <EOL> MPR121_BASELINE_0 = <NUM_LIT> <EOL> MPR121_MHDR = <NUM_LIT> <EOL> MPR121_NHDR = <NUM_LIT> <EOL> MPR121_NCLR = <NUM_LIT> <EOL> MPR121_FDLR = <NUM_LIT> <EOL> MPR121_MHDF = <NUM_LIT> <EOL> MPR121_NHDF = <NUM_LIT> <EOL> MPR121_NCLF = <NUM_LIT> <EOL> MPR121_FDLF = <NUM_LIT> <EOL> MPR121_NHDT = <NUM_LIT> <EOL> MPR121_NCLT = <NUM_LIT> <EOL> MPR121_FDLT = <NUM_LIT> <EOL> MPR121_TOUCHTH_0 = <NUM_LIT> <EOL> MPR121_RELEASETH_0 = <NUM_LIT> <EOL> MPR121_DEBOUNCE = <NUM_LIT> <EOL> MPR121_CONFIG1 = <NUM_LIT> <EOL> MPR121_CONFIG2 = <NUM_LIT> <EOL> MPR121_CHARGECURR_0 = <NUM_LIT> <EOL> MPR121_CHARGETIME_1 = <NUM_LIT> <EOL> MPR121_ECR = <NUM_LIT> <EOL> MPR121_AUTOCONFIG0 = <NUM_LIT> <EOL> MPR121_AUTOCONFIG1 = <NUM_LIT> <EOL> MPR121_UPLIMIT = <NUM_LIT> <EOL> MPR121_LOWLIMIT = <NUM_LIT> <EOL> MPR121_TARGETLIMIT = <NUM_LIT> <EOL> MPR121_GPIODIR = <NUM_LIT> <EOL> MPR121_GPIOEN = <NUM_LIT> <EOL> MPR121_GPIOSET = <NUM_LIT> <EOL> MPR121_GPIOCLR = <NUM_LIT> <EOL> MPR121_GPIOTOGGLE = <NUM_LIT> <EOL> MPR121_SOFTRESET = <NUM_LIT> <EOL> MAX_I2C_RETRIES = <NUM_LIT:5> <EOL> class MPR121 ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def begin ( self , address = MPR121_I2CADDR_DEFAULT , i2c = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if i2c is None : <EOL> import I2C <EOL> i2c = I2C <EOL> I2C . require_repeated_start ( ) <EOL> self . _device = i2c . get_i2c_device ( address , ** kwargs ) <EOL> return self . _reset ( ) <EOL> def _reset ( self ) : <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_SOFTRESET , <NUM_LIT> ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_ECR , <NUM_LIT> ) <EOL> c = self . _i2c_retry ( self . _device . readU8 , MPR121_CONFIG2 ) <EOL> if c != <NUM_LIT> : <EOL> return False <EOL> self . set_thresholds ( <NUM_LIT:12> , <NUM_LIT:6> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_MHDR , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_NHDR , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_NCLR , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_FDLR , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_MHDF , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_NHDF , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_NCLF , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_FDLF , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_NHDT , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_NCLT , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_FDLT , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_DEBOUNCE , <NUM_LIT:0> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_CONFIG1 , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_CONFIG2 , <NUM_LIT> ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_ECR , <NUM_LIT> ) <EOL> return True <EOL> def _i2c_retry ( self , func , * params ) : <EOL> count = <NUM_LIT:0> <EOL> while True : <EOL> try : <EOL> return func ( * params ) <EOL> except IOError as ex : <EOL> if ex . errno != <NUM_LIT> : <EOL> raise ex <EOL> self . _reset ( ) <EOL> count += <NUM_LIT:1> <EOL> if count >= MAX_I2C_RETRIES : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> def set_thresholds ( self , touch , release ) : <EOL> """<STR_LIT>""" <EOL> assert touch >= <NUM_LIT:0> and touch <= <NUM_LIT:255> , '<STR_LIT>' <EOL> assert release >= <NUM_LIT:0> and release <= <NUM_LIT:255> , '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:12> ) : <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_TOUCHTH_0 + <NUM_LIT:2> * i , touch ) <EOL> self . _i2c_retry ( self . _device . write8 , MPR121_RELEASETH_0 + <NUM_LIT:2> * i , release ) <EOL> def filtered_data ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> assert pin >= <NUM_LIT:0> and pin < <NUM_LIT:12> , '<STR_LIT>' <EOL> return self . _i2c_retry ( self . _device . readU16LE , MPR121_FILTDATA_0L + pin * <NUM_LIT:2> ) <EOL> def baseline_data ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> assert pin >= <NUM_LIT:0> and pin < <NUM_LIT:12> , '<STR_LIT>' <EOL> bl = self . _i2c_retry ( self . _device . readU8 , MPR121_BASELINE_0 + pin ) <EOL> return bl << <NUM_LIT:2> <EOL> def touched ( self ) : <EOL> """<STR_LIT>""" <EOL> t = self . _i2c_retry ( self . _device . readU16LE , MPR121_TOUCHSTATUS_L ) <EOL> return t & <NUM_LIT> <EOL> def is_touched ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> assert pin >= <NUM_LIT:0> and pin < <NUM_LIT:12> , '<STR_LIT>' <EOL> t = self . touched ( ) <EOL> return ( t & ( <NUM_LIT:1> << pin ) ) > <NUM_LIT:0> </s>
