Instantly share code, notes, and snippets.
Created
June 5, 2017 21:01
-
Save borpin/b39ddd294d5a741c6d35072756bddcde to your computer and use it in GitHub Desktop.
A python script to display 4 buttons on a TFT attached to a Pi and change text and colour when pressed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
# Needs this to get the event types | |
from pygame.locals import * | |
import os | |
import logging | |
from time import sleep | |
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) | |
#Colours | |
red = (255,0,0) | |
green = (0,255,0) | |
blue = (0,0,255) | |
black = (0,0,0) | |
display_width = 320 | |
display_height = 240 | |
# Assume all buttons of equal size (allow for a 5mm border all round to be added | |
button_dim = (0,0,120,80) | |
# define the centres for the buttons rather than top left | |
button_ctrs =[(85,65),(85,175),(235,65),(235,175)] | |
# I'm going to save the state of each button | |
button_state = [False , False, False, False] | |
# functions from https://pythonprogramming.net/displaying-text-pygame-screen/ | |
# create a text object | |
def text_objects(text, font): | |
textSurface = font.render(text, True, black) | |
return textSurface, textSurface.get_rect() | |
# display text | |
def message_display(text,button): | |
largeText = pygame.font.Font('freesansbold.ttf',75) | |
TextSurf, TextRect = text_objects(text, largeText) | |
TextRect.center = (button_ctrs[button]) | |
lcd.blit(TextSurf, TextRect) | |
# work out which quarter of the screen was pressed | |
def which_button (): | |
pos = pygame.mouse.get_pos() | |
#Find which quarter of the screen we're in | |
x,y = pos | |
button = 5 | |
if x < 160: | |
if y < 120: | |
button = 0 | |
else: | |
button = 1 | |
else: | |
if y < 120: | |
button = 2 | |
else: | |
button = 3 | |
return button | |
######################### | |
# Start doing things | |
######################### | |
# setup LCD screen | |
os.putenv('SDL_FBDEV', '/dev/fb1') | |
os.putenv('SDL_MOUSEDRV', 'TSLIB') | |
os.putenv('SDL_MOUSEDEV', '/dev/input/by-path/platform-20204000.spi-event') | |
#os.putenv('SDL_MOUSEDEV', '/dev/input/event0') | |
pygame.init() | |
pygame.mouse.set_visible(False) | |
# create a screen | |
lcd = pygame.display.set_mode((display_width, display_height)) | |
# set up the initial screen | |
# Add a background | |
lcd.fill(red) | |
# build the array of rects with centers defined | |
buttons = [] | |
for button in range (0,4): | |
buttons.append(pygame.Rect(button_dim)) | |
buttons[button].center = button_ctrs[button] | |
#draw the buttons with a border | |
for button in buttons: | |
# make the border by drawing a bigger rect first | |
pygame.draw.rect(lcd, black, button.inflate(5,5), 0) | |
pygame.draw.rect(lcd, blue, button, 0) | |
message_display("off",buttons.index(button)) | |
# update the whole screen as the borders are outside of the rects defined | |
# so if we just updated those, the borders do not show. | |
pygame.display.flip() | |
# enclose loop in a try - catch to give a clean exit | |
try: | |
print ("Press CTRL+C to exit") | |
while True: | |
# Scan touchscreen events | |
for event in pygame.event.get(): | |
# Just use a button down event. | |
if(event.type is MOUSEBUTTONDOWN): | |
# which button was pressed | |
but_pressed = which_button() | |
# update the buttons state | |
button_state[but_pressed] = not button_state[but_pressed] | |
# update the display based on the state | |
if button_state[but_pressed]: | |
pygame.draw.rect(lcd, green, buttons[but_pressed], 0) | |
message_display("on",but_pressed) | |
else: | |
pygame.draw.rect(lcd, blue, buttons[but_pressed], 0) | |
message_display("off",but_pressed) | |
# just update the pressed button rect | |
pygame.display.update(buttons[but_pressed]) | |
# put a sleep here to de-bounce | |
sleep(0.2) | |
# clear the queue of events that may have happened while we were sleeping | |
pygame.event.clear() | |
except KeyboardInterrupt: | |
# graceful exit (running interactively) | |
print ("Goodbye.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment