Skip to content

Commit

Permalink
fix: make sure kivy.core.window is loaded in setup_headless to av…
Browse files Browse the repository at this point in the history
…oid segmentation fault
  • Loading branch information
sassanh committed Dec 31, 2023
1 parent 9a80b1c commit 05e5f23
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
27 changes: 21 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# Changelog

## Version 0.5.12

- fix: make sure `kivy.core.window` is loaded in `setup_headless` to avoid
segmentation fault

## Version 0.5.11

- docs: remove instructions to deal with legacy Raspbian from `README.md`

## Version 0.5.10

- feat: ensure `setup_headless` is called before instantiating `HeadlessWidget`
- feat: periodically write snapshots of the screen to filesystem in debug mode for remote debugging
- feat: periodically write snapshots of the screen to filesystem in debug mode for
remote debugging
- refactor: fix pyright and ruff errors and warnings by faking modules
- refactor: split `__init__.py` into several files

Expand Down Expand Up @@ -87,7 +97,8 @@

## Version 0.2.1

- fix: make adafruit-circuitpython-rgb-display conditionally install only on raspberry pi
- fix: make adafruit-circuitpython-rgb-display conditionally install only on raspberry
pi

## Version 0.2.0

Expand All @@ -96,17 +107,21 @@
- feat: add typings and docs
- docs: add pagination demo
- chore: update poetry packages and config
- feat: add activate_low_fps_mode and activate_high_fps_mode to manually set fps when needed
- feat: add activate_low_fps_mode and activate_high_fps_mode to manually set fps
when needed
- feat: add logging and improve fps handler
- docs: add sdl installation instructions and a reference to demo.py in README.md
- feat: drop Kivy FPS to min_fps when the user interface is idle
- feat: Add default values for `setup_headless()` based on environment variables
- docs: add demo.py to show all the features in action
- docs: add README.md
- feat: avoid sending data to the lcd when the current frame is the same as the last one
- feat: avoid sending data to the lcd when the current frame is the same as the last
one
- feat: add double buffering
- feat: add synchronous mode to save CPU time by avoiding Kivy from rendering frames when last frame is not yet rendered on the lcd
- feat: add synchronous mode to save CPU time by avoiding Kivy from rendering frames
when last frame is not yet rendered on the lcd

## Version 0.1.0

- feat: implement Headless widget to render content on SPI display without needing a display server/window manager
- feat: implement Headless widget to render content on SPI display without needing
a display server/window manager
59 changes: 32 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Kivy Headless Renderer for Raspberry Pi

This project demonstrates the use of the Kivy framework to create a headless renderer for a Raspberry Pi. The renderer is specifically designed for and tested with the ST7789 SPI display, but it should work with other SPI displays as well. The code utilizes the Adafruit RGB Display library to communicate with the display. The renderer is optimized to not update the LCD if nothing has changed in the frame.
This project uses the Kivy framework to create a headless renderer
for a Raspberry Pi. The renderer is specifically designed for and tested with the
ST7789 SPI display, but it should work with other SPI displays as well. The code
utilizes the Adafruit RGB Display library to communicate with the display. The
renderer is optimized to not update the LCD if nothing has changed in the frame.

## ⚡️ Requirements
## 📋 Requirements

- Raspberry Pi 4 or 5
- SPI Display (tested with ST7789 module)
Expand All @@ -24,9 +28,12 @@ pip install headless-kivy-pi[dev]
poetry --group dev headless-kivy-pi
```

## 🚀 Usage
## 🛠 Usage

1. Call setup_headless() before inheriting the `HeadlessWidget` class for the root widget of your application, and provide the optional parameters as needed. For example (these are all default values, you only need to provide the ones you want to change):
1. Call setup_headless() before inheriting the `HeadlessWidget` class for the root
widget of your application, and provide the optional parameters as needed. For
example (these are all default values, you only need to provide the ones you want
to change):

```python
setup_headless(
Expand All @@ -44,7 +51,8 @@ poetry --group dev headless-kivy-pi
)
```

1. Inherit the `HeadlessWidget` class for the root widget of your Kivy application. For example:
1. Inherit the `HeadlessWidget` class for the root widget of your Kivy application.
For example:

```python
class FboFloatLayout(FloatLayout, HeadlessWidget):
Expand Down Expand Up @@ -89,48 +97,45 @@ The display class to use (default is ST7789).

