def attemptConnect():
    try:
        pygame.init()
        pygame.joystick.init()
        pygame.display.init()

        j = pygame.joystick.Joystick(0)
        j.init()
        print(' [INFO::CONTROLLER] Controller Connected: ' + j.get_name())
        joyName = j.get_name()
        lcd.clear()
        longString = "Controller Connected: " + joyName
        lcd.display_string(longString[0:15], 1)
        time.sleep(1)
        for i in range(0, len(longString) - 14):
            lcd.display_string(longString[i:i + 15], 1)
            time.sleep(.1)
        time.sleep(1)
        lcd.clear()

        lcd.display_string("Waiting for User", 1)
        lcd.display_string("Start+Select+A", 2)

    except pygame.error:
        print(
            ' [INFO::CONTROLLER] Failed to connect to controller... Program restarting in 5 seconds'
        )
        pygame.display.quit()
        pygame.quit()
        lcd.clear()
        for i in range(0, 5):
            lcd.display_string("No Controller!", 1)
            lcd.display_string("Refreshing: " + str(5 - i), 2)
            time.sleep(1)
        attemptConnect()
def sigterm_handler(signal, frame):
	lcd.clear()
	lcd.display_string("     BYE! :)    ", 1)
	sleep(1)
	lcd.backlight(0)
	sys.exit()
			if ((math.floor(percentage)>x*(100/6)+1) and (math.floor(percentage)<(x+1)*(100/6)) and percentage>2):
				lcd.display_string_pos(chr(min(4,math.floor((percentage-(x*100/6))/((100/6/4))+1))),row,9+x)
			else:  
				lcd.display_string_pos(" ",row,9+x)

def sigterm_handler(signal, frame):
	lcd.clear()
	lcd.display_string("     BYE! :)    ", 1)
	sleep(1)
	lcd.backlight(0)
	sys.exit()

signal.signal(signal.SIGTERM, sigterm_handler)

WAIT_TIME = 4
lcd.clear()
lcd.display_string(str.center(str(os.popen('hostname').readline())[:-1],16,' '),1)
lcd.display_string("    STARTING    ",2)
sleep(WAIT_TIME)
lcd.clear()

try:
	while (1):
		
		#CPU & RAM usage
		lcd.display_string("CPU   % "+chr(126)+"      "+chr(127),1)
		lcd.display_string("RAM   % "+chr(126)+"      "+chr(127),2)
		for x in range(WAIT_TIME):
			cpupct=ps.cpu_percent()
			rampct=ps.virtual_memory()[2]
			lcd.display_string_pos(str(math.floor(cpupct)).rjust(3),1,3)