Skip to content

Commit

Permalink
add rotation argument to screen class
Browse files Browse the repository at this point in the history
  • Loading branch information
txoof committed Oct 8, 2020
1 parent 747fb57 commit 8a7d5b0
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 34 deletions.
121 changes: 95 additions & 26 deletions epdlib/Screen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@
"cells": [
{
"cell_type": "code",
"execution_count": 31,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook Screen.ipynb to python\n",
"[NbConvertApp] Writing 10820 bytes to Screen.py\n"
]
}
],
"outputs": [],
"source": [
"!jupyter-nbconvert --to python --template python_clean Screen.ipynb"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -34,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -61,7 +52,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -138,7 +129,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -195,7 +186,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -205,7 +196,7 @@
" `Screen` creates an object that provides methods for assembling images\n",
" and updating a WaveShare EPD.\"\"\"\n",
" \n",
" def __init__(self, resolution=None, elements=None, epd=None):\n",
" def __init__(self, resolution=None, elements=None, epd=None, rotation=0):\n",
" \"\"\"Constructor for Screen class.\n",
" \n",
" Properties:\n",
Expand Down Expand Up @@ -250,16 +241,24 @@
" if epd:\n",
" self.epd = epd\n",
" self.image = self.clearScreen()\n",
"\n",
" \n",
" self.rotation = rotation\n",
" \n",
" self.update = Update()\n",
"\n",
" @property\n",
" def epd(self):\n",
" '''epd type \n",
" \n",
" Sets properties:\n",
" resolution: epd width and height (defaults to landscape)\n",
" image: empty `PIL.Image`\n",
" buffer_no_image: empty `epd.getbuffer(Image)` to support colored screens that require an additional image'''\n",
" return self._epd\n",
" \n",
" @epd.setter\n",
" def epd(self, epd):\n",
" ''''''\n",
" self._epd = epd.EPD()\n",
" # set resolution for screen\n",
" resolution = [epd.EPD_HEIGHT, epd.EPD_WIDTH]\n",
Expand All @@ -269,6 +268,30 @@
" self.image = self.clearScreen()\n",
" self.buffer_no_image = self._epd.getbuffer(Image.new('L', self.resolution, 255))\n",
" \n",
" @property\n",
" def rotation(self):\n",
" return self._rotation\n",
" \n",
" @rotation.setter\n",
" @strict_enforce(int)\n",
" def rotation(self, rotation):\n",
" '''\n",
" set the display rotation'''\n",
" \n",
" if rotation not in [0, 90, -90, 180]:\n",
" raise ValueError('value must be type `int` and [0, 90, -90, 180]')\n",
" self._rotation = rotation\n",
" if rotation == 90 or rotation == -90:\n",
" \n",
" resolution = self.resolution\n",
" # set short dimension first\n",
" resolution.sort()\n",
" # set resolution\n",
" self.resolution = resolution\n",
" # set a new clearscreen image\n",
" self.image = self.clearScreen()\n",
" self.buffer_no_image = self.epd.getbuffer(Image.new('L', self.resolution, 255))\n",
" \n",
" def clearScreen(self):\n",
" '''Sets a clean base image for building screen layout.\n",
" \n",
Expand Down Expand Up @@ -351,6 +374,11 @@
" bool: True if successful\n",
" '''\n",
" epd = self.epd\n",
" # rotate the image as needed\n",
" if self.rotation == 180 or self.rotation == -90:\n",
" image = image.rotate(180)\n",
" \n",
" \n",
" if not self.epd:\n",
" raise UnboundLocalError('no epd object has been assigned')\n",
" try:\n",
Expand All @@ -377,30 +405,71 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def main():\n",
" # set your screent type here\n",
" from waveshare_epd import epd2in7b as my_epd\n",
" print('refresh screen -- screen should flash and be wiped')\n",
" s = Screen()\n",
" from waveshare_epd import epd5in83 as my_epd\n",
" import Layout\n",
" import sys\n",
" \n",
" s.epd = my_epd\n",
" s.initEPD()\n",
" s.writeEPD(s.clearScreen())"
" sys.path.append('../')\n",
" \n",
" for r in [0, 90, -90, 180]:\n",
" print(f'setup for rotation: {r}')\n",
" s = Screen(epd=my_epd, rotation=r)\n",
" myLayout = {\n",
" 'title': { # text only block\n",
" 'image': None, # do not expect an image\n",
" 'max_lines': 3, # number of lines of text\n",
" 'width': 1, # 1/1 of the width - this stretches the entire width of the display\n",
" 'height': 4/7, # 1/3 of the entire height\n",
" 'abs_coordinates': (0, 0), # this block is the key block that all other blocks will be defined in terms of\n",
" 'hcenter': True, # horizontally center text\n",
" 'vcenter': True, # vertically center text \n",
" 'relative': False, # this block is not relative to any other. It has an ABSOLUTE position (0, 0)\n",
" 'font': '../fonts/Font.ttc', # path to font file\n",
" 'font_size': None # Calculate the font size because none was provided\n",
" },\n",
"\n",
" 'artist': {\n",
" 'image': None,\n",
" 'max_lines': 2,\n",
" 'width': 1,\n",
" 'height': 3/7,\n",
" 'abs_coordinates': (0, None), # X = 0, Y will be calculated\n",
" 'hcenter': True,\n",
" 'vcenter': True,\n",
" 'font': '../fonts/Font.ttc',\n",
" 'relative': ['artist', 'title'], # use the X postion from abs_coord from `artist` (this block: 0)\n",
" # calculate the y position based on the size of `title` block\n",
"\n",
" }\n",
" } \n",
" l = Layout.Layout(resolution=s.resolution)\n",
" l.layout = myLayout\n",
" l.update_contents({'title': 'spam, spam, spam, spam & ham', 'artist': 'monty python'})\n",
" print('print some text on the display')\n",
" s.initEPD()\n",
" s.writeEPD(l.concat())\n",
" print('refresh screen -- screen should flash and be wiped')\n",
" s.initEPD()\n",
" s.writeEPD(s.clearScreen())"
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"setup for rotation: 0\n",
"print some text on the display\n",
"refresh screen -- screen should flash and be wiped\n"
]
}
Expand Down
92 changes: 84 additions & 8 deletions epdlib/Screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class Screen:
`Screen` creates an object that provides methods for assembling images
and updating a WaveShare EPD."""

def __init__(self, resolution=None, elements=None, epd=None):
def __init__(self, resolution=None, elements=None, epd=None, rotation=0):
"""Constructor for Screen class.
Properties:
Expand Down Expand Up @@ -224,16 +224,24 @@ def __init__(self, resolution=None, elements=None, epd=None):
if epd:
self.epd = epd
self.image = self.clearScreen()


