async_ltc681x/driver/registers/
cfgb.rs1use super::base::{BitPos, RegisterBlock};
2#[derive(Debug, Clone, Copy)]
6pub struct CfgB {
7 reg: RegisterBlock,
8}
9#[derive(Debug, Clone, Copy, PartialEq)]
11pub enum Gpio {
12 Gpio6,
13 Gpio7,
14 Gpio8,
15 Gpio9,
16}
17
18impl From<Gpio> for BitPos {
19 fn from(value: Gpio) -> Self {
20 match value {
21 Gpio::Gpio6 => BitPos::Pos0,
22 Gpio::Gpio7 => BitPos::Pos1,
23 Gpio::Gpio8 => BitPos::Pos2,
24 Gpio::Gpio9 => BitPos::Pos3,
25 }
26 }
27}
28
29#[derive(Debug, Copy, Clone, PartialEq)]
31pub enum BalancingSwitch {
32 SwitchCell13,
33 SwitchCell14,
34 SwitchCell15,
35 SwitchCell16,
36 SwitchCell17,
37 SwitchCell18,
38}
39impl From<BalancingSwitch> for BitPos {
40 fn from(value: BalancingSwitch) -> Self {
41 match value {
42 BalancingSwitch::SwitchCell13 => BitPos::Pos4,
43 BalancingSwitch::SwitchCell14 => BitPos::Pos5,
44 BalancingSwitch::SwitchCell15 => BitPos::Pos6,
45 BalancingSwitch::SwitchCell16 => BitPos::Pos7,
46 BalancingSwitch::SwitchCell17 => BitPos::Pos8,
47 BalancingSwitch::SwitchCell18 => BitPos::Pos9,
48 }
49 }
50}
51impl Default for CfgB {
55 fn default() -> Self {
56 let reg = Self {
57 reg: RegisterBlock::default(),
58 };
59 reg.release_gpio(Gpio::Gpio6)
60 .release_gpio(Gpio::Gpio7)
61 .release_gpio(Gpio::Gpio8)
62 .release_gpio(Gpio::Gpio9)
63 }
64}
65impl CfgB {
66 #[must_use]
68 pub fn pulldown_gpio(mut self, io: Gpio) -> Self {
69 self.reg.reset_bits_reg1(0b1, io.into());
70 self
71 }
72 #[must_use]
76 pub fn release_gpio(mut self, io: Gpio) -> Self {
77 self.reg.set_bits_reg1(0b1, io.into());
78 self
79 }
80 #[must_use]
82 pub fn activate_single_switch(mut self, switch_nr: BalancingSwitch) -> Self {
83 self.reg.set_bits_reg1(0b1, switch_nr.into());
84 self
85 }
86 #[must_use]
88 pub fn deactivate_single_switch(mut self, switch_nr: BalancingSwitch) -> Self {
89 self.reg.reset_bits_reg1(0b1, switch_nr.into());
90 self
91 }
92}
93
94impl From<RegisterBlock> for CfgB {
95 fn from(value: RegisterBlock) -> Self {
96 Self { reg: value }
97 }
98}
99impl From<CfgB> for RegisterBlock {
100 fn from(value: CfgB) -> Self {
101 value.reg
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108 #[test]
109 fn some_test() {
110 let cfgb = CfgB::default();
111 assert_eq!(cfgb.reg.reg_1, 0b0000_0000_0000_1111);
112
113 let cfgb = cfgb.pulldown_gpio(Gpio::Gpio6).pulldown_gpio(Gpio::Gpio7);
114 assert_eq!(cfgb.reg.reg_1, 0b0000_0000_0000_1100);
115
116 let cfgb = cfgb.pulldown_gpio(Gpio::Gpio8);
117 assert_eq!(cfgb.reg.reg_1, 0b0000_0000_0000_1000);
118
119 let cfgb = cfgb.pulldown_gpio(Gpio::Gpio9);
120 assert_eq!(cfgb.reg.reg_1, 0b0000_0000_0000_0000);
121
122 assert_eq!(
123 CfgB::from(RegisterBlock::default()).reg,
124 RegisterBlock::default()
125 );
126 assert_eq!(RegisterBlock::from(CfgB::default()).reg_1, 15);
127 }
128}