Friday, January 9, 2015

Recording button presses

I had to take a couple minutes to read up on initializing lists. I thought about using the append() method to do this, and I don't think it would really be that hard, but I wanted to go with what I knew, which was working with complete arrays from the start. Maybe because this was so simple, I'll go back and use append() just to make sure I understand it.
The basic idea here is that I wanted to get the button presses recorded into a list so that when the game is functioning I could check to see if the buttons were pressed in the correct order. I've got a few new variables in the program now:
  • MaxLength: This variable just indicates how long the maximum length of the list is going to be. I set it to 5 because I wanted to be able to work through the full list quickly. I'll probably set this to 50 when the game is actually running.
  • Display: I created this just so that I could create a function to display information for debugging, and then turn it off when I don't want it.
  • buttonpush: This is the list that contains the values of the buttons that were pressed. It's initialized as being all zeros and then the values are updated as the program runs.
  • loc: This is the location in the list.
This program is designed so that the kill button has two functions. If there is currently data in the system, then the data is cleared. If there is no data in the system, then it quits.
Here's the code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

def LEDflash(channel):
    GPIO.output(LED[i], GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(LED[i], GPIO.LOW)

def pushed(channel):
    global busy, loc, MaxLength
    if busy == False:
        busy = True
        for i in range(1,5):
            if channel == button[i]:
                pressed = i
                LEDflash(i)
        buttonpush[loc] = pressed
        loc = loc + 1
        ShowData()
        if loc == MaxLength:
            print "MaxLength reached"
            ResetData()
        busy = False

def ResetData():
    global loc, MaxLength, buttonpush
    loc = 0
    buttonpush = [0] * MaxLength
    print "Data cleared"
    ShowData()

def ShowData():
    if Display == True:
        print loc
        print buttonpush

# Game Constants
MaxLength = 5
Display = True

# Label buttons and pins
button = [4, 12, 16, 20, 21]
LED = [22, 5, 6, 13, 19]

# Set GPIO pins
for i in range (0,5):
    GPIO.setup(button[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(LED[i], GPIO.OUT)

for i in range (1,5):
    GPIO.add_event_detect(button[i], GPIO.FALLING, callback=pushed, bouncetime=$

# Initialize variables
busy = False
buttonpush = [0] * MaxLength
loc = 0
game = True

GPIO.output(LED[0], GPIO.HIGH)
while game == True:
    GPIO.wait_for_edge(button[0], GPIO.FALLING)
    if loc == 0:
        game = False
    else:
        ResetData()
GPIO.output(LED[0], GPIO.LOW)
GPIO.cleanup()

I think I am going to use the append() method and rewrite this program again. I think it's going to make the loc variable obsolete because I can use the length of the buttonpush array instead.

No comments:

Post a Comment