2022-07-15 23:55:31 +02:00
|
|
|
// This file is part of the "lamp" program.
|
|
|
|
// Copyright (C) 2022 crapStone
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-07-22 18:47:26 +02:00
|
|
|
mod cli;
|
|
|
|
mod controllers;
|
|
|
|
|
|
|
|
use std::process::exit;
|
|
|
|
|
|
|
|
use controllers::{Controller, LinController, LogController, RawController};
|
|
|
|
|
2021-10-23 22:57:56 +02:00
|
|
|
use crate::cli::build_cli;
|
|
|
|
|
2020-07-22 18:47:26 +02:00
|
|
|
fn main() {
|
2021-10-23 22:57:56 +02:00
|
|
|
let app = build_cli();
|
|
|
|
let matches = app.get_matches();
|
2020-07-22 18:47:26 +02:00
|
|
|
|
|
|
|
let (default_ctrl, ctrls) = controllers::get_controllers();
|
|
|
|
|
|
|
|
let p = ctrls.get(&default_ctrl).unwrap().to_owned();
|
|
|
|
let controller: Box<dyn Controller> = match matches.value_of("ctrl_type") {
|
|
|
|
Some("raw") => Box::new(RawController::new(p)),
|
|
|
|
Some("lin") => Box::new(LinController::new(p)),
|
|
|
|
Some("log") => Box::new(LogController::new(p)),
|
2022-07-16 01:01:48 +02:00
|
|
|
Some(_) | None => panic!("{ERROR_MSG}"),
|
2020-07-22 18:47:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if matches.is_present("list") {
|
|
|
|
for ctrl in ctrls.keys() {
|
2022-07-16 01:01:48 +02:00
|
|
|
println!("{ctrl}");
|
2020-07-22 18:47:26 +02:00
|
|
|
}
|
|
|
|
exit(exitcode::OK);
|
|
|
|
} else if let Some(value) = matches.value_of("set") {
|
2022-07-16 01:01:48 +02:00
|
|
|
let new_value = value.parse::<u32>().unwrap();
|
2020-07-22 18:47:26 +02:00
|
|
|
controller.set_brightness(new_value);
|
|
|
|
} else if let Some(value) = matches.value_of("inc") {
|
2022-07-16 01:01:48 +02:00
|
|
|
let new_value = controller.get_brightness() + value.parse::<u32>().unwrap();
|
2020-07-22 18:47:26 +02:00
|
|
|
controller.set_brightness(new_value.min(controller.get_max_brightness()));
|
|
|
|
} else if let Some(value) = matches.value_of("dec") {
|
2022-07-16 01:01:48 +02:00
|
|
|
let new_value = controller.get_brightness() - value.parse::<u32>().unwrap();
|
2020-07-22 18:47:26 +02:00
|
|
|
controller.set_brightness(new_value.max(0));
|
|
|
|
} else if matches.is_present("get") {
|
|
|
|
println!("{}", controller.get_brightness());
|
|
|
|
} else if matches.is_present("zer") {
|
|
|
|
controller.set_brightness(0);
|
|
|
|
} else if matches.is_present("ful") {
|
|
|
|
controller.set_brightness(controller.get_max_brightness());
|
|
|
|
} else {
|
2021-10-23 22:57:56 +02:00
|
|
|
build_cli().print_long_help().unwrap();
|
2020-07-22 18:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(exitcode::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://xkcd.com/2200/
|
|
|
|
const ERROR_MSG: &str = r#"
|
|
|
|
ERROR!
|
|
|
|
|
|
|
|
If you're seeing this, the code is in what I thought was an unreachable state.
|
|
|
|
|
|
|
|
I could give you advice for what to do. but honestly, why should you trust me?
|
|
|
|
I clearly screwed this up. I'm writing a message that should never appear,
|
|
|
|
yet I know it will probably appear someday.
|
|
|
|
|
|
|
|
On a deep level, I know I'm not up to this task. I'm so sorry.
|
|
|
|
"#;
|