<s> import spidev <EOL> spi = spidev . SpiDev ( ) <EOL> spi . open ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> class MCP3008AnalogSensor ( object ) : <EOL> def __init__ ( self , channels , range = ( <NUM_LIT:0> , <NUM_LIT> ) ) : <EOL> for chan_num in channels : <EOL> if chan_num > <NUM_LIT:7> or chan_num < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . channels = channels <EOL> self . num_values = len ( channels ) <EOL> self . range = range <EOL> def start ( self ) : <EOL> spi = spidev . SpiDev ( ) <EOL> spi . open ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def read ( self ) : <EOL> return [ self . _read_adc ( chan ) for chan in self . channels ] <EOL> def read_normalized ( self ) : <EOL> min , max = self . range <EOL> return [ ( val - min ) / float ( max ) for val in self . read ( ) ] <EOL> def _read_adc ( self , channel ) : <EOL> """<STR_LIT>""" <EOL> r = spi . xfer2 ( [ <NUM_LIT:1> , ( <NUM_LIT:8> + channel ) << <NUM_LIT:4> , <NUM_LIT:0> ] ) <EOL> adc_value = ( ( r [ <NUM_LIT:1> ] & <NUM_LIT:3> ) << <NUM_LIT:8> ) + r [ <NUM_LIT:2> ] <EOL> return adc_value </s>
