Exemplo n.º 1
0
def RaspberryPi():
    drawButtonWithColour(buttons[3,3],BRIGHTRED)
    arraysAddColour(buttons[3,3],BRIGHTRED)
    pygame.display.update()
    drawButtonWithColour(buttons[4,3],BRIGHTRED)
    arraysAddColour(buttons[4,3],BRIGHTRED)
    pygame.display.update()
    
    for rows in range(4,7):
        for columns in range(2,6):
            drawButtonWithColour(buttons[columns,rows],BRIGHTRED)
            arraysAddColour(buttons[columns,rows],BRIGHTRED)
            pygame.display.update()
    drawButtonWithColour(buttons[3,7],BRIGHTRED)
    arraysAddColour(buttons[3,7],BRIGHTRED)
    pygame.display.update()
    drawButtonWithColour(buttons[4,7],BRIGHTRED)
    arraysAddColour(buttons[4,7],BRIGHTRED)
    pygame.display.update()
    drawButtonWithColour(buttons[4,2],BRIGHTGREEN)
    arraysAddColour(buttons[4,2],BRIGHTGREEN)
    pygame.display.update()
    drawButtonWithColour(buttons[4,1],BRIGHTGREEN)
    arraysAddColour(buttons[4,1],BRIGHTGREEN)
    pygame.display.update()
    drawButtonWithColour(buttons[5,1],BRIGHTGREEN)
    arraysAddColour(buttons[5,1],BRIGHTGREEN)
    pygame.display.update()

    bci.multiplexing(greenArray,redArray,yellowArray,500)
    bci.turnOffAll()
    for rows in range(0,8):
            for columns in range(0,8):
                drawButtonWithColour(buttons[rows,columns],DARKGRAY)
Exemplo n.º 2
0
def ChristmasTree():
    tree = (buttons[4,0], buttons[3,1], buttons[4,1], buttons[5,1],
            buttons[2,2], buttons[3,2], buttons[4,2], buttons[5,2],
            buttons[6,2], buttons[2,3], buttons[3,3], buttons[4,3],
            buttons[5,3], buttons[6,3], buttons[1,4], buttons[2,4],
            buttons[3,4], buttons[4,4], buttons[5,4], buttons[6,4],
            buttons[7,4], buttons[1,5], buttons[2,5], buttons[3,5],
            buttons[4,5], buttons[5,5], buttons[6,5], buttons[7,5],
            buttons[0,6], buttons[1,6], buttons[2,6], buttons[3,6],
            buttons[5,6], buttons[6,6], buttons[7,6], buttons[4,6],
            buttons[4,7])

    for index in range(0,35):
        drawButtonWithColour(tree[index],BRIGHTGREEN)
        arraysAddColour(tree[index],GREEN)
        pygame.display.update()
    drawButtonWithColour(tree[35],BRIGHTRED)
    arraysAddColour(tree[35],RED)
    pygame.display.update()
    drawButtonWithColour(tree[36],BRIGHTRED)
    arraysAddColour(tree[36],RED)
    pygame.display.update()
    bci.multiplexing(greenArray,redArray,yellowArray,500)
    bci.turnOffAll()
    for button in tree:
        drawButtonWithColour(button,getButtonColour(button))
Exemplo n.º 3
0
def Hi():
    hi = (buttons[1,2], buttons[1,3], buttons[1,4], buttons[1,5],
          buttons[1,6], buttons[2,4], buttons[3,2], buttons[3,3],
          buttons[3,4], buttons[3,5], buttons[3,6], buttons[5,2],
          buttons[6,2], buttons[7,2], buttons[6,3], buttons[6,4],
          buttons[6,5], buttons[6,6], buttons[7,6], buttons[5,6])
    for button in hi:
        drawButtonWithColour(button,BRIGHTYELLOW)
        arraysAddColour(button,BRIGHTYELLOW)
        pygame.display.update()

    bci.multiplexing(greenArray,redArray,yellowArray,500)
