Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0 90 180 270 rotation #119

Merged
merged 3 commits into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
637 changes: 310 additions & 327 deletions README.md

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions bin/papirus-buttons
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if (os.path.exists(hatdir + '/product')) and (os.path.exists(hatdir + '/vendor')
SW4 = 21
SW5 = -1

def main():
def main(argv):
global SIZE

GPIO.setmode(GPIO.BCM)
Expand All @@ -70,7 +70,8 @@ def main():
if SW5 != -1:
GPIO.setup(SW5, GPIO.IN)

papirus = Papirus()
papirus = Papirus(rotation = int(argv[0]) if len(sys.argv) > 1 else 0)


# Use smaller font for smaller dislays
if papirus.height <= 96:
Expand Down Expand Up @@ -141,4 +142,8 @@ def write_text(papirus, text, size):
papirus.partial_update()

if __name__ == '__main__':
main()
try:
main(sys.argv[1:])
except KeyboardInterrupt:
sys.exit('interrupted')

44 changes: 12 additions & 32 deletions bin/papirus-clock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# express or implied. See the License for the specific language
# governing permissions and limitations under the License.


import os
import sys

Expand Down Expand Up @@ -43,35 +42,14 @@ if not (os.path.exists('/dev/gpiomem') and os.access('/dev/gpiomem', os.R_OK | o
WHITE = 1
BLACK = 0

# fonts are in different places on Raspbian/Angstrom so search
possible_fonts = [
'/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono-Bold.ttf', # R.Pi
'/usr/share/fonts/truetype/freefont/FreeMono.ttf', # R.Pi
'/usr/share/fonts/truetype/LiberationMono-Bold.ttf', # B.B
'/usr/share/fonts/truetype/DejaVuSansMono-Bold.ttf', # B.B
'/usr/share/fonts/TTF/FreeMonoBold.ttf', # Arch
'/usr/share/fonts/TTF/DejaVuSans-Bold.ttf' # Arch
]


FONT_FILE = ''
for f in possible_fonts:
if os.path.exists(f):
FONT_FILE = f
break

if '' == FONT_FILE:
raise 'no font file found'

CLOCK_FONT_SIZE = 40
DATE_FONT_SIZE = 30

MAX_START = 0xffff
CLOCK_FONT_FILE = '/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf'
DATE_FONT_FILE = '/usr/share/fonts/truetype/freefont/FreeMono.ttf'

def main(argv):
"""main program - draw and display a test image"""

papirus = Papirus()
"""main program - draw and display time and date"""

papirus = Papirus(rotation = int(argv[0]) if len(sys.argv) > 1 else 0)

print('panel = {p:s} {w:d} x {h:d} version={v:s} COG={g:d} FILM={f:d}'.format(p=papirus.panel, w=papirus.width, h=papirus.height, v=papirus.version, g=papirus.cog, f=papirus.film))

Expand All @@ -81,7 +59,7 @@ def main(argv):


def demo(papirus):
"""simple partial update demo - draw draw a clock"""
"""simple partial update demo - draw a clock"""

# initially set all white background
image = Image.new('1', papirus.size, WHITE)
Expand All @@ -90,8 +68,10 @@ def demo(papirus):
draw = ImageDraw.Draw(image)
width, height = image.size

clock_font = ImageFont.truetype(FONT_FILE, CLOCK_FONT_SIZE)
date_font = ImageFont.truetype(FONT_FILE, DATE_FONT_SIZE)
clock_font_size = int((width - 4)/(8*0.65)) # 8 chars HH:MM:SS
clock_font = ImageFont.truetype(CLOCK_FONT_FILE, clock_font_size)
date_font_size = int((width - 10)/(10*0.65)) # 10 chars YYYY-MM-DD
date_font = ImageFont.truetype(DATE_FONT_FILE, date_font_size)

# clear the display buffer
draw.rectangle((0, 0, width, height), fill=WHITE, outline=WHITE)
Expand All @@ -107,10 +87,10 @@ def demo(papirus):

if now.day != previous_day:
draw.rectangle((2, 2, width - 2, height - 2), fill=WHITE, outline=BLACK)
draw.text((10, 55), '{y:04d}-{m:02d}-{d:02d}'.format(y=now.year, m=now.month, d=now.day), fill=BLACK, font=date_font)
draw.text((10, clock_font_size + 10), '{y:04d}-{m:02d}-{d:02d}'.format(y=now.year, m=now.month, d=now.day), fill=BLACK, font=date_font)
previous_day = now.day
else:
draw.rectangle((5, 10, width - 5, 10 + CLOCK_FONT_SIZE), fill=WHITE, outline=WHITE)
draw.rectangle((5, 10, width - 5, 10 + clock_font_size), fill=WHITE, outline=WHITE)

draw.text((5, 10), '{h:02d}:{m:02d}:{s:02d}'.format(h=now.hour, m=now.minute, s=now.second), fill=BLACK, font=clock_font)

Expand Down
16 changes: 15 additions & 1 deletion bin/papirus-draw
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
import time
from papirus import Papirus
from PIL import Image
import argparse
Expand Down Expand Up @@ -29,10 +30,19 @@ def main():
p = argparse.ArgumentParser()
p.add_argument('filepath', type=str)
p.add_argument('--type', '-t',type=str, default="resize", help="display type: crop or resize")
p.add_argument('--rotate', '-r',type=int, default=0, help="rotate = 0, 90, 180 or 270")
args = p.parse_args()
papirus = Papirus(rotation = args.rotate)
if args.filepath:
print("Drawing on PaPiRus.......")
draw_image(papirus, args.filepath, args.type)
for i in range(0, 5):
print("Rotation = " + str(papirus.rotation))
draw_image(papirus, args.filepath, args.type)
time.sleep(2)
if papirus.rotation == 270:
papirus.rotation = 0
else:
papirus.rotation = papirus.rotation + 90

def draw_image(papirus, filepath, type):
message = ""
Expand All @@ -49,25 +59,29 @@ def draw_image(papirus, filepath, type):
file.thumbnail((wsize, papirus.size[1]), Image.ANTIALIAS)
# centering the image
xpadding = int((papirus.size[0] - file.size[0]) / 2)
ypadding = int((papirus.size[1] - file.size[1]) / 2)
message = "Landscape image cropped!"
# Portrait image
else:
hsize = int(float(file.size[0]) * float(float(papirus.size[1]) / float(papirus.size[0])))
file.thumbnail((papirus.size[0], hsize), Image.ANTIALIAS)
# centering the image
xpadding = int((papirus.size[0] - file.size[0]) / 2)
ypadding = int((papirus.size[1] - file.size[1]) / 2)
message = "Portrait image cropped!"
# display resize option
else:
# Landscape image
if file.size[0] > file.size[1]:
file.thumbnail(papirus.size, Image.ANTIALIAS)
xpadding = int((papirus.size[0] - file.size[0]) / 2)
ypadding = int((papirus.size[1] - file.size[1]) / 2)
message = "Landscape image resized!"
# Portrait image
else:
file.thumbnail(papirus.size, Image.ANTIALIAS)
xpadding = int((papirus.size[0] - file.size[0]) / 2)
ypadding = int((papirus.size[1] - file.size[1]) / 2)
message = "Portrait image resized!"
# initially set all white background
image = Image.new('1', papirus.size, 1)
Expand Down
2 changes: 1 addition & 1 deletion bin/papirus-temp
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ fi
ie=$(echo "scale=1;(${id}*0.125)*10/10" | bc)
if=$(echo "scale=1;${ie}*1.8+32" | bc)
echo "Temp is ${ie} deg C (${if} deg F)."
papirus-write "Temp is ${ie}\\xb0C (${if}\\xb0F)."
papirus-write "Temp is ${ie}"$'\xb0'"C (${if}"$'\xb0'"F)."
71 changes: 36 additions & 35 deletions bin/papirus-textfill
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from papirus import Papirus
from PIL import ImageFont, ImageDraw, Image
import sys
import os
import time

# Check EPD_SIZE is defined
EPD_SIZE=0.0
Expand All @@ -25,59 +26,59 @@ BLACK = 0


def getFontSize(my_papirus, printstring):
#returns (ideal fontsize, (length of text, height of text)) that maximally
#fills a papirus object for a given string
fontsize = 0
stringlength = 0
stringwidth = 0
#returns (ideal fontsize, (length of text, height of text)) that maximally
#fills a papirus object for a given string
fontsize = 0
stringlength = 0
stringwidth = 0

maxLength = my_papirus.width
maxHeight = my_papirus.height
maxLength = my_papirus.width
maxHeight = my_papirus.height

while (stringlength <= maxLength and stringwidth <= maxHeight):
while (stringlength <= maxLength and stringwidth <= maxHeight):

fontsize += 1
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', fontsize)
size = font.getsize(printstring)
stringlength = size[0]
stringwidth = size[1]
fontsize += 1
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', fontsize)
size = font.getsize(printstring)
stringlength = size[0]
stringwidth = size[1]

font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', fontsize-1)
return fontsize-1, font.getsize(printstring)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', fontsize-1)
return fontsize-1, font.getsize(printstring)


def drawWords(my_papirus, printstring, fontsize, dims):

#initially set all white background
image = Image.new('1', my_papirus.size, WHITE)
#initially set all white background
image = Image.new('1', my_papirus.size, WHITE)

# prepare for drawing
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', fontsize)
# prepare for drawing
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', fontsize)

draw.text(((my_papirus.width-dims[0])/2, (my_papirus.height/2) - (dims[1]/2)), printstring, font=font, fill=BLACK)
draw.text(((my_papirus.width-dims[0])/2, (my_papirus.height/2) - (dims[1]/2)), printstring, font=font, fill=BLACK)

my_papirus.display(image)
my_papirus.update()
my_papirus.display(image)
my_papirus.update()




if (len(sys.argv) == 2):
if (len(sys.argv) >= 2):

printString = sys.argv[1]
printString = sys.argv[1]

if len(printString) > 40:
print 'WARNING: string length is too large for single line printing, truncating at 40 chars'
printString = printString[0:40]
if len(printString) > 40:
print 'WARNING: string length is too large for single line printing, truncating at 40 chars'
printString = printString[0:40]

my_papirus = Papirus()
fontsize, dims= getFontSize(my_papirus, printString)

print "Writing to Papirus...."
drawWords(my_papirus, printString, fontsize, dims)
print "Finished!"
rot = sys.argv[2] if len(sys.argv) >= 3 else '0'
my_papirus = Papirus(rotation = int(rot))
fontsize, dims= getFontSize(my_papirus, printString)
print "Writing to Papirus...."
drawWords(my_papirus, printString, fontsize, dims)
print "Finished!"

else:
print "Needs one commandline argument, a string to print"
print "Usage: " + sys.argv[0] + " \"text to display\" [rotation]"

6 changes: 4 additions & 2 deletions bin/papirus-write
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
import time
from papirus import PapirusTextPos
import argparse

Expand Down Expand Up @@ -29,12 +30,13 @@ def main():
p.add_argument('--posX', '-x', type=int, default=0, help="X position of the start of the text")
p.add_argument('--posY', '-y', type=int, default=0, help="Y position of the start of the text")
p.add_argument('--fsize', '-s',type=int , default=20, help="Font size to use for the text")
p.add_argument('--rotation', '-r',type=int , default=0, help="Rotation one of 0, 90, 180, 270")

args = p.parse_args()

if args.content:
print("Writing to Papirus.......")
text = PapirusTextPos()
text = PapirusTextPos(rotation=args.rotation)
print("Writing to Papirus.......")
text.AddText(args.content, args.posX, args.posY, args.fsize)
print("Finished!")

Expand Down
33 changes: 30 additions & 3 deletions papirus/epd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2013-2015 Pervasive Displays, Inc.
#qCopyright 2013-2015 Pervasive Displays, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,7 +35,7 @@ class EPD(object):
to use:
from EPD import EPD

epd = EPD([path='/path/to/epd'], [auto=boolean])
epd = EPD([path='/path/to/epd'], [auto=boolean], [rotation = 0|90|180|270])

image = Image.new('1', epd.size, 0)
# draw on image
Expand All @@ -56,6 +56,7 @@ def __init__(self, *args, **kwargs):
self._film = 0
self._auto = False
self._lm75b = LM75B()
self._rotation = 0
self._uselm75b = True

if len(args) > 0:
Expand All @@ -65,6 +66,12 @@ def __init__(self, *args, **kwargs):

if ('auto' in kwargs) and kwargs['auto']:
self._auto = True
if ('rotation' in kwargs):
rot = kwargs['rotation']
if rot == 0 or rot == 90 or rot == 180 or rot == 270:
self._rotation = rot
else:
raise EPDError('rotation can only be 0, 90, 180 or 270')

with open(os.path.join(self._epd_path, 'version')) as f:
self._version = f.readline().rstrip('\n')
Expand All @@ -82,7 +89,10 @@ def __init__(self, *args, **kwargs):

if self._width < 1 or self._height < 1:
raise EPDError('invalid panel geometry')

if self._rotation == 90 or self._rotation == 270:
w = self._height
self._height = self._width
self._width = w

@property
def size(self):
Expand Down Expand Up @@ -123,6 +133,20 @@ def auto(self, flag):
else:
self._auto = False

@property
def rotation(self):
return self._rotation

@rotation.setter
def rotation(self, rot):
if rot != 0 and rot != 90 and rot != 180 and rot != 270:
raise EPDError('rotation can only be 0, 90, 180 or 270')
if abs(self._rotation - rot) == 90 or abs(self._rotation - rot) == 270:
w = self._height
self._height = self._width
self._width = w
self._rotation = rot

@property
def use_lm75b(self):
return self._uselm75b
Expand Down Expand Up @@ -152,6 +176,9 @@ def display(self, image):
if image.size != self.size:
raise EPDError('image size mismatch')

if self._rotation != 0:
image = image.rotate(self._rotation)

with open(os.path.join(self._epd_path, 'LE', 'display_inverse'), 'r+b') as f:
f.write(image.tobytes())

Expand Down
4 changes: 2 additions & 2 deletions papirus/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

class PapirusImage():

def __init__(self):
self.papirus = Papirus()
def __init__(self, rotation = 0):
self.papirus = Papirus(rotation = rotation)

def write(self, image):
image = Image.open(image)
Expand Down
Loading