Skip to content

Commit 07e6918

Browse files
committed
feat(simulator)!: removed simulator feature
- moved simulator from mousefood/examples to examples/simulator - removed `simulator` feature - ci: build all examples together
1 parent e92ddf9 commit 07e6918

File tree

11 files changed

+116
-85
lines changed

11 files changed

+116
-85
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,22 @@ jobs:
127127
default: true
128128
- uses: Swatinem/rust-cache@v2
129129
- run: cargo build --release --features=std --target ${{ matrix.target }} -Zbuild-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort
130+
131+
build-examples:
132+
runs-on: ubuntu-latest
133+
name: Build examples
134+
steps:
135+
- uses: actions/checkout@v4
136+
- name: Install native dependencies
137+
run: |
138+
sudo apt-get update
139+
sudo apt-get install -y libsdl2-dev
140+
- name: Install Rust
141+
uses: dtolnay/rust-toolchain@stable
142+
- name: Cache Cargo dependencies
143+
uses: Swatinem/rust-cache@v2
144+
- name: Build examples
145+
# build all crates which manifest's path is ./examples/**/Cargo.toml
146+
# use `xargs` to propagate `cargo build` failures
147+
run: find examples/ -name "Cargo.toml" -print0 | xargs -0 -i cargo build --manifest-path {}
148+

Cargo.lock

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
resolver = "3"
3-
members = ["mousefood"]
3+
members = ["mousefood", "examples/*"]
4+
default-members = ["mousefood"]
45

56
[workspace.package]
67
edition = "2024"

examples/simulator/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "simulator"
3+
edition.workspace = true
4+
license.workspace = true
5+
publish = false
6+
7+
[dependencies]
8+
mousefood = { path = "../../mousefood/" }
9+
embedded-graphics-simulator.workspace = true
10+
ratatui.workspace = true

