def measure(debug=False): global last_rgb_measurement r, g, b, c = tcs.get_raw_data() last_rgb_measurement = [r, b, b, c] if debug: prdbg('R: %5d G: %5d B: %5d C: %5d' % (r, g, b, c)) return r, g, b, c
def rgb_stable(config_rgb): rgb_stable_cnt = config_rgb['stable_cnt'] rgb_stable_dist = config_rgb['stable_dist'] pr('Starting rgb_stable with stable count: %5d, stable_dist: %5d' % (rgb_stable_cnt, rgb_stable_dist)) led_on() while 42: res = get_stable_rgb(rgb_stable_cnt, rgb_stable_dist) prdbg(res) pr('RGB: %25s' % str(res))
def detect_cube_removal(thres): """ Cube is detected by measuring the clear brightness. If the brightness falls below the threshold it is assumed the the cube shields all surrounding light, returns the measured clear reading. """ led_off() while 42: r, g, b, c = measure() if c > thres: prdbg('Cube removed: %4d > %4d' % (c, thres)) return c
def get_station(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) for gpio in DEF_STATION_GPIOS: GPIO.setup(gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) gpios_in = [] for gpio in DEF_STATION_GPIOS: gpios_in.append(GPIO.input(gpio)) for gpios, station in DEF_STATION_GPIO_MAP: prdbg('Trying to match stations: %s %s %s' % (str(gpios_in), str(gpios), str(station))) if tuple(gpios_in) == gpios: pr('Found station: %d' % station) return station raise UndefinedStation('For GPIOs %s no station defined' % str(gpios_in))
def diff(): global tcs while 42: led_on() r, g, b, c = measure() prdbg('R: %5d G: %5d B: %5d C: %5d' % (r, g, b, c)) led_off() r2, g2, b2, c2 = measure() prdbg('R: %5d G: %5d B: %5d C: %5d' % (r2, g2, b2, c2)) rgb = (r, g, b) rgb2 = (r2, g2, b2) rgb_len = get_rgb_length(rgb) rgb2_len = get_rgb_length(rgb2) rgb_diff = get_rgb_distance(rgb, rgb2) clear_diff = abs(c - c2) pr('Len %5d %5d %5d Clear: %5d %5d %5d' % (rgb_len, rgb2_len, rgb_diff, c, c2, clear_diff))
def get_color(rgb, colors, max_rgb_dist): """ Takes an RGB list as argument and matches against DEF_COLORS the closest will be uses as match. Returns None if match distance is larger than max_rgb_distance. """ min_dist = 999999999 match = 0 for color in colors: color_rgb = color[1] dist = get_rgb_distance(rgb, color_rgb) if dist < min_dist: min_dist = dist match = color pr('Found %-15s - Dist %d - Cur. RGB: %-20s RGB %-20s' % (match[0], min_dist, str(rgb), str(match[1]))) if min_dist > max_rgb_dist: prdbg('Max RGB color dist: %d Dist Limit: %d Exiting... ' % (min_dist, max_rgb_dist)) return None return match[0], match[1], min_dist
def get_stable_rgb(count, dist_limit): """ If a number of consecutive (count) RGB measurements is within a maximum distance (dist_limit) the average of all measurements is calculated and returned. """ if count < 2: raise ValueError('Count has to be larger than 2, is %d' % count) while 42: res = [] max_dist = 0 for i in range(count): res.append(measure_rgb(False)) for i in range(count - 1): act_dist = get_rgb_distance(res[i], res[i + 1]) if act_dist > max_dist: max_dist = act_dist if max_dist > dist_limit: prdbg('Max Dist: %d Dist Limit: %d Restarting... ' % (max_dist, dist_limit)) continue break return get_rgb_median(res)
def measure_rgb(debug=False): r, g, b, c = measure(False) if debug: prdbg('R: %5d G: %5d B: %5d' % (r, g, b)) return r, g, b