import sys, pygame import socket import os import subprocess import time import urllib.request BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) LIGHTBLUE = (204, 229, 255) BLUE = (66, 139, 202) LIGHTORANGE = (255, 121, 30) # width and height reflect dimensions of Raspberry Pi tablet width = 800 height = 480 size = (width, height) screen = pygame.display.set_mode(size) # button width, height, coordinates and buffer buttonWidth = 220 buttonHeight = 140 startbutton_x = 40 startbutton_y = 290 buffer = 30 pygame.init() pygame.display.set_caption("Perforce JamBox") background = pygame.image.load("jambox_banner.jpg") message("Welcome to JamBox!") done = False start_msg = "" bottom_msg = "" clock = pygame.time.Clock() time_to_clear = None def button(msg, x, y, width, height, color, activeColor, action=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x + width > mouse[0] > x and y + height > mouse[1] > y: pygame.draw.rect(screen, activeColor, (x, y, width, height)) if click[0] == 1 and action != None: action() else: pygame.draw.rect(screen, color, (x, y, width, height)) buttonText = pygame.font.Font("AmericanTypeWriterRegular.ttf", 30) text = buttonText.render(msg, 1, WHITE) textRect = text.get_rect() textRect.center = ((x + (width/2)), (y + (height/2))) screen.blit(text, textRect) def message(msg, x=149, y=264): smallText = pygame.font.Font("AmericanTypeWriterRegular.ttf", 25) text = smallText.render(msg, 1 , BLUE) textpos = text.get_rect() textpos.center = (x, y) screen.blit(text, textpos) def goDesktop(): subprocess.call("startx", shell=True) subprocess.call("clear", shell=True) subprocess.call("setterm -cursor off", shell=True) def getIP(): gw = os.popen("ip -4 route show default").read().split() s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if gw.__len__() > 0: s.connect((gw[2], 0)) return s.getsockname()[0] else: return "localhost" def getURL(): url = "http://" + getIP() result = urllib.request.urlopen(url) code = result.getcode() if code == 200: msg = "Get started at " + url else: msg = "Unable to access JamBox URL" return msg def goJam(): global bottom_msg global time_to_clear bottom_msg = getURL() def goTest(): global bottom_msg global time_to_clear try: subprocess.check_call("p4 -p localhost:1666 info > /dev/null", shell=True) bottom_msg = "P4 server is up and running." except: bottom_msg = "P4 server is down." time_to_clear = pygame.time.get_ticks() + 2000 def intro(): message("Welcome to JamBox!") button("Test", startbutton_x, startbutton_y, buttonWidth, buttonHeight, BLUE, LIGHTORANGE, goTest) button("JamBox", startbutton_x + buttonWidth + buffer, startbutton_y, buttonWidth, buttonHeight, BLUE, LIGHTORANGE, goJam) button("Desktop", startbutton_x + 2 * (buttonWidth + buffer), startbutton_y, buttonWidth, buttonHeight, BLUE, LIGHTORANGE, goDesktop) while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: done = True screen.fill(LIGHTBLUE) # scale background to fit raspberry pi touch screen screen.blit(pygame.transform.scale(background, (width, 239)), (0,0)) intro() message(start_msg, 460, 260) message(bottom_msg, 390, 455) if time_to_clear: if pygame.time.get_ticks() >= time_to_clear: bottom_msg = "" time_to_clear = None pygame.display.flip() clock.tick(60) pygame.quit()