[spearmint] / spearmint.py Repository:
ViewVC logotype

View of /spearmint.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (download) (as text) (annotate)
Wed Jul 26 13:18:12 2006 UTC (3 years, 3 months ago) by synack
File size: 2253 byte(s)
Initial import
#!/usr/bin/python
import pygame, random, math, sys
from pygame.locals import *

import sys

windowsize = (580, 260)
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode(windowsize)
pygame.display.set_caption("Remote Sonar")
pygame.display.set_icon(pygame.image.load('ball.png'))
clock = pygame.time.Clock()
statusFont = pygame.font.SysFont('Myriad Pro', 24, False, False)
fd = open('sonardata', 'r')
rectlist = []
scanned = []
gray = []
avg = 0

white = (255, 255, 255)

for i in range(0, 255):
	gray.append((i, i, i))

class SweepLine:
	def __init__(self, speed, color, fade):
		self.origin = (windowsize[0] / 2, 0)
		self.speed = speed
		self.direction = 1
		self.color = color
		self.fade = fade

	def drawPoint(self, distance, angle):
		pointX = int(distance * math.cos(angle) + (windowsize[0] / 2))
		pointY = int(distance * math.sin(angle))
		scanned.append([pointX, pointY, 255])
		screen.set_at((pointX, pointY), white)

	def drawScanline(self, angle):
		lineX = 255 * math.cos(angle) + (windowsize[0] / 2)
		lineY = 255 * math.sin(angle)
		pygame.draw.line(screen, self.color, self.origin, (lineX, lineY))

	def update(self, angle, range):
		if angle >= math.pi:
			self.direction = -1
		if angle <= 0:
			self.direction = 1

		for point in scanned:
			point[2] -= self.fade
			if point[2] <= 0:
				scanned.remove(point)
			else:
				screen.set_at((point[0], point[1]), gray[point[2]])

		self.drawScanline(angle)
		self.drawPoint(range, angle)

sweep = SweepLine(0.01, (0, 192, 0), 2)
looping = 1

while looping:
	# Handle events
	for event in pygame.event.get():
		if event.type == QUIT:
			sys.exit(0)
		elif event.type == KEYDOWN:
			if event.key == 273:	# uparrow
				print 'Up'
			elif event.key == 276:	# leftarrow
				print 'Left'
			elif event.key == 274:	# downarrow
				print 'Down'
			elif event.key == 275:	# rightarrow
				print 'Right'

	# Read from the sensor
	line = fd.readline()
	angle = int(line[1:4])
	range = int(line[6:9])

	# Clear the screen
	screen.fill((0, 0, 0))
	rectlist = []

	# Draw!
	sweep.update(math.radians(angle), range)
	screen.blit(statusFont.render(str(range), True, (255, 255, 255)), (windowsize[0] - 90, windowsize[1] - 20))

	# Update the screen
	pygame.display.update()
	clock.tick(60)

synack at csh.rit.edu
ViewVC Help
Powered by ViewVC 1.0.0