<s> import smbus <EOL> import time <EOL> import math <EOL> import RPi . GPIO <EOL> """<STR_LIT>""" <EOL> class GroveI2CColorSensor : <EOL> """<STR_LIT>""" <EOL> COLOR_TABLE = { "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT:255> , "<STR_LIT:g>" : <NUM_LIT:0> , "<STR_LIT:b>" : <NUM_LIT:0> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT:0> , "<STR_LIT:g>" : <NUM_LIT:255> , "<STR_LIT:b>" : <NUM_LIT:0> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT:0> , "<STR_LIT:g>" : <NUM_LIT:0> , "<STR_LIT:b>" : <NUM_LIT:255> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT:255> , "<STR_LIT:g>" : <NUM_LIT:255> , "<STR_LIT:b>" : <NUM_LIT:0> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT:255> , "<STR_LIT:g>" : <NUM_LIT:0> , "<STR_LIT:b>" : <NUM_LIT:255> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT:0> , "<STR_LIT:g>" : <NUM_LIT:255> , "<STR_LIT:b>" : <NUM_LIT:255> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT:255> , "<STR_LIT:g>" : <NUM_LIT:20> , "<STR_LIT:b>" : <NUM_LIT> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT:0.5> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT:255> , "<STR_LIT:g>" : <NUM_LIT> , "<STR_LIT:b>" : <NUM_LIT:0> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT> , "<STR_LIT:g>" : <NUM_LIT> , "<STR_LIT:b>" : <NUM_LIT> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> , "<STR_LIT:r>" : <NUM_LIT:255> , "<STR_LIT:g>" : <NUM_LIT:255> , "<STR_LIT:b>" : <NUM_LIT:255> } , <EOL> "<STR_LIT>" : { "<STR_LIT:x>" : <NUM_LIT:0> , "<STR_LIT:y>" : <NUM_LIT:0> , "<STR_LIT:r>" : <NUM_LIT:0> , "<STR_LIT:g>" : <NUM_LIT:0> , "<STR_LIT:b>" : <NUM_LIT:0> } } <EOL> _I2C_SENSOR_ADDRESS = <NUM_LIT> <EOL> _REGISTER_COMMAND = <NUM_LIT> <EOL> _REGISTER_CONTROL = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_TIMING = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_INTERRUPT_CONTROL = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_INT_SOURCE = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_ID = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_GAIN = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_INTERRUPT_LOW_THRESH_LOW_BYTE = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_INTERRUPT_LOW_THRESH_HIGH_BYTE = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_INTERRUPT_HIGH_THRESH_LOW_BYTE = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_INTERRUPT_HIGH_THRESH_HIGH_BYTE = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_DATA_GREEN_LOW = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_DATA_GREEN_HIGH = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_DATA_RED_LOW = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_DATA_RED_HIGH = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_DATA_BLUE_LOW = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_DATA_BLUE_HIGH = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_DATA_CLEAR_LOW = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_DATA_CLEAR_HIGH = _REGISTER_COMMAND | <NUM_LIT> <EOL> _REGISTER_INTERRUPT_CLEAR = _REGISTER_COMMAND | <NUM_LIT> <EOL> _CONTROL_ADC_IS_VALID = <NUM_LIT> <EOL> _CONTROL_ADC_ENABLE = <NUM_LIT> <EOL> _CONTROL_ADC_DISABLE = <NUM_LIT> <EOL> _CONTROL_ADC_POWER_ON = <NUM_LIT> <EOL> _CONTROL_ADC_POWER_OFF = <NUM_LIT> <EOL> _TIMING_SYNC_EDGE = <NUM_LIT> <EOL> _TIMING_INTEGRATION_MODE_CONTINUOUS = <NUM_LIT> <EOL> _TIMING_INTEGRATION_MODE_MANUAL = <NUM_LIT> <EOL> _TIMING_INTEGRATION_MODE_SYNC_SINGLE_PULSE = <NUM_LIT> <EOL> _TIMING_INTEGRATION_MODE_SYNC_MULTIPLE_PULSE = <NUM_LIT> <EOL> _TIMING_PARAM_INTEGRATION_TIME_12MS = <NUM_LIT> <EOL> _TIMING_PARAM_INTEGRATION_TIME_100MS = <NUM_LIT> <EOL> _TIMING_PARAM_INTEGRATION_TIME_400MS = <NUM_LIT> <EOL> _TIMING_PARAM_SYNC_PULSE_COUNT_1 = <NUM_LIT> <EOL> _TIMING_PARAM_SYNC_PULSE_COUNT_2 = <NUM_LIT> <EOL> _TIMING_PARAM_SYNC_PULSE_COUNT_4 = <NUM_LIT> <EOL> _TIMING_PARAM_SYNC_PULSE_COUNT_8 = <NUM_LIT> <EOL> _TIMING_PARAM_SYNC_PULSE_COUNT_16 = <NUM_LIT> <EOL> _TIMING_PARAM_SYNC_PULSE_COUNT_32 = <NUM_LIT> <EOL> _TIMING_PARAM_SYNC_PULSE_COUNT_64 = <NUM_LIT> <EOL> _TIMING_PARAM_SYNC_PULSE_COUNT_128 = <NUM_LIT> <EOL> _TIMING_PARAM_SYNC_PULSE_COUNT_256 = <NUM_LIT> <EOL> _INTERRUPT_CONTROL_MODE_DISABLE = <NUM_LIT> <EOL> _INTERRUPT_CONTROL_MODE_LEVEL = <NUM_LIT> <EOL> _INTERRUPT_CONTROL_MODE_SMB_ALERT = <NUM_LIT> <EOL> _INTERRUPT_CONTROL_PERSIST_EVERY_CYCLE = <NUM_LIT> <EOL> _INTERRUPT_CONTROL_PERSIST_OUTSIDE_RANGE_ONCE = <NUM_LIT> <EOL> _INTERRUPT_CONTROL_PERSIST_OUTSIDE_RANGE_100MS = <NUM_LIT> <EOL> _INTERRUPT_CONTROL_PERSIST_OUTSIDE_RANGE_1000MS = <NUM_LIT> <EOL> _INTERRUPT_SOURCE_GREEN = <NUM_LIT> <EOL> _INTERRUPT_SOURCE_RED = <NUM_LIT> <EOL> _INTERRUPT_SOURCE_BLUE = <NUM_LIT> <EOL> _INTERRUPT_SOURCE_CLEAR = <NUM_LIT> <EOL> _GAIN_1X = <NUM_LIT> <EOL> _GAIN_4X = <NUM_LIT> <EOL> _GAIN_16X = <NUM_LIT> <EOL> _GAIN_64X = <NUM_LIT> <EOL> _PRESCALER_1 = <NUM_LIT> <EOL> _PRESCALER_2 = <NUM_LIT> <EOL> _PRESCALER_4 = <NUM_LIT> <EOL> _PRESCALER_8 = <NUM_LIT> <EOL> _PRESCALER_16 = <NUM_LIT> <EOL> _PRESCALER_32 = <NUM_LIT> <EOL> _PRESCALER_64 = <NUM_LIT> <EOL> _SLEEP_VALUE = <NUM_LIT> <EOL> def __init__ ( self , bus_number = None ) : <EOL> """<STR_LIT>""" <EOL> if bus_number is None : <EOL> board_revision = RPi . GPIO . RPI_REVISION <EOL> if board_revision == <NUM_LIT:2> or board_revision == <NUM_LIT:3> : <EOL> bus_number = <NUM_LIT:1> <EOL> else : <EOL> bus_number = <NUM_LIT:0> <EOL> self . bus = smbus . SMBus ( bus_number ) <EOL> self . use_continuous_integration ( ) <EOL> self . set_gain_and_prescaler ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def use_continuous_integration ( self , integration_time_in_ms = <NUM_LIT:12> ) : <EOL> """<STR_LIT>""" <EOL> assert integration_time_in_ms == <NUM_LIT:12> or integration_time_in_ms == <NUM_LIT:100> or integration_time_in_ms == <NUM_LIT> , "<STR_LIT>" <EOL> if integration_time_in_ms == <NUM_LIT:12> : <EOL> integration_time_reg = self . _TIMING_PARAM_INTEGRATION_TIME_12MS <EOL> elif integration_time_in_ms == <NUM_LIT:100> : <EOL> integration_time_reg = self . _TIMING_PARAM_INTEGRATION_TIME_100MS <EOL> elif integration_time_in_ms == <NUM_LIT> : <EOL> integration_time_reg = self . _TIMING_PARAM_INTEGRATION_TIME_400MS <EOL> else : <EOL> integration_time_reg = self . _TIMING_PARAM_INTEGRATION_TIME_12MS <EOL> self . bus . write_i2c_block_data ( self . _I2C_SENSOR_ADDRESS , <EOL> self . _REGISTER_TIMING , <EOL> [ self . _TIMING_INTEGRATION_MODE_CONTINUOUS | integration_time_reg ] ) <EOL> time . sleep ( self . _SLEEP_VALUE ) <EOL> def use_manual_integration ( self ) : <EOL> """<STR_LIT>""" <EOL> self . bus . write_i2c_block_data ( self . _I2C_SENSOR_ADDRESS , <EOL> self . _REGISTER_TIMING , <EOL> [ self . _TIMING_INTEGRATION_MODE_MANUAL ] ) <EOL> time . sleep ( self . _SLEEP_VALUE ) <EOL> def set_gain_and_prescaler ( self , gain_multiplier = <NUM_LIT:1> , prescaler_divider = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> assert gain_multiplier == <NUM_LIT:1> or gain_multiplier == <NUM_LIT:4> or gain_multiplier == <NUM_LIT:16> or gain_multiplier == <NUM_LIT:64> , "<STR_LIT>" <EOL> assert prescaler_divider == <NUM_LIT:1> or prescaler_divider == <NUM_LIT:2> or prescaler_divider == <NUM_LIT:4> or prescaler_divider == <NUM_LIT:8> or prescaler_divider == <NUM_LIT:16> or prescaler_divider == <NUM_LIT:32> or prescaler_divider == <NUM_LIT:64> , "<STR_LIT>" <EOL> if gain_multiplier == <NUM_LIT:1> : <EOL> gain_reg = self . _GAIN_1X <EOL> elif gain_multiplier == <NUM_LIT:4> : <EOL> gain_reg = self . _GAIN_4X <EOL> elif gain_multiplier == <NUM_LIT:16> : <EOL> gain_reg = self . _GAIN_16X <EOL> elif gain_multiplier == <NUM_LIT:64> : <EOL> gain_reg = self . _GAIN_64X <EOL> else : <EOL> gain_reg = self . _GAIN_1X <EOL> if prescaler_divider == <NUM_LIT:1> : <EOL> prescaler_reg = self . _PRESCALER_1 <EOL> elif prescaler_divider == <NUM_LIT:2> : <EOL> prescaler_reg = self . _PRESCALER_2 <EOL> elif prescaler_divider == <NUM_LIT:4> : <EOL> prescaler_reg = self . _PRESCALER_4 <EOL> elif prescaler_divider == <NUM_LIT:8> : <EOL> prescaler_reg = self . _PRESCALER_8 <EOL> elif prescaler_divider == <NUM_LIT:16> : <EOL> prescaler_reg = self . _PRESCALER_16 <EOL> elif prescaler_divider == <NUM_LIT:32> : <EOL> prescaler_reg = self . _PRESCALER_32 <EOL> elif prescaler_divider == <NUM_LIT:64> : <EOL> prescaler_reg = self . _PRESCALER_64 <EOL> else : <EOL> prescaler_reg = self . _PRESCALER_1 <EOL> self . bus . write_i2c_block_data ( self . _I2C_SENSOR_ADDRESS , self . _REGISTER_GAIN , [ gain_reg | prescaler_reg ] ) <EOL> time . sleep ( self . _SLEEP_VALUE ) <EOL> def start_integration ( self ) : <EOL> """<STR_LIT>""" <EOL> self . bus . write_i2c_block_data ( <EOL> self . _I2C_SENSOR_ADDRESS , <EOL> self . _REGISTER_CONTROL , <EOL> [ self . _CONTROL_ADC_ENABLE | self . _CONTROL_ADC_POWER_ON ] ) <EOL> def stop_integration ( self ) : <EOL> """<STR_LIT>""" <EOL> self . bus . write_i2c_block_data ( <EOL> self . _I2C_SENSOR_ADDRESS , <EOL> self . _REGISTER_CONTROL , <EOL> [ self . _CONTROL_ADC_DISABLE | self . _CONTROL_ADC_POWER_ON ] ) <EOL> def is_integration_complete ( self ) : <EOL> """<STR_LIT>""" <EOL> integration_status = self . bus . read_i2c_block_data ( self . _I2C_SENSOR_ADDRESS , self . _REGISTER_CONTROL , <NUM_LIT:1> ) <EOL> return integration_status [ <NUM_LIT:0> ] & self . _CONTROL_ADC_IS_VALID == self . _CONTROL_ADC_IS_VALID <EOL> def read_rgbc_word ( self ) : <EOL> """<STR_LIT>""" <EOL> raw_color = self . bus . read_i2c_block_data ( self . _I2C_SENSOR_ADDRESS , self . _REGISTER_DATA_GREEN_LOW , <NUM_LIT:8> ) <EOL> return ( raw_color [ <NUM_LIT:2> ] + raw_color [ <NUM_LIT:3> ] * <NUM_LIT> , <EOL> raw_color [ <NUM_LIT:0> ] + raw_color [ <NUM_LIT:1> ] * <NUM_LIT> , <EOL> raw_color [ <NUM_LIT:4> ] + raw_color [ <NUM_LIT:5> ] * <NUM_LIT> , <EOL> raw_color [ <NUM_LIT:6> ] + raw_color [ <NUM_LIT:7> ] * <NUM_LIT> ) <EOL> def read_rgbc ( self ) : <EOL> """<STR_LIT>""" <EOL> raw_color = self . bus . read_i2c_block_data ( self . _I2C_SENSOR_ADDRESS , self . _REGISTER_DATA_GREEN_LOW , <NUM_LIT:8> ) <EOL> return ( raw_color [ <NUM_LIT:3> ] , <EOL> raw_color [ <NUM_LIT:1> ] , <EOL> raw_color [ <NUM_LIT:5> ] , <EOL> raw_color [ <NUM_LIT:7> ] ) <EOL> def read_xy ( self ) : <EOL> """<STR_LIT>""" <EOL> rgbc = self . read_rgbc_word ( ) <EOL> x_bar = - <NUM_LIT> * rgbc [ <NUM_LIT:0> ] + <NUM_LIT> * rgbc [ <NUM_LIT:1> ] + - <NUM_LIT> * rgbc [ <NUM_LIT:2> ] <EOL> y_bar = - <NUM_LIT> * rgbc [ <NUM_LIT:0> ] + <NUM_LIT> * rgbc [ <NUM_LIT:1> ] + - <NUM_LIT> * rgbc [ <NUM_LIT:2> ] <EOL> z_bar = - <NUM_LIT> * rgbc [ <NUM_LIT:0> ] + <NUM_LIT> * rgbc [ <NUM_LIT:1> ] + <NUM_LIT> * rgbc [ <NUM_LIT:2> ] <EOL> x = x_bar / ( x_bar + y_bar + z_bar ) <EOL> y = y_bar / ( x_bar + y_bar + z_bar ) <EOL> return [ x , y ] <EOL> def read_color_name ( self ) : <EOL> """<STR_LIT>""" <EOL> xy = self . read_xy ( ) <EOL> closest_color = None <EOL> closest_distance = <NUM_LIT:1> <EOL> for current_color in self . COLOR_TABLE : <EOL> current_coordinates = self . COLOR_TABLE [ current_color ] <EOL> current_dist = math . sqrt ( <EOL> ( current_coordinates [ "<STR_LIT:y>" ] - xy [ <NUM_LIT:1> ] ) ** <NUM_LIT:2> + ( current_coordinates [ "<STR_LIT:x>" ] - xy [ <NUM_LIT:0> ] ) ** <NUM_LIT:2> ) <EOL> if current_dist < closest_distance : <EOL> closest_color = current_color <EOL> closest_distance = current_dist <EOL> return closest_color </s>