Exemplo n.º 4
0
def Sun():
    sun = (buttons[3,2], buttons[4,2], buttons[2,3], buttons[3,3],
           buttons[4,3], buttons[5,3], buttons[2,4], buttons[3,4],
           buttons[4,4], buttons[5,4], buttons[3,5], buttons[4,5])
    for index in range(0,12):
        drawButtonWithColour(sun[index],BRIGHTYELLOW)
        arraysAddColour(sun[index],YELLOW)
        pygame.display.update()
    pygame.display.update()    
    bci.multiplexing(greenArray,redArray,yellowArray,500)
    bci.turnOffAll()
    for button in sun:
        drawButtonWithColour(button,getButtonColour(button))
Exemplo n.º 5
0
def Candle():
    for rows in range(4,8):
        for columns in range(2,5):
            drawButtonWithColour(buttons[columns,rows],BRIGHTYELLOW)
            arraysAddColour(buttons[columns,rows],BRIGHTYELLOW)
            pygame.display.update()
    drawButtonWithColour(buttons[3,2],BRIGHTRED)
    arraysAddColour(buttons[3,2],BRIGHTRED)
    pygame.display.update()
    drawButtonWithColour(buttons[3,3],BRIGHTRED)
    arraysAddColour(buttons[3,3],BRIGHTRED)
    pygame.display.update()
    bci.multiplexing(greenArray,redArray,yellowArray,500)
    bci.turnOffAll()
Exemplo n.º 6
0
def checkForQuit():
    # get all the QUIT events
    for event in pygame.event.get(QUIT):
        # terminate if any QUIT events are present
        terminate() 
        bci.turnOffAll()
        # get all the KEYUP events
    for event in pygame.event.get(KEYUP): 
        if event.key == K_ESCAPE:
            # terminate if the KEYUP event was for the Esc key
            terminate() 
            bci.turnOffAll()
        # put the other KEYUP event objects back
        pygame.event.post(event) 
Exemplo n.º 7
0
def FlashingDot(colour,animationSpeed = 100):
    for rows in range(0,8):
        for columns in range(0,8):
            flashColour = colour
            rectangle = buttons[rows,columns]
            origSurf = DISPLAYSURF.copy()
            flashSurf = pygame.Surface((LEDSIZE, LEDSIZE))
            flashSurf = flashSurf.convert_alpha()
            r, g, b = flashColour
            for start, end, step in ((0, 255, 1), (255, 0, -1)): 
                for alpha in range(start, end, animationSpeed * step):
                    checkForQuit()
                    DISPLAYSURF.blit(origSurf, (0, 0))
                    flashSurf.fill((r, g, b, alpha))
                    DISPLAYSURF.blit(flashSurf, rectangle.topleft)
                    pygame.display.update()
                    FPSCLOCK.tick(FPS)
                    bci.turnOnLed(colour,columns,rows)
            bci.turnOffAll()
            DISPLAYSURF.blit(origSurf, (0, 0))
Exemplo n.º 8
0
def Present():
    present = (buttons[2,5], buttons[4,5], buttons[2,7], buttons[4,7],
               buttons[3,5], buttons[3,6], buttons[3,7], buttons[2,6],
               buttons[4,6], buttons[3,4])
    for index in range(0,4):
        drawButtonWithColour(present[index],BRIGHTRED)
        arraysAddColour(present[index],RED)
        pygame.display.update()
    for index in range(4,9):
        drawButtonWithColour(present[index],BRIGHTYELLOW)
        arraysAddColour(present[index],YELLOW)
        pygame.display.update()
    drawButtonWithColour(present[9],BRIGHTGREEN)
    arraysAddColour(present[9],GREEN)
    pygame.display.update()
    bci.multiplexing(greenArray,redArray,yellowArray,500)
    bci.turnOffAll()

    for button in present:
        drawButtonWithColour(button,getButtonColour(button))
