Skip to content

Commit

Permalink
global edp
Browse files Browse the repository at this point in the history
  • Loading branch information
adriankeenan committed Dec 29, 2024
1 parent ad5bc47 commit a664119
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
43 changes: 15 additions & 28 deletions src/epd_utils.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,36 @@
import logging
from contextlib import contextmanager
from sys import path

from PIL import Image
from flask import Response, jsonify
from filelock import Timeout as FileLockTimeout, FileLock

from models import Mode

path.append('lib')
# noinspection PyUnresolvedReferences
from waveshare_epd import epd4in26

@contextmanager
def get_epd_lock():
lock = FileLock('epd.lock', timeout=10)
with lock.acquire():
yield epd4in26.EPD()
def display_clear(epd):
epd.init()
epd.Clear()
epd.sleep()

def display_clear():
with get_epd_lock() as epd:
def display_img(epd, image: Image, mode: Mode):
if mode == Mode.FOUR_GRAY:
epd.init_4GRAY()
epd.display_4Gray(epd.getbuffer_4Gray(image))
if mode == Mode.PARTIAL:
epd.init()
epd.Clear()
epd.sleep()

def display_img(image: Image, mode: Mode):
with get_epd_lock() as epd:
if mode == Mode.FOUR_GRAY:
epd.init_4GRAY()
epd.display_4Gray(epd.getbuffer_4Gray(image))
if mode == Mode.PARTIAL:
epd.init()
epd.display_Partial(epd.getbuffer(image))
else:
epd.init_Fast()
epd.display_Fast(epd.getbuffer(image))
epd.sleep()
epd.display_Partial(epd.getbuffer(image))
else:
epd.init_Fast()
epd.display_Fast(epd.getbuffer(image))
epd.sleep()

def handle_epd_error(e: Exception) -> tuple[Response, int]:
if isinstance(e, IOError):
logging.error(f'IOError on display write: {str(e)}')
return jsonify(message='Unexpected error'), 500
elif isinstance(e, KeyboardInterrupt):
logging.info('Display write interrupted due to KeyboardInterrupt')
epd4in26.epdconfig.module_exit(cleanup=True)
#epd4in26.epdconfig.module_exit(cleanup=True)
return jsonify(message='Cancelled'), 500
elif isinstance(e, FileLockTimeout):
logging.info('Could not obtain device lock')
Expand Down
13 changes: 10 additions & 3 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
from models import Resolution, Rotation, Resize, BackgroundColour, Mode

from img_utils import resize_img, image_changed
from epd_utils import get_epd_lock, handle_epd_error, display_clear, display_img
from epd_utils import handle_epd_error, display_clear, display_img

from sys import path
path.append('lib')
# noinspection PyUnresolvedReferences
from waveshare_epd import epd4in26

IMG_PATH = 'img.png'
DISPLAY_RESOLUTION = Resolution(800, 480)

logging.basicConfig(level=logging.DEBUG)

epd = epd4in26.EPD()

app = Flask(__name__)


Expand Down Expand Up @@ -72,7 +79,7 @@ def show_image() -> tuple[Response, int]:

if update_image:
try:
display_img(image_to_display, mode)
display_img(epd, image_to_display, mode)
image_to_display.save(IMG_PATH)
except Exception as e:
return handle_epd_error(e)
Expand All @@ -83,7 +90,7 @@ def show_image() -> tuple[Response, int]:
@app.route("/", methods=['DELETE'])
def clear_image():
try:
display_clear()
display_clear(epd)
Image.new('1', DISPLAY_RESOLUTION, 255).save(IMG_PATH)
return jsonify(message='Success'), 200
except Exception as e:
Expand Down

0 comments on commit a664119

Please sign in to comment.