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

Add mock display for local development #3

Merged
merged 1 commit into from
Jan 6, 2025
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea
**.pyc

# image storage when running locally
src/img.png
15 changes: 13 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ add the Waveshare SDK to your path (see [Dockerfile](./Dockerfile)).
on a webserver with more than a single worker, should you choose to use a webserver other than the Flask development
webserver.

## How to use
## Setup

### Install
### Install on a Raspberry Pi + Display

Build and run the container:

Expand All @@ -35,6 +35,17 @@ Now you're ready to send images to the display!

`--restart=always` will ensure that the container is restarted on crash and on system boot.

### Running locally

You can run also run the API locally using the `EPD_MOCK=true` env var to mock all calls to the display itself.
You can still check that the correct image is produced by looking at `src/img.png`.

```commandline
EPD_MOCK=true python3 src/server.py
```

## API

### Setting the image

`POST http://rpi:5000`
Expand Down
Empty file added src/epd_mock.py
Empty file.
9 changes: 6 additions & 3 deletions src/epd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

from models import Mode

path.append('lib')
# noinspection PyUnresolvedReferences
from waveshare_epd import epd4in26
try:
path.append('lib')
# noinspection PyUnresolvedReferences
from waveshare_epd import epd4in26
except Exception as e:
logging.warning('could not import wavesare lib')


def get_epd():
Expand Down
19 changes: 16 additions & 3 deletions src/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os
from pathlib import Path
from unittest.mock import Mock

from flask import Flask, request, jsonify, send_file, Response

Expand All @@ -13,15 +15,23 @@
from img_utils import resize_img, image_changed
from epd_utils import handle_epd_error, display_clear, display_img, get_epd


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

logging.basicConfig(level=logging.DEBUG)

register_heif_opener()

epd = get_epd()

def str_is_true(value) -> bool:
return value in [True, 'true', '1']


def mock_epd() -> bool:
return str_is_true(os.getenv('EPD_MOCK'))


epd = get_epd() if mock_epd() is False else Mock()

app = Flask(__name__)

Expand Down Expand Up @@ -70,7 +80,7 @@ def show_image() -> tuple[Response, int]:
except ValueError:
return jsonify(message=f'"mode" invalid, must be one of {", ".join([x for x in Mode])}'), 422

dither = request.form.get('dither', 'true') in ['true', '1']
dither = str_is_true(request.form.get('dither', True))

loc = locals()
image_settings = {i: loc[i] for i in ('mode', 'dither', 'rotate', 'resize', 'background')}
Expand Down Expand Up @@ -104,3 +114,6 @@ def clear_image():
return jsonify(message='Success'), 200
except Exception as e:
return handle_epd_error(e)

if __name__ == "__main__":
app.run(port=5000)
Loading