<commit_before>import datetime
import json
import math
import pygame
import time
import smbus
import sys

# RPI version 1, must use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(0)

# This is the address we setup in the Arduino Program
address = 0x04

pygame.init()
size = width, height = 1024, 768
black = 0, 0, 0
white = 255, 255, 255
orange = 255, 102, 51
green = 61, 235, 61

with open('config.json') as config_file:
    config = json.load(config_file)

teams = config['teams']


def wait_for_key():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                return event

def _center(larger, smaller):
    return (larger/2) - (smaller/2)


def cls(color=black):
    screen.fill(color)


def draw_centered(image, dest):
    this_size = image.get_size()
    dest_size = dest.get_size()
    left = _center(dest_size[0], this_size[0])
    top = _center(dest_size[1], this_size[1])
    dest.blit(image, (left, top))


def read_team_and_player():
    try:
        data = bus.read_i2c_block_data(address, 0, 2)
        team = data[0];
        player = data[1];
        return (team, player)
    except IOError:
        return (-1, -1)


print "DISPLAY", pygame.display.get_driver()
screen = pygame.display.set_mode(size)
plain = pygame.font.Font('BebasNeue.otf', 140)
fancy = pygame.font.Font('LobsterTwo-Regular.otf', 140)
title = fancy.render("TriviaBox", True, white)
listen = plain.render("Listen", True, orange)
nums = [fancy.render(str(num), True, white) for num in range(1, 6)]
outer = screen.get_rect()
inner = outer.inflate(-100, -50)
radian = 3.1415926/180.0

if False:
    print "Welcome"
    cls()
    draw_centered(title, screen)
    pygame.display.flip()
    time.sleep(3)

if False:
    print "Listen"
    cls()
    draw_centered(listen, screen)
    pygame.display.flip()
    wait_for_key()


def ray(cx, cy, angle, radius):
    return (cx + (math.cos(angle) * radius), cy + (math.sin(angle) * radius))


print "Clock"
x = _center(width, 0)
y = _center(height, 0)
expected_fps = 8
time_between_frames = (1.0 / float(expected_fps))
inc = 360 / (5 * expected_fps)
end = 0
elapsed = 0.0
now = datetime.datetime.utcnow()
lag_total = 0.0
computed_points = []
team = -1
player = -1
while end < 360:
    cls()

    if end + inc >= 360:
        computed_points.append(ray(x, y, 360 * radian, 300))
    else:
        for p in range(end, end + inc, 5):  # 5 degree steps always
            computed_points.append(ray(x, y, p * radian, 300))

    lag_start = datetime.datetime.utcnow()
    points = [(x, y)]
    points.extend(computed_points)
    points.append((x,y))
    pygame.draw.polygon(screen, green, points)
    left = 4 - int(elapsed)
    draw_centered(nums[left], screen)
    pygame.display.flip()
    lag_end = datetime.datetime.utcnow()
    diff = lag_end - lag_start
    lag = diff.total_seconds()
    # print "LAG:", lag
    lag_total += lag

    time.sleep((0.9 * time_between_frames) - lag)
    elapsed += time_between_frames
    end += inc

    team, player = read_team_and_player()
    if team > -1 and team < 4:
        break

print "Winner: %d, Player: %d" % (team, player)
print

print "NUM %.3f %s / Lag Total: %f" % (elapsed, datetime.datetime.utcnow() - now, lag_total)
<commit_msg>abstracting for local (non Pi) dev<commit_after>import datetime
import json
import math
import pygame
import time
import smbus
import sys

import tty, termios

# RPI version 1, must use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(0)

# This is the address we setup in the Arduino Program
address = 0x04

pygame.init()
#size = width, height = 1024, 768
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
width, height = size
print "CONSOLE", size
black = 0, 0, 0
white = 255, 255, 255
orange = 255, 102, 51
green = 61, 235, 61

with open('config.json') as config_file:
    config = json.load(config_file)

teams = config['teams']


def wait_for_key():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                return event