#### `double_buffering`

Is set to `True`, it will let Kivy generate the next frame while sending the last frame to the display.
Is set to `True`, it will let Kivy generate the next frame while sending the last
frame to the display.

#### `synchronous_clock`

If set to `True`, Kivy will wait for the LCD before rendering next frames. This will cause Headless to skip frames if they are rendered before the LCD has finished displaying the previous frames. If set to False, frames will be rendered asynchronously, letting Kivy render frames regardless of display being able to catch up or not at the expense of possible frame skipping.
If set to `True`, Kivy will wait for the LCD before rendering next frames. This will
cause Headless to skip frames if they are rendered before the LCD has finished displaying
the previous frames. If set to False, frames will be rendered asynchronously, letting
Kivy render frames regardless of display being able to catch up or not at the expense
of possible frame skipping.

#### `automatic_fps`

If set to `True`, it will monitor the hash of the screen data, if this hash changes, it will increase the fps to the maximum and if the hash doesn't change for a while, it will drop the fps to the minimum.
If set to `True`, it will monitor the hash of the screen data, if this hash changes,
it will increase the fps to the maximum and if the hash doesn't change for a while,
it will drop the fps to the minimum.

#### `clear_at_exit`

If set to `True`, it will clear the screen before exiting.

## ⚒️ Contribution
## 🤝 Contributing

You need to have [Poetry](https://python-poetry.org/) installed on your machine.

To install poetry in Raspbian you need to follow these instructions to install rust compiler, this is temporary until [this issue](https://github.com/python-poetry/poetry/issues/7645) is resolved:

```sh
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
sudo apt-get install pkg-config libssl-dev
curl -sSL https://install.python-poetry.org | python3 -
```

After having poetry, to install the required dependencies, run the following command:
After having poetry, to install the required dependencies, run the following command
in the root directory of the project:

```sh
poetry install
```

Also be aware of [this issue](https://github.com/python-poetry/poetry/issues/1917) and until it is resolved you can manually disable keyring by prefixing your poetry commands like this:

```sh
PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring poetry install
```

## ⚠️ Important Note

This project has only been tested with the ST7789 SPI display module. Other display modules might not be compatible or may require changing the parameters or even modifications to the code.
This project has only been tested with the ST7789 SPI display module. Other display
modules might not be compatible or may require changing the parameters or even modifications
to the code.

## 📜 License
## 🔒 License

This project is released under the Apache-2.0 License.
This project is released under the Apache-2.0 License. See the [LICENSE](./LICENSE)
file for more details.
12 changes: 5 additions & 7 deletions headless_kivy_pi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ def __init__(self: HeadlessWidget, **kwargs: Any) -> None: # noqa: ANN401
)
raise RuntimeError(msg)

if HeadlessWidget.width is None or HeadlessWidget.height is None:
msg = '"setup_headless" should be called before instantiating "Headless"'
raise RuntimeError(msg)
if self.debug_mode:
self.last_second = int(time.time())
self.rendered_frames = 0
Expand Down Expand Up @@ -135,9 +132,9 @@ def __init__(self: HeadlessWidget, **kwargs: Any) -> None: # noqa: ANN401
interval=True,
)
self.render_trigger()
App.get_running_app().bind(
on_stop=lambda _: self.render_trigger.cancel(),
)
app = App.get_running_app()
if app:
app.bind(on_stop=lambda _: self.render_trigger.cancel())

def add_widget(
self: HeadlessWidget,
Expand Down Expand Up @@ -356,6 +353,8 @@ def setup_headless(
msg,
)

from kivy.core.window import Window

if IS_RPI:
Config.set('graphics', 'window_state', 'hidden')
spi = board.SPI()
Expand Down Expand Up @@ -386,7 +385,6 @@ def setup_headless(
),
)
else:
from kivy.core.window import Window
from screeninfo import get_monitors

monitor = get_monitors()[0]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "headless-kivy-pi"
version = "0.5.10"
version = "0.5.12"
description = "Headless renderer for Kivy framework on Raspberry Pi"
authors = ["Sassan Haradji <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit 05e5f23

Please sign in to comment.