def run_selected_item(self): # start child process selected = self.selected_item() GPIO_cleanup() proc = subprocess.Popen([sys.executable, selected["file"]], stdout=subprocess.PIPE, close_fds=False) # make proc.stdout a non-blocking file fd = proc.stdout.fileno() fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) finished = False percentage = 0 # percentage of loading # display loading screen until child process wants the led matrix while not proc.poll() and not finished: x_pos = led_matrix.width() y_pos = int(led_matrix.height()/2) - int(loading_text.height) while x_pos >= -loading_text.width: led_matrix.erase() # print "LOADING..." led_matrix.sprite(loading_text, (x_pos, y_pos)) # print progress bar led_matrix.rect((0,y_pos + int(loading_text.height) + 2), (int(percentage*led_matrix.width()), 3), fill=True) led_matrix.show() x_pos -= 1 # read stdout of the game process try: data = proc.stdout.readline() except: data = False # check if child process is ready to take control of matrix if data: game_printout = data.decode("utf-8") # update progress bar if "P**" is given if game_printout[0] == "P" and game_printout[1:-1].isdigit(): new_percentage = int(game_printout[1:-1]) if 0 <= new_percentage <= 100: percentage = int(new_percentage)/100 # update percentage # break out of while loop to let game take over led matrix elif game_printout == "READY\n": finished = True break time.sleep(0.05) led_matrix.erase() # clear the display led_matrix.show() # TODO: find out if we need to clean up led matrix too # wait till child process finishes proc.wait() GPIO_setup() # resetup GPIO
def draw(self): # display menu items pos_y = 0 selected_item = self.selected_item() # display all other items regularly for item in self.items: if pos_y >= led_matrix.height(): # don't diplay items outside of display break if item["title"] == selected_item["title"]: # display selected text scrolling x = self.scrolling_text_pos led_matrix.sprite(selected_item["text"], (x, pos_y)) if self.scrolling_text_clock == self.scrolling_text_cycle: self.scrolling_text_clock = 0 if self.scrolling_text_pos < -selected_item["text"].width: self.scrolling_text_pos = led_matrix.width() - 1 else: self.scrolling_text_pos -= 1 self.scrolling_text_clock += 1 else: led_matrix.sprite(item["text"], (0, pos_y)) pos_y += item["text"].height + 1
sidebar.draw() led_matrix.show() blinking_clock -= 1 time.sleep(.1) elif curr_state == State.IDLE: # display scrolling virtical text y_pos = -title.height while y_pos < led_matrix.height(): # if state changes stop scrolling and go to that state if curr_state != State.IDLE: break # display title in the center of the screen led_matrix.erase() led_matrix.sprite( title, (int(led_matrix.width() / 2) - int(title.width / 2), y_pos)) led_matrix.show() y_pos += 1 time.sleep(.1) elif curr_state == State.RESET: score = 0 stack = None if led_matrix.width() < 16: stack = Stack() else: stack = Stack( 8) # if screen too width only use left half for stack sidebar = None sidebar = Sidebar(8)
) # This clears the led matrix display incase anything is currently being displayed. # 1. Create sprite variables of a face with mouth open and closed from .spr files. mouth_closed = led_matrix.LEDSprite(os.path.abspath("mouth_closed.spr")) mouth_open = led_matrix.LEDSprite(os.path.abspath("mouth_open.spr")) # 2. Make the RaspberryPi say "Hello World" speaker.say("Hello World") # 3. Create a while loop that keeps looping until the Raspberry Pi has stopped talking. while speaker.is_talking(): # 4. Clear the led matrix led_matrix.erase() # 5. Draw the face with its mouth open led_matrix.sprite(mouth_open) # 6. Show the new face on the display and add a delay led_matrix.show() time.sleep(.1) # 7. Clear the led matrix of the previous face led_matrix.erase() # 8. Draw the face with its mouth closed led_matrix.sprite(mouth_closed) #9. Show the new face on the display and add a delay led_matrix.show() time.sleep(.1)
time.sleep(0.01) elif curr_state == IN_GAME: menu.run_selected_item() # run game and wait for it to die curr_state = IN_MENU elif curr_state == KONAMI: from random import shuffle, randint words = ["Brian", "Jason", "Jon", "Joe", "Steph", "Jed", "Tess"] shuffle(words) raspberrySTEM = "RaspberrySTEM" for name in words: sprite = led_matrix.LEDText(name, font_name="large") y_pos = randint(0,led_matrix.height()-sprite.height) x_pos = led_matrix.width() while x_pos >= -sprite.width: led_matrix.erase() led_matrix.sprite(sprite, (x_pos, y_pos)) led_matrix.show() x_pos -= 1 time.sleep(.05) logo = led_matrix.LEDText(raspberrySTEM, font_name="large") y_pos = int(led_matrix.height()/2) - int(logo.height/2) x_pos = led_matrix.width() while x_pos >= -logo.width: led_matrix.erase() led_matrix.sprite(logo, (x_pos, y_pos)) led_matrix.show() x_pos -= 1 time.sleep(0.05) curr_state = IN_MENU
# Create sprite variables of the frames appended to the list frames = [] # originally create an empty list for i in range(1,8): # a for loop that counts from 1 to 7 # add mani.spr sprite to frame list, (where i is our current number 1-7) frames.append(led_matrix.LEDSprite(os.path.abspath("man" + str(i) + ".spr"))) # Create a while loop that loops forever. while True: # Loop through each of the frames for current_sprite in frames: # Erase display to clear previous frame led_matrix.erase() # If the button is pressed we want to display the sprite jumping. if my_button.is_pressed(): # draw the sprite up 2 pixels to simulate it jumping led_matrix.sprite(current_sprite, (0,2)) else: # if not jumping draw the sprite at the origin led_matrix.sprite(current_sprite, (0,0)) # Show sprite on screen led_matrix.show() # 8. Delay to show the frame for a fraction of a second time.sleep(.25)
man1 = led_matrix.LEDSprite(os.path.abspath("man1.spr")) man2 = led_matrix.LEDSprite(os.path.abspath("man2.spr")) man3 = led_matrix.LEDSprite(os.path.abspath("man3.spr")) man4 = led_matrix.LEDSprite(os.path.abspath("man4.spr")) man5 = led_matrix.LEDSprite(os.path.abspath("man5.spr")) man6 = led_matrix.LEDSprite(os.path.abspath("man6.spr")) man7 = led_matrix.LEDSprite(os.path.abspath("man7.spr")) # Put the sprite in a list to make it easy to loop through them. frames = [man1, man2, man3, man4, man5, man6, man7] # Create a while loop that loops forever. while True: # Loop through each of the frames for current_sprite in frames: # Erase display to clear previous frame led_matrix.erase() # Draw current display led_matrix.sprite(current_sprite) # Show frame on screen led_matrix.show() # Delay to show the frame for a fraction of a second time.sleep(.25)
field.new_apple() ret = field.add_striker() if ret == False: state = State.SCORE time.sleep(.1) elif state == State.IDLE: x = led_matrix.width() while x > -title.width: # break if state has changed, (don't wait for scroll to finish) if state != State.IDLE: break led_matrix.erase() led_matrix.sprite(title, (x, led_matrix.height()/2 - (title.height/2))) led_matrix.show() x -= 1 time.sleep(.05) elif state == State.SCORE: led_matrix.erase() led_matrix.text(str(len(field.strikers))) # led_matrix.text(str(len(field.horizontal_strikers) + len(field.vertical_strikers))) led_matrix.show() elif state == State.EXIT: GPIO.cleanup() led_matrix.cleanup() sys.exit(0) else:
led_matrix.show() time.sleep(0.01) elif curr_state == IN_GAME: menu.run_selected_item() # run game and wait for it to die curr_state = IN_MENU elif curr_state == KONAMI: from random import shuffle, randint words = ["Brian", "Jason", "Jon", "Joe", "Steph", "Jed", "Tess"] shuffle(words) for name in words: sprite = led_matrix.LEDText(name, font_name="large") y_pos = randint(0, led_matrix.height() - sprite.height) x_pos = led_matrix.width() while x_pos >= -sprite.width: led_matrix.erase() led_matrix.sprite(sprite, (x_pos, y_pos)) led_matrix.show() x_pos -= 1 time.sleep(.05) logo = led_matrix.LEDText("Ready Set STEM", font_name="large") y_pos = int(led_matrix.height() / 2) - int(logo.height / 2) x_pos = led_matrix.width() while x_pos >= -logo.width: led_matrix.erase() led_matrix.sprite(logo, (x_pos, y_pos)) led_matrix.show() x_pos -= 1 time.sleep(0.05) curr_state = IN_MENU
snake.growing = True # snake starts growing snake.grow_clock = GROW_CYCLES # reset grow clock time.sleep(.20) elif curr_state == State.IDLE: # display horizontal scrolling title title = led_matrix.LEDText("SNAKE", font_name="large") x_pos = led_matrix.width() - 1 y_pos = led_matrix.height()/2 - title.height/2 while x_pos > -title.width - 1: # break if state has changed, so we don't have to wait for it to finish if curr_state != State.IDLE: break led_matrix.erase() led_matrix.sprite(title, (x_pos, 1)) led_matrix.show() time.sleep(.1) x_pos -= 1 elif curr_state == State.RESET: snake = Snake() field = Field(led_matrix.width(), led_matrix.height()) score = 0 led_matrix.erase() snake.draw() field.new_apple(snake) field.draw_apple() led_matrix.show() curr_state = State.PLAYING
snake.growing = True # snake starts growing snake.grow_clock = GROW_CYCLES # reset grow clock time.sleep(.20) elif curr_state == State.IDLE: # display horizontal scrolling title title = led_matrix.LEDText("SNAKE", font_name="large") x_pos = led_matrix.width() - 1 y_pos = led_matrix.height() / 2 - title.height / 2 while x_pos > -title.width - 1: # break if state has changed, so we don't have to wait for it to finish if curr_state != State.IDLE: break led_matrix.erase() led_matrix.sprite(title, (x_pos, 1)) led_matrix.show() time.sleep(.1) x_pos -= 1 elif curr_state == State.RESET: snake = Snake() field = Field(led_matrix.width(), led_matrix.height()) score = 0 led_matrix.erase() snake.draw() field.new_apple(snake) field.draw_apple() led_matrix.show() curr_state = State.PLAYING
# notify menu we are ready for the led matrix print("READY") sys.stdout.flush() while True: if curr_state == State.IDLE: # display scrolling virtical text y_pos = - title.height while y_pos < led_matrix.height(): # if state changes stop scrolling and go to that state if curr_state != State.IDLE: break # display title in the center of the screen led_matrix.erase() led_matrix.sprite(title, (int(led_matrix.width()/2) - int(title.width/2), y_pos)) led_matrix.show() y_pos += 1 time.sleep(.1) elif curr_state == State.PLAYING: # show the blocks led_matrix.erase() draw_blocks() # draw edge lines if not whole screen if LEFT_EDGE != 0: led_matrix.line((LEFT_EDGE, 0), (LEFT_EDGE, HEIGHT-1)) if RIGHT_EDGE != WIDTH: led_matrix.line((RIGHT_EDGE, 0), (RIGHT_EDGE, HEIGHT-1))
def draw(self, pos=None): """Draws piece on led matrix""" if pos is None: led_matrix.sprite(self.sprite, self.pos) else: led_matrix.sprite(self.sprite, pos)
elif field.player_collided_with_apple(): field.new_apple() ret = field.add_striker() if ret == False: state = State.SCORE time.sleep(.1) elif state == State.IDLE: x = led_matrix.width() while x > -title.width: # break if state has changed, (don't wait for scroll to finish) if state != State.IDLE: break led_matrix.erase() led_matrix.sprite(title, (x, led_matrix.height() / 2 - (title.height / 2))) led_matrix.show() x -= 1 time.sleep(.05) elif state == State.SCORE: led_matrix.erase() led_matrix.text(str(len(field.strikers))) # led_matrix.text(str(len(field.horizontal_strikers) + len(field.vertical_strikers))) led_matrix.show() elif state == State.EXIT: GPIO.cleanup() led_matrix.cleanup() sys.exit(0) else:
def draw(self, pos=None): """Draws piece on led matrix""" if pos is None: led_matrix.sprite(self.sprite, self.pos) else: led_matrix.sprite(self.sprite, pos)
my_button = button.Button(4) # Create sprite variables of the frames appended to the list frames = [] # originally create an empty list for i in range(1, 8): # a for loop that counts from 1 to 7 # add mani.spr sprite to frame list, (where i is our current number 1-7) frames.append( led_matrix.LEDSprite(os.path.abspath("man" + str(i) + ".spr"))) # Create a while loop that loops forever. while True: # Loop through each of the frames for current_sprite in frames: # Erase display to clear previous frame led_matrix.erase() # If the button is pressed we want to display the sprite jumping. if my_button.is_pressed(): # draw the sprite up 2 pixels to simulate it jumping led_matrix.sprite(current_sprite, (0, 2)) else: # if not jumping draw the sprite at the origin led_matrix.sprite(current_sprite, (0, 0)) # Show sprite on screen led_matrix.show() # 8. Delay to show the frame for a fraction of a second time.sleep(.25)
# Get the width of the text in pixels. text_width = my_text_sprite.width # this is the number of LEDs wide our my_text_sprite is. # Set the original x value to be the width of the led matrix display. x = led_matrix.width() # this is the number of LEDs wide the display is. # Make another while loop that keeps moving the text to the left. # - When x is less then -text_width, our text will not be on the display anymore. # - When this happens we want to stop this while loop and run through the outside while loop. while x > -text_width: # when x is less then -text_width, our text will not be on the display anymore # Erase the previous drawn text led_matrix.erase() # Draw the text at this current x position led_matrix.sprite(text, (x, 0)) # Show the text on the screen led_matrix.show() # Hold for a second so we can see the text. (If we don't do this the text will move too quickly to see it.) time.sleep(.1) # We will wait 1 second before going through the loop again. # Subtract 1 from x, so the next loop around moves the text one more to the left. x = x - 1 # Yay!! Now our text is continuously scrolling to the left. # Try changing the number of seconds in the delay on step 15. (Or remove the line altogether!)
# create flag to indicate to display some dice initially on start up just_started = True # get base_elevation base_elevation = accel.angles()[2] # set change in angle/acceleration needed to roll dice THRESHOLD = 20 # notify menu we are ready for the led matrix print("READY") sys.stdout.flush() while True: # exit if start button is pressed if GPIO.input(START) == 0 or GPIO.input(SELECT) == 0: led_matrix.cleanup() GPIO.cleanup() sys.exit(0) # roll dice if A button is pressed or accelerometer detects steep enough angle if just_started or GPIO.input(A) == 0 or abs(accel.angles()[2] - base_elevation) > THRESHOLD: led_matrix.erase() # clear old dice values # set a new random die at each matrix for y in range(0, led_matrix.height(), 8): for x in range(0, led_matrix.width(), 8): led_matrix.sprite(random.choice(dice), (x+1,y+1)) just_started = False led_matrix.show()
mouth_open = led_matrix.LEDSprite(os.path.abspath("mouth_open.spr")) # 2. Create a speech object of "Hello World" my_speech = speaker.Speech("Hello World") # 3. Play my_speech my_speech.play() # 4. Create a while loop that keeps looping until the Raspberry Pi has stopped talking. while my_speech.is_playing(): # 5. Clear the led matrix led_matrix.erase() # 6. Draw the face with its mouth open led_matrix.sprite(mouth_open) # 7. Show the new face on the display and add a delay led_matrix.show() time.sleep(.1) # 8. Clear the led matrix of the previous face led_matrix.erase() # 9. Draw the face with its mouth closed led_matrix.sprite(mouth_closed) #10. Show the new face on the display and add a delay led_matrix.show() time.sleep(.1)
moving_right = True # if True it is moving right, if False it is moving left # Create an x variable that indicates the current x position the sprite is on the display x = 0 # initially we want our x value to be 0 # Create a while loop that loops forever. while True: # Loop through each of the frames for current_sprite in frames: # Erase display to clear previous sprite led_matrix.erase() if moving_right: # show the sprite as normal (facing right) led_matrix.sprite(current_sprite, (x,0)) else: # show the sprite flipped vertical (faceing left) flipped_sprite = current_sprite.flipped_vertical() led_matrix.sprite(flipped_sprite, (x,0)) # Show sprite on screen led_matrix.show() # Update the x value to move it left or right if moving_right: x = x + 1 # increase the x if we are moving right else: x = x - 1 # decrease the x if we are moving left # if the sprite has hit the right side of the display and we are moving right, the sprite should switch to moving left