Skip to content
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
with:
components: rustfmt
- name: Check formatting
run: cargo fmt -- --check
run: cargo fmt --check --all
- name: Cache Cargo dependencies
uses: Swatinem/rust-cache@v2

Expand All @@ -65,6 +65,8 @@ jobs:
components: clippy
- name: Run clippy action
uses: clechasseur/rs-clippy-check@v4
with:
args: --workspace
- name: Cache Cargo dependencies
uses: Swatinem/rust-cache@v2

Expand Down Expand Up @@ -127,3 +129,4 @@ jobs:
default: true
- uses: Swatinem/rust-cache@v2
- run: cargo build --release --features=std --target ${{ matrix.target }} -Zbuild-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort

10 changes: 9 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "3"
members = ["mousefood"]
members = ["mousefood", "examples/*"]
default-members = ["mousefood"]

[workspace.package]
edition = "2024"
Expand Down
10 changes: 10 additions & 0 deletions examples/simulator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "simulator"
edition.workspace = true
license.workspace = true
publish = false

[dependencies]
mousefood = { path = "../../mousefood/" }
embedded-graphics-simulator.workspace = true
ratatui.workspace = true
24 changes: 24 additions & 0 deletions examples/simulator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- cargo-rdme start -->

# Simulator

Run mousefood apps on your computer inside a simulator! Uses [embedded-graphics-simulator](https://crates.io/crates/embedded-graphics-simulator).

## Requirements

This app requires [SDL2](https://wiki.libsdl.org/SDL2/Installation) to be installed.

If you use [nix](https://nixos.org) you can run `nix-shell -p SDL2`
before running the application.

## Run

To start this demo, simply run:

```shell
cargo run -p simulator
```

A window will open with the simulator running.

<!-- cargo-rdme end -->
72 changes: 72 additions & 0 deletions examples/simulator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! # Simulator
//!
//! Run mousefood apps on your computer inside a simulator! Uses [embedded-graphics-simulator](https://crates.io/crates/embedded-graphics-simulator).
//!
//! ## Requirements
//!
//! This app requires [SDL2](https://wiki.libsdl.org/SDL2/Installation) to be installed.
//!
//! If you use [nix](https://nixos.org) you can run `nix-shell -p SDL2`
//! before running the application.
//!
//! ## Run
//!
//! To start this demo, simply run:
//!
//! ```shell
//! cargo run -p simulator
//! ```
//!
//! A window will open with the simulator running.

use embedded_graphics_simulator::{OutputSettings, SimulatorDisplay, SimulatorEvent, Window};
use mousefood::embedded_graphics::geometry;
use mousefood::error::Error;
use mousefood::prelude::*;
use ratatui::widgets::{Block, Paragraph, Wrap};
use ratatui::{Frame, Terminal, style::*};

fn main() -> Result<(), Error> {
// Create window where the simulation will happen
let mut simulator_window = Window::new(
"mousefood simulator",
&OutputSettings {
scale: 4,
max_fps: 30,
..Default::default()
},
);

// Define properties of the display which will be shown in the simulator window
let mut display = SimulatorDisplay::<Bgr565>::new(geometry::Size::new(128, 64));

let backend_config = EmbeddedBackendConfig {
// Define how to display newly rendered widgets to the simulator window
flush_callback: Box::new(move |display| {
simulator_window.update(display);
if simulator_window.events().any(|e| e == SimulatorEvent::Quit) {
panic!("simulator window closed");
}
}),
..Default::default()
};
let backend: EmbeddedBackend<SimulatorDisplay<_>, _> =
EmbeddedBackend::new(&mut display, backend_config);

// Start ratatui with our simulator backend
let mut terminal = Terminal::new(backend)?;

// Run an infinite loop, where widgets will be rendered
loop {
terminal.draw(draw)?;
}
}

fn draw(frame: &mut Frame) {
let text = "Ratatui on embedded devices!";
let paragraph = Paragraph::new(text.dark_gray()).wrap(Wrap { trim: true });
let bordered_block = Block::bordered()
.border_style(Style::new().yellow())
.title("Mousefood");
frame.render_widget(paragraph.block(bordered_block), frame.area());
}
6 changes: 0 additions & 6 deletions mousefood/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ exclude.workspace = true
ratatui-core.workspace = true
thiserror.workspace = true
embedded-graphics.workspace = true
embedded-graphics-simulator = { workspace = true, optional = true }
embedded-graphics-unicodefonts = { workspace = true, optional = true }
weact-studio-epd = { workspace = true, optional = true }

Expand All @@ -28,13 +27,8 @@ paste.workspace = true
[features]
default = ["fonts"]
std = ["thiserror/std", "ratatui-core/std"]
simulator = ["dep:embedded-graphics-simulator"]
fonts = ["dep:embedded-graphics-unicodefonts"]
epd-weact = ["dep:weact-studio-epd"]

[[example]]
name = "simulator"
required-features = ["simulator"]

[lints]
workspace = true
27 changes: 0 additions & 27 deletions mousefood/examples/simulator.rs

This file was deleted.

47 changes: 4 additions & 43 deletions mousefood/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use embedded_graphics::geometry::{self, Dimensions};
use embedded_graphics::mono_font::{MonoFont, MonoTextStyleBuilder};
use embedded_graphics::pixelcolor::{PixelColor, Rgb888};
use embedded_graphics::text::Text;
#[cfg(feature = "simulator")]
use embedded_graphics_simulator::{OutputSettings, SimulatorDisplay, SimulatorEvent, Window};
use ratatui_core::backend::{Backend, ClearType};
use ratatui_core::layout;
use ratatui_core::style;
Expand Down Expand Up @@ -62,16 +60,10 @@ where
D: DrawTarget<Color = C> + 'display,
C: PixelColor + 'display,
{
#[cfg(not(feature = "simulator"))]
display: &'display mut D,
#[cfg(feature = "simulator")]
display: &'display mut SimulatorDisplay<C>,
display_type: PhantomData<D>,

#[cfg(not(feature = "simulator"))]
flush_callback: Box<dyn FnMut(&mut D)>,
#[cfg(feature = "simulator")]
flush_callback: Box<dyn FnMut(&mut SimulatorDisplay<C>)>,

buffer: framebuffer::HeapBuffer<C>,

Expand All @@ -83,9 +75,6 @@ where

columns_rows: layout::Size,
pixels: layout::Size,

#[cfg(feature = "simulator")]
simulator_window: Window,
}

impl<'display, D, C> EmbeddedBackend<'display, D, C>
Expand All @@ -94,10 +83,8 @@ where
C: PixelColor + Into<Rgb888> + From<Rgb888> + From<TermColor> + 'static,
{
fn init(
#[cfg(not(feature = "simulator"))] display: &'display mut D,
#[cfg(feature = "simulator")] display: &'display mut SimulatorDisplay<C>,
#[cfg(not(feature = "simulator"))] flush_callback: impl FnMut(&mut D) + 'static,
#[cfg(feature = "simulator")] flush_callback: impl FnMut(&mut SimulatorDisplay<C>) + 'static,
display: &'display mut D,
flush_callback: impl FnMut(&mut D) + 'static,
font_regular: MonoFont<'static>,
font_bold: Option<MonoFont<'static>>,
font_italic: Option<MonoFont<'static>>,
Expand All @@ -120,24 +107,13 @@ where
width: pixels.width / font_regular.character_size.width as u16,
},
pixels,
#[cfg(feature = "simulator")]
simulator_window: Window::new(
"mousefood emulator",
&OutputSettings {
scale: 4,
max_fps: 30,
..Default::default()
},
),
}
}

/// Creates a new `EmbeddedBackend` using default fonts.
pub fn new(
#[cfg(not(feature = "simulator"))] display: &'display mut D,
#[cfg(feature = "simulator")] display: &'display mut SimulatorDisplay<C>,
#[cfg(not(feature = "simulator"))] config: EmbeddedBackendConfig<D, C>,
#[cfg(feature = "simulator")] config: EmbeddedBackendConfig<SimulatorDisplay<C>, C>,
display: &'display mut D,
config: EmbeddedBackendConfig<D, C>,
) -> EmbeddedBackend<'display, D, C> {
Self::init(
display,
Expand All @@ -147,19 +123,6 @@ where
config.font_italic,
)
}

#[cfg(feature = "simulator")]
fn update_simulation(&mut self) -> Result<()> {
self.simulator_window.update(self.display);
if self
.simulator_window
.events()
.any(|e| e == SimulatorEvent::Quit)
{
return Err(crate::error::Error::SimulatorQuit);
}
Ok(())
}
}

type Result<T, E = crate::error::Error> = core::result::Result<T, E>;
Expand Down Expand Up @@ -281,8 +244,6 @@ where
.fill_contiguous(&self.display.bounding_box(), &self.buffer)
.map_err(|_| crate::error::Error::DrawError)?;
(self.flush_callback)(self.display);
#[cfg(feature = "simulator")]
self.update_simulation()?;
Ok(())
}
}
4 changes: 0 additions & 4 deletions mousefood/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ pub enum Error {
/// Selected [`ClearType`] is not supported by Mousefood.
#[error("ClearType::{0} is not supported by Mousefood")]
ClearTypeUnsupported(alloc::string::String),
/// Simulator window was closed.
#[cfg(feature = "simulator")]
#[error("simulator window closed")]
SimulatorQuit,
}
3 changes: 0 additions & 3 deletions mousefood/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ pub mod prelude;
pub use backend::{EmbeddedBackend, EmbeddedBackendConfig};
pub use embedded_graphics;

#[cfg(feature = "simulator")]
pub use embedded_graphics_simulator as simulator;

#[cfg(feature = "fonts")]
pub use embedded_graphics_unicodefonts as fonts;