mirror of
https://codeberg.org/Fl1tzi/microdeck.git
synced 2024-05-19 19:20:20 +00:00
move to lowercase module options, allow changing increment in counter, change name of module init -> new, change env variable for configuration
This commit is contained in:
parent
1cdea8eff4
commit
ff8599ec6b
|
@ -180,7 +180,7 @@ impl Button {
|
||||||
|
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
pub fn load_config() -> Result<Config, ConfigError> {
|
pub fn load_config() -> Result<Config, ConfigError> {
|
||||||
let config_file: PathBuf = match env::var_os("DACH_DECKER_CONFIG") {
|
let config_file: PathBuf = match env::var_os("MICRODECK_CONFIG") {
|
||||||
Some(path) => {
|
Some(path) => {
|
||||||
debug!("Using env variable: {:?}", path);
|
debug!("Using env variable: {:?}", path);
|
||||||
PathBuf::from(path)
|
PathBuf::from(path)
|
||||||
|
|
|
@ -254,7 +254,7 @@ impl Device {
|
||||||
}
|
}
|
||||||
// switch space if needed
|
// switch space if needed
|
||||||
if options.0.module == "space" {
|
if options.0.module == "space" {
|
||||||
let name = match options.0.options.get("NAME") {
|
let name = match options.0.options.get("name") {
|
||||||
Some(n) => n.clone(),
|
Some(n) => n.clone(),
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
|
@ -41,9 +41,9 @@ pub type ModuleInitFunction = fn(Arc<Button>, ModuleCache) -> ModuleFuture;
|
||||||
|
|
||||||
pub fn retrieve_module_from_name(name: &str) -> Option<ModuleInitFunction> {
|
pub fn retrieve_module_from_name(name: &str) -> Option<ModuleInitFunction> {
|
||||||
match name {
|
match name {
|
||||||
"space" => Some(Space::init as ModuleInitFunction),
|
"space" => Some(Space::new as ModuleInitFunction),
|
||||||
"counter" => Some(Counter::init as ModuleInitFunction),
|
"counter" => Some(Counter::new as ModuleInitFunction),
|
||||||
"image" => Some(Image::init as ModuleInitFunction),
|
"image" => Some(Image::new as ModuleInitFunction),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -193,17 +193,19 @@ pub type ReturnError = Box<dyn Error + Send + Sync>;
|
||||||
pub type ChannelReceiver = mpsc::Receiver<HostEvent>;
|
pub type ChannelReceiver = mpsc::Receiver<HostEvent>;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
/// An object safe module trait.
|
/// An object safe module Trait for representing a single Module.
|
||||||
///
|
|
||||||
/// - init() -> function for checking config and creating module
|
|
||||||
/// - run() -> function that happens when the device actually runs
|
|
||||||
pub trait Module: Sync + Send {
|
pub trait Module: Sync + Send {
|
||||||
async fn init(
|
/// Function for validating configuration and creating module instance. Every time the config
|
||||||
|
/// is checked this function gets called. It therefore should validate the most efficient
|
||||||
|
/// things first.
|
||||||
|
async fn new(
|
||||||
config: Arc<Button>,
|
config: Arc<Button>,
|
||||||
mut cache: ModuleCache,
|
mut cache: ModuleCache,
|
||||||
) -> Result<ModuleObject, ButtonConfigError>
|
) -> Result<ModuleObject, ButtonConfigError>
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
/// Function for actually running the module and interacting with the device. Errors that
|
||||||
|
/// happen here should be mostly prevented.
|
||||||
async fn run(
|
async fn run(
|
||||||
&mut self,
|
&mut self,
|
||||||
device: DeviceAccess,
|
device: DeviceAccess,
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub struct Blank;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Module for Blank {
|
impl Module for Blank {
|
||||||
async fn init(
|
async fn new(
|
||||||
_config: Arc<Button>,
|
_config: Arc<Button>,
|
||||||
_cache: ModuleCache,
|
_cache: ModuleCache,
|
||||||
) -> Result<ModuleObject, ButtonConfigError> {
|
) -> Result<ModuleObject, ButtonConfigError> {
|
||||||
|
|
|
@ -19,22 +19,25 @@ pub struct Counter {
|
||||||
title: String,
|
title: String,
|
||||||
title_size: f32,
|
title_size: f32,
|
||||||
number_size: f32,
|
number_size: f32,
|
||||||
|
increment: u32
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Module for Counter {
|
impl Module for Counter {
|
||||||
async fn init(
|
async fn new(
|
||||||
config: Arc<Button>,
|
config: Arc<Button>,
|
||||||
_cache: ModuleCache,
|
_cache: ModuleCache,
|
||||||
) -> Result<ModuleObject, ButtonConfigError> {
|
) -> Result<ModuleObject, ButtonConfigError> {
|
||||||
let title = config.parse_module("TITLE", " ".to_string()).res()?;
|
let title = config.parse_module("title", " ".to_string()).res()?;
|
||||||
let title_size = config.parse_module("TITLE_SIZE", 15.0).res()?;
|
let title_size = config.parse_module("title_size", 15.0).res()?;
|
||||||
let number_size = config.parse_module("NUMBER_SIZE", 25.0).res()?;
|
let number_size = config.parse_module("number_size", 25.0).res()?;
|
||||||
|
let increment = config.parse_module("increment", 1).res()?;
|
||||||
|
|
||||||
Ok(Box::new(Counter {
|
Ok(Box::new(Counter {
|
||||||
title,
|
title,
|
||||||
title_size,
|
title_size,
|
||||||
number_size,
|
number_size,
|
||||||
|
increment
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +67,7 @@ impl Module for Counter {
|
||||||
match event {
|
match event {
|
||||||
HostEvent::ButtonPressed => {
|
HostEvent::ButtonPressed => {
|
||||||
// just return to zero if u32 MAX is reached
|
// just return to zero if u32 MAX is reached
|
||||||
counter = counter.checked_add(1).unwrap_or(0);
|
counter = counter.checked_add(self.increment).unwrap_or(0);
|
||||||
let image = render_text(
|
let image = render_text(
|
||||||
&streamdeck,
|
&streamdeck,
|
||||||
&self.title,
|
&self.title,
|
||||||
|
|
|
@ -18,12 +18,12 @@ pub struct Image {
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Module for Image {
|
impl Module for Image {
|
||||||
async fn init(
|
async fn new(
|
||||||
config: Arc<Button>,
|
config: Arc<Button>,
|
||||||
mut cache: ModuleCache,
|
mut cache: ModuleCache,
|
||||||
) -> Result<ModuleObject, ButtonConfigError> {
|
) -> Result<ModuleObject, ButtonConfigError> {
|
||||||
let path = config.parse_module("PATH", String::new()).required()?;
|
let path = config.parse_module("path", String::new()).required()?;
|
||||||
let scale = config.parse_module("SCALE", 100.0).res()?;
|
let scale = config.parse_module("scale", 100.0).res()?;
|
||||||
|
|
||||||
let image = cache
|
let image = cache
|
||||||
.load_image(path, 1)
|
.load_image(path, 1)
|
||||||
|
|
|
@ -17,11 +17,11 @@ pub struct Space {
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Module for Space {
|
impl Module for Space {
|
||||||
async fn init(
|
async fn new(
|
||||||
config: Arc<Button>,
|
config: Arc<Button>,
|
||||||
_cache: ModuleCache,
|
_cache: ModuleCache,
|
||||||
) -> Result<ModuleObject, ButtonConfigError> {
|
) -> Result<ModuleObject, ButtonConfigError> {
|
||||||
let name = config.parse_module("NAME", "Unknown".to_string()).res()?;
|
let name = config.parse_module("name", "Unknown".to_string()).res()?;
|
||||||
Ok(Box::new(Space { name }))
|
Ok(Box::new(Space { name }))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue