#############################################################################################
# ECE 5725 FINAL PROJECT
# Title: piwatch.py
# Date: May 18, 2017
# Authors: Erissa Irani and Gulnar Mirza
# NetIDs: eli8 and gzm3
# Description: The following code implements our PiWatch project, using GPIO, PyGame, Mplayer, and the piTFT touch screen.
#############################################################################################
import pygame # Import Library and initialize pygame
from pygame.locals import *
import os
import math
import RPi.GPIO as GPIO
import time
import datetime
import sys
import subprocess
from twilio.rest import Client
import random
# set GPIO mode to broadcom numbering
GPIO.setmode(GPIO.BCM)
GPIO.setup(27,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(17,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(22,GPIO.IN,pull_up_down=GPIO.PUD_UP)
# piTFT environment variables
os.putenv('SDL_VIDEODRIVER', 'fbcon') # Display on piTFT
os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB') # Track mouse clicks on piTFT
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')
#############################################################################################
#-------------------------------------------------------------- LIBRARY FUNCTIONS -----------------------------------------------------------------------
#############################################################################################
first_row_y = 160
second_row_y = 200
first_col_x = 80
col_incr = 40
text_y = 40
text_x = 160
# home screen left/right arrows
left_arrow_x = 15
left_arrow_y = 200
right_arrow_x = 305
right_arrow_y = 200
#############################################################################################
#-------------------------------------------------------------- function to draw lock screen -----------------------------------------------------------------------
#############################################################################################
def draw_lock_screen(error, timer_red):
passcode_x=80
passcode_y=100
x_incr=40
# User prompt text
my_font=pygame.font.Font(None,25)
if (error):
text_surface = my_font.render('Passcode Incorrect, try again', True, red)
else:
text_surface = my_font.render('Enter passcode to unlock PiWatch', True, white)
rect = text_surface.get_rect(center=(text_x,text_y))
screen.blit(text_surface, rect)
# Draw number buttons
my_font=pygame.font.Font(None,30)
my_numbers={'0':(first_col_x,first_row_y), '1':(first_col_x + col_incr,first_row_y), '2':(first_col_x + 2*col_incr,first_row_y), '3': (first_col_x + 3*col_incr,first_row_y),
'4':(first_col_x,second_row_y), '5':(first_col_x + col_incr,second_row_y), '6':(first_col_x + 2*col_incr, second_row_y)}
for my_text, text_pos in my_numbers.items():
text_surface = my_font.render('%s'%my_text, True, white)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
# Draw clear button
my_font=pygame.font.Font(None,30)
text_surface = my_font.render('X', True, red)
rect = text_surface.get_rect(center=(first_col_x + 3*col_incr,second_row_y))
screen.blit(text_surface, rect)
# Draw current entry
for i in range(len(currently_entered)):
text_surface=my_font.render('%s'%currently_entered[i],True,yellow)
rect=text_surface.get_rect(center=(passcode_x+x_incr*i,passcode_y))
screen.blit(text_surface,rect)
pygame.draw.rect(screen,white,(60,70,160,50),4)
# Timer global notifier -- red text
if (timer_red == 1):
my_font=pygame.font.Font(None,50)
text_surface = my_font.render('TIMER!', True, red)
rect = text_surface.get_rect(center=(160,120))
screen.blit(text_surface, rect)
pygame.display.flip()
#############################################################################################
#-------------------------------------------------------------- function to check passcode input event ----------------------------------------------------------
#############################################################################################
def passcode_press(pos, error):
# Check which button was pressed, append to current string
x,y = pos
if (y>first_row_y-20 and y<first_row_y+20):
#0-3 pressed
if (x>first_col_x-10 and x<first_col_x+10):
print('0 pressed')
currently_entered.append(str(0))
error=0
elif(x>first_col_x+col_incr-10 and x<first_col_x+col_incr+10):
print('1 pressed')
currently_entered.append(str(1))
error=0
elif(x>first_col_x+2*col_incr-10 and x<first_col_x+2*col_incr+10):
print('2 pressed')
currently_entered.append(str(2))
error=0
elif(x>first_col_x+3*col_incr-10 and x<first_col_x+3*col_incr+10):
print('3 pressed')
currently_entered.append(str(3))
error=0
elif (y>second_row_y-20 and y<second_row_y+20):
#4-X pressed
if (x>first_col_x-10 and x<first_col_x+10):
print('4 pressed')
currently_entered.append(str(4))
error=0
elif(x>first_col_x+col_incr-10 and x<first_col_x+col_incr+10):
print('5 pressed')
currently_entered.append(str(5))
error=0
elif(x>first_col_x+2*col_incr-10 and x<first_col_x+2*col_incr+10):
print('6 pressed')
currently_entered.append(str(6))
error=0
elif(x>first_col_x+3*col_incr-10 and x<first_col_x+3*col_incr+10):
print('x pressed')
# clear last entry
currently_entered.pop(len(currently_entered)-1)
error=0
#x pressed
return error
#############################################################################################
#-------------------------------------------------------------- function to check passcode entry -----------------------------------------------------------------------
#############################################################################################
def check_passcode(list):
check=True
wrong=0
for i in range(0,4):
if list[i]!=passcode[i]:
check=False
if (check==True):
locked=0
list=[]
else:
locked=1
wrong=1
list=[]
screen.fill(black)
pygame.display.flip()
return locked,list,wrong
#############################################################################################
#-------------------------------------------------------------- function to draw home screen -----------------------------------------------------------------------
#############################################################################################
def draw_homescreen(color_list, hs_number, timer_red):
# define app icon colors and locations
hs_one_colors=[red,blue,green]
hs_two_colors = [cyan,pink]
screen.fill(black)
app_y=50
app_x=70
app_deltax=120
app_deltay=80
color_index=0
left_arrow_x = 20
left_arrow_y = 200
right_arrow_x = 300
right_arrow_y = 200
left_circle_x = 130
right_circle_x = 190
circle_y = left_arrow_y + 5
# Draw apps on homescreen 1
my_font=pygame.font.Font(None,30)
if (hs_number is 1):
one_apps={'PiTime':(app_x,app_y),'PiCalc':(app_x+app_deltax,app_y),
'PiMsg':(app_x,app_y + app_deltay)}
for my_text, text_pos in one_apps.items():
pygame.draw.rect(screen,hs_one_colors[color_index],(text_pos[0]-40,text_pos[1]-20,80,40),0)
text_surface=my_font.render('%s'%my_text,True,(255,255,255))
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
color_index+=1
# draw helper circles
pygame.draw.circle(screen,white,(left_circle_x,circle_y),10,0)
pygame.draw.circle(screen,white,(right_circle_x,circle_y),10,2)
# Draw apps on homescreen 2
elif (hs_number is 2):
two_apps = {'PiMusic':(app_x,app_y),
'PiSimon':(app_x+app_deltax,app_y)}
for my_text, text_pos in two_apps.items():
pygame.draw.rect(screen,hs_two_colors[color_index],(text_pos[0]-40,text_pos[1]-20,80,40),0)
text_surface=my_font.render('%s'%my_text,True,(255,255,255))
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
color_index+=1
# draw helper circles
pygame.draw.circle(screen,white,(left_circle_x,circle_y),10,2)
pygame.draw.circle(screen,white,(right_circle_x,circle_y),10,0)
# draw left and right arrows
my_font=pygame.font.Font(None,50)
text_surface = my_font.render('<', True, white)
rect = text_surface.get_rect(center=(left_arrow_x, left_arrow_y))
screen.blit(text_surface, rect)
text_surface = my_font.render('>', True, white)
rect = text_surface.get_rect(center=(right_arrow_x, right_arrow_y))
screen.blit(text_surface, rect)
# Global timer notifier
if (timer_red == 1):
my_font=pygame.font.Font(None,50)
text_surface = my_font.render('TIMER!', True, red)
rect = text_surface.get_rect(center=(160,120))
screen.blit(text_surface, rect)
pygame.display.flip()
#############################################################################################
#-------------------------------------------------------------- function to check homescreen input event -----------------------------------------------------------------------
#############################################################################################
def hs_press(pos,hs_number, current_app):
app_y=50
app_x=70
app_deltax=120
app_deltay=80
x,y = pos
if (y>left_arrow_y-20 and y<left_arrow_y+20):
if (x > left_arrow_x - 20 and x < left_arrow_x + 20):
# left arrow pressed
if (hs_number is 2): # only do it if on rightmost screen
hs_number = 1
elif (x > right_arrow_x - 20 and x < right_arrow_x + 20):
# right arrow pressed
if (hs_number is 1): # only do it if on leftmost screen
hs_number = 2
if (y>app_y-20 and y<app_y+20):
#first row y
if (x>app_x-40 and x<app_x+40):
# first row, first col app pressed
if (hs_number is 1):
# piTime pressed
current_app = 1
elif (hs_number is 2):
# piMusic pressed
current_app = 4
msg = "current_app: " + str(current_app)
print(msg)
elif(x>app_x+app_deltax-40 and x<app_x+app_deltax+40):
# first row, second col app pressed
if (hs_number is 1):
# PiCalc pressed
current_app = 2
elif (hs_number is 2):
# PiDrum pressed
current_app = 5
msg = "current_app: " + str(current_app)
print(msg)
elif (y>app_y+app_deltay-20 and y<app_y+app_deltay+20):
# second row y
if (x>app_x-40 and x<app_x+40):
# second row, first col app pressed
if (hs_number is 1):
# piMsg pressed
current_app = 3
msg = "current_app: " + str(current_app)
print(msg)
return hs_number, current_app
#############################################################################################
#-------------------------------------------------------------- function to draw picalc app -----------------------------------------------------------------------
#############################################################################################
def draw_picalc(list,ind, timer_red):
num_y=100
num_x=50
num_deltax=55
num_deltay=55
op_x=25
op_deltax = 45
# draw number buttons and operators
row_one={'0':(num_x,num_y),'1':(num_x+num_deltax,num_y),
'2':(num_x + 2*num_deltax,num_y), '3':(num_x + 3*num_deltax,num_y),'4':(num_x + 4*num_deltax,num_y),
'5':(num_x, num_y + num_deltay), '6':(num_x + num_deltax, num_y + num_deltay),
'7':(num_x + 2*num_deltax, num_y + num_deltay), '8':(num_x + 3*num_deltax, num_y + num_deltay),
'9':(num_x + 4*num_deltax, num_y + num_deltay),'c':(op_x,num_y+2*num_deltay),
'.':(op_x+op_deltax, num_y + 2*num_deltay),'+':(op_x + 2*op_deltax, num_y + 2*num_deltay),
'-':(op_x + 3*op_deltax, num_y + 2*num_deltay), 'x':(op_x + 4*op_deltax, num_y + 2*num_deltay),
'/':(op_x + 5*op_deltax, num_y + 2*num_deltay),'=':(op_x + 6*op_deltax, num_y + 2*num_deltay)}
for my_text, text_pos in row_one.items():
if ((my_text is '.') or (my_text is '+') or (my_text is '-') or (my_text is 'x') or (my_text is '/')):
my_font=pygame.font.Font(None,50)
color = yellow
elif ((my_text is 'c') or (my_text is '=')):
my_font=pygame.font.Font(None,50)
color = cyan
else:
my_font=pygame.font.Font(None,30)
color = white
text_surface=my_font.render('%s'%my_text,True,color)
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
# display current entry based on index of current operand
my_font=pygame.font.Font(None,25)
my_text = list[ind]
text_pos = (280, 40)
text_surface=my_font.render('%s'%my_text,True,pink)
rect=text_surface.get_rect(midright=text_pos)
screen.blit(text_surface,rect)
pygame.draw.rect(screen,white,(20,20,280,40),2)
# global timer notifier
if (timer_red == 1):
my_font=pygame.font.Font(None,50)
text_surface = my_font.render('TIMER!', True, red)
rect = text_surface.get_rect(center=(160,120))
screen.blit(text_surface, rect)
pygame.display.flip()
#############################################################################################
#-------------------------------------------------------------- function to check picalc input -----------------------------------------------------------------------
#############################################################################################
def picalc_press(pos, list, ind, sign, decimal_pressed):
x,y = pos
num_y=100
num_x=50
num_deltax=55
num_deltay=55
op_x=25
op_deltax = 45
# sign: #0=none, 1=add, 2=subtract, 3=multiply, 4=divide
if (y>num_y-20 and y<num_y+20):
#0-4 pressed
# append to term
if (x>num_x-10 and x<num_x+10):
print('0 pressed')
list[ind]=list[ind]+str(0)
elif(x>num_x+num_deltax-10 and x<num_x+num_deltax+10):
print('1 pressed')
list[ind]=list[ind]+str(1)
elif(x>num_x+2*num_deltax-10 and x<num_x+2*num_deltax+10):
print('2 pressed')
list[ind]=list[ind]+str(2)
elif(x>num_x+3*num_deltax-10 and x<num_x+3*num_deltax+10):
print('3 pressed')
list[ind]=list[ind]+str(3)
elif(x>num_x+4*num_deltax-10 and x<num_x+4*num_deltax+10):
print('4 pressed')
list[ind]=list[ind]+str(4)
print list
elif (y>num_y+num_deltay-20 and y<num_y+num_deltay+20):
#5-9 pressed
# append to term
if (x>num_x-10 and x<num_x+10):
print('5 pressed')
list[ind]=list[ind]+str(5)
elif(x>num_x+num_deltax-10 and x<num_x+num_deltax+10):
print('6 pressed')
list[ind]=list[ind]+str(6)
elif(x>num_x+2*num_deltax-10 and x<num_x+2*num_deltax+10):
print('7 pressed')
list[ind]=list[ind]+str(7)
elif(x>num_x+3*num_deltax-10 and x<num_x+3*num_deltax+10):
print('8 pressed')
list[ind]=list[ind]+str(8)
elif(x>num_x+4*num_deltax-10 and x<num_x+4*num_deltax+10):
print('9 pressed')
list[ind]=list[ind]+str(9)
print list
elif (y>num_y+2*num_deltay-20 and y<num_y+2*num_deltay+20):
#clear/=/./signs pressed
if (x>op_x-10 and x<op_x+10):
print('c pressed')
clear=1
#PiCalc reset
list=['','',''] #first=first term, second=second term, third=answer
ind=0 #0=add numbers to first term; 1=add to second term; 2=for displaying answer (should never be 2 in picalc_calc())
sign=0
elif(x>op_x+op_deltax-10 and x<op_x+op_deltax+10):
print('. pressed')
# safety check if number already has decimal
if (not decimal_pressed):
decimal_pressed=1
list[ind] = list[ind]+ "."
elif(x>op_x+2*op_deltax-10 and x<op_x+2*op_deltax+10):
print('+ pressed')
sign=1
# change to next term
if (ind is 0):
ind = ind + 1
decimal_pressed = 0
# continue calculation
elif (ind is 2):
new_list = ['','','']
new_list[0] = list[ind]
list = new_list
ind = 1
decimal_pressed = 0
elif(x>op_x+3*op_deltax-10 and x<op_x+3*op_deltax+10):
print('- pressed')
sign=2
# change to next term
if (ind is 0):
ind = ind + 1
decimal_pressed = 0
# continue calculation
elif (ind is 2):
new_list = ['','','']
new_list[0] = list[ind]
list = new_list
ind = 1
decimal_pressed = 0
elif(x>op_x+4*op_deltax-10 and x<op_x+4*op_deltax+10):
print('x pressed')
sign=3
# change to next term
if (ind is 0):
ind = ind + 1
decimal_pressed = 0
# continue calculation
elif (ind is 2):
new_list = ['','','']
new_list[0] = list[ind]
list = new_list
ind = 1
decimal_pressed = 0
elif(x>op_x+5*op_deltax-10 and x<op_x+5*op_deltax+10):
print('/ pressed')
sign=4
# change to next term
if (ind is 0):
ind = ind + 1
decimal_pressed = 0
# continue calculation
elif (ind is 2):
new_list = ['','','']
new_list[0] = list[ind]
list = new_list
ind = 1
decimal_pressed = 0
elif(x>op_x+6*op_deltax-10 and x<op_x+6*op_deltax+10):
print('= pressed')
# change to display term
if (ind is 1):
ind = ind + 1
decimal_pressed = 0
# do the math
if (sign is 1):
list[ind] = str(float(list[ind - 2]) + float(list[ind - 1]))
elif (sign is 2):
list[ind] = str(float(list[ind - 2]) - float(list[ind - 1]))
elif (sign is 3):
list[ind] = str(float(list[ind - 2]) * float(list[ind - 1]))
elif (sign is 4):
print ind
print list
# dividing, reset if denominator is 0
if (list[ind - 1] == '0'):
#PiCalc reset
list=['','',''] #first=first term, second=second term, third=answer
ind=0
sign=0
else:
list[ind] = str(float(list[ind - 2]) / float(list[ind - 1]))
return ind,list,sign,decimal_pressed
#############################################################################################
#-------------------------------------------------------------- function to draw pitime app -----------------------------------------------------------------------
#############################################################################################
def draw_pitime(time_screen,stopwatch_stopped, stopwatch_start_time, stopwatch_elapsed_time, stopwatch_prev_time, timer_str, timer_stopped, timer_start_time, timer_str_start, timer_red):
# draw mode buttons
my_buttons={'Clock': (40,210),'Timer': (150,210),'Stopwatch': (260,210)}
my_font=pygame.font.Font(None,25)
for my_text, text_pos in my_buttons.items():
text_surface = my_font.render('%s'%my_text, True, white)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
# Clock screen
if (time_screen==0):
curr_time=time.strftime("%H:%M:%S",time.localtime())
pygame.draw.rect(screen,yellow,(10,190,60,40),2)
my_timefont=pygame.font.Font(None,70)
time_surface=my_timefont.render(str(curr_time),True,yellow)
rect=time_surface.get_rect(center=(160,60))
screen.blit(time_surface,rect)
# Timer screen
elif (time_screen==1):
# if timer is going, update elapsed time and display
if (timer_stopped == 0):
curr_time = time.strftime("%H:%M:%S",time.localtime())
timer_str = timediff_timer(curr_time, timer_start_time, timer_str_start)
# number is drawn in red if timer runs out
if ((timer_str == "00:00:00") and (timer_str_start != "00:00:00") and (timer_stopped == 0)):
timer_red = 1
timer_stopped = 1
# number is drawn in red if timer runs out
if (timer_red == 1):
str_color = red
else: # else draw standard color
str_color = yellow
# draw clear/stop/start buttons
my_buttons2={'Clear':(50,150),'Start':(150,150),'Stop':(250,150)}
for my_text, text_pos in my_buttons2.items():
text_surface = my_font.render('%s'%my_text, True, cyan)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
pygame.draw.rect(screen,yellow,(120,190,60,40),2)
my_timefont=pygame.font.Font(None,70)
time_surface=my_timefont.render(timer_str,True,str_color)
rect=time_surface.get_rect(center=(160,60))
screen.blit(time_surface,rect)
# Stopwatch screen
elif (time_screen==2):
#stopwatch="00:00"
if(not stopwatch_stopped):
# check elapsed time since currently counting update
curr_time = datetime.datetime.now().strftime("%H:%M:%S.%f")
stopwatch_elapsed_time = timediff_stopwatch(stopwatch_start_time, curr_time, stopwatch_prev_time)
# draw clear/stop/start buttons
my_buttons2={'Clear':(50,150),'Start':(150,150),'Stop':(250,150)}
for my_text, text_pos in my_buttons2.items():
text_surface = my_font.render('%s'%my_text, True, cyan)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
pygame.draw.rect(screen,yellow,(210,190,100,40),2)
my_timefont=pygame.font.Font(None,70)
time_surface=my_timefont.render(stopwatch_elapsed_time,True,yellow)
rect=time_surface.get_rect(center=(160,60))
screen.blit(time_surface,rect)
# Global timer notifier
if (timer_red == 1):
my_font=pygame.font.Font(None,50)
text_surface = my_font.render('TIMER!', True, red)
rect = text_surface.get_rect(center=(160,120))
screen.blit(text_surface, rect)
pygame.display.flip()
return timer_str, timer_red, timer_stopped
#############################################################################################
#-------------------------------------------------------------- function to check pitime input -----------------------------------------------------------------------
#############################################################################################
def pitime_press(time_screen,pos,stopwatch_stopped, stopwatch_start_time, stopwatch_elapsed_time, stopwatch_prev_time, timer_str, timer_stopped, timer_start_time, timer_str_start, timer_red):
x,y=pos
mode_x=20
mode_y=210
delta_x=110
# convert timer_str to int values
timer_min = timer_str[3] + timer_str[4]
timer_hour = timer_str[0] + timer_str[1]
timer_sec = timer_str[6] + timer_str[7]
timer_min_int = int(timer_min)
timer_hour_int = int(timer_hour)
timer_sec_int = int(timer_sec)
if (y>mode_y-10 and y<mode_y+20): #bottom row of buttons
if (x>mode_x-10 and x<mode_x+50):
# clock
print('time screen:0')
time_screen=0
if (x>mode_x+delta_x-10 and x<mode_x+delta_x+50):
# timer
print('time screen:1')
time_screen=1
if (x>mode_x+2*delta_x-40 and x<mode_x+2*delta_x+80):
# stopwatch
print('time screen:2')
time_screen=2
# Timer Clear/Start/Stop
if (time_screen==1):
op_x=30
op_y=150
op_deltax=100
if (y>op_y-20 and y<op_y+20): #op buttons
if (x>op_x-10 and x<op_x+30):
print('clear')
timer_stopped = 1
# clear timer str
timer_min_int = 0
timer_hour_int = 0
timer_sec_int = 0
# back to standard color
timer_red = 0
if (x>op_x+op_deltax-10 and x<op_x+op_deltax+40):
print('start')
if (timer_str_start != "00:00:00"):
# start timer and get start time
timer_stopped = 0
timer_start_time = time.strftime("%H:%M:%S",time.localtime())
if (x>op_x+2*op_deltax-20 and x<op_x+2*op_deltax+80):
print('stop')
timer_stopped = 1
# back to standard color
timer_red = 0
# Stopwatch Clear/Start/Stop
if (time_screen==2):
op_x=30
op_y=150
op_deltax=100
if (y>op_y-20 and y<op_y+20): #op buttons
if (x>op_x-10 and x<op_x+40):
print('clear')
# reset stopwatch
stopwatch_stopped = 1
stopwatch_elapsed_time = "00:00:00.000"
stopwatch_start_time = "00:00:00.000"
stopwatch_prev_time = "00:00:00.000"
if (x>op_x+op_deltax-10 and x<op_x+op_deltax+40):
print('start')
stopwatch_stopped = 0
# get start time
stopwatch_start_time = datetime.datetime.now().strftime("%H:%M:%S.%f")
if (x>op_x+2*op_deltax-20 and x<op_x+2*op_deltax+80):
print('stop')
stopwatch_stopped = 1
# calculate elapsed time, updated prev elapsed time
if (stopwatch_start_time != "00:00:00.000"):
curr_time = datetime.datetime.now().strftime("%H:%M:%S.%f")
stopwatch_elapsed_time = timediff_stopwatch(stopwatch_start_time, curr_time, stopwatch_prev_time)
stopwatch_prev_time = stopwatch_elapsed_time
# timer controls for min/sec
if (time_screen==1 and (timer_stopped == 1)):
if (y>40 and y<80):
if (x>130 and x<180):
print('increase minutes')
# back to standard color
timer_red = 0
# increase minutes, modulo 60
if(timer_min_int == 59):
timer_min_int = 0
if (timer_hour_int < 24):
timer_hour_int = timer_hour_int + 1
else:
timer_min_int = timer_min_int + 1
elif (x>200 and x<250):
print('increase seconds')
# back to standard color
timer_red = 0
# increase seconds by 5, modulo 60
if(timer_sec_int == 55):
timer_sec_int = 0
if(timer_min_int == 59):
timer_min_int = 0
if (timer_hour_int < 24):
timer_hour_int = timer_hour_int + 1
else:
timer_min_int = timer_min_int + 1
else:
timer_sec_int = timer_sec_int + 5
elif (x>70 and x<120):
print('increase hours')
# back to standard color
timer_red = 0
# increase hours up to 24
if (timer_hour_int < 24):
timer_hour_int = timer_hour_int + 1
# convert timer ints back to timer_str
timer_min = str(timer_min_int).zfill(2)
timer_hour = str(timer_hour_int).zfill(2)
timer_sec = str(timer_sec_int).zfill(2)
timer_str = timer_hour + ":" + timer_min + ":" + timer_sec
timer_str_start = timer_str
print timer_str
return time_screen, stopwatch_stopped, stopwatch_start_time, stopwatch_elapsed_time, stopwatch_prev_time, timer_str, timer_stopped, timer_start_time, timer_str_start, timer_red
#############################################################################################
#----------------------------------------------------- function to perform time comparison for stopwatch ----------------------------------------------------------
#############################################################################################
def timediff_stopwatch(start, current, prev):
# function to perform calculation for stopwatch elapsed time, using start time, current time, and prev elapsed time
# convert times to int of total milliseconds
start_msec = start[9] + start[10] + start[11]
start_sec = start[6] + start[7]
start_min = start[3] + start[4]
start_hour = start[0] + start[1]
start_int_sec = (3600*int(start_hour)) + (60*int(start_min)) + int(start_sec)
start_int_msec = int(start_msec) + 1000*start_int_sec
curr_msec = current[9] + current[10] + current[11]
curr_sec = current[6] + current[7]
curr_min = current[3] + current[4]
curr_hour = current[0] + current[1]
curr_int_sec = (3600*int(curr_hour)) + (60*int(curr_min)) + int(curr_sec)
curr_int_msec = int(curr_msec) + 1000*curr_int_sec
prev_msec = prev[9] + prev[10] + prev[11]
prev_sec = prev[6] + prev[7]
prev_min = prev[3] + prev[4]
prev_hour = prev[0] + prev[1]
prev_int_sec = (3600*int(prev_hour)) + (60*int(prev_min)) + int(prev_sec)
prev_int_msec = int(prev_msec) + 1000*prev_int_sec
# calculate elapsed msec
elapsed_msec = curr_int_msec - start_int_msec + prev_int_msec
# convert back to hours, min, and sec
elapsed_msec_int = elapsed_msec % 1000
elapsed_sec = int(elapsed_msec / 1000)
elapsed_sec_int = elapsed_sec % 60
elapsed_min = int(elapsed_sec / 60)
elapsed_min_int = elapsed_min % 60
elapsed_hour_int = int(elapsed_min / 60)
# convert to string
elapsed_msec_str = str(elapsed_msec_int).zfill(3)
elapsed_sec_str = str(elapsed_sec_int).zfill(2)
elapsed_min_str = str(elapsed_min_int).zfill(2)
elapsed_hour_str = str(elapsed_hour_int).zfill(2)
elapsed = elapsed_hour_str + ":" + elapsed_min_str + ":" + elapsed_sec_str + "." + elapsed_msec_str
return elapsed
#############################################################################################
#----------------------------------------------------- function to perform time comparison for timer ----------------------------------------------------------
#############################################################################################
def timediff_timer(current, start, timer_str):
# calculates and converts remaining time to a string, using current time, start time, and initial timer value
# convert strings to int with total number of seconds
start_sec = start[6] + start[7]
start_min = start[3] + start[4]
start_hour = start[0] + start[1]
start_int = (3600*int(start_hour)) + (60*int(start_min)) + int(start_sec)
curr_sec = current[6] + current[7]
curr_min = current[3] + current[4]
curr_hour = current[0] + current[1]
curr_int = (3600*int(curr_hour)) + (60*int(curr_min)) + int(curr_sec)
timer_sec = timer_str[6] + timer_str[7]
timer_min = timer_str[3] + timer_str[4]
timer_hour = timer_str[0] + timer_str[1]
timer_int = (3600*int(timer_hour)) + (60*int(timer_min)) + int(timer_sec)
# calculate remaining time
elapsed_int = curr_int - start_int
timer_int = timer_int - elapsed_int
# convert back to hours and minutes
timer_sec_int = timer_int % 60
timer_min = int(timer_int / 60)
timer_min_int = timer_min % 60
timer_hour_int = int(timer_min / 60)
# no negative time possible
if (timer_int < 0):
timer_sec_int = 0
timer_min_int = 0
timer_hour_int = 0
# convert back to print string
timer_sec = str(timer_sec_int).zfill(2)
timer_min = str(timer_min_int).zfill(2)
timer_hour = str(timer_hour_int).zfill(2)
timer_new_str = timer_hour + ":" + timer_min + ":" + timer_sec
return timer_new_str
#############################################################################################
#-------------------------------------------------------------- function to draw pimusic app -----------------------------------------------------------------------
#############################################################################################
def draw_pimusic(pimusic_screen,playing, song_num, timer_red):
if (pimusic_screen is 0):
# draw playlist screen
instr_x = 160
instr_y = 40
p1_x = 160
p2_x = 160
p1_y = 120
p2_y = 200
# draw instruction
my_font=pygame.font.Font(None,50)
text_surface = my_font.render('Select a playlist', True, yellow)
rect = text_surface.get_rect(center=(instr_x,instr_y))
pygame.draw.rect(screen,green,(0,80,320,158),2)
pygame.draw.rect(screen,green,(0,160,320,80),2)
if ((playing == 1) and (song_num < 3)):
# playlist 1 is playing
pygame.draw.polygon(screen, white, [[50, 110], [70, 120], [50, 130]], 0)
if ((playing == 1) and (song_num > 2)):
# playlist 2 is playing
pygame.draw.polygon(screen, white, [[50, 190], [70, 200], [50, 210]], 0)
screen.blit(text_surface, rect)
# draw playlists
my_buttons={'Popular': (p1_x,p1_y),'Classic': (p2_x,p2_y)}
for my_text, text_pos in my_buttons.items():
my_font=pygame.font.Font(None,35)
text_surface = my_font.render('%s'%my_text, True, blue)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
### Playlist 1 ###
elif (pimusic_screen is 1):
# draw playlist 1 buttons
ctrl_x = 40
ctrl_y = 30
delta_x = 70
# draw "back"
my_font=pygame.font.Font(None,35)
text_surface = my_font.render('BACK', True, yellow)
rect = text_surface.get_rect(center=(ctrl_x + 3*delta_x + 10, ctrl_y))
screen.blit(text_surface, rect)
# draw play/pause
if (playing):
my_font=pygame.font.Font(None,40)
text_surface = my_font.render('| |', True, green)
rect = text_surface.get_rect(center=(ctrl_x + delta_x, ctrl_y))
screen.blit(text_surface, rect)
else:
pygame.draw.polygon(screen, green, [[ctrl_x + delta_x - 10, ctrl_y - 10], [ctrl_x + delta_x + 10, ctrl_y], [ctrl_x + delta_x - 10, ctrl_y + 10]], 0)
my_buttons={'<<': (ctrl_x,ctrl_y),'>>': (ctrl_x + 2*delta_x ,ctrl_y)}
my_font=pygame.font.Font(None,45)
for my_text, text_pos in my_buttons.items():
text_surface = my_font.render('%s'%my_text, True, green)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
song_x = 160
song_y = 90
delta_y = 60
# draw songs
if (song_num is 0):
pygame.draw.rect(screen,blue,(0,120,320,60),2)
pygame.draw.rect(screen,pink,(0,60,320,60),2)
pygame.draw.rect(screen,blue,(0,180,320,59),2)
if (playing):
pygame.draw.polygon(screen, white, [[15, song_y - 10], [15 + 20, song_y], [15, song_y + 10]], 0)
elif (song_num is 1):
pygame.draw.rect(screen,blue,(0,60,320,60),2)
pygame.draw.rect(screen,blue,(0,180,320,59),2)
pygame.draw.rect(screen,pink,(0,120,320,60),2)
if (playing):
pygame.draw.polygon(screen, white, [[15, song_y + delta_y - 10], [15 + 20, song_y + delta_y], [15, song_y + delta_y + 10]], 0)
elif (song_num is 2):
pygame.draw.rect(screen,blue,(0,60,320,60),2)
pygame.draw.rect(screen,blue,(0,120,320,60),2)
pygame.draw.rect(screen,pink,(0,180,320,59),2)
if (playing):
pygame.draw.polygon(screen, white, [[15, song_y + 2*delta_y - 10], [15 + 20, song_y + 2*delta_y], [15, song_y + 2*delta_y + 10]], 0)
else:
pygame.draw.rect(screen,blue,(0,60,320,60),2)
pygame.draw.rect(screen,blue,(0,120,320,60),2)
pygame.draw.rect(screen,blue,(0,180,320,59),2)
my_buttons={'I Feel It Coming': (song_x,song_y),'Beauty and the Beast': (song_x,song_y + delta_y),'Put Your Records On': (song_x,song_y + 2*delta_y)}
for my_text, text_pos in my_buttons.items():
my_font=pygame.font.Font(None,35)
text_surface = my_font.render('%s'%my_text, True, blue)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
#### Playlist 2 ###
elif (pimusic_screen is 2):
# draw playlist 2 buttons
ctrl_x = 40
ctrl_y = 30
delta_x = 70
# draw "back"
my_font=pygame.font.Font(None,35)
text_surface = my_font.render('BACK', True, yellow)
rect = text_surface.get_rect(center=(ctrl_x + 3*delta_x + 10, ctrl_y))
screen.blit(text_surface, rect)
# draw play/pause
if (playing):
my_font=pygame.font.Font(None,40)
text_surface = my_font.render('| |', True, green)
rect = text_surface.get_rect(center=(ctrl_x + delta_x, ctrl_y))
screen.blit(text_surface, rect)
else:
pygame.draw.polygon(screen, green, [[ctrl_x + delta_x - 10, ctrl_y - 10], [ctrl_x + delta_x + 10, ctrl_y], [ctrl_x + delta_x - 10, ctrl_y + 10]], 0)
my_buttons={'<<': (ctrl_x,ctrl_y),'>>': (ctrl_x + 2*delta_x ,ctrl_y)}
my_font=pygame.font.Font(None,45)
for my_text, text_pos in my_buttons.items():
text_surface = my_font.render('%s'%my_text, True, green)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
song_x = 160
song_y = 90
delta_y = 60
# draw songs
if (song_num is 3):
pygame.draw.rect(screen,blue,(0,120,320,60),2)
pygame.draw.rect(screen,pink,(0,60,320,60),2)
pygame.draw.rect(screen,blue,(0,180,320,59),2)
if (playing):
pygame.draw.polygon(screen, white, [[15, song_y - 10], [15 + 20, song_y], [15, song_y + 10]], 0)
elif (song_num is 4):
pygame.draw.rect(screen,blue,(0,60,320,60),2)
pygame.draw.rect(screen,blue,(0,180,320,59),2)
pygame.draw.rect(screen,pink,(0,120,320,60),2)
if (playing):
pygame.draw.polygon(screen, white, [[15, song_y + delta_y - 10], [15 + 20, song_y + delta_y], [15, song_y + delta_y + 10]], 0)
elif (song_num is 5):
pygame.draw.rect(screen,blue,(0,60,320,60),2)
pygame.draw.rect(screen,blue,(0,120,320,60),2)
pygame.draw.rect(screen,pink,(0,180,320,59),2)
if (playing):
pygame.draw.polygon(screen, white, [[15, song_y + 2*delta_y - 10], [15 + 20, song_y + 2*delta_y], [15, song_y + 2*delta_y + 10]], 0)
else:
pygame.draw.rect(screen,blue,(0,60,320,60),2)
pygame.draw.rect(screen,blue,(0,120,320,60),2)
pygame.draw.rect(screen,blue,(0,180,320,59),2)
my_buttons={'Fur Elise': (song_x,song_y),'Lucky': (song_x,song_y + delta_y),'Mozart No. 21': (song_x,song_y + 2*delta_y)}
for my_text, text_pos in my_buttons.items():
my_font=pygame.font.Font(None,35)
text_surface = my_font.render('%s'%my_text, True, blue)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
# Global timer notifier
if (timer_red == 1):
my_font=pygame.font.Font(None,50)
text_surface = my_font.render('TIMER!', True, red)
rect = text_surface.get_rect(center=(160,120))
screen.blit(text_surface, rect)
pygame.display.flip()
#############################################################################################
#-------------------------------------------------------------- function to check pimusic input -----------------------------------------------------------------------
#############################################################################################
def pimusic_press(pos, pimusic_screen, playing, song_num):
x,y = pos
# if in one of playlists already
if ((pimusic_screen == 1) or (pimusic_screen == 2)):
ctrl_x = 40
ctrl_y = 30
delta_x = 70
song_x = 160
song_y = 90
delta_y = 60
# check control press
if (y > ctrl_y -20 and y < ctrl_y + 17):
if (x > ctrl_x - 30 and x < ctrl_x + 30):
print("<< pressed")
subprocess.call('echo "seek -30 0" > mp3_fifo',shell=True)
elif (x > ctrl_x + delta_x - 30 and x < ctrl_x + delta_x + 30):
print("play/pause pressed")
playing = not playing
subprocess.call('echo "pause" > mp3_fifo', shell=True)
elif (x > ctrl_x + 2*delta_x - 30 and x < ctrl_x + 2*delta_x + 25):
print(">> pressed")
subprocess.call('echo "seek 30 0" > mp3_fifo',shell=True)
elif (x > ctrl_x + 3*delta_x + 8 - 30 and x < ctrl_x + 3*delta_x + 12 + 30):
print("BACK pressed")
pimusic_screen = 0
# check song press
elif (y > song_y - 30 and y < song_y + 30):
#print("Song 1")
# separate for song detection
if (pimusic_screen == 1):
if (song_num != 0):
playing = 0
subprocess.call('echo "stop" > mp3_fifo', shell=True)
# song for playlist 1
cmd = 'mplayer -loop 0 -novideo Weeknd.mp3 -ao alsa -input file=/home/pi/finalproject/mp3_fifo &'
subprocess.call(cmd, shell=True)
subprocess.call('echo "pause" > mp3_fifo', shell=True)
song_num = 0
else:
if (playing == 1):
if (x > 160):
# increase volume
subprocess.call('echo "volume 10" > mp3_fifo', shell=True)
else:
# decrease volume
subprocess.call('echo "volume -10" > mp3_fifo', shell=True)
elif (pimusic_screen == 2):
if (song_num != 3):
playing = 0
subprocess.call('echo "stop" > mp3_fifo', shell=True)
# song for playlist 2
cmd = 'mplayer -loop 0 -novideo FurElise.mp3 -ao alsa -input file=/home/pi/finalproject/mp3_fifo &'
subprocess.call(cmd, shell=True)
subprocess.call('echo "pause" > mp3_fifo', shell=True)
song_num = 3
else:
if (playing == 1):
if (x > 160):
# increase volume
subprocess.call('echo "volume 10" > mp3_fifo', shell=True)
else:
# decrease volume
subprocess.call('echo "volume -10" > mp3_fifo', shell=True)
elif (y > song_y + delta_y - 30 and y < song_y + delta_y + 30):
if (pimusic_screen == 1):
if (song_num != 1):
playing = 0
subprocess.call('echo "stop" > mp3_fifo', shell=True)
# song for playlist 1
cmd = 'mplayer -loop 0 -novideo BATB.mp3 -ao alsa -input file=/home/pi/finalproject/mp3_fifo &'
subprocess.call(cmd, shell=True)
subprocess.call('echo "pause" > mp3_fifo', shell=True)
song_num = 1
else:
if (playing == 1):
if (x > 160):
# increase volume
subprocess.call('echo "volume 10" > mp3_fifo', shell=True)
else:
# decrease volume
subprocess.call('echo "volume -10" > mp3_fifo', shell=True)
elif (pimusic_screen == 2):
if (song_num != 4):
playing = 0
subprocess.call('echo "stop" > mp3_fifo', shell=True)
# song for playlist 2
cmd = 'mplayer -loop 0 -novideo Lucky.mp3 -ao alsa -input file=/home/pi/finalproject/mp3_fifo &'
subprocess.call(cmd, shell=True)
subprocess.call('echo "pause" > mp3_fifo', shell=True)
song_num = 4
else:
if (playing == 1):
if (x > 160):
# increase volume
subprocess.call('echo "volume 10" > mp3_fifo', shell=True)
else:
# decrease volume
subprocess.call('echo "volume -10" > mp3_fifo', shell=True)
elif (y > song_y + 2*delta_y - 30 and y < song_y + 2*delta_y + 30):
#print("Song 3")
if (pimusic_screen == 1):
if (song_num != 2):
playing = 0
subprocess.call('echo "stop" > mp3_fifo', shell=True)
# song for playlist 1
cmd = 'mplayer -loop 0 -novideo Records.mp3 -ao alsa -input file=/home/pi/finalproject/mp3_fifo &'
subprocess.call(cmd, shell=True)
subprocess.call('echo "pause" > mp3_fifo', shell=True)
song_num = 2
else:
if (playing == 1):
if (x > 160):
# increase volume
subprocess.call('echo "volume 10" > mp3_fifo', shell=True)
else:
# decrease volume
subprocess.call('echo "volume -10" > mp3_fifo', shell=True)
elif (pimusic_screen == 2):
if (song_num != 5):
playing = 0
subprocess.call('echo "stop" > mp3_fifo', shell=True)
# song for playlist 2
cmd = 'mplayer -loop 0 -novideo Mozart.mp3 -ao alsa -input file=/home/pi/finalproject/mp3_fifo &'
subprocess.call(cmd, shell=True)
subprocess.call('echo "pause" > mp3_fifo', shell=True)
song_num = 5
else:
if (playing == 1):
if (x > 160):
# increase volume
subprocess.call('echo "volume 10" > mp3_fifo', shell=True)
else:
# decrease volume
subprocess.call('echo "volume -10" > mp3_fifo', shell=True)
# on main menu
else:
# check playlist press
p1_y = 120
p2_y = 200
if (y > p1_y -40 and y < p1_y + 40):
print("Playlist 1")
pimusic_screen = 1
elif (y > p2_y -40 and y < p2_y + 40):
print("Playlist 2")
pimusic_screen = 2
else:
pimusic_screen = 0 # need this??
return pimusic_screen, playing, song_num
#############################################################################################
#-------------------------------------------------------------- function to draw pimessage app -----------------------------------------------------------------------
#############################################################################################
def draw_pimsg(msg_screen, timer_red):
# Main message screen, draw control buttons
if (msg_screen==0):
my_buttons={'Clear': (40,210),'Text': (150,210),'Send': (260,210)}
my_font=pygame.font.Font(None,25)
for my_text, text_pos in my_buttons.items():
text_surface = my_font.render('%s'%my_text, True, white)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
my_txt_font=pygame.font.Font(None,15)
pygame.draw.rect(screen,yellow,(10,20,300,60),2)
msg_surface=my_txt_font.render('%s'%text_message,True,cyan)
msg_rect=msg_surface.get_rect(center=(150,40))
screen.blit(msg_surface,msg_rect)
# Pre-set messages screen, draw buttons
elif (msg_screen==1):
my_buttons={'Back': (40,210)}
my_font=pygame.font.Font(None,25)
for my_text, text_pos in my_buttons.items():
text_surface = my_font.render('%s'%my_text, True, white)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
my_buttons2={'Hello!':(40,40),'What\'s up?':(40,70),'On my way.':(40,100),'OK':(40,130),'Sure!':(40,160),'No Problem!':(160,40),'Where are you?':(160,70),'Thank you':(160,100),'Talk later?':(160,130),'Sorry, can\'t talk right now...':(160,160), 'Great!':(280,40),'Yes':(280,70),'Almost there!':(280,100),'No':(280,130),'Here':(280,160)}
my_font2=pygame.font.Font(None,15)
for my_text, text_pos in my_buttons2.items():
text_surface = my_font2.render('%s'%my_text, True, white)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
# Global timer notifier
if (timer_red == 1):
my_font=pygame.font.Font(None,50)
text_surface = my_font.render('TIMER!', True, red)
rect = text_surface.get_rect(center=(160,120))
screen.blit(text_surface, rect)
pygame.display.flip()
#############################################################################################
#-------------------------------------------------------------- function to check pimessage input -----------------------------------------------------------------------
#############################################################################################
def pimsg_press(client,text_message,send,msg_screen,pos):
x,y=pos
send=0
s0_x=40
s0_y=210
s0_deltax=110
s1_x=40
s1_y=40
s1_deltax=120
s1_deltay=30
# detect control presses
if (msg_screen==0):
if (y>s0_y-20 and y<s0_y+20):
if (x>s0_x-20 and x<s0_x+20):
print('Clear pushed')
text_message=''
elif (x>s0_x+s0_deltax-20 and x<s0_x+s0_deltax+20):
print ('Text message menu pushed')
msg_screen=1
elif (x>s0_x+2*s0_deltax-20 and x<s0_x+2*s0_deltax+20):
print ('Send pushed')
send=1
client.messages.create(from_='+16072755445',to='+16156361096',body=text_message)
text_message=''
# detect presses for pre-set messages or "back"
elif (msg_screen==1):
if (y>190 and y<230): #back pressed
if (x>s1_x-20 and x<s1_x+20):
msg_screen=0
print('Back pressed')
if (y>s1_y-10 and y<s1_y+10): #first row pushed
if (x>s1_x-15 and x<s1_x+20):
text_message=text_message+'Hello! '
elif (x>s1_x+s1_deltax-30 and x<s1_x+s1_deltax+30):
text_message=text_message+'No Problem! '
elif (x>s1_x+2*s1_deltax-25 and x<s1_x+2*s1_deltax+25):
text_message=text_message+'Great! '
elif (y>s1_y+s1_deltay-10 and y<s1_y+s1_deltay+10): #second row pushed
if (x>s1_x-25 and x<s1_x+25):
text_message=text_message+'What\'s up? '
elif (x>s1_x+s1_deltax-35 and x<s1_x+s1_deltax+35):
text_message=text_message+'Where are you? '
elif (x>s1_x+2*s1_deltax-10 and x<s1_x+2*s1_deltax+10):
text_message=text_message+'Yes '
elif (y>s1_y+2*s1_deltay-10 and y<s1_y+2*s1_deltay+10): #third row pushed
if (x>s1_x-25 and x<s1_x+25):
text_message=text_message+'On my way '
elif (x>s1_x+s1_deltax-25 and x<s1_x+s1_deltax+25):
text_message=text_message+'Thank you. '
elif (x>s1_x+2*s1_deltax-25 and x<s1_x+2*s1_deltax+25):
text_message=text_message+'Almost there! '
elif (y>s1_y+3*s1_deltay-10 and y<s1_y+3*s1_deltay+10): #fourth row pushed
if (x>s1_x-15 and x<s1_x+15):
text_message=text_message+'OK '
elif (x>s1_x+s1_deltax-25 and x<s1_x+s1_deltax+25):
text_message=text_message+'Talk later? '
elif (x>s1_x+2*s1_deltax-15 and x<s1_x+2*s1_deltax+15):
text_message=text_message+'No. '
elif (y>s1_y+4*s1_deltay-10 and y<s1_y+4*s1_deltay+10): #fifth row pushed
if (x>s1_x-20 and x<s1_x+20):
text_message=text_message+'Sure! '
elif (x>s1_x+s1_deltax-50 and x<s1_x+s1_deltax+50):
text_message=text_message+'Sorry, can\'t talk right now... '
elif (x>s1_x+2*s1_deltax-20 and x<s1_x+2*s1_deltax+20):
text_message=text_message+'Here. '
return text_message,msg_screen,send
#############################################################################################
#-------------------------------------------------------------- function to draw pisimon app -----------------------------------------------------------------------
#############################################################################################
def draw_pisimon(play_screen, length, timer_red):
pygame.draw.rect(screen,red,(60,60,100,60))
pygame.draw.rect(screen,yellow,(160,60,100,60))
pygame.draw.rect(screen,green,(60,120,100,60))
pygame.draw.rect(screen,blue,(160,120,100,60))
my_text = 'Level ' + str(length - 2)
my_font=pygame.font.Font(None,25)
text_surface = my_font.render('%s'%my_text, True, white)
rect = text_surface.get_rect(center=(80,30))
screen.blit(text_surface, rect)
if (timer_red == 1):
my_font=pygame.font.Font(None,50)
text_surface = my_font.render('TIMER!', True, red)
rect = text_surface.get_rect(center=(160,180))
screen.blit(text_surface, rect)
if (play_screen==0):
my_buttons={'Press Here to Begin Playing': (160,210)}
elif (play_screen == 1):
my_buttons={'Watch the pattern!': (160,210)}
elif (play_screen == 2):
my_buttons={'Enter the pattern!': (160,210)}
elif (play_screen == 3):
my_buttons={'GAME OVER!': (160,120)}
my_font=pygame.font.Font(None,50)
elif (play_screen == 4):
my_buttons={'CORRECT!': (160,120)}
my_font=pygame.font.Font(None,50)
for my_text, text_pos in my_buttons.items():
text_surface = my_font.render('%s'%my_text, True, white)
rect = text_surface.get_rect(center=text_pos)
screen.blit(text_surface, rect)
pygame.display.flip()
#############################################################################################
#-------------------------------------------------------------- function to animate tile flash -----------------------------------------------------------------------
#############################################################################################
def draw_pisimon_flash(color, delay, play_screen, length, timer_red):
screen.fill(black)
print "in draw flash"
draw_pisimon(play_screen,length, timer_red)
if (color == green):
pygame.draw.rect(screen,white,(60,120,100,60),4)
elif (color == red):
pygame.draw.rect(screen,white,(60,60,100,60),4)
elif (color == yellow):
pygame.draw.rect(screen,white,(160,60,100,60),4)
elif (color == blue):
pygame.draw.rect(screen,white,(160,120,100,60),4)
pygame.display.flip()
pygame.time.wait(delay)
screen.fill(black)
draw_pisimon(play_screen,length, timer_red)
pygame.display.flip()
pygame.time.wait(delay)
#############################################################################################
#-------------------------------------------------------------- function to display results -----------------------------------------------------------------------
#############################################################################################
def draw_pisimon_result(play_screen, length, timer_red):
screen.fill(black)
draw_pisimon(play_screen,length, timer_red)
pygame.display.flip()
pygame.time.wait(2*1000)
#############################################################################################
#-------------------------------------------------------------- function to animate pattern -----------------------------------------------------------------------
#############################################################################################
def draw_list_flash (master_list, delay, play_screen,length, timer_red):
for color in master_list:
draw_pisimon_flash(color,delay,play_screen,length, timer_red)
#############################################################################################
#-------------------------------------------------------------- function to check pisimon input -----------------------------------------------------------------------
#############################################################################################
def pisimon_press(pos,play_screen, master_list, user_list, delay, length, timer_red):
x,y = pos
# detect presses: colors and "start"
if (play_screen==0):
if (y>=200 and y<=220):
if (x>=45 and y<=280):
play_screen=1
print('click detected')
draw_list_flash(master_list,delay,play_screen,length, timer_red)
play_screen=2
elif (play_screen==2):
#check until len(user_list)=len(master_list)
#if user_list==master_list
if (y>=60 and y<=120):
if (x>=60 and x<=160):
user_list.append(red)
elif (x>160 and x<=260):
user_list.append(yellow)
elif (y>120 and y<=180):
if (x>=60 and x<=160):
user_list.append(green)
elif (x>160 and x<=260):
user_list.append(blue)
if (len(user_list) == len(master_list)):
length,user_list,master_list,play_screen,delay= pisimon_control(length,user_list,master_list,delay, timer_red)
print user_list
return play_screen, master_list, user_list, length,delay
#############################################################################################
#-------------------------------------------------------------- function to control pisimon game state -----------------------------------------------------------------------
#############################################################################################
def pisimon_control(length,user_list,master_list,delay, timer_red):
if (user_list == master_list):
#correct
# generate next sequence, restart
play_screen = 4
draw_pisimon_result(play_screen,length, timer_red)
play_screen = 1
length = length + 1
# make it harder as you go
if (length > 4):
delay = 800
elif (length > 6):
delay = 650
elif (length > 8):
delay = 500
elif (length > 10):
delay = 350
elif (length > 12):
delay = 200
user_list=[]
master_list = gen_pisimon_list(length)
draw_list_flash(master_list,delay,play_screen,length, timer_red)
play_screen=2
else:
# incorrect
# display game over and "click to restart"
play_screen = 3
draw_pisimon_result(play_screen,length, timer_red)
#PiSimon reset
play_screen=0
length = 3
user_list=[]
master_list = gen_pisimon_list(length)
return length,user_list,master_list,play_screen,delay
#############################################################################################
#-------------------------------------------------------------- function to generate random pattern -----------------------------------------------------------------------
#############################################################################################
def gen_pisimon_list(length):
list=[]
for i in range(0, length):
num = random.randint(1, 4)
if (num == 1):
list.append(red)
elif (num == 2):
list.append(blue)
elif (num == 3):
list.append(green)
elif (num == 4):
list.append(yellow)
return list
#############################################################################################
#-------------------------------------------------------------- Main Program Flow -----------------------------------------------------------------------
#############################################################################################
# init pygame
pygame.init()
pygame.mouse.set_visible(False)
# display setting and colors
size = width, height = 320, 240
black = 0, 0, 0
white = 255, 255, 255
blue = 0, 0, 255
red = 255, 0, 0
green = 0, 255, 0
pink = 255, 0, 255
cyan = 0, 255, 255
orange = 255, 128, 0
yellow = 255, 255, 0
screen = pygame.display.set_mode(size)
locked = 1
passcode_error=0
passcode = ['1','2','3','4']
currently_entered = []
hs_buttons_colors=[red,green,blue,pink,cyan]
hs_number = 1
current_app = 99 #no apps
#for picalc
terms=['','',''] #first=first term, second=second term, third=answer
first_float=0 #0=int, 1=float
second_float=0
term_toggle=0 #0=add numbers to first term; 1=add to second term; 2=for displaying answer (should never be 2 in picalc_calc())
sign=0
decimal_pressed = 0
#for pitime
time_screen=0
stopwatch_start_time = "00:00:00.000"
stopwatch_elapsed_time = "00:00:00.000"
stopwatch_prev_time = "00:00:00.000"
stopwatch_stopped = 1
timer_str_start = "00:00:00"
timer_str = "00:00:00"
timer_stopped = 1
timer_start_time = "00:00:00"
timer_red = 0
# for pimusic
pimusic_screen = 0
playing = 0
song_num = 0
# start mplayer with playlist 1, song 0
cmd = 'mplayer -loop 0 -novideo Weeknd.mp3 -ao alsa -input file=/home/pi/finalproject/mp3_fifo &'
subprocess.call(cmd, shell=True)
cmd = 'echo "pause" > mp3_fifo'
subprocess.call(cmd, shell=True)
#for message app
msg_screen=0
send=0
text_message=''
screen.fill(black)
client=Client('ACf26adf116b1a934b37d87e5327d4f3c4','de8b5c965a22217df9c0fcb29c58abb0')
#for simon game
length = 3
play_screen=0
simon_delay=1000
master_list = gen_pisimon_list(length)
user_list=[]
screen.fill(black)
while (True):
# clear screen
screen.fill(black)
# if timer is going, update elapsed time and display
if (timer_stopped == 0):
curr_time = time.strftime("%H:%M:%S",time.localtime())
timer_str = timediff_timer(curr_time, timer_start_time, timer_str_start)
# number is drawn in red if timer runs out
if ((timer_str == "00:00:00") and (timer_str_start != "00:00:00") and (timer_stopped == 0)):
timer_red = 1
timer_stopped = 1
# draw lock screen if still locked
if (locked):
draw_lock_screen(passcode_error, timer_red)
# detect passcode presses and update list
for event in pygame.event.get():
if(event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
passcode_error=passcode_press(pos,passcode_error)
if (len(currently_entered)==4):
draw_lock_screen(passcode_error, timer_red)
locked,currently_entered,passcode_error=check_passcode(currently_entered)
else:
# Return to lock screen
if (not GPIO.input(17)):
locked=1
current_app=99
hs_number=1
#PiCalc reset
terms=['','',''] #first=first term, second=second term, third=answer
first_float=0 #0=int, 1=float
second_float=0
term_toggle=0 #0=add numbers to first term; 1=add to second term; 2=for displaying answer (should never be 2 in picalc_calc())
sign=0
#PiSimon reset
play_screen=0
simon_delay=1000
length = 3
master_list = gen_pisimon_list(length)
user_list=[]
# Return to home screen 1
if (not GPIO.input(22)):
locked=0
current_app=99
hs_number=1
#PiCalc reset
terms=['','',''] #first=first term, second=second term, third=answer
term_toggle=0 #0=add numbers to first term; 1=add to second term; 2=for displaying answer (should never be 2 in picalc_calc())
sign=0
#PiSimon reset
play_screen=0
simon_delay=1000
length = 3
master_list = gen_pisimon_list(length)
user_list=[]
if (current_app is 99 and (locked == 0)): # draw home screen
draw_homescreen(hs_buttons_colors, hs_number, timer_red)
for event in pygame.event.get():
if(event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
hs_number, current_app = hs_press(pos,hs_number, current_app)
elif (current_app is 2):
# PiCalc
draw_picalc(terms, term_toggle, timer_red)
for event in pygame.event.get():
if(event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
term_toggle,terms,sign,decimal_pressed = picalc_press(pos,terms,term_toggle,sign, decimal_pressed)
# draw updated picalc
screen.fill(black)
draw_picalc(terms, term_toggle, timer_red)
pygame.display.flip()
elif (current_app is 1):
# PiTime
timer_str, timer_red, timer_stopped = draw_pitime(time_screen,stopwatch_stopped, stopwatch_start_time, stopwatch_elapsed_time, stopwatch_prev_time, timer_str, timer_stopped, timer_start_time, timer_str_start, timer_red)
for event in pygame.event.get():
if(event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
time_screen,stopwatch_stopped, stopwatch_start_time, stopwatch_elapsed_time, stopwatch_prev_time, \
timer_str, timer_stopped, timer_start_time,timer_str_start, timer_red = \
pitime_press(time_screen,pos,stopwatch_stopped, stopwatch_start_time, stopwatch_elapsed_time, stopwatch_prev_time, \
timer_str, timer_stopped, timer_start_time,timer_str_start, timer_red)
# draw updated pitime
screen.fill(black)
timer_str, timer_red, timer_stopped = draw_pitime(time_screen,stopwatch_stopped, stopwatch_start_time, stopwatch_elapsed_time, stopwatch_prev_time, timer_str, timer_stopped, timer_start_time, timer_str_start, timer_red)
elif (current_app is 4):
# draw piMusic
draw_pimusic(pimusic_screen, playing, song_num, timer_red)
# check press
for event in pygame.event.get():
if(event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
pimusic_screen, playing, song_num = pimusic_press(pos, pimusic_screen, playing, song_num)
elif (current_app is 3):
#PiMsg
draw_pimsg(msg_screen, timer_red)
for event in pygame.event.get():
if(event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
text_message,msg_screen,send=pimsg_press(client,text_message,send,msg_screen,pos)
print text_message
elif (current_app is 5):
draw_pisimon(play_screen,length, timer_red)
for event in pygame.event.get():
if(event.type is MOUSEBUTTONDOWN):
pos = pygame.mouse.get_pos()
elif(event.type is MOUSEBUTTONUP):
play_screen, master_list, user_list,length,delay = pisimon_press(pos,play_screen,master_list,user_list,simon_delay,length, timer_red)
else:
screen.fill(black)
pygame.display.flip()
# Active low input ==> button press occurs if input goes to 0
if (not GPIO.input(27)):
# Rightmost button pressed ==> exit python program
# quit mplayer
cmd = 'echo "stop" > mp3_fifo'
subprocess.call(cmd, shell=True)
sys.exit()