lvgl-rs/examples/demo.rs

114 lines
3.6 KiB
Rust
Raw Normal View History

use cstr_core::{CStr, CString};
use embedded_graphics::pixelcolor::Rgb565;
2020-04-21 08:52:21 +00:00
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
2020-05-27 20:37:22 +00:00
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
2020-04-21 08:52:21 +00:00
};
2020-04-12 10:19:28 +00:00
use lvgl;
2020-06-04 18:14:43 +00:00
use lvgl::style::Style;
2020-05-31 10:22:18 +00:00
use lvgl::widgets::{Label, LabelAlign};
2020-06-07 18:47:49 +00:00
use lvgl::{Align, Color, LvError, Part, State, Widget, UI};
2020-04-18 17:20:38 +00:00
use lvgl_sys;
use std::thread::sleep;
2020-06-19 12:14:46 +00:00
use std::time::{Duration, Instant};
2020-04-11 11:06:16 +00:00
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));
2020-05-30 07:32:27 +00:00
let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("PineTime", &output_settings);
2020-04-11 11:06:16 +00:00
2020-06-07 18:47:49 +00:00
let mut ui = UI::init()?;
2020-04-11 11:06:16 +00:00
// Implement and register your display:
2020-06-16 21:19:26 +00:00
ui.disp_drv_register(display).unwrap();
2020-04-11 11:06:16 +00:00
// Create screen and widgets
2020-06-07 18:47:49 +00:00
let mut screen = ui.scr_act()?;
2020-04-12 10:19:28 +00:00
2020-06-04 18:14:43 +00:00
let mut screen_style = Style::default();
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
screen_style.set_radius(State::DEFAULT, 0);
2020-06-07 18:47:49 +00:00
screen.add_style(Part::Main, screen_style)?;
2020-04-18 17:20:38 +00:00
2020-06-07 18:47:49 +00:00
let mut time = Label::new(&mut screen)?;
2020-06-04 18:14:43 +00:00
let mut style_time = Style::default();
//style_time.set_text_font(font_noto_sans_numeric_28);
style_time.set_text_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
2020-06-07 18:47:49 +00:00
time.add_style(Part::Main, style_time)?;
time.set_align(&mut screen, Align::Center, 0, 0)?;
time.set_text(CString::new("20:46").unwrap().as_c_str())?;
2020-06-07 18:47:49 +00:00
time.set_width(240)?;
time.set_height(240)?;
let mut bt = Label::new(&mut screen)?;
bt.set_width(50)?;
bt.set_height(80)?;
bt.set_recolor(true)?;
bt.set_text(CString::new("#5794f2 \u{F293}#").unwrap().as_c_str())?;
2020-06-07 18:47:49 +00:00
bt.set_label_align(LabelAlign::Left)?;
bt.set_align(&mut screen, Align::InTopLeft, 0, 0)?;
fn set_text<S>(text: S) -> Result<(), ()>
where
S: AsRef<cstr_core::CStr>,
{
let _v: *const i8 = text.as_ref().as_ptr();
Ok(())
}
let mut t: heapless::String<heapless::consts::U8> = heapless::String::from("test");
t.push('\0').unwrap();
set_text(CStr::from_bytes_with_nul(t.as_bytes()).unwrap()).unwrap();
2020-06-16 23:06:02 +00:00
set_text(cstr_core::CStr::from_bytes_with_nul("test\0".as_bytes()).unwrap()).unwrap();
set_text(cstr_core::CString::new("test").unwrap().as_c_str()).unwrap();
2020-06-07 18:47:49 +00:00
let mut power = Label::new(&mut screen)?;
power.set_recolor(true)?;
power.set_width(80)?;
power.set_height(20)?;
power.set_text(CString::new("#fade2a 20%#").unwrap().as_c_str())?;
2020-06-07 18:47:49 +00:00
power.set_label_align(LabelAlign::Right)?;
power.set_align(&mut screen, Align::InTopRight, 0, 0)?;
2020-04-11 11:06:16 +00:00
2020-04-18 21:11:52 +00:00
let mut i = 0;
2020-06-19 13:45:26 +00:00
let mut loop_started = Instant::now();
2020-04-11 11:06:16 +00:00
'running: loop {
2020-04-18 21:11:52 +00:00
if i > 59 {
i = 0;
}
let val = format!("21:{:02}", i);
time.set_text(CString::new(val.as_str()).unwrap().as_c_str())?;
2020-04-18 21:11:52 +00:00
i = 1 + i;
2020-06-19 13:45:26 +00:00
ui.task_handler();
window.update(ui.get_display_ref().unwrap());
for event in window.events() {
match event {
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
2020-06-19 13:45:26 +00:00
sleep(Duration::from_secs(1));
2020-04-11 11:06:16 +00:00
2020-06-19 13:45:26 +00:00
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}
2020-04-21 08:52:21 +00:00
2020-04-11 11:06:16 +00:00
Ok(())
}
2020-04-11 17:39:20 +00:00
2020-04-18 17:20:38 +00:00
// Reference to native font for LittlevGL, defined in the file: "fonts_noto_sans_numeric_80.c"
// TODO: Create a macro for defining a safe wrapper for fonts.
// Maybe sometihng like:
//
2020-05-30 05:40:13 +00:00
// font_declare! {
// NotoSansNumeric80 = noto_sans_numeric_80;
// };
//
2020-04-13 18:52:06 +00:00
extern "C" {
pub static mut noto_sans_numeric_80: lvgl_sys::lv_font_t;
}