aboutsummaryrefslogtreecommitdiff
path: root/renderer.py
blob: a52bfd75c7d5a71b0873a70c0dfed28d07a65d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Renderer(object):
  def render_game(self, game_board):
    """
    renderBoard
    @param gameBoard -- dictionary of tuples of location (x,y), 0 indexed from
    the top left of the board.
    @param boardIndex -- 0 for the left board, 1 for the right board.
    """
    raise NotImplementedError

  def color_deref(self, color_str):
    return Color(color_str)

import pygame
from pygame.locals import Color
class PygameRenderer(Renderer):
 
  """
  Based heavily off of PygameRenderer in SmootLight.  Renders Tetris to a 
  pygame Window.
  """

  DISPLAY_SIZE = (1000,1000)
  OFFSET = (100, 100)
  SCALE = 10
  
  def __init__(self):
    pygame.init()
    self.screen = pygame.display.set_mode(self.DISPLAY_SIZE)
    self.background = pygame.Surface(self.screen.get_size())
    self.background = self.background.convert()
    self.background.fill(Color(0,0,0))

  def render_game(self, game_board):
    for (x,y) in game_board:
      pygame.draw.circle(self.background, self.color_deref(game_board[(x,y)]), 
                         (self.OFFSET[0] + x*self.SCALE, self.OFFSET[1] + y*self.SCALE), self.SCALE)
    self.screen.blit(self.background, (0,0))
    pygame.display.flip()

import util
class LedRenderer(Renderer):
  """
  Renderer for the LEDs.  Based heavily on IndoorRenderer in Smootlight and 
  general Smootlight abstraction patterns
  """
  POWER_SUPPLY_IPS = [0,0,0,0] #TODO: Fill in
  SOCK_PORT = 6038
  sockets = {}
 
  def render_game(self, game_board):
    packets = self.map_to_packets(game_board)
    packets_with_destinations = zip(self.POWER_SUPPLY_IPS, packets)
    for (ip, (port, packet)) in packets:
      if not ip in self.sockets:
        self.sockets[ip] = util.getConnectedSocket(ip, self.SOCK_PORT)
      final_packet = util.composePixelStripPacket(packet, port) 
      try:
        self.sockets[ip].send(packet, 0x00)
      except:
        print 'failure sending packet'
  
  def map_to_packets(game_board):
    """
    Performs the mapping between a game_board and a list of (port,packet) pairs.  The port,packet
    pairs should line up with the ip's in IP_ADDRESSES
    """
    #TODO(rcoh): Right this when we decide on a layout