Exemplo n.º 9
0
def FlashDisplay(colour):
    drawAllButtonsWithColour(colour)
    pygame.display.update()
    bci.turnOnAll(colour)
    pygame.time.wait(500)
Exemplo n.º 10
0
def main():
    global FPSCLOCK, yellowArray, redArray, greenArray, bus, piano1, \
           piano2, piano3, piano4, piano5, piano6, piano7, piano8, \
           DISPLAYSURF, CLEAR_SURF, CLEAR_BUTTON, NONE_SURF, \
           NONE_BUTTON, BASICFONT, LEDInitialColour, GREEN_SURF, \
           GREEN_BUTTON, YELLOW_SURF, YELLOW_BUTTON, RED_SURF, \
           RED_BUTTON, SEE_SURF, SEE_BUTTON, BASICTEXTFONT, \
           DEMO_SURF, DEMO_BUTTON, stateOfSelection, previousSelection, \
           GREENMODE, REDMODE, YELLOWMODE, NONEMODE

    # For the LED matrix - we need 3 arrays - one for each colour,
    # where we will keep what values need to be send to the matrix to
    # lit up a pattern
    yellowArray = [0,0,0,0,0,0,0,0]
    greenArray = [0,0,0,0,0,0,0,0]
    redArray = [0,0,0,0,0,0,0,0]
    
    # Initialising the game state
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    pygame.display.set_caption('Pi Light')

    BASICFONT = pygame.font.Font('freesansbold.ttf', 16)
    BASICTEXTFONT = pygame.font.Font('freesansbold.ttf', 30)
    infoSurfOne = BASICFONT.render('Choose your pattern. On your left you can select colours.',
                                   2, WHITE)
    infoSurfTwo = BASICFONT.render('To unselect a button press NONE. When you are finished press SEE.',
                                   2, WHITE)
    infoSurfThree = BASICFONT.render('To clear all selection press CLEAR. To see some demos press DEMO.',
                                     2, WHITE)
    infoRectOne = infoSurfOne.get_rect()
    infoRectOne.topleft = (10, WINDOWHEIGHT - 60)
    infoRectTwo = infoSurfTwo.get_rect()
    infoRectTwo.topleft = (10, WINDOWHEIGHT - 40)
    infoRectThree = infoSurfThree.get_rect()
    infoRectThree.topleft = (10, WINDOWHEIGHT - 20)

    # Sounds to play when a button is pressed
    piano1 = pygame.mixer.Sound('piano-a.wav')
    piano2 = pygame.mixer.Sound('piano-b.wav')
    piano3 = pygame.mixer.Sound('piano-c.wav')
    piano4 = pygame.mixer.Sound('piano-d.wav')
    piano5 = pygame.mixer.Sound('piano-e.wav')
    piano6 = pygame.mixer.Sound('piano-f.wav')
    piano7 = pygame.mixer.Sound('piano-#f.wav')
    piano8 = pygame.mixer.Sound('piano-g.wav')

    # Create the change LED colour buttons
    GREEN_SURF,GREEN_BUTTON = makeText(' GREEN  ',TEXTColour,GREEN,
                                       XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE),
                                       YMARGIN + 1*(LEDSIZE + BUTTONGAPSIZE))
    YELLOW_SURF,YELLOW_BUTTON = makeText('YELLOW',TEXTColour,YELLOW,
                                         XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE),
                                         YMARGIN + 2*(LEDSIZE + BUTTONGAPSIZE))
    RED_SURF,RED_BUTTON = makeText('    RED    ',TEXTColour,RED,
                                   XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE),
                                   YMARGIN + 3*(LEDSIZE + BUTTONGAPSIZE))

    # Create a button to unselect a single LED
    NONE_SURF,NONE_BUTTON = makeText('  NONE   ',TEXTColour,DARKGRAY,
                                     XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE),
                                     YMARGIN + 4*(LEDSIZE + BUTTONGAPSIZE))

    # Create a button to clear the entire board
    CLEAR_SURF,CLEAR_BUTTON = makeText(' CLEAR  ',TEXTColour,GRAY,
                                       XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE),
                                       YMARGIN + 5*(LEDSIZE + BUTTONGAPSIZE))

    # Create a button, which will display the pattern,
    # selected by the user
    SEE_SURF, SEE_BUTTON = makeText('    SEE     ',TEXTColour,BLUE,
                                    XMARGIN + 1.5*(LEDSIZE + BUTTONGAPSIZE),
                                    YMARGIN + 8*(LEDSIZE + BUTTONGAPSIZE))

    # Create a button to dispaly some demos
    DEMO_SURF, DEMO_BUTTON = makeText('   DEMO   ',TEXTColour,PURPLE,
                                      XMARGIN + 4.5*(LEDSIZE + BUTTONGAPSIZE),
                                      YMARGIN + 8*(LEDSIZE + BUTTONGAPSIZE))
    
    # Initialize some variables for a new game
    pattern = [] # stores the pattern of LEDs clicked
    
    # Set the Colour of all the LEDs to be the LEDColour
    initialColour(pattern)
            
    # When false it indicates that the player cannot click on the gray buttons
    drawOnBoard = True

    # Light green LEDs
    GREENMODE = 1

    # Light red LEDs
    REDMODE = 2

    # Light yellow LEDs
    YELLOWMODE = 3

    # Unselect a LED
    NONEMODE = 4

    # Which of the above is selected, initially green 
    stateOfSelection = GREENMODE

    # Which mode was previously selected
    previousSelection = 0
    while True: # main game loop

        bci.turnOffAll()
        # Initialise the chip 
        bci.initialise()
        
        
        clickedButton = None # button that was clicked 
        DISPLAYSURF.fill(backgroundColour)
        drawAllButtons()

        # If something is drawn it is shown on the board
        for button in pattern:
            drawButtonWithColour(button, getFlashColour(button))
                    
        DISPLAYSURF.blit(infoSurfOne, infoRectOne)
        DISPLAYSURF.blit(infoSurfTwo, infoRectTwo)
        DISPLAYSURF.blit(infoSurfThree, infoRectThree)
         
        checkForQuit()
        for event in pygame.event.get(): # event handling loop
            if event.type == MOUSEBUTTONUP and event.button == 1:
                mousex, mousey = event.pos
                clickedButton = getButtonClicked(mousex, mousey)

                # If the pressed button was GREEN - allows user to turn on
                # green LEDs
                if clickedButton == GREEN:
                    flashButtonAnimationBig(clickedButton)
                    stateOfSelection = GREENMODE

                # If the pressed button was GREEN - allows user to turn on
                # yellow LEDs
                elif clickedButton == YELLOW:
                    flashButtonAnimationBig(clickedButton)
                    stateOfSelection = YELLOWMODE

                # If the pressed button was GREEN - allows user to turn on
                # red LEDs
                elif clickedButton == RED:
                    flashButtonAnimationBig(clickedButton)
                    stateOfSelection = REDMODE

                # If the pressed button was GREEN - allows user to turn off
                # all coloured LEDs
                elif clickedButton == DARKGRAY:
                    flashButtonAnimationBig(clickedButton)
                    previousSelection = stateOfSelection
                    stateOfSelection = NONEMODE

                # Clears everything
                elif clickedButton == GRAY:
                    flashButtonAnimationBig(clickedButton)
                    if stateOfSelection == NONEMODE:
                        stateOfSelection = previousSelection
                    pattern = []
                    initialColour(pattern)
                    yellowArray = [0,0,0,0,0,0,0,0]
                    greenArray = [0,0,0,0,0,0,0,0]
                    redArray = [0,0,0,0,0,0,0,0]

                #If the pressed button was SEE - display the pattern 
                elif clickedButton == BLUE:
                    drawOnBoard = False

                    # If nothing has been selected - skip and allow the
                    # to select again
                    if pattern == []:
                        drawOnBoard = True
                    else:
                        pygame.display.update()
                        pygame.time.wait(1000)
                        for button in pattern:
                            drawButtonWithColour(button,
                                                 getButtonColour(button))
                        flashButtonAnimationBig(clickedButton)
                        for button in pattern:
                            flashColour(button)
                            if button != None:
                                bci.turnOnLed(getFlashColour(button),
                                              getButtonRow(button),
                                              getButtonColumn(button))
                            pygame.time.wait(FLASHDELAY)
                        bci.multiplexing(greenArray,redArray,
                                         yellowArray,300)

                        # Turn off all the Leds and clear all arrays 
                        bci.turnOffAll()
                        pattern = []
                        yellowArray = [0,0,0,0,0,0,0,0]
                        greenArray = [0,0,0,0,0,0,0,0]
                        redArray = [0,0,0,0,0,0,0,0]
                        for rows in range(0,8):
                            for columns in range(0,8):
                                buttonsColour[rows,columns] = DARKGRAY
                        pygame.display.update()
                        drawOnBoard = True
                        normalMode = True

                # If the pressed button was DEMO - play the demo
                elif clickedButton == PURPLE:
                    flashButtonAnimationBig(clickedButton)
                    for index in range(0,5):
                        FlashDisplay(BRIGHTYELLOW)
                        drawAllButtonsWithColour(DARKGRAY)

                        # Turn on the Leds
                        bci.turnOffAll()
                        pygame.display.update()
                        FlashDisplay(BRIGHTRED)
                        drawAllButtonsWithColour(DARKGRAY)

                        # Turn off the Leds
                        bci.turnOffAll()
                        pygame.display.update()
                        FlashDisplay(BRIGHTGREEN)
                        drawAllButtonsWithColour(DARKGRAY)

                        # Turn on the Leds
                        bci.turnOffAll()
                        pygame.display.update()
                        index = index + 1

                    # Show some random shapes
                    RaspberryPi()
                    bci.turnOffAll()
                    yellowArray = [0,0,0,0,0,0,0,0]
                    greenArray = [0,0,0,0,0,0,0,0]
                    redArray = [0,0,0,0,0,0,0,0]
                    drawAllButtonsWithColour(DARKGRAY)
                    
                    Hi()
                    bci.turnOffAll()
                    yellowArray = [0,0,0,0,0,0,0,0]
                    greenArray = [0,0,0,0,0,0,0,0]
                    redArray = [0,0,0,0,0,0,0,0]
                    drawAllButtonsWithColour(DARKGRAY)
                   
                    FlashingDot(BRIGHTYELLOW)
                    bci.turnOffAll()
                    
                    ChristmasTree()
                    bci.turnOffAll()
                    yellowArray = [0,0,0,0,0,0,0,0]
                    greenArray = [0,0,0,0,0,0,0,0]
                    redArray = [0,0,0,0,0,0,0,0]
                                      
                    drawAllButtonsWithColour(DARKGRAY)
                    pygame.display.update()
                    FlashingDot(BRIGHTGREEN)
                    bci.turnOffAll()

                    Sun()
                    bci.turnOffAll()
                    yellowArray = [0,0,0,0,0,0,0,0]
                    greenArray = [0,0,0,0,0,0,0,0]
                    redArray = [0,0,0,0,0,0,0,0]
                    drawAllButtonsWithColour(DARKGRAY)
                    pygame.display.update()
                    
                    FlashingDot(BRIGHTRED)
                    bci.turnOffAll()
                    yellowArray = [0,0,0,0,0,0,0,0]
                    greenArray = [0,0,0,0,0,0,0,0]
                    redArray = [0,0,0,0,0,0,0,0]
                    drawAllButtonsWithColour(DARKGRAY)
                    pygame.display.update()
                                        
                    Present()
                    bci.turnOffAll()
                    yellowArray = [0,0,0,0,0,0,0,0]
                    greenArray = [0,0,0,0,0,0,0,0]
                    redArray = [0,0,0,0,0,0,0,0]
                    drawAllButtonsWithColour(DARKGRAY)
                                        
                    Candle()
                    bci.turnOffAll()
                    yellowArray = [0,0,0,0,0,0,0,0]
                    greenArray = [0,0,0,0,0,0,0,0]
                    redArray = [0,0,0,0,0,0,0,0]
                    drawAllButtonsWithColour(DARKGRAY)
                    
                    ChasingLights()
                    bci.turnOffAll()

                # Deal with buttons from the matrix pressed
                else:
                    if drawOnBoard:
                        # Save the current colour for later use 
                        previousColour = getButtonColour(clickedButton)
                        if clickedButton in pattern:
                            changeButtonColour(clickedButton,pattern)
                            arrayChangeColour(clickedButton,previousColour)
                        else:
                            if stateOfSelection != NONEMODE:
                                changeButtonColour(clickedButton,pattern)
                                pattern.append(clickedButton)
                                arraysAdd(clickedButton)
                
        pygame.display.update()
        FPSCLOCK.tick(FPS)
