<s> import smbus <EOL> import time <EOL> import math <EOL> import RPi . GPIO as GPIO <EOL> import struct <EOL> rev = GPIO . RPI_REVISION <EOL> if rev == <NUM_LIT:2> or rev == <NUM_LIT:3> : <EOL> bus = smbus . SMBus ( <NUM_LIT:1> ) <EOL> else : <EOL> bus = smbus . SMBus ( <NUM_LIT:0> ) <EOL> address = <NUM_LIT> <EOL> dRead_cmd = [ <NUM_LIT:1> ] <EOL> dWrite_cmd = [ <NUM_LIT:2> ] <EOL> aRead_cmd = [ <NUM_LIT:3> ] <EOL> aWrite_cmd = [ <NUM_LIT:4> ] <EOL> pMode_cmd = [ <NUM_LIT:5> ] <EOL> uRead_cmd = [ <NUM_LIT:7> ] <EOL> version_cmd = [ <NUM_LIT:8> ] <EOL> acc_xyz_cmd = [ <NUM_LIT:20> ] <EOL> rtc_getTime_cmd = [ <NUM_LIT:30> ] <EOL> dht_temp_cmd = [ <NUM_LIT> ] <EOL> ledBarInit_cmd = [ <NUM_LIT:50> ] <EOL> ledBarOrient_cmd = [ <NUM_LIT> ] <EOL> ledBarLevel_cmd = [ <NUM_LIT> ] <EOL> ledBarSetOne_cmd = [ <NUM_LIT> ] <EOL> ledBarToggleOne_cmd = [ <NUM_LIT> ] <EOL> ledBarSet_cmd = [ <NUM_LIT> ] <EOL> ledBarGet_cmd = [ <NUM_LIT> ] <EOL> fourDigitInit_cmd = [ <NUM_LIT> ] <EOL> fourDigitBrightness_cmd = [ <NUM_LIT> ] <EOL> fourDigitValue_cmd = [ <NUM_LIT> ] <EOL> fourDigitValueZeros_cmd = [ <NUM_LIT> ] <EOL> fourDigitIndividualDigit_cmd = [ <NUM_LIT> ] <EOL> fourDigitIndividualLeds_cmd = [ <NUM_LIT> ] <EOL> fourDigitScore_cmd = [ <NUM_LIT> ] <EOL> fourDigitAnalogRead_cmd = [ <NUM_LIT> ] <EOL> fourDigitAllOn_cmd = [ <NUM_LIT> ] <EOL> fourDigitAllOff_cmd = [ <NUM_LIT> ] <EOL> storeColor_cmd = [ <NUM_LIT> ] <EOL> chainableRgbLedInit_cmd = [ <NUM_LIT> ] <EOL> chainableRgbLedTest_cmd = [ <NUM_LIT> ] <EOL> chainableRgbLedSetPattern_cmd = [ <NUM_LIT> ] <EOL> chainableRgbLedSetModulo_cmd = [ <NUM_LIT> ] <EOL> chainableRgbLedSetLevel_cmd = [ <NUM_LIT> ] <EOL> unused = <NUM_LIT:0> <EOL> def write_i2c_block ( address , block ) : <EOL> try : <EOL> return bus . write_i2c_block_data ( address , <NUM_LIT:1> , block ) <EOL> except IOError : <EOL> print "<STR_LIT>" <EOL> return - <NUM_LIT:1> <EOL> def read_i2c_byte ( address ) : <EOL> try : <EOL> return bus . read_byte ( address ) <EOL> except IOError : <EOL> print "<STR_LIT>" <EOL> return - <NUM_LIT:1> <EOL> def read_i2c_block ( address ) : <EOL> try : <EOL> return bus . read_i2c_block_data ( address , <NUM_LIT:1> ) <EOL> except IOError : <EOL> print "<STR_LIT>" <EOL> return - <NUM_LIT:1> <EOL> def digitalRead ( pin ) : <EOL> write_i2c_block ( address , dRead_cmd + [ pin , unused , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> n = read_i2c_byte ( address ) <EOL> return n <EOL> def digitalWrite ( pin , value ) : <EOL> write_i2c_block ( address , dWrite_cmd + [ pin , value , unused ] ) <EOL> return <NUM_LIT:1> <EOL> def pinMode ( pin , mode ) : <EOL> if mode == "<STR_LIT>" : <EOL> write_i2c_block ( address , pMode_cmd + [ pin , <NUM_LIT:1> , unused ] ) <EOL> elif mode == "<STR_LIT>" : <EOL> write_i2c_block ( address , pMode_cmd + [ pin , <NUM_LIT:0> , unused ] ) <EOL> return <NUM_LIT:1> <EOL> def analogRead ( pin ) : <EOL> bus . write_i2c_block_data ( address , <NUM_LIT:1> , aRead_cmd + [ pin , unused , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> bus . read_byte ( address ) <EOL> number = bus . read_i2c_block_data ( address , <NUM_LIT:1> ) <EOL> return number [ <NUM_LIT:1> ] * <NUM_LIT> + number [ <NUM_LIT:2> ] <EOL> def analogWrite ( pin , value ) : <EOL> write_i2c_block ( address , aWrite_cmd + [ pin , value , unused ] ) <EOL> return <NUM_LIT:1> <EOL> def temp ( pin , model = '<STR_LIT:1.0>' ) : <EOL> if model == '<STR_LIT>' : <EOL> bValue = <NUM_LIT> <EOL> elif model == '<STR_LIT>' : <EOL> bValue = <NUM_LIT> <EOL> else : <EOL> bValue = <NUM_LIT> <EOL> a = analogRead ( pin ) <EOL> resistance = ( float ) ( <NUM_LIT> - a ) * <NUM_LIT> / a <EOL> t = ( float ) ( <NUM_LIT:1> / ( math . log ( resistance / <NUM_LIT> ) / bValue + <NUM_LIT:1> / <NUM_LIT> ) - <NUM_LIT> ) <EOL> return t <EOL> def ultrasonicRead ( pin ) : <EOL> write_i2c_block ( address , uRead_cmd + [ pin , unused , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> read_i2c_byte ( address ) <EOL> number = read_i2c_block ( address ) <EOL> return ( number [ <NUM_LIT:1> ] * <NUM_LIT> + number [ <NUM_LIT:2> ] ) <EOL> def version ( ) : <EOL> write_i2c_block ( address , version_cmd + [ unused , unused , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> read_i2c_byte ( address ) <EOL> number = read_i2c_block ( address ) <EOL> return "<STR_LIT>" % ( number [ <NUM_LIT:1> ] , number [ <NUM_LIT:2> ] , number [ <NUM_LIT:3> ] ) <EOL> def acc_xyz ( ) : <EOL> write_i2c_block ( address , acc_xyz_cmd + [ unused , unused , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> read_i2c_byte ( address ) <EOL> number = read_i2c_block ( address ) <EOL> if number [ <NUM_LIT:1> ] > <NUM_LIT:32> : <EOL> number [ <NUM_LIT:1> ] = - ( number [ <NUM_LIT:1> ] - <NUM_LIT> ) <EOL> if number [ <NUM_LIT:2> ] > <NUM_LIT:32> : <EOL> number [ <NUM_LIT:2> ] = - ( number [ <NUM_LIT:2> ] - <NUM_LIT> ) <EOL> if number [ <NUM_LIT:3> ] > <NUM_LIT:32> : <EOL> number [ <NUM_LIT:3> ] = - ( number [ <NUM_LIT:3> ] - <NUM_LIT> ) <EOL> return ( number [ <NUM_LIT:1> ] , number [ <NUM_LIT:2> ] , number [ <NUM_LIT:3> ] ) <EOL> def rtc_getTime ( ) : <EOL> write_i2c_block ( address , rtc_getTime_cmd + [ unused , unused , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> read_i2c_byte ( address ) <EOL> number = read_i2c_block ( address ) <EOL> return number <EOL> def dht ( pin , module_type ) : <EOL> write_i2c_block ( address , dht_temp_cmd + [ pin , module_type , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> try : <EOL> read_i2c_byte ( address ) <EOL> number = read_i2c_block ( address ) <EOL> if number == - <NUM_LIT:1> : <EOL> return - <NUM_LIT:1> <EOL> except ( TypeError , IndexError ) : <EOL> return - <NUM_LIT:1> <EOL> f = <NUM_LIT:0> <EOL> for element in reversed ( number [ <NUM_LIT:1> : <NUM_LIT:5> ] ) : <EOL> hex_val = hex ( element ) <EOL> try : <EOL> h_val = hex_val [ <NUM_LIT:2> ] + hex_val [ <NUM_LIT:3> ] <EOL> except IndexError : <EOL> h_val = '<STR_LIT:0>' + hex_val [ <NUM_LIT:2> ] <EOL> if f == <NUM_LIT:0> : <EOL> h = h_val <EOL> f = <NUM_LIT:1> <EOL> else : <EOL> h = h + h_val <EOL> t = round ( struct . unpack ( '<STR_LIT>' , h . decode ( '<STR_LIT>' ) ) [ <NUM_LIT:0> ] , <NUM_LIT:2> ) <EOL> h = '<STR_LIT>' <EOL> for element in reversed ( number [ <NUM_LIT:5> : <NUM_LIT:9> ] ) : <EOL> hex_val = hex ( element ) <EOL> try : <EOL> h_val = hex_val [ <NUM_LIT:2> ] + hex_val [ <NUM_LIT:3> ] <EOL> except IndexError : <EOL> h_val = '<STR_LIT:0>' + hex_val [ <NUM_LIT:2> ] <EOL> if f == <NUM_LIT:0> : <EOL> h = h_val <EOL> f = <NUM_LIT:1> <EOL> else : <EOL> h = h + h_val <EOL> hum = round ( struct . unpack ( '<STR_LIT>' , h . decode ( '<STR_LIT>' ) ) [ <NUM_LIT:0> ] , <NUM_LIT:2> ) <EOL> return [ t , hum ] <EOL> def ledBar_init ( pin , orientation ) : <EOL> write_i2c_block ( address , ledBarInit_cmd + [ pin , orientation , unused ] ) <EOL> return <NUM_LIT:1> <EOL> def ledBar_orientation ( pin , orientation ) : <EOL> write_i2c_block ( address , ledBarOrient_cmd + [ pin , orientation , unused ] ) <EOL> return <NUM_LIT:1> <EOL> def ledBar_setLevel ( pin , level ) : <EOL> write_i2c_block ( address , ledBarLevel_cmd + [ pin , level , unused ] ) <EOL> return <NUM_LIT:1> <EOL> def ledBar_setLed ( pin , led , state ) : <EOL> write_i2c_block ( address , ledBarSetOne_cmd + [ pin , led , state ] ) <EOL> return <NUM_LIT:1> <EOL> def ledBar_toggleLed ( pin , led ) : <EOL> write_i2c_block ( address , ledBarToggleOne_cmd + [ pin , led , unused ] ) <EOL> return <NUM_LIT:1> <EOL> def ledBar_setBits ( pin , state ) : <EOL> byte1 = state & <NUM_LIT:255> <EOL> byte2 = state >> <NUM_LIT:8> <EOL> write_i2c_block ( address , ledBarSet_cmd + [ pin , byte1 , byte2 ] ) <EOL> return <NUM_LIT:1> <EOL> def ledBar_getBits ( pin ) : <EOL> write_i2c_block ( address , ledBarGet_cmd + [ pin , unused , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> read_i2c_byte ( <NUM_LIT> ) <EOL> block = read_i2c_block ( <NUM_LIT> ) <EOL> return block [ <NUM_LIT:1> ] ^ ( block [ <NUM_LIT:2> ] << <NUM_LIT:8> ) <EOL> def fourDigit_init ( pin ) : <EOL> write_i2c_block ( address , fourDigitInit_cmd + [ pin , unused , unused ] ) <EOL> return <NUM_LIT:1> <EOL> def fourDigit_number ( pin , value , leading_zero ) : <EOL> byte1 = value & <NUM_LIT:255> <EOL> byte2 = value >> <NUM_LIT:8> <EOL> if ( leading_zero ) : <EOL> write_i2c_block ( address , fourDigitValue_cmd + [ pin , byte1 , byte2 ] ) <EOL> else : <EOL> write_i2c_block ( address , fourDigitValueZeros_cmd + [ pin , byte1 , byte2 ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def fourDigit_brightness ( pin , brightness ) : <EOL> write_i2c_block ( address , fourDigitBrightness_cmd + [ pin , brightness , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def fourDigit_digit ( pin , segment , value ) : <EOL> write_i2c_block ( address , fourDigitIndividualDigit_cmd + [ pin , segment , value ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def fourDigit_segment ( pin , segment , leds ) : <EOL> write_i2c_block ( address , fourDigitIndividualLeds_cmd + [ pin , segment , leds ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def fourDigit_score ( pin , left , right ) : <EOL> write_i2c_block ( address , fourDigitScore_cmd + [ pin , left , right ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def fourDigit_monitor ( pin , analog , duration ) : <EOL> write_i2c_block ( address , fourDigitAnalogRead_cmd + [ pin , analog , duration ] ) <EOL> time . sleep ( duration + <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def fourDigit_on ( pin ) : <EOL> write_i2c_block ( address , fourDigitAllOn_cmd + [ pin , unused , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def fourDigit_off ( pin ) : <EOL> write_i2c_block ( address , fourDigitAllOff_cmd + [ pin , unused , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def storeColor ( red , green , blue ) : <EOL> write_i2c_block ( address , storeColor_cmd + [ red , green , blue ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def chainableRgbLed_init ( pin , numLeds ) : <EOL> write_i2c_block ( address , chainableRgbLedInit_cmd + [ pin , numLeds , unused ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def chainableRgbLed_test ( pin , numLeds , testColor ) : <EOL> write_i2c_block ( address , chainableRgbLedTest_cmd + [ pin , numLeds , testColor ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def chainableRgbLed_pattern ( pin , pattern , whichLed ) : <EOL> write_i2c_block ( address , chainableRgbLedSetPattern_cmd + [ pin , pattern , whichLed ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def chainableRgbLed_modulo ( pin , offset , divisor ) : <EOL> write_i2c_block ( address , chainableRgbLedSetModulo_cmd + [ pin , offset , divisor ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def chainableRgbLed_setLevel ( pin , level , reverse ) : <EOL> write_i2c_block ( address , chainableRgbLedSetLevel_cmd + [ pin , level , reverse ] ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> </s>
