energy.rs (1389B)
1 use std::{fmt::Display, rc::Rc}; 2 3 use xplm::data::{borrowed::DataRef, ArrayRead, DataRead, ReadWrite}; 4 5 use crate::error::Error; 6 7 pub(crate) struct EnergyRefs { 8 pub(crate) gpu_on: DataRef<bool, ReadWrite>, 9 pub(crate) battery: DataRef<[i32], ReadWrite>, 10 pub(crate) apu_on: DataRef<bool, ReadWrite>, 11 } 12 13 #[derive(Clone)] 14 pub(crate) struct Energy { 15 inner: Rc<EnergyRefs>, 16 } 17 18 impl Energy { 19 pub(crate) fn new() -> Result<Self, Error> { 20 Ok(Self { 21 inner: Rc::new(EnergyRefs { 22 gpu_on: DataRef::find("sim/cockpit/electrical/gpu_on")?.writeable()?, 23 battery: DataRef::find("sim/cockpit2/electrical/battery_on")?.writeable()?, 24 apu_on: DataRef::find("sim/cockpit2/electrical/APU_generator_on")?.writeable()?, 25 }), 26 }) 27 } 28 29 pub(crate) fn gpu_on(&self) -> bool { 30 self.inner.gpu_on.get() 31 } 32 33 pub(crate) fn battery_on(&self) -> bool { 34 self.inner.battery.as_vec().contains(&1) 35 } 36 37 pub(crate) fn apu_on(&self) -> bool { 38 self.inner.apu_on.get() 39 } 40 } 41 42 impl Display for Energy { 43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 44 write!( 45 f, 46 "GPU enabled: {}, Battery enabled: {}, APU Gen enabled: {}", 47 self.gpu_on(), 48 self.battery_on(), 49 self.apu_on(), 50 ) 51 } 52 }