Jazzmarazz wrote:Rvan! Can you comment your pygame code for me?
Here it is.
#!/usr/bin/python
#author: friendofmegaman, rvan
#Display captured frame data on the PC. Expects a 0x1E00 header and four
#pixels per byte. Uses pygame, which is faster than pylab's scatter.
import pygame
from pygame.locals import *
#Set upscale factor, so the image can be made larger on the PC.
#This is configurable.
upscale = 2
width = 160
height = 144
#Set default palette. We use the same colors as BGB.
grey = []
grey.append((224,248,208))
grey.append((136,192,112))
grey.append((52, 104, 86))
grey.append((8, 24, 32))
#Load the captured binary file.
fd = open('/path/to/screen.bin', 'rb')
data = fd.read()
fd.close()
#Split the data into frames at the frame header. This will need to be changed
#if using a headerless or 3 pixels per byte format.
frames = data.split('\x1E\x00')
print len(frames), 'frames'
class Pixel:
"""Self-incrementing pixel class. When called with a two-bit color value, an
instance of this class will set the appropriate color at that pixel and
then automatically increment the x and y values."""
x = 0;
y = 0;
def __call__(self, col):
#Iterate over the PC pixels which represent one GB pixel and set the
#color for each.
for upy in range(upscale):
for upx in range(upscale):
window.set_at(((self.x)*upscale+upx,
(self.y)*upscale+upy), grey[col])
#Increment the x and y values as necessary.
self.x +=1
if self.x==160:
self.x = 0
self.y +=1
#Instantiate Pixel.
pixel = Pixel()
#Initialize the pygame window.
window = pygame.display.set_mode((width*upscale, height*upscale))
#(friendofmegaman wrote this loop. These are rvan's comments and may not be
#entirely accurate.)
for frame in frames:
#Find the first complete frame.
if len(frame)!=5760:
continue
#Iterate over each pixel.
for y in range(144):
for x in range(160):
"""This section is what will need to be changed to accommodate a
different pixel format. The important part is the call to pixel(),
which needs to simply be called once for each pixel, in order."""
#Convert the two-dimensional pixel address into a one-dimensional
#global (i.e., whole screen) bit address.
bit_offset = y*320+x*2
#Convert the global bit address into a byte address and a
#within-byte bit address.
array_offset = bit_offset/8
byte_offset = bit_offset%8
#Load the relevant byte from the array and the relevant two bits
#into px.
byte = ord(frame[array_offset])
px = (byte>>byte_offset)&0b11
#Set the color. Remember that Pixel increments automatically.
pixel(px)
#Update the display to show the pixels.
pygame.display.update()
#Leave the display up until the window is closed or escape is pressed.
quit = False
while not quit:
event = pygame.event.wait()
if event.type == pygame.QUIT or event.type == KEYDOWN and \
event.key == K_ESCAPE:
quit = True