Skip to content

Commit

Permalink
added max brightness config and runtime setup (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
night-crawler committed Sep 10, 2024
1 parent 0ba441c commit 6d760ae
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package() {

install -Dm644 "$srcdir/$pkgname-$pkgver/led_matrix_monitoring.service" "$pkgdir/usr/lib/systemd/system/$pkgname.service"
install -Dm644 "$srcdir/$pkgname-$pkgver/example_config.toml" "$pkgdir/etc/led_matrix/monitoring.toml"
install -Dm644 "$srcdir/$pkgname-$pkgver/max_brightness_value" "$pkgdir/etc/led_matrix/max_brightness_value"

install -Dm644 "README.md" "$pkgdir/usr/share/doc/${pkgname}/README.md"
install -Dm644 "LICENSE" "$pkgdir/usr/share/licenses/${pkgname}/LICENSE"
Expand Down
3 changes: 3 additions & 0 deletions example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ temperatures = [
]

[render]
max_brightness = 255
max_brightness_file = "/etc/led_matrix/max_brightness_value"

left = [
{ Cpu = { mid_point = 10, max_height = 10, k = 1.0 } },
{ AverageCpu = { start_x = 7, start_y = 20, end_y = 9, k = 1.0 } },
Expand Down
1 change: 1 addition & 0 deletions max_brightness_value
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
255
2 changes: 1 addition & 1 deletion src/api/uds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mod tests {

#[test]
fn test_make_unix_socket_request() {
let mut renderer = Renderer::new();
let mut renderer = Renderer::new(255);
renderer
.render_cpu(10, 10, &[100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], 4.0)
.unwrap();
Expand Down
5 changes: 5 additions & 0 deletions src/config/collector_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ pub struct CollectorConfig {

#[derive(Debug, Serialize, Deserialize)]
pub struct RenderConfig {
pub max_brightness: Option<u8>,
pub max_brightness_file: Option<String>,

#[serde(default)]
pub left: Vec<RenderType>,

Expand Down Expand Up @@ -147,6 +150,8 @@ mod tests {
};

let render_config = RenderConfig {
max_brightness: Some(255),
max_brightness_file: None,
left: vec![
RenderType::Cpu {
mid_point: 10,
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ fn main() -> anyhow::Result<()> {

let uds = api::uds::UdsClient::new(&config.socket)?;
let mut collector = Collector::new(config.collector)?;

let mut max_brightness = config.render.max_brightness.unwrap_or(255);
loop {
if let Some(file) = config.render.max_brightness_file.as_ref() {
max_brightness = std::fs::read_to_string(file)?.trim().parse()?;
}

collector.update();
let mut left_renderer = Renderer::new();
let mut left_renderer = Renderer::new(max_brightness);
for render_type in config.render.left.iter() {
left_renderer.render(render_type, collector.get_state())?;
}

let mut right_renderer = Renderer::new();
let mut right_renderer = Renderer::new(max_brightness);
for render_type in config.render.right.iter() {
right_renderer.render(render_type, collector.get_state())?;
}
Expand Down
14 changes: 7 additions & 7 deletions src/render/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub struct Renderer {
}

impl Renderer {
pub fn new() -> Self {
pub fn new(max_brightness: u8) -> Self {
let buf = ImageBuffer::new(WIDTH, HEIGHT);
Renderer {
buf,
max_brightness: 255,
max_brightness,
}
}

Expand Down Expand Up @@ -378,7 +378,7 @@ mod tests {

#[test]
fn test_render_cpu() {
let mut renderer = Renderer::new();
let mut renderer = Renderer::new(255);
assert!(renderer.render_cpu(10, 10, &LOAD, 6.0).is_ok());
renderer.save_to_file("./target/cpu.png").unwrap();

Expand All @@ -402,7 +402,7 @@ mod tests {
(900, 900),
];

let mut renderer = Renderer::new();
let mut renderer = Renderer::new(255);
assert!(renderer
.plot_io(27, 7, data_points.iter().cloned(), 7.0)
.is_ok());
Expand All @@ -411,7 +411,7 @@ mod tests {

#[test]
fn test_render_horizontal_bar() {
let mut renderer = Renderer::new();
let mut renderer = Renderer::new(255);
assert!(renderer
.render_horizontal_bar(100, 100, 33, 0, 9, 6.0)
.is_ok());
Expand All @@ -423,7 +423,7 @@ mod tests {

#[test]
fn test_render_vertical_bar() {
let mut renderer = Renderer::new();
let mut renderer = Renderer::new(255);
assert!(renderer
.render_vertical_bar(100, 100, 0, 0, 10, 6.0)
.is_ok());
Expand All @@ -435,7 +435,7 @@ mod tests {

#[test]
fn test_render_battery() {
let mut renderer = Renderer::new();
let mut renderer = Renderer::new(255);
assert!(renderer.render_battery(0, 10, 20).is_ok());
renderer.save_to_file("./target/battery.png").unwrap();
}
Expand Down

0 comments on commit 6d760ae

Please sign in to comment.