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:
Fl1tzi 2023-12-08 17:58:18 +01:00
parent 1cdea8eff4
commit ff8599ec6b
No known key found for this signature in database
GPG key ID: 06B333727810C686
7 changed files with 26 additions and 21 deletions

View file

@ -180,7 +180,7 @@ impl Button {
#[tracing::instrument]
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) => {
debug!("Using env variable: {:?}", path);
PathBuf::from(path)

View file

@ -254,7 +254,7 @@ impl Device {
}
// switch space if needed
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(),
None => return,
};

View file

@ -41,9 +41,9 @@ pub type ModuleInitFunction = fn(Arc<Button>, ModuleCache) -> ModuleFuture;
pub fn retrieve_module_from_name(name: &str) -> Option<ModuleInitFunction> {
match name {
"space" => Some(Space::init as ModuleInitFunction),
"counter" => Some(Counter::init as ModuleInitFunction),
"image" => Some(Image::init as ModuleInitFunction),
"space" => Some(Space::new as ModuleInitFunction),
"counter" => Some(Counter::new as ModuleInitFunction),
"image" => Some(Image::new as ModuleInitFunction),
_ => None,
}
}
@ -193,17 +193,19 @@ pub type ReturnError = Box<dyn Error + Send + Sync>;
pub type ChannelReceiver = mpsc::Receiver<HostEvent>;
#[async_trait]
/// An object safe module trait.
///
/// - init() -> function for checking config and creating module
/// - run() -> function that happens when the device actually runs
/// An object safe module Trait for representing a single Module.
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>,
mut cache: ModuleCache,
) -> Result<ModuleObject, ButtonConfigError>
where
Self: Sized;
/// Function for actually running the module and interacting with the device. Errors that
/// happen here should be mostly prevented.
async fn run(
&mut self,
device: DeviceAccess,

View file

@ -13,7 +13,7 @@ pub struct Blank;
#[async_trait]
impl Module for Blank {
async fn init(
async fn new(
_config: Arc<Button>,
_cache: ModuleCache,
) -> Result<ModuleObject, ButtonConfigError> {

View file

@ -19,22 +19,25 @@ pub struct Counter {
title: String,
title_size: f32,
number_size: f32,
increment: u32
}
#[async_trait]
impl Module for Counter {
async fn init(
async fn new(
config: Arc<Button>,
_cache: ModuleCache,
) -> Result<ModuleObject, ButtonConfigError> {
let title = config.parse_module("TITLE", " ".to_string()).res()?;
let title_size = config.parse_module("TITLE_SIZE", 15.0).res()?;
let number_size = config.parse_module("NUMBER_SIZE", 25.0).res()?;
let title = config.parse_module("title", " ".to_string()).res()?;
let title_size = config.parse_module("title_size", 15.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 {
title,
title_size,
number_size,
increment
}))
}
@ -64,7 +67,7 @@ impl Module for Counter {
match event {
HostEvent::ButtonPressed => {
// 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(
&streamdeck,
&self.title,

View file

@ -18,12 +18,12 @@ pub struct Image {
#[async_trait]
impl Module for Image {
async fn init(
async fn new(
config: Arc<Button>,
mut cache: ModuleCache,
) -> Result<ModuleObject, ButtonConfigError> {
let path = config.parse_module("PATH", String::new()).required()?;
let scale = config.parse_module("SCALE", 100.0).res()?;
let path = config.parse_module("path", String::new()).required()?;
let scale = config.parse_module("scale", 100.0).res()?;
let image = cache
.load_image(path, 1)

View file

@ -17,11 +17,11 @@ pub struct Space {
#[async_trait]
impl Module for Space {
async fn init(
async fn new(
config: Arc<Button>,
_cache: ModuleCache,
) -> 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 }))
}