#import smbus import spidev import time import pygame import pygame.midi import math import serial from pygame.locals import * from time import sleep #bus = smbus.SMBus(1) #address = 0x60 def DrawScreen(): screen.fill((0,0,0)) pygame.draw.rect(screen, (255,255,255), pygame.Rect(0,421, 640, 20), 1) pygame.draw.circle(screen, (64,64,64), (300,210), 210, 3) pygame.draw.circle(screen, (64,64,64), (300,210), 27, 1) pygame.draw.circle(screen, (64,64,64), (300,210), 73, 1) pygame.draw.circle(screen, (64,64,64), (300,210), 118, 1) pygame.draw.circle(screen, (64,64,64), (300,210), 163, 1) pygame.draw.polygon(screen, (255,255,255), [[600, 4],[580, 24],[590,24],[590,44],[610,44],[610,24],[620,24],[600,4]], 0) font = pygame.font.Font(None,50) s = "N" text1 = font.render(s,0,(255,255,255),(0,0,0)) #screen.fill((255, 255, 255)) screen.blit(text1, (588,42)) return pygame.init() #pygame.midi.init() size = width, height = 640, 450 #screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN|pygame.DOUBLEBUF|pygame.HWSURFACE) screen = pygame.display.set_mode((size)) #background = pygame.Surface(screen.get_size()) #background = background.convert() port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=0.1) spi = spidev.SpiDev() spi.open(0,0) #midiOutput = pygame.midi.Output(2) #midiOutput.set_instrument(2,1) DrawScreen() bearing = 0 note = 74 ox = 0 oy = 0 px = 0 py = 0 zx = 0 zy = 0 minSig = 0 maxSig = 1024 maxBearing = 0 maxSignal = 0 loop = True while loop: adc = spi.xfer2([0x00, 0x00, 0x00]) #print adc data = ((adc[0]&3) << 8) + adc[1] #print data #data=1023 sig = data * 0.623 #sig = 636 pygame.draw.line(screen, (0,0,0), (2,430), (638, 430), 17) pygame.draw.line(screen, (0,255,0), (2,430), (sig, 430), 17) m1 = (minSig * .623)+2 m2 = ((maxSig * .623)+2) pygame.draw.line(screen, (0,0,0), (2,446), (638, 446), 8) pygame.draw.line(screen, (0,255,128), (m1,446), (m2, 446), 8) note = int(data * 0.1) #midiOutput.note_on(note, 127) port.write(chr(18)) c = port.read() if (len(c)>0) : bearing = ord(c) * 360 / 255 #signal = "" #signal = port.read() signal = data diff = maxSig - minSig if (signal maxSig: signal = 1024 else : j2 = signal - minSig j3 = float(j2) / float(diff) signal = j3 * 1024 sig = (signal * 0.175) + 28 #if len(signal) > 0: #print signal #sig = 50 + (ord(signal)/2) #print bearing #print bear255 bradians = math.radians(bearing-90) if ox > 0: pygame.draw.line(screen, (0,0,0), (300,210), (oy, ox), 4) if px > 0: pygame.draw.line(screen, (0,0,0), (300,210), (py, px), 4) sx = 210 + math.sin(bradians) * sig sy = 300 + math.cos(bradians) * sig ox = 210 + math.sin(bradians) * 24 oy = 300 + math.cos(bradians) * 24 bradians = math.radians(bearing+90) px = 210 + math.sin(bradians) * 24 py = 300 + math.cos(bradians) * 24 if (signal > maxSignal) : maxSignal = signal maxBearing = bearing font = pygame.font.Font(None,50) s = "{0:03.0f}".format(bearing) text1 = font.render(s,0,(0,128,255),(0,0,0)) #screen.fill((255, 255, 255)) screen.blit(text1, (0,0)) s = "{0:04.0f}".format(signal) text1 = font.render(s,0,(0,255,128),(0,0,0)) #screen.fill((255, 255, 255)) screen.blit(text1, (0,380)) s = "{0:03.0f}".format(maxBearing) text1 = font.render(s,0,(255,64,0),(0,0,0)) #screen.fill((255, 255, 255)) screen.blit(text1, (570,380)) #if ox > 0: # pygame.draw.line(screen, (0,0,0), (300,200), (oy, ox), 6) if zx > 0: pygame.draw.line(screen, (0,128,255), (zy, zx), (sy, sx), 2) if ox > 0: pygame.draw.line(screen, (255,0,0), (300,210), (oy, ox), 4) if px > 0: pygame.draw.line(screen, (255,255,255), (300,210), (py, px), 4) pygame.display.update() zx = sx zy = sy sleep(.05) #midiOutput.note_off(note, 127) for event in pygame.event.get(): if event.type == pygame.QUIT: loop = False if event.type == KEYDOWN: if event.key == pygame.K_q: loop = False if event.key == pygame.K_c: maxBearing = 0 maxSignal = 0 DrawScreen() if event.key == pygame.K_q: loop = False if event.key == pygame.K_F1: if minSig > 1: minSig -= 1 if event.key == pygame.K_F2: if minSig > 10: minSig -= 10 if event.key == pygame.K_F3: if minSig < 1013: minSig += 10 if event.key == pygame.K_F4: if minSig < 1023: minSig += 1 if event.key == pygame.K_1: if maxSig > 1: maxSig -= 1 if event.key == pygame.K_2: if maxSig > 10: maxSig -= 10 if event.key == pygame.K_3: if maxSig < 1013: maxSig += 10 if event.key == pygame.K_4: if maxSig < 1023: maxSig += 1 #midiOutput.note_off(note, 127) #pygame.midi.quit pygame.display.quit() pygame.quit()