Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from pros-rs/lcd-colors
Browse files Browse the repository at this point in the history
feat(lcd): allow custom color schemes
  • Loading branch information
Gavin-Niederman authored Nov 18, 2023
2 parents 77f6a2d + c3b18d9 commit 66031da
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 25 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@
"vexlink"
],
"rust-analyzer.check.allTargets": false,
"rust-analyzer.cargo.features": [
"xapi"
],
"rust-analyzer.check.targets": [
"${workspaceFolder}/armv7a-vexos-eabi.json",
"wasm32-unknown-unknown",
]
}
3 changes: 3 additions & 0 deletions pros-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ no-link = []

[build-dependencies]
cfg-if = "1.0"

[dependencies]
cfg-if = "1.0"
72 changes: 62 additions & 10 deletions pros-sys/src/llemu.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
#[cfg(feature = "xapi")]
compile_error!("LVGL bindings (xapi) are a todo for now");
// #[cfg(feature = "xapi")]
// compile_error!("LVGL bindings (xapi) are a todo for now");

use cfg_if::cfg_if;

pub const LCD_BTN_LEFT: core::ffi::c_int = 4;
pub const LCD_BTN_CENTER: core::ffi::c_int = 2;
pub const LCD_BTN_RIGHT: core::ffi::c_int = 1;

pub type lcd_button_cb_fn_t = Option<unsafe extern "C" fn()>;

#[cfg(feature = "xapi")]
#[repr(C)]
pub struct lcd_s_t {
//TODO
cfg_if! {
if #[cfg(feature = "xapi")] {
// #[repr(C)]
// pub struct lcd_s_t {
// //TODO
// }

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct lv_color_t {
pub blue: u8,
pub green: u8,
pub red: u8,
pub alpha: u8,
}

impl From<u32> for lv_color_t {
fn from(color: u32) -> Self {
Self {
blue: (color & 0xFF) as u8,
green: ((color >> 8) & 0xFF) as u8,
red: ((color >> 16) & 0xFF) as u8,
alpha: ((color >> 24) & 0xFF) as u8,
}
}
}

impl From<lv_color_t> for u32 {
fn from(color: lv_color_t) -> Self {
(color.blue as u32)
| ((color.green as u32) << 8)
| ((color.red as u32) << 16)
| ((color.alpha as u32) << 24)
}
}
}
}

