Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
adriankeenan committed Jan 6, 2025
1 parent b94648a commit 9cb6a8e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
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)

0 comments on commit 9cb6a8e

Please sign in to comment.