<s> """<STR_LIT>""" <EOL> import bbio <EOL> class ADXL345 ( object ) : <EOL> RANGE_2G = <NUM_LIT:0> <EOL> RANGE_4G = <NUM_LIT:1> <EOL> RANGE_8G = <NUM_LIT:2> <EOL> RANGE_16G = <NUM_LIT:3> <EOL> G_PER_BIT = [ <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> <EOL> ] <EOL> INT_DOUBLE_TAP = <NUM_LIT:1> << <NUM_LIT:5> <EOL> INT_SINGLE_TAP = <NUM_LIT:1> << <NUM_LIT:6> <EOL> INT1 = <NUM_LIT:1> <EOL> INT2 = <NUM_LIT:2> <EOL> REG_THRESH_TAP = <NUM_LIT> <EOL> REG_DUR_TAP = <NUM_LIT> <EOL> REG_LATENT_TAP = <NUM_LIT> <EOL> REG_WINDOW_TAP = <NUM_LIT> <EOL> REG_TAP_AXES = <NUM_LIT> <EOL> TAP_AXES_X_EN = <NUM_LIT:1> << <NUM_LIT:2> <EOL> TAP_AXES_Y_EN = <NUM_LIT:1> << <NUM_LIT:1> <EOL> TAP_AXES_Z_EN = <NUM_LIT:1> <EOL> REG_POWER_CTL = <NUM_LIT> <EOL> POWER_CTL_MEASURE = <NUM_LIT:1> << <NUM_LIT:3> <EOL> REG_INT_ENABLE = <NUM_LIT> <EOL> REG_INT_MAP = <NUM_LIT> <EOL> REG_INT_SOURCE = <NUM_LIT> <EOL> REG_DATA_FORMAT = <NUM_LIT> <EOL> REG_DATAX0 = <NUM_LIT> <EOL> REG_DATAX1 = <NUM_LIT> <EOL> REG_DATAY0 = <NUM_LIT> <EOL> REG_DATAY1 = <NUM_LIT> <EOL> REG_DATAZ0 = <NUM_LIT> <EOL> REG_DATAZ1 = <NUM_LIT> <EOL> MODE_SPI = <NUM_LIT:1> <EOL> SPI_CLOCK_MODE = <NUM_LIT:3> <EOL> MODE_I2C = <NUM_LIT:0> <EOL> I2C_ADDR = <NUM_LIT> <EOL> I2C_ALT_ADDR = <NUM_LIT> <EOL> def __init__ ( self , bus , spi_cs = <NUM_LIT:0> , i2c_addr = <NUM_LIT> , spi_frequency = <NUM_LIT> ) : <EOL> if bus == bbio . I2C1 or bus == bbio . I2C2 : <EOL> self . mode = self . MODE_I2C <EOL> elif bus == bbio . SPI0 or bus == bbio . SPI1 : <EOL> self . mode = self . MODE_SPI <EOL> self . spi_cs = spi_cs <EOL> self . spi_frequency = spi_frequency <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . bus = bus <EOL> self . i2c_addr = i2c_addr <EOL> self . writeReg ( self . REG_POWER_CTL , self . POWER_CTL_MEASURE ) <EOL> self . setRange ( self . RANGE_2G ) <EOL> def getXYZ ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . readReg ( self . REG_DATAX0 , <NUM_LIT:6> ) <EOL> samples = [ <NUM_LIT:0> ] * <NUM_LIT:3> <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> samples [ i ] = ( data [ i + i + <NUM_LIT:1> ] << <NUM_LIT:8> ) | data [ i + i ] <EOL> if samples [ i ] >= <NUM_LIT> : samples [ i ] -= <NUM_LIT> <EOL> samples [ i ] *= self . G_PER_BIT [ self . accel_range ] <EOL> return samples <EOL> def setRange ( self , accel_range ) : <EOL> """<STR_LIT>""" <EOL> accel_range &= <NUM_LIT> <EOL> self . writeReg ( self . REG_DATA_FORMAT , accel_range ) <EOL> self . accel_range = accel_range <EOL> def enableInterrupt ( self , interrupt , int_pin ) : <EOL> """<STR_LIT>""" <EOL> if interrupt != self . INT_DOUBLE_TAP and interrupt != self . INT_SINGLE_TAP : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if int_pin != self . INT1 and int_pin != self . INT2 : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> int_map = self . readReg ( self . REG_INT_MAP ) [ <NUM_LIT:0> ] <EOL> if ( int_pin == self . INT2 ) : <EOL> int_map |= interrupt <EOL> else : <EOL> int_map &= ~ interrupt <EOL> self . writeReg ( self . REG_INT_MAP , int_map ) <EOL> int_enable = self . readReg ( self . REG_INT_ENABLE ) [ <NUM_LIT:0> ] <EOL> int_enable &= ~ interrupt <EOL> self . writeReg ( self . REG_INT_ENABLE , int_enable ) <EOL> self . getInterrupts ( ) <EOL> int_enable |= interrupt <EOL> self . writeReg ( self . REG_INT_ENABLE , int_enable ) <EOL> def disableInterrupt ( self , interrupt ) : <EOL> """<STR_LIT>""" <EOL> if interrupt != self . INT_DOUBLE_TAP and interrupt != self . INT_SINGLE_TAP : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> int_enable = self . readReg ( self . REG_INT_ENABLE ) [ <NUM_LIT:0> ] <EOL> int_enable &= ~ interrupt <EOL> self . writeReg ( self . REG_INT_ENABLE , int_enable ) <EOL> def enableTapDetection ( self , threshold_g = <NUM_LIT:3> , duration_ms = <NUM_LIT:20> , <EOL> latency_ms = <NUM_LIT:100> , window_ms = <NUM_LIT:1000> ) : <EOL> """<STR_LIT>""" <EOL> self . writeReg ( self . REG_TAP_AXES , <NUM_LIT:0> ) <EOL> enable = self . TAP_AXES_X_EN | self . TAP_AXES_Y_EN | self . TAP_AXES_Z_EN <EOL> self . writeReg ( self . REG_TAP_AXES , enable ) <EOL> self . setTapThreshold ( threshold_g ) <EOL> self . setTapDuration ( duration_ms ) <EOL> self . setTapLatency ( latency_ms ) <EOL> self . setTapWindow ( window_ms ) <EOL> def setTapThreshold ( self , threshold ) : <EOL> """<STR_LIT>""" <EOL> threshold /= <NUM_LIT> <EOL> threshold = int ( threshold + <NUM_LIT:0.5> ) <EOL> if threshold > <NUM_LIT:255> : threshold = <NUM_LIT:255> <EOL> self . writeReg ( self . REG_THRESH_TAP , threshold ) <EOL> def setTapDuration ( self , duration ) : <EOL> """<STR_LIT>""" <EOL> duration /= <NUM_LIT> <EOL> duration = int ( duration + <NUM_LIT:0.5> ) <EOL> if duration > <NUM_LIT:255> : duration = <NUM_LIT:255> <EOL> self . writeReg ( self . REG_DUR_TAP , duration ) <EOL> def setTapLatency ( self , latency ) : <EOL> """<STR_LIT>""" <EOL> latency /= <NUM_LIT> <EOL> latency = int ( latency + <NUM_LIT:0.5> ) <EOL> if latency > <NUM_LIT:255> : latency = <NUM_LIT:255> <EOL> self . writeReg ( self . REG_LATENT_TAP , latency ) <EOL> def setTapWindow ( self , window ) : <EOL> """<STR_LIT>""" <EOL> window /= <NUM_LIT> <EOL> window = int ( window + <NUM_LIT:0.5> ) <EOL> if window > <NUM_LIT:255> : window = <NUM_LIT:255> <EOL> self . writeReg ( self . REG_WINDOW_TAP , window ) <EOL> def getInterrupts ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . readReg ( self . REG_INT_SOURCE ) [ <NUM_LIT:0> ] <EOL> def writeReg ( self , reg , val ) : <EOL> """<STR_LIT>""" <EOL> reg &= <NUM_LIT> <EOL> val &= <NUM_LIT> <EOL> if self . mode == self . MODE_I2C : <EOL> self . bus . write ( self . i2c_addr , [ reg , val ] ) <EOL> elif self . mode == self . MODE_SPI : <EOL> self . bus . setClockMode ( self . spi_cs , self . SPI_CLOCK_MODE ) <EOL> self . bus . setMaxFrequency ( self . spi_cs , self . spi_frequency ) <EOL> self . bus . write ( self . spi_cs , [ reg , val ] ) <EOL> def readReg ( self , reg , n_bytes = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> reg &= <NUM_LIT> <EOL> if self . mode == self . MODE_I2C : <EOL> return self . bus . readTransaction ( self . i2c_addr , reg , n_bytes ) <EOL> elif self . mode == self . MODE_SPI : <EOL> reg |= <NUM_LIT:1> << <NUM_LIT:7> <EOL> if ( n_bytes > <NUM_LIT:1> ) : <EOL> reg |= <NUM_LIT:1> << <NUM_LIT:6> <EOL> self . bus . setClockMode ( self . spi_cs , self . SPI_CLOCK_MODE ) <EOL> self . bus . setMaxFrequency ( self . spi_cs , self . spi_frequency ) <EOL> return self . bus . transfer ( self . spi_cs , [ reg ] + [ <NUM_LIT:0> ] * n_bytes ) [ <NUM_LIT:1> : ] </s>