extern "C" {
Expand Down Expand Up @@ -149,8 +183,26 @@ extern "C" {
\return The buttons pressed as a bit mask*/
pub fn lcd_read_buttons() -> u8;

#[cfg(feature = "xapi")]
pub fn lcd_set_background_color(); //TODO
#[cfg(feature = "xapi")]
pub fn lcd_set_text_color(); //TODO
cfg_if! {
if #[cfg(feature = "xapi")] {
/** Changes the color of the LCD background to a provided color expressed in
type lv_color_t.
\param color
A color of type lv_color_t
\return void
*/
pub fn lcd_set_background_color(color: lv_color_t);
/** Changes the text color of the LCD to a provided color expressed in
type lv_color_t.
\param color
A color of type lv_color_t
\return void
*/
pub fn lcd_set_text_color(color: lv_color_t);
}
}
}
5 changes: 1 addition & 4 deletions pros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ readme = "../README.md"
[dependencies]
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
spin = "0.9.8"
pros-sys = { version = "0.3.0", path = "../pros-sys" }
pros-sys = { version = "0.3.0", path = "../pros-sys", features = ["xapi"] }
snafu = { version = "0.7.5", default-features = false, features = [
"rust_1_61",
] }
no_std_io = { version = "0.6.0", features = ["alloc"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
dlmalloc = { version = "0.2.4", features = ["global"] }

[features]
lvgl = ["pros-sys/xapi"]
18 changes: 18 additions & 0 deletions pros/src/lcd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Print to and handle button presses on the V5 touchscreen.
use snafu::Snafu;

use crate::lvgl::colors::LcdColor;
use crate::sync::Mutex;

#[macro_use]
Expand All @@ -8,6 +10,22 @@ pub mod buttons;

pub(crate) mod writer;

/// Sets the background color of the LCD.
pub fn set_background_color(color: LcdColor) {
unsafe {
pros_sys::lcd_initialize();
pros_sys::lcd_set_background_color(*color);
}
}

/// Sets the text color of the LCD.
pub fn set_text_color(color: LcdColor) {
unsafe {
pros_sys::lcd_initialize();
pros_sys::lcd_set_background_color(*color);
}
}

lazy_static::lazy_static! {
pub(crate) static ref WRITER: Mutex<writer::ConsoleLcd> = {
Mutex::new(writer::ConsoleLcd::new())
Expand Down
10 changes: 1 addition & 9 deletions pros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,17 @@ pub mod position;
pub mod sensors;
pub mod sync;
pub mod task;

#[doc(hidden)]
pub use pros_sys as __pros_sys;

#[cfg(target_os = "vexos")]
mod vexos_env;
#[cfg(target_arch = "wasm32")]
mod wasm_env;

#[cfg(not(feature = "lvgl"))]
#[macro_use]
pub mod lcd;

#[cfg(feature = "lvgl")]
#[macro_use]
pub mod lvgl;

pub mod adi;
pub mod link;
pub mod lvgl;

pub type Result<T = ()> = core::result::Result<T, alloc::boxed::Box<dyn core::error::Error>>;

Expand Down
71 changes: 71 additions & 0 deletions pros/src/lvgl/colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! LVGL colors and presets.
use core::ops::{Deref, DerefMut};
use pros_sys::lv_color_t;

/// A color that can be used on the LCD.
/// The color space is dependent on the LCD driver.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LcdColor(pub lv_color_t);

impl LcdColor {
/// Create an RGB color without any transparency.
pub const fn new_rgb(red: u8, green: u8, blue: u8) -> Self {
Self(lv_color_t {
red,
green,
blue,
alpha: 0xFF,
})
}
/// Create an RGBA color with a certain opacity.
pub const fn new_rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
Self(lv_color_t {
red,
green,
blue,
alpha,
})
}
}

impl From<lv_color_t> for LcdColor {
fn from(other: lv_color_t) -> Self {
Self(other)
}
}

impl Deref for LcdColor {
type Target = lv_color_t;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for LcdColor {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl LcdColor {
pub const WHITE: Self = Self::new_rgb(0xFF, 0xFF, 0xFF);
pub const SILVER: Self = Self::new_rgb(0xC0, 0xC0, 0xC0);
pub const GRAY: Self = Self::new_rgb(0x80, 0x80, 0x80);
pub const BLACK: Self = Self::new_rgb(0x00, 0x00, 0x00);
pub const RED: Self = Self::new_rgb(0xFF, 0x00, 0x00);
pub const MAROON: Self = Self::new_rgb(0x80, 0x00, 0x00);
pub const YELLOW: Self = Self::new_rgb(0xFF, 0xFF, 0x00);
pub const OLIVE: Self = Self::new_rgb(0x80, 0x80, 0x00);
pub const LIME: Self = Self::new_rgb(0x00, 0xFF, 0x00);
pub const GREEN: Self = Self::new_rgb(0x00, 0x80, 0x00);
pub const CYAN: Self = Self::new_rgb(0x00, 0xFF, 0xFF);
pub const AQUA: Self = Self::CYAN;
pub const TEAL: Self = Self::new_rgb(0x00, 0x80, 0x80);
pub const BLUE: Self = Self::new_rgb(0x00, 0x00, 0xFF);
pub const NAVY: Self = Self::new_rgb(0x00, 0x00, 0x80);
pub const MAGENTA: Self = Self::new_rgb(0xFF, 0x00, 0xFF);
pub const PURPLE: Self = Self::new_rgb(0x80, 0x00, 0x80);
pub const ORANGE: Self = Self::new_rgb(0xFF, 0xA5, 0x00);
}
2 changes: 1 addition & 1 deletion pros/src/lvgl/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@

pub mod colors;
26 changes: 25 additions & 1 deletion pros/src/sensors/vision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use alloc::vec::Vec;
use pros_sys::{PROS_ERR, VISION_OBJECT_ERR_SIG};
use snafu::Snafu;

use crate::error::{bail_errno, bail_on, map_errno, PortError};
use crate::{
error::{bail_errno, bail_on, map_errno, PortError},
lvgl::colors::LcdColor,
};

/// Represents a vision sensor plugged into the vex.
pub struct VisionSensor {
Expand Down Expand Up @@ -168,6 +171,27 @@ impl From<u32> for Rgb {
}
}

impl From<Rgb> for LcdColor {
fn from(other: Rgb) -> Self {
Self(pros_sys::lv_color_t {
red: other.r,
green: other.g,
blue: other.b,
alpha: 0xFF,
})
}
}

impl From<LcdColor> for Rgb {
fn from(other: LcdColor) -> Self {
Self {
r: other.red,
g: other.green,
b: other.blue,
}
}
}

#[repr(u32)]
pub enum VisionZeroPoint {
TopLeft,
Expand Down

0 comments on commit 66031da

Please sign in to comment.