#/usr/bin/env python
import serial
import time
import pygame.mixer
import pygame, Buttons
import subprocess
from pygame.locals import *
#Initialize pygame
pygame.init()
sx=2
sy=2
sl=158
sh=36
sz=sh+4
#str1="Dmx mp3 Play"
str1="Play "
class Button_Example:
def __init__(self):
self.main()
def restart(self):
command = "/usr/bin/sudo /sbin/shutdown -r now"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print output
def shutdown(self):
command = "/usr/bin/sudo /sbin/shutdown -h now"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print output
#Create a display
def display(self):
self.screen = pygame.display.set_mode((sl*2+4,250),0,32) #480,320
pygame.display.set_caption(str1)
#Update the display and show the button
def update_display(self):
self.screen.fill(( 0, 0, 64))
#Parameters: surface, color, x, y, length, height, width, text, text_color
self.Button1.create_button(self.screen, (107,142,35), sx, sy+sz*0 , sl, sh, 0, "< 1 >", (255,255,255))
self.Button2.create_button(self.screen, (107,142,35), sx, sy+sz*1 , sl, sh, 0, "< 2 >", (255,255,255))
self.Button3.create_button(self.screen, (107,142,35), sx, sy+sz*2 , sl, sh, 0, "< 3 >", (255,255,255))
self.Button4.create_button(self.screen, (107,142,35),sx+sl, sy+sz*0 , sl, sh, 0,"< 4 >", (255,255,255))
self.Button5.create_button(self.screen, (107,142,35),sx+sl, sy+sz*1 , sl, sh, 0,"< 5 >", (255,255,255))
self.Button6.create_button(self.screen, (107,142,35),sx+sl, sy+sz*2 , sl, sh, 0,"< 6 >", (255,255,255))
self.Button0.create_button(self.screen, (107,142,35), sx, sy+sz*3+6 , sl, sh, 0,"<Stop>", (255,255,255))
#self.Button9.create_button(self.screen, (107, 35,35),sx+sl, sy+sz*3+6 , sl, sh, 0,"<End!>" , (255,255,255))
self.Button9.create_button(self.screen,(107, 35,35),sx+sl, sy+sz*3+6 , sl, sh, 0,"shutdown" , (255,255,255))
pygame.display.flip()
#Run the loop
def main(self):
self.Button1 = Buttons.Button()
self.Button2 = Buttons.Button()
self.Button3 = Buttons.Button()
self.Button4 = Buttons.Button()
self.Button5 = Buttons.Button()
self.Button6 = Buttons.Button()
self.Button0 = Buttons.Button()
self.Button9 = Buttons.Button()
self.display()
pygame.mixer.init()
while True:
self.update_display()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == MOUSEBUTTONDOWN:
if self.Button1.pressed(pygame.mouse.get_pos()):
#print "Give me a command!"
pygame.mixer.music.load('001.mp3')
pygame.mixer.music.play()
pygame.display.set_caption(str1+"< 1 >")
if self.Button2.pressed(pygame.mouse.get_pos()):
pygame.mixer.music.load('002.mp3')
pygame.mixer.music.play()
pygame.display.set_caption(str1+"< 2 >")
if self.Button3.pressed(pygame.mouse.get_pos()):
pygame.mixer.music.load('003.mp3')
pygame.mixer.music.play()
pygame.display.set_caption(str1+"< 3 >")
if self.Button4.pressed(pygame.mouse.get_pos()):
pygame.mixer.music.load('004.mp3')
pygame.mixer.music.play()
pygame.display.set_caption(str1+"< 4 >")
if self.Button5.pressed(pygame.mouse.get_pos()):
pygame.mixer.music.load('005.mp3')
pygame.mixer.music.play()
pygame.display.set_caption(str1+"< 5 >")
if self.Button6.pressed(pygame.mouse.get_pos()):
pygame.mixer.music.load('006.mp3')
pygame.mixer.music.play()
pygame.display.set_caption(str1+"< 6 >")
if self.Button0.pressed(pygame.mouse.get_pos()):
pygame.mixer.music.stop()
pygame.display.set_caption(str1+"<Stop>")
if self.Button9.pressed(pygame.mouse.get_pos()):
pygame.mixer.music.stop()
#pygame.display.set_caption(str1+"End")
pygame.display.set_caption(str1+"<<shutdown>>")
time.sleep(2)
self.shutdown()
pygame.quit()
if __name__ == '__main__':
# con=serial.Serial('/dev/ttyACM0',9600)
pm = pygame.mixer.Sound("start.wav")
pm.play()
obj = Button_Example()