Created
February 8, 2022 18:01
-
-
Save tandpfun/7f10c7bd01d6cd7ef7cb204fc51c7beb to your computer and use it in GitHub Desktop.
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 time | |
import digitalio | |
import board | |
from PIL import Image, ImageDraw, ImageFont | |
import adafruit_rgb_display.st7789 as st7789 | |
import RPi.GPIO as GPIO | |
import textwrap #library for wrapping text | |
import json | |
import requests | |
from requests import get | |
truth_url = 'https://api.truthordarebot.xyz/v1/truth?rating=pg&rating=pg13' | |
dare_url = 'https://api.truthordarebot.xyz/v1/dare?rating=pg&rating=pg13' | |
# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4): | |
cs_pin = digitalio.DigitalInOut(board.CE0) | |
dc_pin = digitalio.DigitalInOut(board.D25) | |
GPIO.setmode(GPIO.BCM) | |
reset_pin = None | |
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
# Config for display baudrate (default max is 24mhz): | |
BAUDRATE = 64000000 | |
# Setup SPI bus using hardware SPI: | |
spi = board.SPI() | |
# Create the ST7789 display: | |
disp = st7789.ST7789( | |
spi, | |
cs=cs_pin, | |
dc=dc_pin, | |
rst=reset_pin, | |
baudrate=BAUDRATE, | |
width=240, | |
height=240, | |
x_offset=0, | |
y_offset=80, | |
) | |
# Create blank image for drawing. | |
# Make sure to create image with mode 'RGB' for full color. | |
height = disp.width # we swap height/width to rotate it to landscape! | |
width = disp.height | |
image = Image.new("RGB", (width, height)) | |
rotation = 180 | |
# Get drawing object to draw on image. | |
draw = ImageDraw.Draw(image) | |
# First define some constants to allow easy resizing of shapes. | |
padding = 0 | |
top = padding | |
bottom = height - padding | |
# Alternatively load a TTF font. Make sure the .ttf font file is in the | |
# same directory as the python script! | |
# Some other nice fonts to try: http://www.dafont.com/bitmap.php | |
fontsm = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 25) | |
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 45) | |
# Turn on the backlight | |
backlight = digitalio.DigitalInOut(board.D22) | |
backlight.switch_to_output() | |
backlight.value = True | |
def dare(): | |
question_req = get(dare_url).json() | |
print (question_req['question']) | |
question = question_req['question'] | |
showQuestion('Dare', question, (255,0,100)) | |
def truth(): | |
question_req = get(truth_url).json() | |
print (question_req['question']) | |
question = question_req['question'] | |
showQuestion('Truth', question, (0,150,255)) | |
def showQuestion(questionType, question, color): | |
x = 0 | |
y = top | |
draw.rectangle((0, 0, width, height), outline=0, fill=color) | |
# text wrapping | |
wrapper = textwrap.TextWrapper(width=15) | |
word_list = wrapper.wrap(text=question) | |
split_message = '' | |
for i in word_list[:-1]: | |
split_message = split_message + i + '\n' | |
split_message += word_list[-1] | |
# Write text. | |
draw.text((x+50, y+10),questionType, font=font, fill=(255,255,255)) | |
draw.text((x+10, y+75),split_message, font=fontsm, fill=(255,255,255)) | |
disp.image(image, rotation) | |
last_truth_press = True | |
last_dare_press = True | |
while True: | |
truth_state = GPIO.input(23) | |
if truth_state == False and last_truth_press == True: | |
last_truth_press = False | |
truth() | |
if truth_state == True: | |
last_truth_press = True | |
dare_state = GPIO.input(24) | |
if dare_state == False and last_dare_press == True: | |
last_dare_press = False | |
dare() | |
if dare_state == True: | |
last_dare_press = True | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment