pub struct OutputSettingsBuilder { /* private fields */ }
Expand description
Output settings builder.
Implementations§
Source§impl OutputSettingsBuilder
impl OutputSettingsBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates new output settings builder.
Examples found in repository?
9fn main() {
10 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
11
12 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
13 let centered = TextStyleBuilder::new()
14 .baseline(Baseline::Middle)
15 .alignment(Alignment::Center)
16 .build();
17
18 Text::with_text_style(
19 "embedded-graphics",
20 display.bounding_box().center(),
21 large_text,
22 centered,
23 )
24 .draw(&mut display)
25 .unwrap();
26
27 let output_settings = OutputSettingsBuilder::new().scale(2).build();
28 let output_image = display.to_grayscale_output_image(&output_settings);
29
30 println!(
31 "<img src=\"data:image/png;base64,{}\">",
32 output_image.to_base64_png().unwrap()
33 );
34}
More examples
9fn main() {
10 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
11
12 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
13 let centered = TextStyleBuilder::new()
14 .baseline(Baseline::Middle)
15 .alignment(Alignment::Center)
16 .build();
17
18 Text::with_text_style(
19 "embedded-graphics",
20 display.bounding_box().center(),
21 large_text,
22 centered,
23 )
24 .draw(&mut display)
25 .unwrap();
26
27 let output_settings = OutputSettingsBuilder::new().scale(2).build();
28 let output_image = display.to_rgb_output_image(&output_settings);
29
30 let path = std::env::args_os()
31 .nth(1)
32 .expect("expected PNG file name argument");
33 output_image.save_png(path).unwrap();
34}
11fn main() {
12 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
13
14 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
15 let centered = TextStyleBuilder::new()
16 .baseline(Baseline::Middle)
17 .alignment(Alignment::Center)
18 .build();
19
20 Text::with_text_style(
21 "embedded-graphics",
22 display.bounding_box().center(),
23 large_text,
24 centered,
25 )
26 .draw(&mut display)
27 .unwrap();
28
29 // Uncomment one of the `theme` lines to use a different theme.
30 let output_settings = OutputSettingsBuilder::new()
31 //.theme(BinaryColorTheme::LcdGreen)
32 //.theme(BinaryColorTheme::LcdWhite)
33 .theme(BinaryColorTheme::LcdBlue)
34 //.theme(BinaryColorTheme::OledBlue)
35 //.theme(BinaryColorTheme::OledWhite)
36 .build();
37
38 let mut window = Window::new("Themes", &output_settings);
39 window.show_static(&display);
40}
31fn main() -> Result<(), core::convert::Infallible> {
32 // Prepare the audio "engine" with gate control
33 let gate = Arc::new(AtomicBool::new(false));
34 let audio_wrapper = AudioWrapper::new(gate.clone());
35
36 let audio_spec = AudioSpecDesired {
37 freq: Some(SAMPLE_RATE),
38 channels: Some(1),
39 samples: Some(32),
40 };
41
42 // Initialize the SDL audio subsystem.
43 //
44 // `sdl2` allows multiple instances of the SDL context to exist, which makes
45 // it possible to access SDL subsystems which aren't used by the simulator.
46 // But keep in mind that only one `EventPump` can exists and the simulator
47 // window creation will fail if the `EventPump` is claimed in advance.
48 let sdl = sdl2::init().unwrap();
49 let audio_subsystem = sdl.audio().unwrap();
50
51 // Start audio playback by opening the device and setting the custom callback.
52 let audio_device = audio_subsystem
53 .open_playback(None, &audio_spec, |_| audio_wrapper)
54 .unwrap();
55 audio_device.resume();
56
57 let output_settings = OutputSettingsBuilder::new()
58 .scale(4)
59 .theme(embedded_graphics_simulator::BinaryColorTheme::OledWhite)
60 .build();
61
62 let mut window = Window::new("Simulator audio example", &output_settings);
63
64 let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
65 let text_position = Point::new(25, 30);
66 let text = Text::new("Press space...", text_position, text_style);
67
68 let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
69 text.draw(&mut display).unwrap();
70 'running: loop {
71 window.update(&display);
72
73 for event in window.events() {
74 match event {
75 SimulatorEvent::Quit => break 'running,
76 SimulatorEvent::KeyDown {
77 keycode, repeat, ..
78 } if keycode == Keycode::Space && !repeat => {
79 gate.store(true, Ordering::SeqCst);
80 display.clear(BinaryColor::On).unwrap();
81 }
82 SimulatorEvent::KeyUp { keycode, .. } => match keycode {
83 Keycode::Space => {
84 gate.store(false, Ordering::SeqCst);
85 display.clear(BinaryColor::Off).unwrap();
86 text.draw(&mut display).unwrap();
87 }
88 _ => {}
89 },
90 _ => {}
91 }
92 }
93 }
94
95 Ok(())
96}
39fn main() -> Result<(), core::convert::Infallible> {
40 // Create three simulated monochrome 128x64 OLED displays.
41
42 let mut oled_displays = Vec::new();
43 for i in 0..3 {
44 let mut oled: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
45
46 Text::with_text_style(
47 &format!("Display {i}"),
48 oled.bounding_box().center(),
49 OLED_TEXT,
50 CENTERED,
51 )
52 .draw(&mut oled)
53 .unwrap();
54
55 oled_displays.push(oled);
56 }
57
58 // Create a simulated color 320x240 TFT display.
59
60 let mut tft: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(320, 240));
61 tft.clear(Rgb565::new(5, 10, 5)).unwrap();
62
63 Text::with_text_style(
64 &format!("Draw here"),
65 tft.bounding_box().center(),
66 TFT_TEXT,
67 CENTERED,
68 )
69 .draw(&mut tft)
70 .unwrap();
71
72 // The simulated displays can now be added to common simulator window.
73
74 let window_size = Size::new(1300, 500);
75 let mut window = MultiWindow::new("Multiple displays example", window_size);
76 window.clear(Rgb888::CSS_DIM_GRAY);
77
78 let oled_settings = OutputSettingsBuilder::new()
79 .theme(BinaryColorTheme::OledBlue)
80 .scale(2)
81 .build();
82 let oled_size = oled_displays[0].output_size(&oled_settings);
83
84 for (oled, anchor) in oled_displays.iter().zip(
85 [
86 AnchorPoint::TopLeft,
87 AnchorPoint::TopCenter,
88 AnchorPoint::TopRight,
89 ]
90 .into_iter(),
91 ) {
92 let offset = display_offset(window_size, oled_size, anchor);
93 window.add_display(&oled, offset, &oled_settings);
94 }
95
96 let tft_settings = OutputSettings::default();
97 let tft_size = tft.output_size(&tft_settings);
98 let tft_offset = display_offset(window_size, tft_size, AnchorPoint::BottomCenter);
99
100 window.add_display(&tft, tft_offset, &tft_settings);
101
102 let border_style = PrimitiveStyleBuilder::new()
103 .stroke_width(5)
104 .stroke_alignment(StrokeAlignment::Inside)
105 .build();
106
107 let mut mouse_down = false;
108
109 'running: loop {
110 // Call `update_display` for all display. Note that the window won't be
111 // updated until `window.flush` is called.
112 for oled in &oled_displays {
113 window.update_display(oled);
114 }
115 window.update_display(&tft);
116 window.flush();
117
118 for event in window.events() {
119 match event {
120 SimulatorEvent::MouseMove { point } => {
121 // Mouse events use the window coordinate system.
122 // `translate_mouse_position` can be used to translate the
123 // mouse position into the display coordinate system.
124
125 for oled in &mut oled_displays {
126 let is_inside = window.translate_mouse_position(oled, point).is_some();
127
128 let style = PrimitiveStyleBuilder::from(&border_style)
129 .stroke_color(BinaryColor::from(is_inside))
130 .build();
131
132 oled.bounding_box().into_styled(style).draw(oled).unwrap();
133 }
134
135 if mouse_down {
136 if let Some(point) = window.translate_mouse_position(&tft, point) {
137 Circle::with_center(point, 10)
138 .into_styled(PrimitiveStyle::with_fill(Rgb565::CSS_DODGER_BLUE))
139 .draw(&mut tft)
140 .unwrap();
141 }
142 }
143 }
144 SimulatorEvent::MouseButtonDown {
145 mouse_btn: MouseButton::Left,
146 ..
147 } => {
148 mouse_down = true;
149 }
150 SimulatorEvent::MouseButtonUp {
151 mouse_btn: MouseButton::Left,
152 ..
153 } => {
154 mouse_down = false;
155 }
156 SimulatorEvent::Quit => break 'running,
157 _ => {}
158 }
159 }
160 }
161
162 Ok(())
163}
Sourcepub fn scale(self, scale: u32) -> Self
pub fn scale(self, scale: u32) -> Self
Sets the pixel scale.
A scale of 2
or higher is useful for viewing the simulator on high DPI displays.
§Panics
Panics if the scale is set to 0
.
Examples found in repository?
9fn main() {
10 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
11
12 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
13 let centered = TextStyleBuilder::new()
14 .baseline(Baseline::Middle)
15 .alignment(Alignment::Center)
16 .build();
17
18 Text::with_text_style(
19 "embedded-graphics",
20 display.bounding_box().center(),
21 large_text,
22 centered,
23 )
24 .draw(&mut display)
25 .unwrap();
26
27 let output_settings = OutputSettingsBuilder::new().scale(2).build();
28 let output_image = display.to_grayscale_output_image(&output_settings);
29
30 println!(
31 "<img src=\"data:image/png;base64,{}\">",
32 output_image.to_base64_png().unwrap()
33 );
34}
More examples
9fn main() {
10 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
11
12 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
13 let centered = TextStyleBuilder::new()
14 .baseline(Baseline::Middle)
15 .alignment(Alignment::Center)
16 .build();
17
18 Text::with_text_style(
19 "embedded-graphics",
20 display.bounding_box().center(),
21 large_text,
22 centered,
23 )
24 .draw(&mut display)
25 .unwrap();
26
27 let output_settings = OutputSettingsBuilder::new().scale(2).build();
28 let output_image = display.to_rgb_output_image(&output_settings);
29
30 let path = std::env::args_os()
31 .nth(1)
32 .expect("expected PNG file name argument");
33 output_image.save_png(path).unwrap();
34}
31fn main() -> Result<(), core::convert::Infallible> {
32 // Prepare the audio "engine" with gate control
33 let gate = Arc::new(AtomicBool::new(false));
34 let audio_wrapper = AudioWrapper::new(gate.clone());
35
36 let audio_spec = AudioSpecDesired {
37 freq: Some(SAMPLE_RATE),
38 channels: Some(1),
39 samples: Some(32),
40 };
41
42 // Initialize the SDL audio subsystem.
43 //
44 // `sdl2` allows multiple instances of the SDL context to exist, which makes
45 // it possible to access SDL subsystems which aren't used by the simulator.
46 // But keep in mind that only one `EventPump` can exists and the simulator
47 // window creation will fail if the `EventPump` is claimed in advance.
48 let sdl = sdl2::init().unwrap();
49 let audio_subsystem = sdl.audio().unwrap();
50
51 // Start audio playback by opening the device and setting the custom callback.
52 let audio_device = audio_subsystem
53 .open_playback(None, &audio_spec, |_| audio_wrapper)
54 .unwrap();
55 audio_device.resume();
56
57 let output_settings = OutputSettingsBuilder::new()
58 .scale(4)
59 .theme(embedded_graphics_simulator::BinaryColorTheme::OledWhite)
60 .build();
61
62 let mut window = Window::new("Simulator audio example", &output_settings);
63
64 let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
65 let text_position = Point::new(25, 30);
66 let text = Text::new("Press space...", text_position, text_style);
67
68 let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
69 text.draw(&mut display).unwrap();
70 'running: loop {
71 window.update(&display);
72
73 for event in window.events() {
74 match event {
75 SimulatorEvent::Quit => break 'running,
76 SimulatorEvent::KeyDown {
77 keycode, repeat, ..
78 } if keycode == Keycode::Space && !repeat => {
79 gate.store(true, Ordering::SeqCst);
80 display.clear(BinaryColor::On).unwrap();
81 }
82 SimulatorEvent::KeyUp { keycode, .. } => match keycode {
83 Keycode::Space => {
84 gate.store(false, Ordering::SeqCst);
85 display.clear(BinaryColor::Off).unwrap();
86 text.draw(&mut display).unwrap();
87 }
88 _ => {}
89 },
90 _ => {}
91 }
92 }
93 }
94
95 Ok(())
96}
39fn main() -> Result<(), core::convert::Infallible> {
40 // Create three simulated monochrome 128x64 OLED displays.
41
42 let mut oled_displays = Vec::new();
43 for i in 0..3 {
44 let mut oled: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
45
46 Text::with_text_style(
47 &format!("Display {i}"),
48 oled.bounding_box().center(),
49 OLED_TEXT,
50 CENTERED,
51 )
52 .draw(&mut oled)
53 .unwrap();
54
55 oled_displays.push(oled);
56 }
57
58 // Create a simulated color 320x240 TFT display.
59
60 let mut tft: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(320, 240));
61 tft.clear(Rgb565::new(5, 10, 5)).unwrap();
62
63 Text::with_text_style(
64 &format!("Draw here"),
65 tft.bounding_box().center(),
66 TFT_TEXT,
67 CENTERED,
68 )
69 .draw(&mut tft)
70 .unwrap();
71
72 // The simulated displays can now be added to common simulator window.
73
74 let window_size = Size::new(1300, 500);
75 let mut window = MultiWindow::new("Multiple displays example", window_size);
76 window.clear(Rgb888::CSS_DIM_GRAY);
77
78 let oled_settings = OutputSettingsBuilder::new()
79 .theme(BinaryColorTheme::OledBlue)
80 .scale(2)
81 .build();
82 let oled_size = oled_displays[0].output_size(&oled_settings);
83
84 for (oled, anchor) in oled_displays.iter().zip(
85 [
86 AnchorPoint::TopLeft,
87 AnchorPoint::TopCenter,
88 AnchorPoint::TopRight,
89 ]
90 .into_iter(),
91 ) {
92 let offset = display_offset(window_size, oled_size, anchor);
93 window.add_display(&oled, offset, &oled_settings);
94 }
95
96 let tft_settings = OutputSettings::default();
97 let tft_size = tft.output_size(&tft_settings);
98 let tft_offset = display_offset(window_size, tft_size, AnchorPoint::BottomCenter);
99
100 window.add_display(&tft, tft_offset, &tft_settings);
101
102 let border_style = PrimitiveStyleBuilder::new()
103 .stroke_width(5)
104 .stroke_alignment(StrokeAlignment::Inside)
105 .build();
106
107 let mut mouse_down = false;
108
109 'running: loop {
110 // Call `update_display` for all display. Note that the window won't be
111 // updated until `window.flush` is called.
112 for oled in &oled_displays {
113 window.update_display(oled);
114 }
115 window.update_display(&tft);
116 window.flush();
117
118 for event in window.events() {
119 match event {
120 SimulatorEvent::MouseMove { point } => {
121 // Mouse events use the window coordinate system.
122 // `translate_mouse_position` can be used to translate the
123 // mouse position into the display coordinate system.
124
125 for oled in &mut oled_displays {
126 let is_inside = window.translate_mouse_position(oled, point).is_some();
127
128 let style = PrimitiveStyleBuilder::from(&border_style)
129 .stroke_color(BinaryColor::from(is_inside))
130 .build();
131
132 oled.bounding_box().into_styled(style).draw(oled).unwrap();
133 }
134
135 if mouse_down {
136 if let Some(point) = window.translate_mouse_position(&tft, point) {
137 Circle::with_center(point, 10)
138 .into_styled(PrimitiveStyle::with_fill(Rgb565::CSS_DODGER_BLUE))
139 .draw(&mut tft)
140 .unwrap();
141 }
142 }
143 }
144 SimulatorEvent::MouseButtonDown {
145 mouse_btn: MouseButton::Left,
146 ..
147 } => {
148 mouse_down = true;
149 }
150 SimulatorEvent::MouseButtonUp {
151 mouse_btn: MouseButton::Left,
152 ..
153 } => {
154 mouse_down = false;
155 }
156 SimulatorEvent::Quit => break 'running,
157 _ => {}
158 }
159 }
160 }
161
162 Ok(())
163}
Sourcepub fn theme(self, theme: BinaryColorTheme) -> Self
pub fn theme(self, theme: BinaryColorTheme) -> Self
Sets the binary color theme.
The binary color theme defines the mapping between the two display colors
and the output. The variants provided by the BinaryColorTheme
enum
simulate the color scheme of commonly used display types.
Most binary color displays are relatively small individual pixels are hard to recognize on higher resolution screens. Because of this some scaling is automatically applied to the output when a theme is set and no scaling was specified explicitly.
Note that a theme should only be set when an monochrome display is used. Setting a theme when using a color display will cause an corrupted output.
Examples found in repository?
11fn main() {
12 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
13
14 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
15 let centered = TextStyleBuilder::new()
16 .baseline(Baseline::Middle)
17 .alignment(Alignment::Center)
18 .build();
19
20 Text::with_text_style(
21 "embedded-graphics",
22 display.bounding_box().center(),
23 large_text,
24 centered,
25 )
26 .draw(&mut display)
27 .unwrap();
28
29 // Uncomment one of the `theme` lines to use a different theme.
30 let output_settings = OutputSettingsBuilder::new()
31 //.theme(BinaryColorTheme::LcdGreen)
32 //.theme(BinaryColorTheme::LcdWhite)
33 .theme(BinaryColorTheme::LcdBlue)
34 //.theme(BinaryColorTheme::OledBlue)
35 //.theme(BinaryColorTheme::OledWhite)
36 .build();
37
38 let mut window = Window::new("Themes", &output_settings);
39 window.show_static(&display);
40}
More examples
31fn main() -> Result<(), core::convert::Infallible> {
32 // Prepare the audio "engine" with gate control
33 let gate = Arc::new(AtomicBool::new(false));
34 let audio_wrapper = AudioWrapper::new(gate.clone());
35
36 let audio_spec = AudioSpecDesired {
37 freq: Some(SAMPLE_RATE),
38 channels: Some(1),
39 samples: Some(32),
40 };
41
42 // Initialize the SDL audio subsystem.
43 //
44 // `sdl2` allows multiple instances of the SDL context to exist, which makes
45 // it possible to access SDL subsystems which aren't used by the simulator.
46 // But keep in mind that only one `EventPump` can exists and the simulator
47 // window creation will fail if the `EventPump` is claimed in advance.
48 let sdl = sdl2::init().unwrap();
49 let audio_subsystem = sdl.audio().unwrap();
50
51 // Start audio playback by opening the device and setting the custom callback.
52 let audio_device = audio_subsystem
53 .open_playback(None, &audio_spec, |_| audio_wrapper)
54 .unwrap();
55 audio_device.resume();
56
57 let output_settings = OutputSettingsBuilder::new()
58 .scale(4)
59 .theme(embedded_graphics_simulator::BinaryColorTheme::OledWhite)
60 .build();
61
62 let mut window = Window::new("Simulator audio example", &output_settings);
63
64 let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
65 let text_position = Point::new(25, 30);
66 let text = Text::new("Press space...", text_position, text_style);
67
68 let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
69 text.draw(&mut display).unwrap();
70 'running: loop {
71 window.update(&display);
72
73 for event in window.events() {
74 match event {
75 SimulatorEvent::Quit => break 'running,
76 SimulatorEvent::KeyDown {
77 keycode, repeat, ..
78 } if keycode == Keycode::Space && !repeat => {
79 gate.store(true, Ordering::SeqCst);
80 display.clear(BinaryColor::On).unwrap();
81 }
82 SimulatorEvent::KeyUp { keycode, .. } => match keycode {
83 Keycode::Space => {
84 gate.store(false, Ordering::SeqCst);
85 display.clear(BinaryColor::Off).unwrap();
86 text.draw(&mut display).unwrap();
87 }
88 _ => {}
89 },
90 _ => {}
91 }
92 }
93 }
94
95 Ok(())
96}
39fn main() -> Result<(), core::convert::Infallible> {
40 // Create three simulated monochrome 128x64 OLED displays.
41
42 let mut oled_displays = Vec::new();
43 for i in 0..3 {
44 let mut oled: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
45
46 Text::with_text_style(
47 &format!("Display {i}"),
48 oled.bounding_box().center(),
49 OLED_TEXT,
50 CENTERED,
51 )
52 .draw(&mut oled)
53 .unwrap();
54
55 oled_displays.push(oled);
56 }
57
58 // Create a simulated color 320x240 TFT display.
59
60 let mut tft: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(320, 240));
61 tft.clear(Rgb565::new(5, 10, 5)).unwrap();
62
63 Text::with_text_style(
64 &format!("Draw here"),
65 tft.bounding_box().center(),
66 TFT_TEXT,
67 CENTERED,
68 )
69 .draw(&mut tft)
70 .unwrap();
71
72 // The simulated displays can now be added to common simulator window.
73
74 let window_size = Size::new(1300, 500);
75 let mut window = MultiWindow::new("Multiple displays example", window_size);
76 window.clear(Rgb888::CSS_DIM_GRAY);
77
78 let oled_settings = OutputSettingsBuilder::new()
79 .theme(BinaryColorTheme::OledBlue)
80 .scale(2)
81 .build();
82 let oled_size = oled_displays[0].output_size(&oled_settings);
83
84 for (oled, anchor) in oled_displays.iter().zip(
85 [
86 AnchorPoint::TopLeft,
87 AnchorPoint::TopCenter,
88 AnchorPoint::TopRight,
89 ]
90 .into_iter(),
91 ) {
92 let offset = display_offset(window_size, oled_size, anchor);
93 window.add_display(&oled, offset, &oled_settings);
94 }
95
96 let tft_settings = OutputSettings::default();
97 let tft_size = tft.output_size(&tft_settings);
98 let tft_offset = display_offset(window_size, tft_size, AnchorPoint::BottomCenter);
99
100 window.add_display(&tft, tft_offset, &tft_settings);
101
102 let border_style = PrimitiveStyleBuilder::new()
103 .stroke_width(5)
104 .stroke_alignment(StrokeAlignment::Inside)
105 .build();
106
107 let mut mouse_down = false;
108
109 'running: loop {
110 // Call `update_display` for all display. Note that the window won't be
111 // updated until `window.flush` is called.
112 for oled in &oled_displays {
113 window.update_display(oled);
114 }
115 window.update_display(&tft);
116 window.flush();
117
118 for event in window.events() {
119 match event {
120 SimulatorEvent::MouseMove { point } => {
121 // Mouse events use the window coordinate system.
122 // `translate_mouse_position` can be used to translate the
123 // mouse position into the display coordinate system.
124
125 for oled in &mut oled_displays {
126 let is_inside = window.translate_mouse_position(oled, point).is_some();
127
128 let style = PrimitiveStyleBuilder::from(&border_style)
129 .stroke_color(BinaryColor::from(is_inside))
130 .build();
131
132 oled.bounding_box().into_styled(style).draw(oled).unwrap();
133 }
134
135 if mouse_down {
136 if let Some(point) = window.translate_mouse_position(&tft, point) {
137 Circle::with_center(point, 10)
138 .into_styled(PrimitiveStyle::with_fill(Rgb565::CSS_DODGER_BLUE))
139 .draw(&mut tft)
140 .unwrap();
141 }
142 }
143 }
144 SimulatorEvent::MouseButtonDown {
145 mouse_btn: MouseButton::Left,
146 ..
147 } => {
148 mouse_down = true;
149 }
150 SimulatorEvent::MouseButtonUp {
151 mouse_btn: MouseButton::Left,
152 ..
153 } => {
154 mouse_down = false;
155 }
156 SimulatorEvent::Quit => break 'running,
157 _ => {}
158 }
159 }
160 }
161
162 Ok(())
163}
Sourcepub fn pixel_spacing(self, pixel_spacing: u32) -> Self
pub fn pixel_spacing(self, pixel_spacing: u32) -> Self
Sets the gap between pixels.
Most lower resolution displays have visible gaps between individual pixels.
This effect can be simulated by setting the pixel spacing to a value greater
than 0
.
Sourcepub fn build(self) -> OutputSettings
pub fn build(self) -> OutputSettings
Builds the output settings.
Examples found in repository?
9fn main() {
10 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
11
12 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
13 let centered = TextStyleBuilder::new()
14 .baseline(Baseline::Middle)
15 .alignment(Alignment::Center)
16 .build();
17
18 Text::with_text_style(
19 "embedded-graphics",
20 display.bounding_box().center(),
21 large_text,
22 centered,
23 )
24 .draw(&mut display)
25 .unwrap();
26
27 let output_settings = OutputSettingsBuilder::new().scale(2).build();
28 let output_image = display.to_grayscale_output_image(&output_settings);
29
30 println!(
31 "<img src=\"data:image/png;base64,{}\">",
32 output_image.to_base64_png().unwrap()
33 );
34}
More examples
9fn main() {
10 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
11
12 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
13 let centered = TextStyleBuilder::new()
14 .baseline(Baseline::Middle)
15 .alignment(Alignment::Center)
16 .build();
17
18 Text::with_text_style(
19 "embedded-graphics",
20 display.bounding_box().center(),
21 large_text,
22 centered,
23 )
24 .draw(&mut display)
25 .unwrap();
26
27 let output_settings = OutputSettingsBuilder::new().scale(2).build();
28 let output_image = display.to_rgb_output_image(&output_settings);
29
30 let path = std::env::args_os()
31 .nth(1)
32 .expect("expected PNG file name argument");
33 output_image.save_png(path).unwrap();
34}
11fn main() {
12 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
13
14 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
15 let centered = TextStyleBuilder::new()
16 .baseline(Baseline::Middle)
17 .alignment(Alignment::Center)
18 .build();
19
20 Text::with_text_style(
21 "embedded-graphics",
22 display.bounding_box().center(),
23 large_text,
24 centered,
25 )
26 .draw(&mut display)
27 .unwrap();
28
29 // Uncomment one of the `theme` lines to use a different theme.
30 let output_settings = OutputSettingsBuilder::new()
31 //.theme(BinaryColorTheme::LcdGreen)
32 //.theme(BinaryColorTheme::LcdWhite)
33 .theme(BinaryColorTheme::LcdBlue)
34 //.theme(BinaryColorTheme::OledBlue)
35 //.theme(BinaryColorTheme::OledWhite)
36 .build();
37
38 let mut window = Window::new("Themes", &output_settings);
39 window.show_static(&display);
40}
31fn main() -> Result<(), core::convert::Infallible> {
32 // Prepare the audio "engine" with gate control
33 let gate = Arc::new(AtomicBool::new(false));
34 let audio_wrapper = AudioWrapper::new(gate.clone());
35
36 let audio_spec = AudioSpecDesired {
37 freq: Some(SAMPLE_RATE),
38 channels: Some(1),
39 samples: Some(32),
40 };
41
42 // Initialize the SDL audio subsystem.
43 //
44 // `sdl2` allows multiple instances of the SDL context to exist, which makes
45 // it possible to access SDL subsystems which aren't used by the simulator.
46 // But keep in mind that only one `EventPump` can exists and the simulator
47 // window creation will fail if the `EventPump` is claimed in advance.
48 let sdl = sdl2::init().unwrap();
49 let audio_subsystem = sdl.audio().unwrap();
50
51 // Start audio playback by opening the device and setting the custom callback.
52 let audio_device = audio_subsystem
53 .open_playback(None, &audio_spec, |_| audio_wrapper)
54 .unwrap();
55 audio_device.resume();
56
57 let output_settings = OutputSettingsBuilder::new()
58 .scale(4)
59 .theme(embedded_graphics_simulator::BinaryColorTheme::OledWhite)
60 .build();
61
62 let mut window = Window::new("Simulator audio example", &output_settings);
63
64 let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
65 let text_position = Point::new(25, 30);
66 let text = Text::new("Press space...", text_position, text_style);
67
68 let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
69 text.draw(&mut display).unwrap();
70 'running: loop {
71 window.update(&display);
72
73 for event in window.events() {
74 match event {
75 SimulatorEvent::Quit => break 'running,
76 SimulatorEvent::KeyDown {
77 keycode, repeat, ..
78 } if keycode == Keycode::Space && !repeat => {
79 gate.store(true, Ordering::SeqCst);
80 display.clear(BinaryColor::On).unwrap();
81 }
82 SimulatorEvent::KeyUp { keycode, .. } => match keycode {
83 Keycode::Space => {
84 gate.store(false, Ordering::SeqCst);
85 display.clear(BinaryColor::Off).unwrap();
86 text.draw(&mut display).unwrap();
87 }
88 _ => {}
89 },
90 _ => {}
91 }
92 }
93 }
94
95 Ok(())
96}
39fn main() -> Result<(), core::convert::Infallible> {
40 // Create three simulated monochrome 128x64 OLED displays.
41
42 let mut oled_displays = Vec::new();
43 for i in 0..3 {
44 let mut oled: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
45
46 Text::with_text_style(
47 &format!("Display {i}"),
48 oled.bounding_box().center(),
49 OLED_TEXT,
50 CENTERED,
51 )
52 .draw(&mut oled)
53 .unwrap();
54
55 oled_displays.push(oled);
56 }
57
58 // Create a simulated color 320x240 TFT display.
59
60 let mut tft: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(320, 240));
61 tft.clear(Rgb565::new(5, 10, 5)).unwrap();
62
63 Text::with_text_style(
64 &format!("Draw here"),
65 tft.bounding_box().center(),
66 TFT_TEXT,
67 CENTERED,
68 )
69 .draw(&mut tft)
70 .unwrap();
71
72 // The simulated displays can now be added to common simulator window.
73
74 let window_size = Size::new(1300, 500);
75 let mut window = MultiWindow::new("Multiple displays example", window_size);
76 window.clear(Rgb888::CSS_DIM_GRAY);
77
78 let oled_settings = OutputSettingsBuilder::new()
79 .theme(BinaryColorTheme::OledBlue)
80 .scale(2)
81 .build();
82 let oled_size = oled_displays[0].output_size(&oled_settings);
83
84 for (oled, anchor) in oled_displays.iter().zip(
85 [
86 AnchorPoint::TopLeft,
87 AnchorPoint::TopCenter,
88 AnchorPoint::TopRight,
89 ]
90 .into_iter(),
91 ) {
92 let offset = display_offset(window_size, oled_size, anchor);
93 window.add_display(&oled, offset, &oled_settings);
94 }
95
96 let tft_settings = OutputSettings::default();
97 let tft_size = tft.output_size(&tft_settings);
98 let tft_offset = display_offset(window_size, tft_size, AnchorPoint::BottomCenter);
99
100 window.add_display(&tft, tft_offset, &tft_settings);
101
102 let border_style = PrimitiveStyleBuilder::new()
103 .stroke_width(5)
104 .stroke_alignment(StrokeAlignment::Inside)
105 .build();
106
107 let mut mouse_down = false;
108
109 'running: loop {
110 // Call `update_display` for all display. Note that the window won't be
111 // updated until `window.flush` is called.
112 for oled in &oled_displays {
113 window.update_display(oled);
114 }
115 window.update_display(&tft);
116 window.flush();
117
118 for event in window.events() {
119 match event {
120 SimulatorEvent::MouseMove { point } => {
121 // Mouse events use the window coordinate system.
122 // `translate_mouse_position` can be used to translate the
123 // mouse position into the display coordinate system.
124
125 for oled in &mut oled_displays {
126 let is_inside = window.translate_mouse_position(oled, point).is_some();
127
128 let style = PrimitiveStyleBuilder::from(&border_style)
129 .stroke_color(BinaryColor::from(is_inside))
130 .build();
131
132 oled.bounding_box().into_styled(style).draw(oled).unwrap();
133 }
134
135 if mouse_down {
136 if let Some(point) = window.translate_mouse_position(&tft, point) {
137 Circle::with_center(point, 10)
138 .into_styled(PrimitiveStyle::with_fill(Rgb565::CSS_DODGER_BLUE))
139 .draw(&mut tft)
140 .unwrap();
141 }
142 }
143 }
144 SimulatorEvent::MouseButtonDown {
145 mouse_btn: MouseButton::Left,
146 ..
147 } => {
148 mouse_down = true;
149 }
150 SimulatorEvent::MouseButtonUp {
151 mouse_btn: MouseButton::Left,
152 ..
153 } => {
154 mouse_down = false;
155 }
156 SimulatorEvent::Quit => break 'running,
157 _ => {}
158 }
159 }
160 }
161
162 Ok(())
163}