Last active
January 21, 2017 18:32
-
-
Save joachimesque/204f2f62422a2561972caa7b8f03e31f 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
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
# | |
# Dice display for Raspberry Pi + PaPiRus Zero 1.2 | |
# V1.2 | |
# By Joachim Robert | |
# http://github.com/joachimesque | |
# Shared under the WTFPL - Do What the Fuck You Want to Public License | |
# | |
# Script inspired by the papirus-buttons demo script | |
# by PiSupply for their PaPiRus | |
# https://github.com/PiSupply/PaPiRus/blob/master/bin/papirus-buttons | |
# BTW, the script is erroneous with the PaPiRus Zero: | |
# There are more buttons, and they aren't linked to the same GPIO pins | |
# https://pinout.xyz/pinout/papirus_zero is also wrong IIRC | |
# or it's my board that's wrong. | |
# | |
# dPoly fonts by Neale Davidson | |
# The font files are shareware (but free for Personnal Use) | |
# They have to be downloaded on | |
# http://www.pixelsagas.com/?download=dpoly | |
# | |
# HOW TO USE ? | |
# save this gist on your RasPi (using wget or curl) | |
# download the fonts, unzip to directory `fonts` | |
# or change `DICE_FONT_DIR` to the unzip directory | |
# rename the font files to D4.otf, D6.otf, D8.otf, D12.otf and D20.otf | |
# | |
# > sudo python papi-dice.py [number of dice from 1 to 3] | |
# | |
# and then you press one of the buttons | |
# | |
# TODO | |
# - get an Enviro pHAT to try the accelerometer, | |
# see if I can get the dice to roll by shaking my RasPi Zero | |
# | |
import os | |
from papirus import Papirus | |
from PIL import Image | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
from time import sleep | |
import argparse | |
from random import randint | |
import RPi.GPIO as GPIO | |
user = os.getuid() | |
if user !=0: | |
print "Root pls." | |
quit() | |
# Command line usage | |
# papi-dice [1-3] | |
# defaults : 2 | |
WHITE = 1 | |
BLACK = 0 | |
SIZE = 21 | |
SW1 = 26 | |
SW2 = 19 | |
SW3 = 20 | |
SW4 = 16 | |
SW5 = 21 | |
DEFAULT_FONT = '/usr/share/fonts/truetype/freefont/FreeMono.ttf' | |
DICE_FONT_DIR = 'fonts' | |
def main(): | |
p = argparse.ArgumentParser() | |
p.add_argument('content', type=int, default=2) | |
args = p.parse_args() | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(SW1, GPIO.IN) | |
GPIO.setup(SW2, GPIO.IN) | |
GPIO.setup(SW3, GPIO.IN) | |
GPIO.setup(SW4, GPIO.IN) | |
GPIO.setup(SW5, GPIO.IN) | |
papirus = Papirus() | |
if args.content < 1 or args.content > 3: | |
print "Please choose a number between 1 and 3." | |
quit() | |
write_text(papirus, "D= 4 6 8 12 20") | |
while True: | |
if GPIO.input(SW1) == False: | |
shuffle_and_display(papirus,4, args.content) | |
if GPIO.input(SW2) == False: | |
shuffle_and_display(papirus,6, args.content) | |
if GPIO.input(SW3) == False: | |
shuffle_and_display(papirus,8, args.content) | |
if GPIO.input(SW4) == False: | |
shuffle_and_display(papirus,12, args.content) | |
if GPIO.input(SW5) == False: | |
shuffle_and_display(papirus,20, args.content) | |
sleep(.2) | |
def write_text(papirus, text, dice=False, output_font=DEFAULT_FONT, size=SIZE, pos=(5,20)): | |
image = Image.new('1', papirus.size, WHITE) | |
draw = ImageDraw.Draw(image) | |
font = ImageFont.truetype(output_font, size) | |
if dice: # Dice display mode | |
draw.text(pos, text, font=font, fill=BLACK) | |
else: # Normal text mode | |
line_size = (papirus.width / (size * 0.65)) | |
current_line = 0 | |
text_lines = [""] | |
for word in text.split(): | |
if (len(text_lines[current_line]) + len(word)) < line_size: | |
text_lines[current_line] += " " + word | |
else: | |
text_lines.append("") | |
current_line += 1 | |
text_lines[current_line] += " " + word | |
current_line = 0 | |
for l in text_lines: | |
current_line += 1 | |
draw.text( (0, ((size*current_line)-size)), l, font=font, fill=BLACK) | |
papirus.display(image) | |
papirus.update() | |
print "screen updated" | |
def shuffle_and_display(papirus, nb_sides, nb_dice): | |
if nb_dice == 1: | |
size = 90 | |
elif nb_dice == 2: | |
size = 80 | |
else: | |
size = 70 | |
# shuffle | |
output = [] | |
for i in range(0, nb_dice): | |
output.append(randint(1,nb_sides)) | |
# control the output | |
print output, | |
# The font files are in the `fonts` folder | |
font = DICE_FONT_DIR + '/D' + str(nb_sides) + '.otf' | |
# Depending on the # of sides, the font has different dimensions | |
if nb_sides == 4: | |
output = ''.join(str(e) for e in output) | |
char = (85,73,8) # (px width at 100pt, px height, horizontal spacing) | |
elif nb_sides == 8: | |
output = ''.join(str(e) for e in output) | |
char = (78,78,10) # (px width at 100pt, px height, horizontal spacing) | |
elif nb_sides == 12: | |
output = numer_to_letter(output) | |
char = (79,78,19) # (px width at 100pt, px height, horizontal spacing) | |
elif nb_sides == 20: | |
output = numer_to_letter(output) | |
char = (74,73,14) # (px width at 100pt, px height, horizontal spacing) | |
else: # default option, always a D6 | |
output = ''.join(str(e) for e in output) | |
char = (74,73,7) # (px width at 100pt, px height, horizontal spacing) | |
char_list = [] | |
for i,x in enumerate(char): | |
char_list.append(x * size / 100) | |
char = tuple(char_list) | |
pos_x = (papirus.size[0] - (char[0] * nb_dice + char[2] * (nb_dice - 1))) / 2 | |
pos_y = (papirus.size[1] - char[0]) / 2 | |
pos = (pos_x, pos_y) | |
print pos, | |
write_text(papirus, output, True, font, size, pos) | |
def numer_to_letter(numer): | |
alpha = '' | |
alpha += ''.join(chr(96 + character) for character in numer) | |
return alpha | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment