lvgl-rs/examples/button_click.rs

103 lines
3.3 KiB
Rust
Raw Normal View History

2020-06-16 21:19:26 +00:00
use cstr_core::CString;
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
2020-06-20 16:46:36 +00:00
2020-06-20 10:15:10 +00:00
use lvgl::input_device::{BufferStatus, InputData, Pointer};
2020-06-04 18:14:43 +00:00
use lvgl::style::Style;
2020-06-12 16:54:28 +00:00
use lvgl::widgets::{Btn, Label};
2020-06-19 21:56:37 +00:00
use lvgl::{self, Align, Color, LvError, Part, State, Widget, UI};
2020-06-20 16:46:36 +00:00
2020-06-20 10:15:10 +00:00
use std::thread::sleep;
use std::time::{Duration, Instant};
2020-06-07 18:47:49 +00:00
fn main() -> Result<(), LvError> {
2020-06-19 17:43:14 +00:00
let display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(lvgl::HOR_RES_MAX, lvgl::VER_RES_MAX));
let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("Bar Example", &output_settings);
2020-06-07 18:47:49 +00:00
let mut ui = UI::init()?;
2020-06-20 10:31:37 +00:00
// Register your display:
2020-06-16 21:19:26 +00:00
ui.disp_drv_register(display)?;
2020-06-20 10:31:37 +00:00
// Define the initial state of your input
2020-06-20 16:46:36 +00:00
let mut latest_touch_status = InputData::Touch(Point::new(0, 0)).released().once();
2020-06-20 10:15:10 +00:00
2020-06-20 10:31:37 +00:00
// Register a new input device that's capable of reading the current state of the input
2020-06-20 16:46:36 +00:00
let mut touch_screen = Pointer::new(|| latest_touch_status);
2020-06-19 21:56:37 +00:00
ui.indev_drv_register(&mut touch_screen)?;
// Create screen and widgets
2020-06-07 18:47:49 +00:00
let mut screen = ui.scr_act()?;
2020-06-02 17:59:41 +00:00
let mut screen_style = Style::default();
2020-06-04 18:14:43 +00:00
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
2020-06-07 18:47:49 +00:00
screen.add_style(Part::Main, screen_style)?;
// Create the button
2020-06-07 18:47:49 +00:00
let mut button = Btn::new(&mut screen)?;
button.set_align(&mut screen, Align::InLeftMid, 30, 0)?;
button.set_size(180, 80)?;
let mut btn_lbl = Label::new(&mut button)?;
2020-06-16 21:19:26 +00:00
btn_lbl.set_text(CString::new("Click me!").unwrap().as_c_str())?;
2020-06-12 16:54:28 +00:00
let mut btn_state = false;
button.on_event(|mut btn, event| {
2020-06-20 10:27:46 +00:00
println!("Button received event: {:?}", event);
if let lvgl::Event::Clicked = event {
2020-06-12 16:54:28 +00:00
if btn_state {
let nt = CString::new("Click me!").unwrap();
btn_lbl.set_text(nt.as_c_str()).unwrap();
2020-06-12 16:54:28 +00:00
} else {
let nt = CString::new("Clicked!").unwrap();
btn_lbl.set_text(nt.as_c_str()).unwrap();
2020-06-12 16:54:28 +00:00
}
btn_state = !btn_state;
btn.toggle().unwrap();
}
2020-06-07 18:47:49 +00:00
})?;
2020-06-19 12:14:46 +00:00
let mut loop_started = Instant::now();
2020-06-20 10:15:10 +00:00
let mut latest_touch_point = Point::new(0, 0);
'running: loop {
2020-06-19 13:45:26 +00:00
ui.task_handler();
window.update(ui.get_display_ref().unwrap());
2020-06-20 10:15:10 +00:00
let mut events = window.events().peekable();
if events.peek().is_none() {
2020-06-20 16:46:36 +00:00
latest_touch_status = InputData::Touch(latest_touch_point.clone())
.released()
.once();
2020-06-20 10:15:10 +00:00
}
for event in events {
match event {
SimulatorEvent::MouseButtonUp {
mouse_btn: _,
point,
} => {
println!("Clicked on: {:?}", point);
// Send a event to the button directly
2020-06-20 10:15:10 +00:00
latest_touch_point = point.clone();
2020-06-20 16:46:36 +00:00
latest_touch_status = InputData::Touch(point).pressed().once();
}
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
2020-06-20 10:19:13 +00:00
sleep(Duration::from_millis(15));
2020-06-20 10:15:10 +00:00
2020-06-19 13:45:26 +00:00
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}
Ok(())
}