self.rotation = rotation

self.update = Update()

@property
def epd(self):
'''epd type
Sets properties:
resolution: epd width and height (defaults to landscape)
image: empty `PIL.Image`
buffer_no_image: empty `epd.getbuffer(Image)` to support colored screens that require an additional image'''
return self._epd

@epd.setter
def epd(self, epd):
''''''
self._epd = epd.EPD()
# set resolution for screen
resolution = [epd.EPD_HEIGHT, epd.EPD_WIDTH]
Expand All @@ -243,6 +251,30 @@ def epd(self, epd):
self.image = self.clearScreen()
self.buffer_no_image = self._epd.getbuffer(Image.new('L', self.resolution, 255))

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

@rotation.setter
@strict_enforce(int)
def rotation(self, rotation):
'''
set the display rotation'''

if rotation not in [0, 90, -90, 180]:
raise ValueError('value must be type `int` and [0, 90, -90, 180]')
self._rotation = rotation
if rotation == 90 or rotation == -90:

resolution = self.resolution
# set short dimension first
resolution.sort()
# set resolution
self.resolution = resolution
# set a new clearscreen image
self.image = self.clearScreen()
self.buffer_no_image = self.epd.getbuffer(Image.new('L', self.resolution, 255))

def clearScreen(self):
'''Sets a clean base image for building screen layout.
Expand Down Expand Up @@ -325,6 +357,11 @@ def writeEPD(self, image, sleep=True):
bool: True if successful
'''
epd = self.epd
# rotate the image as needed
if self.rotation == 180 or self.rotation == -90:
image = image.rotate(180)


if not self.epd:
raise UnboundLocalError('no epd object has been assigned')
try:
Expand Down Expand Up @@ -355,13 +392,52 @@ def writeEPD(self, image, sleep=True):

def main():
# set your screent type here
from waveshare_epd import epd2in7b as my_epd
print('refresh screen -- screen should flash and be wiped')
s = Screen()
from waveshare_epd import epd5in83 as my_epd
import Layout
import sys

sys.path.append('../')

s.epd = my_epd
s.initEPD()
s.writeEPD(s.clearScreen())
for r in [0, 90, -90, 180]:
print(f'setup for rotation: {r}')
s = Screen(epd=my_epd, rotation=r)
myLayout = {
'title': { # text only block
'image': None, # do not expect an image
'max_lines': 3, # number of lines of text
'width': 1, # 1/1 of the width - this stretches the entire width of the display
'height': 4/7, # 1/3 of the entire height
'abs_coordinates': (0, 0), # this block is the key block that all other blocks will be defined in terms of
'hcenter': True, # horizontally center text
'vcenter': True, # vertically center text
'relative': False, # this block is not relative to any other. It has an ABSOLUTE position (0, 0)
'font': '../fonts/Font.ttc', # path to font file
'font_size': None # Calculate the font size because none was provided
},

'artist': {
'image': None,
'max_lines': 2,
'width': 1,
'height': 3/7,
'abs_coordinates': (0, None), # X = 0, Y will be calculated
'hcenter': True,
'vcenter': True,
'font': '../fonts/Font.ttc',
'relative': ['artist', 'title'], # use the X postion from abs_coord from `artist` (this block: 0)
# calculate the y position based on the size of `title` block

}
}
l = Layout.Layout(resolution=s.resolution)
l.layout = myLayout
l.update_contents({'title': 'spam, spam, spam, spam & ham', 'artist': 'monty python'})
print('print some text on the display')
s.initEPD()
s.writeEPD(l.concat())
print('refresh screen -- screen should flash and be wiped')
s.initEPD()
s.writeEPD(s.clearScreen())



Expand Down

0 comments on commit 8a7d5b0

Please sign in to comment.