examples/simulator/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Simulator
2+
3+
Run mousefood apps on your computer inside a simulator! Uses [embedded-graphics-simulator](https://crates.io/crates/embedded-graphics-simulator).
4+
5+
## Requirements
6+
7+
This app requires [SDL2](https://wiki.libsdl.org/SDL2/Installation) to be installed.
8+
9+
If you use [nix](https://nixos.org) you can run `nix-shell -p SDL2` before running the application.
10+
11+
## Run
12+
13+
To start this demo, simply run:
14+
15+
```shell
16+
cargo run -p simulator
17+
```
18+
19+
A window will open with the simulator running.

examples/simulator/src/main.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#![doc = include_str!("../README.md")]
2+
3+
use embedded_graphics_simulator::{OutputSettings, SimulatorDisplay, SimulatorEvent, Window};
4+
use mousefood::embedded_graphics::geometry;
5+
use mousefood::error::Error;
6+
use mousefood::prelude::*;
7+
use ratatui::widgets::{Block, Paragraph, Wrap};
8+
use ratatui::{Frame, Terminal, style::*};
9+
10+
fn main() -> Result<(), Error> {
11+
// Create window where the simulation will happen
12+
let mut simulator_window = Window::new(
13+
"mousefood simulator",
14+
&OutputSettings {
15+
scale: 4,
16+
max_fps: 30,
17+
..Default::default()
18+
},
19+
);
20+
21+
// Define properties of the display which will be shown in the simulator window
22+
let mut display = SimulatorDisplay::<Bgr565>::new(geometry::Size::new(128, 64));
23+
24+
let backend_config = EmbeddedBackendConfig {
25+
// Define how to display newly rendered widgets to the simulator window
26+
flush_callback: Box::new(move |display| {
27+
simulator_window.update(display);
28+
if simulator_window.events().any(|e| e == SimulatorEvent::Quit) {
29+
panic!("simulator window closed");
30+
}
31+
}),
32+
..Default::default()
33+
};
34+
let backend: EmbeddedBackend<SimulatorDisplay<_>, _> =
35+
EmbeddedBackend::new(&mut display, backend_config);
36+
37+
// Start ratatui with our simulator backend
38+
let mut terminal = Terminal::new(backend)?;
39+
40+
// Run an infinite loop, where widgets will be rendered
41+
loop {
42+
terminal.draw(draw)?;
43+
}
44+
}
45+
46+
fn draw(frame: &mut Frame) {
47+
let text = "Ratatui on embedded devices!";
48+
let paragraph = Paragraph::new(text.dark_gray()).wrap(Wrap { trim: true });
49+
let bordered_block = Block::bordered()
50+
.border_style(Style::new().yellow())
51+
.title("Mousefood");
52+
frame.render_widget(paragraph.block(bordered_block), frame.area());
53+
}

mousefood/Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ exclude.workspace = true
1616
ratatui-core.workspace = true
1717
thiserror.workspace = true
1818
embedded-graphics.workspace = true
19-
embedded-graphics-simulator = { workspace = true, optional = true }
2019
embedded-graphics-unicodefonts = { workspace = true, optional = true }
2120
weact-studio-epd = { workspace = true, optional = true }
2221

@@ -28,13 +27,8 @@ paste.workspace = true
2827
[features]
2928
default = ["fonts"]
3029
std = ["thiserror/std", "ratatui-core/std"]
31-
simulator = ["dep:embedded-graphics-simulator"]
3230
fonts = ["dep:embedded-graphics-unicodefonts"]
3331
epd-weact = ["dep:weact-studio-epd"]
3432

35-
[[example]]
36-
name = "simulator"
37-
required-features = ["simulator"]
38-
3933
[lints]
4034
workspace = true

mousefood/examples/simulator.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

mousefood/src/backend.rs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use embedded_graphics::geometry::{self, Dimensions};
1010
use embedded_graphics::mono_font::{MonoFont, MonoTextStyleBuilder};
1111
use embedded_graphics::pixelcolor::{PixelColor, Rgb888};
1212
use embedded_graphics::text::Text;
13-
#[cfg(feature = "simulator")]
14-
use embedded_graphics_simulator::{OutputSettings, SimulatorDisplay, SimulatorEvent, Window};
1513
use ratatui_core::backend::{Backend, ClearType};
1614
use ratatui_core::layout;
1715
use ratatui_core::style;
@@ -62,16 +60,10 @@ where
6260
D: DrawTarget<Color = C> + 'display,
6361
C: PixelColor + 'display,
6462
{
65-
#[cfg(not(feature = "simulator"))]
6663
display: &'display mut D,
67-
#[cfg(feature = "simulator")]
68-
display: &'display mut SimulatorDisplay<C>,
6964
display_type: PhantomData<D>,
7065

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

7668
buffer: framebuffer::HeapBuffer<C>,
7769

@@ -83,9 +75,6 @@ where
8375

8476
columns_rows: layout::Size,
8577
pixels: layout::Size,
86-
87-
#[cfg(feature = "simulator")]
88-
simulator_window: Window,
8978
}
9079

9180
impl<'display, D, C> EmbeddedBackend<'display, D, C>
@@ -94,10 +83,8 @@ where
9483
C: PixelColor + Into<Rgb888> + From<Rgb888> + From<TermColor> + 'static,
9584
{
9685
fn init(
97-
#[cfg(not(feature = "simulator"))] display: &'display mut D,
98-
#[cfg(feature = "simulator")] display: &'display mut SimulatorDisplay<C>,
99-
#[cfg(not(feature = "simulator"))] flush_callback: impl FnMut(&mut D) + 'static,
100-
#[cfg(feature = "simulator")] flush_callback: impl FnMut(&mut SimulatorDisplay<C>) + 'static,
86+
display: &'display mut D,
87+
flush_callback: impl FnMut(&mut D) + 'static,
10188
font_regular: MonoFont<'static>,
10289
font_bold: Option<MonoFont<'static>>,
10390
font_italic: Option<MonoFont<'static>>,
@@ -120,24 +107,13 @@ where
120107
width: pixels.width / font_regular.character_size.width as u16,
121108
},
122109
pixels,
123-
#[cfg(feature = "simulator")]
124-
simulator_window: Window::new(
125-
"mousefood emulator",
126-
&OutputSettings {
127-
scale: 4,
128-
max_fps: 30,
129-
..Default::default()
130-
},
131-
),
132110
}
133111
}
134112

135113
/// Creates a new `EmbeddedBackend` using default fonts.
136114
pub fn new(
137-
#[cfg(not(feature = "simulator"))] display: &'display mut D,
138-
#[cfg(feature = "simulator")] display: &'display mut SimulatorDisplay<C>,
139-
#[cfg(not(feature = "simulator"))] config: EmbeddedBackendConfig<D, C>,
140-
#[cfg(feature = "simulator")] config: EmbeddedBackendConfig<SimulatorDisplay<C>, C>,
115+
display: &'display mut D,
116+
config: EmbeddedBackendConfig<D, C>,
141117
) -> EmbeddedBackend<'display, D, C> {
142118
Self::init(
143119
display,
@@ -147,19 +123,6 @@ where
147123
config.font_italic,
148124
)
149125
}
150-
151-
#[cfg(feature = "simulator")]
152-
fn update_simulation(&mut self) -> Result<()> {
153-
self.simulator_window.update(self.display);
154-
if self
155-
.simulator_window
156-
.events()
157-
.any(|e| e == SimulatorEvent::Quit)
158-
{
159-
return Err(crate::error::Error::SimulatorQuit);
160-
}
161-
Ok(())
162-
}
163126
}
164127

165128
type Result<T, E = crate::error::Error> = core::result::Result<T, E>;
@@ -281,8 +244,6 @@ where
281244
.fill_contiguous(&self.display.bounding_box(), &self.buffer)
282245
.map_err(|_| crate::error::Error::DrawError)?;
283246
(self.flush_callback)(self.display);
284-
#[cfg(feature = "simulator")]
285-
self.update_simulation()?;
286247
Ok(())
287248
}
288249
}

mousefood/src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,4 @@ pub enum Error {
99
/// Selected [`ClearType`] is not supported by Mousefood.
1010
#[error("ClearType::{0} is not supported by Mousefood")]
1111
ClearTypeUnsupported(alloc::string::String),
12-
/// Simulator window was closed.
13-
#[cfg(feature = "simulator")]
14-
#[error("simulator window closed")]
15-
SimulatorQuit,
1612
}

0 commit comments

Comments
 (0)