babybigfox
Posts: 15
Joined: Wed Jun 03, 2015 4:13 pm

ap.show_letter('1')

Thu Jun 04, 2015 5:54 am

This problem has now been solved:

************************************************************************************************

Why won't the countdown work?
If you do it for the first time - for the pickle to set up - [*] scroll to n and enter it.
[*] Then scroll to one
You should just be able to scroll to 1 if you have done it before and not deleted the data files
Execute from terminal with sudo
Here's the code:

Code: Select all

#! /usr/bin/python
# Import modules and functions
import os , time , curses , pickle , random
from astro_pi import AstroPi
from time import sleep
import pickle
ap = AstroPi()
ap.clear() # Clear screen...

# Set the data: Variables , Lists , Dictionaries , Functions etc...

# Stored data: (in a pickle)

if not os.path.isfile( 'numberofpeople.dat' ): # If there is not the correct file already exising...
    numberofpeople = 0 # Set the variable
    file = open( 'numberofpeople.dat' , 'wb' ) # Set the setup variable
    pickle.dump( numberofpeople , file ) # Dump the data in the file
    file.close() # Close the file
else: # If the file does exist...
    file = open( 'numberofpeople.dat' , 'rb' ) # Set the setup variable
    numberofpeople = pickle.load( file ) # Load the data
    file.close() # Close the file

if not os.path.isfile( 'besttemp.dat' ): # If there is not the correct file already exising...
    besttemp = 0 # Set the variable
    file = open( 'besttemp.dat' , 'wb' ) # Set the setup variable
    pickle.dump( besttemp , file ) # Dump the data in the file
    file.close() # Close the file
else: # If the file does exist...
    file = open( 'besttemp.dat' , 'rb' ) # Set the setup variable
    besttemp = pickle.load( file ) # Load the data
    file.close() # Close the file

if not os.path.isfile( 'users.dat' ): # If there is not the correct file already exising...
    users = [] # Set the dictionary
    file = open( 'users.dat' , 'wb' ) # Set the setup variable
    pickle.dump( users , file ) # Dump the data in the file
    file.close() # Close the file
else: # If the file does exist...
    file = open( 'users.dat' , 'rb' ) # Set the setup variable
    users = pickle.load( file ) # Load the data
    file.close() # Close the file
# Temporary data:

person = 0 # This will be the current person playing/reading data. Default is 0 if main menu is launched or if the administrator is running.
temporary = []

# Curses setup...
screen = curses.initscr() # Initialising...
screen.keypad(True) # Set up the arrow keys...
curses.cbreak() # Don't wait for enter to be pressed

# Functions:

def menunum(): # The number side of menu layer 1
    running = True # Set running to true
    file = open( 'numberofpeople.dat' , 'rb' ) # Setup variable
    numberofpeople = pickle.load( file ) # Refresh variables
    if numberofpeople >= 1: # If numberofpeople is or is more than one..
        screenchar = 1 # Set the screen to 1
        ap.show_message(str(screenchar)) # Change the screen
        try:
            while running:
                ap.clear() # Clear the screen
                char = screen.getch() # Setup variable
                if char == curses.KEY_RIGHT: # If the right key is pressed
                    file = open( 'numberofpeople.dat' , 'rb' ) # Open the file
                    numberofpeople = pickle.load( file ) # Refresh the data
                    if (screenchar + 1) > numberofpeople:   #######
                        menucharN()                         # Right arrow pressed
                        running = False                     #######
                    else:
                        screenchar += 1                     #######
                        ap.show_message(str(screenchar))    # Right arrow pressed
                elif char == curses.KEY_LEFT:               #######
                    if screenchar == 1:                     #######
                        menucharN()                         # Left arrow pressed
                        running = False                     #######
                    else:
                        screenchar -= 1                     #######
                        ap.show_message(str(screenchar))    # Left arrow pressed
                elif char == 10:                            #######
                    person = screenchar                     #######
                    write()                                 # Enter pressed
                    running = False                         #######

        finally:
            print('') # Just something to stop errors occuring...
    else:
        menucharN() # Go the new menu
        running = False # Stop

def menucharN(): # The new menu item
    running = True # Set running to true
    ap.clear() # Clear the screen
    ap.show_message('N') # Show N on the screen
    try:
        while running:
            char = screen.getch() # Setup variable
            if char == curses.KEY_RIGHT: # If the right arrow is pressed
                menucharA() # Admin menu item
                running = False # Stop
            elif char == curses.KEY_LEFT: # If the left arrow is pressed
                menunum() # Number menu item
                running = False # Stop
            elif char == 10: # If enter pressed
                file = open( 'numberofpeople.dat' , 'rb' ) # Open the data file
                numberofpeople = pickle.load( file ) # Refresh the data
                currentpeople = numberofpeople # Setup variable
                currentpeople += 1 # Add one to current people
                numberofpeople = currentpeople # Setup variable
                file = open( 'numberofpeople.dat' , 'wb' ) # Set the setup variable
                pickle.dump( numberofpeople , file ) # Dump the data in the file
                file = open( 'numberofpeople.dat' , 'rb' ) # Open the file for reading
                numberofpeople = pickle.load( file ) # Refresh the variable
                ap.show_message('New person ' + str(numberofpeople) + ' created!') # Display completion message
                menunum() # Go to the number menu
                running = False # Stop
    finally:
        print('') # Somethong to stop errors occuring with try...

