ECE 5725 Final Project


Designed by

Jiamin Zeng(jz863), Zhenyan Zhang(zz488)


Code Appendix

final.py


              from pygame.locals import*
              from gpiozero import MotionSensor
              import RPi.GPIO as GPIO
              import time
              import os
              import pygame

              os.putenv('SDL_VIDEODRIVER','fbcon')
              os.putenv('SDL_FBDEV', '/dev/fb1')

              pygame.mixer.pre_init(44100, -16, 2, 2048)
              pygame.mixer.init()
              pygame.init()
              pygame.mouse.set_visible(False)

              filename = "/home/pi/Desktop/final/sound/"

              sound = ['1.wav','2.wav','3.wav','4.wav','5.wav','6.wav','7.wav']

              BLACK = (191,239,255)
              WHITE = (139,35,35)
              Do_COLOR = (255,10,10)
              Re_COLOR = (255,69,0)
              Mi_COLOR = (255,185,15)
              Fa_COLOR = (34,139,34)
              So_COLOR = (0,104,139)
              La_COLOR = (30,144,255)
              Si_COLOR = (85,26,139)


              screen = pygame.display.set_mode((320,240))

              piano = pygame.image.load("/home/pi/Desktop/final/piano.jpg")
              piano = pygame.transform.scale(piano,(320,200))
              piano_rect = piano.get_rect()
              piano_rect = piano_rect.move(0,60)

              my_font_1 = pygame.font.Font(None, 20)
              my_font_2 = pygame.font.Font(None, 30)

              Quit_mark = {'Quit Button->':(280,30)}
              Do_mark   = {'Do':(23,200)}
              Re_mark   = {'Re':(70,200)}
              Mi_mark   = {'Mi':(117,200)}
              Fa_mark   = {'Fa':(160,200)}
              So_mark   = {'So':(207,200)}
              La_mark   = {'La':(250,200)}
              Si_mark   = {'Si':(297,200)}

              for my_text, text_pos in Quit_mark.items():
                Quit_surface = my_font_1.render(my_text, True, WHITE)
                Quit_rect    = Quit_surface.get_rect(center = text_pos)

              for my_text, text_pos in Do_mark.items():
                Do_surface = my_font_2.render(my_text, True, Do_COLOR)
                Do_rect    = Do_surface.get_rect(center = text_pos)

              for my_text, text_pos in Re_mark.items():
                Re_surface = my_font_2.render(my_text, True, Re_COLOR)
                Re_rect    = Re_surface.get_rect(center = text_pos)

              for my_text, text_pos in Mi_mark.items():
                Mi_surface = my_font_2.render(my_text, True, Mi_COLOR)
                Mi_rect    = Mi_surface.get_rect(center = text_pos)

              for my_text, text_pos in Fa_mark.items():
                Fa_surface = my_font_2.render(my_text, True, Fa_COLOR)
                Fa_rect    = Fa_surface.get_rect(center = text_pos)

              for my_text, text_pos in So_mark.items():
                So_surface = my_font_2.render(my_text, True, So_COLOR)
                So_rect    = So_surface.get_rect(center = text_pos)

              for my_text, text_pos in La_mark.items():
                La_surface = my_font_2.render(my_text, True, La_COLOR)
                La_rect    = La_surface.get_rect(center = text_pos)

              for my_text, text_pos in Si_mark.items():
                Si_surface = my_font_2.render(my_text, True, Si_COLOR)
                Si_rect    = Si_surface.get_rect(center = text_pos)

              screen.fill(BLACK)
              screen.blit(piano,piano_rect)
              screen.blit(Quit_surface,Quit_rect)
              pygame.display.flip()

              QuitButton = False

              pir_Do = MotionSensor(21)
              pir_Re = MotionSensor(20)
              pir_Mi = MotionSensor(16)
              pir_Fa = MotionSensor(12)
              pir_So = MotionSensor(23)
              pir_La = MotionSensor(13)
              pir_Si = MotionSensor(6)

              print("Waiting for PIR to settle")
              pir_Do.wait_for_no_motion()
              pir_Re.wait_for_no_motion()
              pir_Mi.wait_for_no_motion()
              pir_Fa.wait_for_no_motion()
              pir_So.wait_for_no_motion()
              pir_La.wait_for_no_motion()
              pir_Si.wait_for_no_motion()

              print("Ready")

              GPIO.setmode(GPIO.BCM)

              keys = [21,20,16,12,23,13,6,17]

              for key in keys:
                GPIO.setup(key, GPIO.IN, pull_up_down=GPIO.PUD_UP)

              def GPIO21_callback(channel):
                print "Do detected"
                key = filename + str(sound[0])
                pygame.mixer.music.load(key)
                pygame.mixer.music.play(0)

                screen.blit(Do_surface,Do_rect)
                pygame.display.flip()
                time.sleep(0.5)
                screen.fill(BLACK)
                screen.blit(piano,piano_rect)
                screen.blit(Quit_surface,Quit_rect)
                pygame.display.flip()

              def GPIO20_callback(channel):
                print "Re detected"
                key = filename + str(sound[1])
                pygame.mixer.music.load(key)
                pygame.mixer.music.play(0)

                screen.blit(Re_surface,Re_rect)
                pygame.display.flip()
                time.sleep(0.5)
                screen.fill(BLACK)
                screen.blit(piano,piano_rect)
                screen.blit(Quit_surface,Quit_rect)
                pygame.display.flip()


              def GPIO16_callback(channel):
                print "Mi detected"
                key = filename + str(sound[2])
                pygame.mixer.music.load(key)
                pygame.mixer.music.play(0)

                screen.blit(Mi_surface,Mi_rect)
                pygame.display.flip()
                time.sleep(0.5)
                screen.fill(BLACK)
                screen.blit(piano,piano_rect)
                screen.blit(Quit_surface,Quit_rect)
                pygame.display.flip()

              def GPIO12_callback(channel):
                print "Fa detected"
                key = filename + str(sound[3])
                pygame.mixer.music.load(key)
                pygame.mixer.music.play(0)

                screen.blit(Fa_surface,Fa_rect)
                pygame.display.flip()
                time.sleep(0.5)
                screen.fill(BLACK)
                screen.blit(piano,piano_rect)
                screen.blit(Quit_surface,Quit_rect)
                pygame.display.flip()

              def GPIO23_callback(channel):
                print "So detected"
                key = filename + str(sound[4])
                pygame.mixer.music.load(key)
                pygame.mixer.music.play(0)

                screen.blit(So_surface,So_rect)
                pygame.display.flip()
                time.sleep(0.5)
                screen.fill(BLACK)
                screen.blit(piano,piano_rect)
                screen.blit(Quit_surface,Quit_rect)
                pygame.display.flip()

              def GPIO13_callback(channel):
                print "La detected"
                key = filename + str(sound[5])
                pygame.mixer.music.load(key)
                pygame.mixer.music.play(0)

                screen.blit(La_surface,La_rect)
                pygame.display.flip()
                time.sleep(0.5)
                screen.fill(BLACK)
                screen.blit(piano,piano_rect)
                screen.blit(Quit_surface,Quit_rect)
                pygame.display.flip()

              def GPIO6_callback(channel):
                print "Si detected"
                key = filename + str(sound[6])
                pygame.mixer.music.load(key)
                pygame.mixer.music.play(0)

                screen.blit(Si_surface,Si_rect)
                pygame.display.flip()
                time.sleep(0.5)
                screen.fill(BLACK)
                screen.blit(piano,piano_rect)
                screen.blit(Quit_surface,Quit_rect)
                pygame.display.flip()

              def GPIO17_callback(channel):
                global QuitButton
                print "17 detected"
                QuitButton = True

              GPIO.add_event_detect(21, GPIO.FALLING, callback=GPIO21_callback,bouncetime=1000)
              GPIO.add_event_detect(20, GPIO.FALLING, callback=GPIO20_callback,bouncetime=1000)
              GPIO.add_event_detect(16, GPIO.FALLING, callback=GPIO16_callback,bouncetime=1000)
              GPIO.add_event_detect(12, GPIO.FALLING, callback=GPIO12_callback,bouncetime=1000)
              GPIO.add_event_detect( 6, GPIO.FALLING, callback=GPIO6_callback, bouncetime=1000)
              GPIO.add_event_detect(23, GPIO.FALLING, callback=GPIO23_callback,bouncetime=1000)
              GPIO.add_event_detect(13, GPIO.FALLING, callback=GPIO13_callback,bouncetime=1000)
              GPIO.add_event_detect(17, GPIO.FALLING, callback=GPIO17_callback,bouncetime=1000)

              while (not QuitButton):
                time.sleep(0.02)
                try:
                  zzy = True

                except KeyboardInterrupt:
                  GPIO.cleanup()


              GPIO.cleanup()