cadmus_core/frontlight/
premixed.rs

1use super::{Frontlight, LightLevels};
2use crate::device::CURRENT_DEVICE;
3use anyhow::Error;
4use std::fs::File;
5use std::fs::OpenOptions;
6use std::io::Write;
7use std::path::Path;
8
9const FRONTLIGHT_WHITE: &str = "/sys/class/backlight/mxc_msp430.0/brightness";
10
11// Forma
12const FRONTLIGHT_ORANGE_A: &str = "/sys/class/backlight/tlc5947_bl/color";
13// Libra H₂O, Clara HD, Libra 2, Clara BW, Libra Colour, Clara Colour
14const FRONTLIGHT_ORANGE_B: &str = "/sys/class/backlight/lm3630a_led/color";
15// Sage, Libra 2, Clara 2E, Elipsa 2E
16const FRONTLIGHT_ORANGE_C: &str = "/sys/class/leds/aw99703-bl_FL1/color";
17
18pub struct PremixedFrontlight {
19    intensity: f32,
20    warmth: f32,
21    white: File,
22    orange: File,
23}
24
25impl PremixedFrontlight {
26    pub fn new(intensity: f32, warmth: f32) -> Result<PremixedFrontlight, Error> {
27        let white = OpenOptions::new().write(true).open(FRONTLIGHT_WHITE)?;
28        let orange_path = if Path::new(FRONTLIGHT_ORANGE_C).exists() {
29            FRONTLIGHT_ORANGE_C
30        } else if Path::new(FRONTLIGHT_ORANGE_B).exists() {
31            FRONTLIGHT_ORANGE_B
32        } else {
33            FRONTLIGHT_ORANGE_A
34        };
35        let orange = OpenOptions::new().write(true).open(orange_path)?;
36        Ok(PremixedFrontlight {
37            intensity,
38            warmth,
39            white,
40            orange,
41        })
42    }
43}
44
45impl Frontlight for PremixedFrontlight {
46    fn set_intensity(&mut self, intensity: f32) {
47        let white = intensity.round() as i16;
48        write!(self.white, "{}", white).unwrap();
49        self.intensity = intensity;
50    }
51
52    fn set_warmth(&mut self, warmth: f32) {
53        let mut orange = (warmth / 10.0).round() as i16;
54        if CURRENT_DEVICE.mark() != 8 {
55            orange = 10 - orange;
56        }
57        write!(self.orange, "{}", orange).unwrap();
58        self.warmth = warmth;
59    }
60
61    fn levels(&self) -> LightLevels {
62        LightLevels {
63            intensity: self.intensity,
64            warmth: self.warmth,
65        }
66    }
67}