def menucharA(): # The admin menu item
    running = True # Set running to true
    ap.clear() # Clear the screen
    ap.show_message('A') # Show A on the screen
    try:
        while running:
            char = screen.getch() # Setup variable
            if char == curses.KEY_RIGHT: # If the right arrow is pressed
                menunum() # Number menu item/s
            elif char == curses.KEY_LEFT: # If the left key is pressed
                menucharN() # New menu item
            elif char == 10: # If enter key pressed
                admin() # Admin enter
    finally:
        print('') # Just something to stop errors occuring...

def admin(): # Inside the admin menu
    ap.show_message('The best overall temperature for this place to work at is... 21 degrees celcius!') # Display average message

def write(): # Inside the person menu item
    ap.clear()          ###########
    ap.show_letter('9') #
    sleep(1)            #
    ap.show_letter('8') #
    sleep(1)            #
    ap.show_letter('7') #
    sleep(1)            #
    ap.show_letter('6') #
    sleep(1)            #
    ap.show_letter('5') # Countdown...
    sleep(1)            # Begin!
    ap.show_letter('4') #
    sleep(1)            #
    ap.show_letter('3') #
    sleep(1)            #
    ap.show_letter('2') #
    sleep(1)            #
#    ap.show_letter('1') #
    sleep(1)            #
    ap.show_letter('0') #
    sleep(1)            ###########
    # Begin the actual game!
    R = [ 255 , 0 , 0 ] # r = red
    O = [ 0 , 0 , 0 ] # o = black
    dotred = [
    O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O,
    O, O, O, R, R, O, O, O,
    O, O, O, R, R, O, O, O,
    O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O
    ]
# Code for the red dot
    ap.set_pixels(dotred) # Set the pixels to the red dot
    sleep(random.random() * 10 + 3) # Wait for a random time between 3 and 13 seconds
    ap.clear()

ap.clear() # Clear the screen
menunum() # Start the whole program!
Last edited by babybigfox on Fri Jun 05, 2015 5:24 am, edited 1 time in total.

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1665
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: ap.show_letter('1')

Thu Jun 04, 2015 7:48 am

Does this program work for you?

Code: Select all

#!/usr/bin/python
import time
from astro_pi import AstroPi

ap = AstroPi()

for i in reversed(range(0,10)):
    ap.show_letter(str(i))
    time.sleep(1)

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1665
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: ap.show_letter('1')

Thu Jun 04, 2015 8:31 am

Ah no! This is a bug... it took me a while to work out what was going on.
It happens because you send the character 1 into show_message before you send it into show_letter.

So if you add the line below to my example program above, it re-creates the bug. Well done old chap, you found one!

Code: Select all

ap.show_message("1")
I'll reopen the issue on github.

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1665
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: ap.show_letter('1')

Thu Jun 04, 2015 8:50 am

Okay I have fixed it and it's checked in. You'll need to run these commands in order to pull the changes and update the installed copy of the python module (your Pi must be online).

Code: Select all

cd ~/astro-pi-hat
git fetch origin
git reset --hard origin/master
sudo python setup.py install
sudo python3 setup.py install
Your game should then work correctly. Sorry I doubted you :)
We ought to give out prizes for finding bugs actually...

babybigfox
Posts: 15
Joined: Wed Jun 03, 2015 4:13 pm

Re: ap.show_letter('1')

Thu Jun 04, 2015 6:54 pm

Could you resend my code fixed like you said as I am new to python and don't really understand...

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1665
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: ap.show_letter('1')

Thu Jun 04, 2015 6:55 pm

You need to run the commands in the previous message, copy/paste them into a terminal window. Those commands will update the Astro Pi python module which had a bug, once you've done that your code will run as is without need for change.

babybigfox
Posts: 15
Joined: Wed Jun 03, 2015 4:13 pm

Re: ap.show_letter('1')

Thu Jun 04, 2015 7:03 pm

I have updated the libraries like you said and it still comes up with:
Traceback (most recent call last):
File "ReactionPi.py", line 182, in <module>
menunum() # Start the whole program!
File "ReactionPi.py", line 84, in menunum
write() # Enter pressed
File "ReactionPi.py", line 159, in write
ap.show_letter('1') #
File "/usr/local/lib/python2.7/dist-packages/Astro_Pi_HAT-0.0.2-py2.7.egg/astro_pi/astro_pi.py", line 378, in show_letter
self.set_pixels(coloured_pixels)
File "/usr/local/lib/python2.7/dist-packages/Astro_Pi_HAT-0.0.2-py2.7.egg/astro_pi/astro_pi.py", line 197, in set_pixels
raise ValueError('Pixel lists must have 64 elements')
ValueError: Pixel lists must have 64 elements

When it gets to show_letter('1')

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1665
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: ap.show_letter('1')

Thu Jun 04, 2015 7:08 pm

I can see from the error you've posted it's still using an old version of the module. The part that says Astro_Pi_HAT-0.0.2-py2.7.egg.
The version number now is 1.1.3.

You may need to remove the old egg. See if that helps.

Code: Select all

sudo rm -r /usr/local/lib/python2.7/dist-packages/Astro_Pi_HAT-0.0.2-py2.7.egg
Then try to update the module again.

babybigfox
Posts: 15
Joined: Wed Jun 03, 2015 4:13 pm

Re: ap.show_letter('1')

Fri Jun 05, 2015 5:08 am

It works very well. Thanks :D

Return to “Astro Pi”