def test_get_calibrated_values(): """Test against fake calibrated values stored in hardware mock.""" _setup() values = as7262.get_calibrated_values() # Deal with floating point nonsense values = [round(x, 1) for x in values] assert values == CALIBRATED_VALUES
def read_color(self): white_thress = 550 black_thress = 50 try: as7262.soft_reset() hw_code, hw_version, fw_version = as7262.get_version() as7262.set_gain(2) as7262.set_integration_time(0.1) as7262.set_measurement_mode(3) as7262.set_illumination_led(1) values = as7262.get_calibrated_values() as7262.set_measurement_mode(3) as7262.set_illumination_led(0) string = ("{},{},{},{},{},{}").format(*values) colors = string.split(",") r = float(colors[0]) o = float(colors[1]) y = float(colors[2]) g = float(colors[3]) b = float(colors[4]) v = float(colors[5]) float_colors = list(map(float, colors)) max_c = max(float_colors) max_s = 'RED' if o == max_c: max_c = o max_s = 'RED' #'ORANGE' elif r == max_c: max_c = r max_s = 'RED' #'RED' elif y == max_c: max_c = y max_s = 'YELLOW' elif g == max_c: max_c = g max_s = 'GREEN' elif b == max_c: max_c = b max_s = 'BLUE' elif v == max_c: max_c = v max_s = 'BLUE' #'VIOLET' mean = statistics.mean(float_colors) if mean < black_thress: max_s = 'BLACK' elif mean > white_thress: max_s = 'WHITE' self.dictionary["color"] = max_s except: self.infologger.write_error("Error: I2C: reading color.") self.master.status_vector["INFRARED"] = 0 self.dictionary["color"] = None
as7262.soft_reset() as7262.set_gain(64) as7262.set_integration_time(17.857) as7262.set_measurement_mode(2) as7262.set_illumination_led(1) try: input = raw_input except NameError: pass input( "Setting white point baseline.\n\nHold a white sheet of paper ~5cm in front of the sensor and press a key...\n" ) baseline = as7262.get_calibrated_values() time.sleep(1) input("Baseline set. Press a key to continue...\n") sys.stdout.flush() try: while True: values = as7262.get_calibrated_values() values = [ int(x / y * MAX_VALUE) for x, y in zip(list(values), list(baseline)) ] values = [ int(min(value, MAX_VALUE) / MAX_VALUE * BAR_WIDTH) for value in values ]
humidity = ((data0 * 256 + data1) * 125 / 65536.0) - 6 time.sleep(0.3) bus.write_byte(SI7021_BUS_ADDR, TEMP_INST) time.sleep(0.3) data0 = bus.read_byte(SI7021_BUS_ADDR) data1 = bus.read_byte(SI7021_BUS_ADDR) temperature = ((data0 * 256 + data1) * 175.72 / 65536.0) - 46.85 ######## Retrieving spectral data values = as7262.get_calibrated_values() ######## Publishing data to MQTT broker message = json.dumps({ "date": datetime.datetime.now().strftime("%Y%m%d%H%M%S"), "red": values.red, "orange": values.orange, "yellow": values.yellow, "green": values.green, "blue": values.blue,