2013-12-11 3 views
4

나는 라즈베리 파이를 가지고 있으며 gpio 핀 중 하나에 펄스를 보내고 있습니다. 따라서이 핀에서 인터럽트를 감지하는 파이썬 코드가 있으며 매초 최대 2 회의 인터럽트가 발생합니다. 이제 파이 게임 응용 프로그램에이 값 (인터럽트의 총 개수)을 전달하려고합니다.파이 게임에서 스레딩 사용

현재 인터럽트를 감지하는 파이썬 코드는 총 번호를 기록합니다. 인터럽트가 감지되면 파일에 인터럽트 한 다음 파이 게임 응용 프로그램이 파일에서 해당 번호를 읽습니다. 따라서 파이 게임 응용 프로그램과 인터럽트 감지 코드가 병렬로 실행되기를 원하기 때문에 스레드를 사용하여 파이 게임에서 인터럽트 감지 코드를 어떻게 통합 할 수 있습니까? 나는 파이 게임이 쓰레드에 안전하지 않은 곳을 읽었다.

인터럽트 검출 내 코드 :

GPIO.setmode(GPIO.BCM) 
count = 0 
GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
def my_callback(channel): 
    file = open('hello.txt','w') 
    global count 
    count += 1 
    file.write(str(count)) 
GPIO.add_event_detect(2,GPIO.BOTH, callback=my_callback) 

while True: 
    print "Waiting for input." 
    sleep(60) 
GPIO.cleanup() 

파이 게임 응용 프로그램 코드 :

pygame.init() 
size=[640,640] 
screen=pygame.display.set_mode(size) 
pygame.display.set_caption("Test") 
done=False 
clock=pygame.time.Clock() 
font = pygame.font.SysFont("consolas", 25, True) 
frame_rate = 20 
frame_count = 0 
count = 0 
while done == False: 
    for event in pygame.event.get(): # User did something 
     if event.type == pygame.QUIT: # If user clicked close 
      done=True # Flag that we are done so we exit this loop 
      pygame.quit() 
      sys.exit() 

    f = open("hello.txt", "r") 
    count = int(f.read()) 
    output_string = "ACTUAL   %s" %count 
    text = font.render(output_string,True,red) 
    screen.blit(text, [250,420]) 
    frame_count += 1 
    clock.tick(frame_rate) 
    pygame.display.flip() 

pygame.quit() 
+0

끄기 - 주제 : 반복 "hello.txt"개방에 대한 여러분의 코드 ... – martineau

답변

5

당신은 사용할 수 있습니다 예를 들어, threadafe Queue 클래스는 스레드가 서로 통신 할 수있게합니다.

quick'n'dirty 예 :

import pygame 
from pygame.color import Color 
from Queue import Queue 
from threading import Thread 

q = Queue() 

def worker(): 
    GPIO.setmode(GPIO.BCM) 
    GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
    def my_callback(channel): 
     q.put(True) 
    GPIO.add_event_detect(2,GPIO.BOTH, callback=my_callback) 

    while True: 
     print "Waiting for input." 
     sleep(60) 
    GPIO.cleanup() 


t = Thread(target=worker) 
t.daemon = True 
t.start() 

pygame.init() 

screen = pygame.display.set_mode([640,640]) 
clock = pygame.time.Clock() 
font = pygame.font.SysFont("consolas", 25, True) 
count = 0 
pygame.display.set_caption("Test") 

done = False 
while not done: 
    screen.fill(Color('black')) 
    for event in pygame.event.get(): # User did something 
     if event.type == pygame.QUIT: # If user clicked close 
      done = True 
    try: 
     q.get() 
     count += 1 
    except: pass 
    output_string = "ACTUAL   %s" % count 
    text = font.render(output_string, True, Color('red')) 
    screen.blit(text, [250,420]) 
    clock.tick(20) 
    pygame.display.flip() 
+0

그래서'pygame' 스레드 안전, 또는 당신의 코드 문제가되지 않는다? – martineau

-1

이것은 당신이 스레드를 할 수있는 아주 간단한 방법이지만, 대신 파이 게임을 사용, 그것은 파이썬에 포함되어있는 "스레드"라이브러리를 사용하여, 그래서 당신은 그것을 얻기 위해 여분의 것을 할 필요가 없습니다.

import thread 
def functionthing(): 
    #coding 
    pass 
thread.start_new(functionthing,(<arguments>)) 
+0

Doug : 질문은 파이 게임과 함께 스레딩을 사용하는 것입니다. 코드에서 대답하지 않습니다. – martineau