def _center(larger, smaller):
    return (larger/2) - (smaller/2)


def cls(color=black):
    screen.fill(color)


def draw_centered(image, dest):
    this_size = image.get_size()
    dest_size = dest.get_size()
    left = _center(dest_size[0], this_size[0])
    top = _center(dest_size[1], this_size[1])
    dest.blit(image, (left, top))


def read_team_and_player():
    try:
        data = bus.read_i2c_block_data(address, 0, 2)
        team = data[0];
        player = data[1];
        return (team, player)
    except IOError:
        return (-1, -1)


def read_team_and_player_kb():
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            key = event.key
            if key >= ord('1') and key <= ord('8'):
                ch = event.key - ord('1')
                return (ch % 4, ch / 4)
            else:
                return (-2, -2)
    return (-1, -1)


print "DISPLAY", pygame.display.get_driver()
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
plain = pygame.font.Font('BebasNeue.otf', 140)
fancy = pygame.font.Font('LobsterTwo-Regular.otf', 140)
title = fancy.render("TriviaBox", True, white)
listen = plain.render("Listen", True, orange)
team_text = plain.render(  "Team  : ", True, white)
player_text = plain.render("Player: ", True, white)
answer = plain.render("Give Your Answer", True, orange)
buzz_in = plain.render("Buzz In", True, orange)
nums = [fancy.render(str(num), True, white) for num in range(1, 6)]
outer = screen.get_rect()
inner = outer.inflate(-100, -50)
radian = 3.1415926/180.0

def ray(cx, cy, angle, radius):
    return (cx + (math.cos(angle) * radius), cy + (math.sin(angle) * radius))

def clock(extra_text, offset):
    print "Clock"
    x = _center(width, 0)
    y = _center(height, 0)
    expected_fps = 8
    time_between_frames = (1.0 / float(expected_fps))
    inc = 360 / (5 * expected_fps)
    end = 0
    elapsed = 0.0
    now = datetime.datetime.utcnow()
    lag_total = 0.0
    computed_points = []
    team = -1
    player = -1
    while end < 360:
        cls()

        if end + inc >= 360:
            computed_points.append(ray(x, y, 360 * radian, 300))
        else:
            for p in range(end, end + inc, 5):  # 5 degree steps always
                computed_points.append(ray(x, y, p * radian, 300))

        lag_start = datetime.datetime.utcnow()
        points = [(x, y)]
        points.extend(computed_points)
        points.append((x,y))
        pygame.draw.polygon(screen, green, points)
        left = 4 - int(elapsed)
        draw_centered(nums[left], screen)
        pygame.display.flip()
        lag_end = datetime.datetime.utcnow()
        diff = lag_end - lag_start
        lag = diff.total_seconds()
        # print "LAG:", lag
        lag_total += lag

        time.sleep((0.9 * time_between_frames) - lag)
        elapsed += time_between_frames
        end += inc

        team, player = read_team_and_player_kb()
        if team > -1 and team < 4:
            break

    print "Winner: %d, Player: %d" % (team, player)
    print

    print "NUM %.3f %s / Lag Total: %f" % (elapsed, datetime.datetime.utcnow() - now, lag_total)
    return team, player

def get_answer(team, player):
    cls()
    draw_centered(answer, screen)
    pygame.display.flip()


if False:
    print "Welcome"
    cls()
    draw_centered(title, screen)
    pygame.display.flip()
    time.sleep(3)

if True:
    print "Listen"
    cls()
    draw_centered(listen, screen)
    pygame.display.flip()
    while True:
        team, player = read_team_and_player_kb()
        if team == -1:
            continue
        if team > -1:
            team, player = clock(buzz_in, (0, -200))
        if team > -1:
            get_answer(team, player)

{'fraction_non_alphanumeric': 0.13556496088239067, 'fraction_numerical': 0.032320123124278566, 'mean_word_length': 2.750841750841751, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4836953', 'n_tokens_mistral': 2954, 'n_tokens_neox': 2728, 'n_words': 726}