コード例 #1
0
ファイル: eeg.py プロジェクト: TieSoul/tangentlabs
def main():
    #@TODO: make names for all these magic numbers...

    video.init()


    colors = [
        Color(0xff, 0x00, 0x00), # red
        Color(0xff, 0x66, 0x00), # orange
        Color(0xff, 0xff, 0x00), # yellow
        Color(0x00, 0xff, 0x00), # green
        Color(0x00, 0xff, 0xff), # cyan
        Color(0x66, 0x66, 0xff), # blue
        Color(0x99, 0x00, 0x99), # indigo
        Color(0xff, 0x00, 0xff), # violet
        ]
    
        
    maxPower = 500000.0 #(16777215 * 1.0) / 2
    
    eegProtocol = thinkgear.ThinkGearProtocol("com3")
    eeg = eegProtocol.get_packets()
    
    screen = makeWindow(winsize=(640, 480))
    grad = makeGradient()


    chart = video.Surface(480, 100)
    chart.fill(Color(0,0,0))

    value = 0
    center = 50

    maxRaw = 150.0 # only loking for REALLY low power signals
    minRaw = -maxRaw

    while keepLooping():

        packs = eeg.next()
        for d in packs:

            if isinstance(d, thinkgear.ThinkGearRawWaveData):
                # shift one pixel over:
                chart.scroll(-1, 0)
                lastValue = value
                value = int(48.0 * max(minRaw, min(d.value, maxRaw)) / maxRaw)
                draw.line(chart, Color(0xff,0xff,0xff),
                         477, center+lastValue, 478, center+value)

            elif isinstance(d, thinkgear.ThinkGearEEGPowerData):
                data = d.value
                screen.fill(Color(0,0,0))
                for i, v in enumerate(data):
                    x = 100 + (i * 45)
                    w = 40
                    h = max(2, int(340.0 * min(v, maxPower) / maxPower))
                    y = 400 - h
                    draw.rect(screen, colors[i], Rect(x, y, w, h))
            else:
                break
        else: # no break

            # redraw the screen:            
            screen.blit(chart, Rect(50, 200, 0, 0))
            screen.flip()

        pass # end of while
    eegProtocol.io.close()
コード例 #2
0
ファイル: draw.py プロジェクト: gdos/pgreloaded.sdl12
def draw_line (screen):
    wm.set_caption ("draw.line examples")
    draw.line (screen, black, 4, 40, 17, 320)
    draw.line (screen, red, 280, 7, 40, 220, 4)
    draw.line (screen, green, 33, 237, 580, 370, 8)
    draw.line (screen, blue, 0, 0, 640, 480, 16)
コード例 #3
0
ファイル: joystick.py プロジェクト: gdos/pgreloaded.sdl12
def run ():
    video.init ()
    joystick.init ()

    if joystick.num_joysticks () == 0:
        print ("No joysticks found. Exiting.")
        return
    
    screen = video.set_mode (640, 480)
    screen.fill (white)
    screen.flip ()

    wm.set_caption ("Joystick demo")

    crect = pygame2.Rect (400, 300)
    srect = pygame2.Rect (640, 480)
    crect.center = srect.center
    joyrect = pygame2.Rect (4, 4)
    joyrect.center = srect.center

    joy = joystick.Joystick (0)

    btndict = {}
    xoff, yoff = 50, crect.top
    for i in range (joy.num_buttons):
        btndict[i] = [False, pygame2.Rect (xoff, yoff, 20, 20)]
        yoff += 25

    xpart = ((crect.width - joyrect.width) / 2) / 32768.0
    ypart = ((crect.height - joyrect.height) / 2) / 32768.0
    
    okay = True
    while okay:
        for ev in event.get ():
            if ev.type == sdlconst.QUIT:
                okay = False
            elif ev.type == sdlconst.KEYDOWN and ev.key == sdlconst.K_ESCAPE:
                okay = False
            elif ev.type == sdlconst.JOYAXISMOTION and ev.joy == joy.index:
                if ev.axis == 0:
                    # x axis movement
                    if ev.value == 0:
                        joyrect.centerx = crect.centerx
                    else:
                        joyrect.centerx = crect.centerx + ev.value * xpart
                elif ev.axis == 1:
                    # y axis movement
                    if ev.value == 0:
                        joyrect.centery = crect.centery
                    else:
                        joyrect.centery = crect.centery + ev.value * ypart

            elif ev.type == sdlconst.JOYBUTTONDOWN and ev.joy == joy.index:
                btndict[ev.button][0] = True
            elif ev.type == sdlconst.JOYBUTTONUP and ev.joy == joy.index:
                btndict[ev.button][0] = False
            elif ev.type == sdlconst.JOYHATMOTION and ev.joy == joy.index:
                pass
            elif ev.type == sdlconst.JOYBALLMOTION and ev.joy == joy.index:
                pass

            draw.rect (screen, black, crect)
            draw.line (screen, white, crect.midtop, crect.midbottom)
            draw.line (screen, white, crect.midleft, crect.midright)
            draw.rect (screen, green, joyrect)

            for (state, rect) in btndict.values():
                if state:
                    draw.rect (screen, green, rect)
                else:
                    draw.rect (screen, black, rect)
            
            screen.flip ()
    video.quit ()