for value in iter: prev_vals.append(value) while len(prev_vals) > backlog: prev_vals.popleft() # Median part, could be split out vals = list(prev_vals) vals.sort() mid_idx = len(vals) / 2 if len(vals) & 1: yield vals[mid_idx] else: yield (vals[mid_idx - 1] + vals[mid_idx]) / 2.0 import rattler for meas in medians(rattler.measurements()): adjs = [] p = adjs.append # Use Z to determine facing direction if meas.z > 0.8: p("flat, display down") elif meas.z > 0.3: p("tilted, display down") elif meas.z < -0.8: p("flat, display up") elif meas.z < -0.3: p("tilted, display up") # Use X & Y to determine which side is down xy_tilt = ((abs(meas.x) + abs(meas.y)) / 2)
latency_medians = medians(latencies) bg_r = r1.copy().move(0, 0) line_len = min(bg_r.w, bg_r.h) / 2.0 bg_surf = pygame.Surface(bg_r.size, HWSURFACE) bg_surf.fill(bg) draw.circle(bg_surf, cross_cl, bg_r.center, ri(line_len), 1) draw.line(bg_surf, cross_cl, (bg_r.left, bg_r.centery), (bg_r.right, bg_r.centery)) draw.line(bg_surf, cross_cl, (bg_r.centerx, bg_r.top), (bg_r.centerx, bg_r.bottom)) draw.rect(bg_surf, cross_cl, bg_r, 2) n_skips = 0 for meas in rattler.measurements(): lat = latency_medians.next() sys.stdout.write("\x1b[2K\rlatency = %.4fms" % (lat * 1000, )) sys.stdout.flush() # Try to keep synchronized curr_ts = datetime.datetime.now() secs_ago = dt2secs(curr_ts - meas.timestamp) if secs_ago > (lat * 1.25): n_skips += 1 sys.stdout.write(", skips = %d" % (n_skips, )) sys.stdout.flush() continue else: n_skips = 0
latencies = LoopLatencies() latency_medians = medians(latencies) bg_r = r1.copy().move(0, 0) line_len = min(bg_r.w, bg_r.h) / 2.0 bg_surf = pygame.Surface(bg_r.size, HWSURFACE) bg_surf.fill(bg) draw.circle(bg_surf, cross_cl, bg_r.center, ri(line_len), 1) draw.line(bg_surf, cross_cl, (bg_r.left, bg_r.centery), (bg_r.right, bg_r.centery)) draw.line(bg_surf, cross_cl, (bg_r.centerx, bg_r.top), (bg_r.centerx, bg_r.bottom)) draw.rect(bg_surf, cross_cl, bg_r, 2) n_skips = 0 for meas in rattler.measurements(): lat = latency_medians.next() sys.stdout.write("\x1b[2K\rlatency = %.4fms" % (lat * 1000,)) sys.stdout.flush() # Try to keep synchronized curr_ts = datetime.datetime.now() secs_ago = dt2secs(curr_ts - meas.timestamp) if secs_ago > (lat * 1.25): n_skips += 1 sys.stdout.write(", skips = %d" % (n_skips,)) sys.stdout.flush() continue else: n_skips = 0