Skip to content

Commit

Permalink
gracefully handle different colorspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
nia-e committed May 1, 2023
1 parent 363d413 commit d024323
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
1 change: 0 additions & 1 deletion lvgl-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ lazy_static = "1.4.0"
proc-macro2 = "1.0.56"
Inflector = "0.11.4"
syn = { version = "2.0.13", features = ["full"]}

26 changes: 26 additions & 0 deletions lvgl-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use quote::{format_ident, ToTokens};
use regex::Regex;
use std::collections::HashMap;
use std::error::Error;
use std::str::FromStr;
use syn::{FnArg, ForeignItem, ForeignItemFn, Item, ReturnType};

type CGResult<T> = Result<T, Box<dyn Error>>;
Expand Down Expand Up @@ -376,6 +377,21 @@ impl From<Box<syn::Type>> for LvType {
}
}

#[repr(u8)]
#[derive(Clone, Copy)]
pub enum ColorTypes {
C1 = 1,
C8 = 8,
C16 = 16,
C32 = 32,
}

impl ColorTypes {
pub fn as_code(&self) -> TokenStream {
TokenStream::from_str(&format!("pub(crate) type LvInternalColor = lvgl_sys::lv_color{}_t;", *self as u8)).unwrap()
}
}

pub struct CodeGen {
functions: Vec<LvFunc>,
widgets: Vec<LvWidget>,
Expand Down Expand Up @@ -466,6 +482,16 @@ impl CodeGen {
pub fn get_function_names(&self) -> CGResult<Vec<String>> {
Ok(self.functions.iter().map(|f| f.name.clone()).collect())
}

pub fn get_color(&self, depth: u32) -> Option<ColorTypes> {
match depth {
1 => Some(ColorTypes::C1),
8 => Some(ColorTypes::C8),
16 => Some(ColorTypes::C16),
32 => Some(ColorTypes::C32),
_ => None,
}
}
}

#[cfg(test)]
Expand Down
12 changes: 9 additions & 3 deletions lvgl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ fn main() {
.iter()
.flat_map(|w| w.code(&()))
.collect();
let color_impl: TokenStream = codegen.get_color(lvgl_sys::LV_COLOR_DEPTH).unwrap().as_code();

let code = quote! {
let widgets_code = quote! {
#(#widgets_impl)*
};

let color_code = quote! {
#color_impl
};

let mut file = File::create(rs).unwrap();
writeln!(
file,
"/* automatically generated by lvgl-codegen */\n{}",
code
"/* automatically generated by lvgl-codegen */\n{}\n{}",
widgets_code,
color_code
)
.unwrap();
}
7 changes: 3 additions & 4 deletions lvgl/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::functions::CoreError;
use crate::{disp_drv_register, disp_get_default, get_str_act, RunOnce, NativeObject, LvResult};
use crate::Screen;
use crate::{disp_drv_register, disp_get_default, get_str_act, RunOnce, Screen, NativeObject, LvResult, LvInternalColor};
use crate::{Box, Color};
use core::cell::RefCell;
use core::convert::TryInto;
Expand Down Expand Up @@ -79,7 +78,7 @@ impl<'a> Display {
unsafe extern "C" fn(
*mut lvgl_sys::lv_disp_drv_t,
*const lvgl_sys::lv_area_t,
*mut lvgl_sys::lv_color16_t,
*mut LvInternalColor,
),
>,
rounder_cb: Option<
Expand Down Expand Up @@ -232,7 +231,7 @@ impl<'a, const N: usize> DisplayDriver<N> {
unsafe extern "C" fn(
*mut lvgl_sys::_lv_disp_drv_t,
*const lvgl_sys::lv_area_t,
*mut lvgl_sys::lv_color16_t,
*mut LvInternalColor,
),
>,
rounder_cb: Option<
Expand Down
2 changes: 2 additions & 0 deletions lvgl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub use functions::*;
pub use lv_core::*;
pub use support::*;

pub(crate) use widgets::LvInternalColor;

mod display;
mod functions;
mod support;
Expand Down

0 comments on commit d024323

Please sign in to comment.