import pygame
from pygame.locals import *
import os 
from time import sleep 
import RPi.GPIO as GPIO

# Setup the GPIOs as outputs - only 4 and 17 are available
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)

# Colours 
WHITE = (255,255,255)

os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')

pygame.init()
pygame.mouse.set_visible(False)
lcd = pygame.display.set_mode((320,240))
lcd.fill((0,0,0))
pygame.display.update()

font_big = pygame.font.Font(None,50)

touch_buttons = {'17 on': (80,60), '4 on':(240,60), '17 off':(80,180), '4 off':(240, 180)}

for k,v in touch_buttons.items():
	text_surface = font_big.render('%s'%k, True, WHITE)
	rect = text_surface.get_rect(center=v)
	lcd.blit(text_surface, rect)

pygame.display.update()

while True:
	# Scan touchscreen events 
	for event in pygame.event.get():
		if(event.type is MOUSEBUTTONDOWN):
			pos = pygame.mouse.get_pos()
			print pos 
		elif(event.type is MOUSEBUTTONUP):
			pos = pygame.mouse.get_pos()
			print pos
			# Find which quarter of the screen we're in
			x,y = pos
			if y < 120:
				if x < 160:
					GPIO.output(17, False)
				else:
					GPIO.output(4, False)
			else:
				if x < 160:
					GPIO.output(17, True)
				else: 
					GPIO.output(4, True)
	sleep(0.1)

import pygame
import os 
import pygameui as ui
import logging
import RPi.GPIO as GPIO

# Setup the GPIOs as outputs - only 4 and 17 are available
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)

log_format = '%(asctime)-6s: %(name)s - %(levelname)s - %(message)s'
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(log_format))
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(console_handler)

os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')

MARGIN = 20

class PiTft(ui.Scene):
	def __init__(self):
		ui.Scene.__init__(self)

		self.on17_button = ui.Button(ui.Rect(MARGIN, MARGIN, 130, 90), '17 on')
		self.on17_button.on_clicked.connect(self.gpi_button)
		self.add_child(self.on17_button)

                self.on4_button = ui.Button(ui.Rect(170, MARGIN, 130, 90), '4 on')
                self.on4_button.on_clicked.connect(self.gpi_button)
                self.add_child(self.on4_button)

                self.off17_button = ui.Button(ui.Rect(MARGIN, 130, 130, 90), '17 off')
                self.off17_button.on_clicked.connect(self.gpi_button)
                self.add_child(self.off17_button)

                self.off4_button = ui.Button(ui.Rect(170, 130, 130, 90), '4 off')
                self.off4_button.on_clicked.connect(self.gpi_button)
                self.add_child(self.off4_button)

	def gpi_button(self, btn, mbtn):
		logger.info(btn.text)
		
		if btn.text == '17 on':
			GPIO.output(17, False)
		elif btn.text == '4 on':
			GPIO.output(4, False)
		elif btn.text == '17 off':
			GPIO.output(17, True)
		elif btn.text == '4 off':
			GPIO.output(4, True)

ui.init('Raspberry Pi UI', (320, 240))
pygame.mouse.set_visible(False)
ui.scene.push(PiTft())
ui.run()


switch to PygameUI