Exemplo n.º 11
0
def ChasingLights():
    for rows in range(0,8):
        if rows % 2 == 0:
            for columns in range(0,10):
                if columns >= 0 and columns < 8:
                    drawButtonWithColour(buttons[columns,rows],
                                         BRIGHTYELLOW)
                    bci.turnOnLed(BRIGHTYELLOW,rows,columns)
                    pygame.display.update()
                if columns > 0 and columns < 9:
                    drawButtonWithColour(buttons[columns - 1,rows],
                                         BRIGHTGREEN)
                    bci.turnOnLed(BRIGHTGREEN,rows,columns - 1)
                    pygame.display.update()
                if columns > 1:
                    drawButtonWithColour(buttons[columns - 2,rows],
                                         BRIGHTRED)
                    bci.turnOnLed(BRIGHTRED,rows,columns - 2)
                    pygame.display.update()
        else:
            for columns in range(8,-3, -1):
                if columns < 8 and columns >= 0:
                    drawButtonWithColour(buttons[columns,rows],
                                         BRIGHTYELLOW)
                    bci.turnOnLed(BRIGHTYELLOW,rows,columns)
                    pygame.display.update()
                if columns < 7 and columns >= -1 :
                    drawButtonWithColour(buttons[columns + 1,rows],
                                         BRIGHTGREEN)
                    bci.turnOnLed(BRIGHTGREEN,rows,columns + 1)
                    pygame.display.update()
                if columns < 6 and columns >= -2:
                    drawButtonWithColour(buttons[columns + 2,rows],
                                         BRIGHTRED)
                    bci.turnOnLed(BRIGHTRED,rows,columns + 2)
                    pygame.display.update()
Exemplo n.º 12
0
#!/usr/bin/env python

import alsaaudio as aa
import smbus
from struct import unpack
import numpy as np
import wave
import time
import Bicolour_Interface


# Initialise matrix
Bicolour_Interface.initialise()
matrix = [0,0,0,0,0,0,0,0]
frequencyRange = []
scaling = [2,2,2,2,2,2,2,2]

# Set up audio

# Open the wave file
wavfile = wave.open('/home/pi/Beethoven_Symphony_n.wav','r')

# We are going to use the frame rate from the file as a
# sample rate in our output
sampleRate = wavfile.getframerate()

# Set the frame size - advisable to be  multiple of 8
frameSize = 2048

# Setup the output 
output = aa.PCM(aa.PCM_PLAYBACK, aa.PCM_NORMAL)