Skip to content

Commit 1d8c475

Browse files
committed
default
1 parent f46b047 commit 1d8c475

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

src/control_word.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ impl ControlWord0 {
1212
pub const BRIGHTNESS_MASK: u8 = 0b0000_1111;
1313
pub const BRIGHTNESS_DEFAULT: u8 = 0b0000_1100;
1414

15-
pub fn new() -> Self {
16-
let mut word = ControlWord0(Self::WORD_SELECT_BIT); // MSB is always 0
17-
word.set_brightness_bits(Self::BRIGHTNESS_DEFAULT);
18-
word.set_peak_current_bits(PeakCurrent::default());
19-
word.set_wake_bit(SleepMode::default());
20-
word
21-
}
22-
2315
pub fn set_brightness_bits(&mut self, brightness: u8) {
2416
// just truncate the bits rather than enforce or check for a max value
2517
self.0 = (self.0 & !Self::BRIGHTNESS_MASK) | (brightness & Self::BRIGHTNESS_MASK);
@@ -38,6 +30,16 @@ impl ControlWord0 {
3830
}
3931
}
4032

33+
impl Default for ControlWord0 {
34+
fn default() -> Self {
35+
let mut control_word = ControlWord0(Self::WORD_SELECT_BIT); // MSB is always 0
36+
control_word.set_brightness_bits(Self::BRIGHTNESS_DEFAULT);
37+
control_word.set_peak_current_bits(PeakCurrent::default());
38+
control_word.set_wake_bit(SleepMode::default());
39+
control_word
40+
}
41+
}
42+
4143
pub enum SleepMode {
4244
Sleep = 0b0000_0000,
4345
Normal = 0b0100_0000,
@@ -57,12 +59,6 @@ pub enum PeakCurrent {
5759
Max12_8Ma = 0b0011_0000,
5860
}
5961

60-
// impl PeakCurrent {
61-
// pub fn bitmask(&self) -> u8 {
62-
// *self as u8
63-
// }
64-
// }
65-
6662
impl Default for PeakCurrent {
6763
fn default() -> Self {
6864
PeakCurrent::Max12_8Ma
@@ -77,13 +73,6 @@ impl ControlWord1 {
7773
pub const DATA_OUT_BIT: u8 = 0b0000_0001;
7874
pub const EXT_OSC_PRESCALER_BIT: u8 = 0b0000_0010;
7975

80-
pub fn new() -> Self {
81-
let mut word = ControlWord1(Self::WORD_SELECT_BIT);
82-
word.set_data_out_mode_bit(DataOutMode::default());
83-
word.set_ext_osc_prescaler_bit(ExtOscPrescaler::default());
84-
word
85-
}
86-
8776
pub fn set_data_out_mode_bit(&mut self, bit: DataOutMode) {
8877
self.0 = (self.0 & !Self::DATA_OUT_BIT) | (bit as u8);
8978
}
@@ -97,6 +86,15 @@ impl ControlWord1 {
9786
}
9887
}
9988

89+
impl Default for ControlWord1 {
90+
fn default() -> Self {
91+
let mut control_word = ControlWord1(Self::WORD_SELECT_BIT);
92+
control_word.set_data_out_mode_bit(DataOutMode::default());
93+
control_word.set_ext_osc_prescaler_bit(ExtOscPrescaler::default());
94+
control_word
95+
}
96+
}
97+
10098
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10199
pub enum DataOutMode {
102100
Serial = 0b0000_0000,
@@ -111,12 +109,12 @@ impl Default for DataOutMode {
111109

112110
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113111
pub enum ExtOscPrescaler {
114-
Clock1 = 0b0000_0000,
115-
Clock8 = 0b0000_0001,
112+
Direct = 0b0000_0000,
113+
Div8 = 0b0000_0001,
116114
}
117115

118116
impl Default for ExtOscPrescaler {
119117
fn default() -> Self {
120-
ExtOscPrescaler::Clock1
118+
ExtOscPrescaler::Direct
121119
}
122120
}

src/lib.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![no_std]
22

3-
43
mod control_word;
54
mod font5x7;
65

@@ -127,27 +126,21 @@ where
127126
blank: blank_ref_cell,
128127
osc_sel: osc_sel_ref_cell,
129128
reset: reset_ref_cell,
130-
control_word_0: ControlWord0::new(),
131-
control_word_1: ControlWord1::new(),
129+
control_word_0: ControlWord0::default(),
130+
control_word_1: ControlWord1::default(),
132131
data_out_mode: DataOutMode::Serial,
133132
font_ascii_start_index: font5x7::FONT5X7.load_at(0) - 1,
134133
})
135134
}
136135

137-
// pub fn begin(&mut self) -> Result<(), Hcms29xxError<PinErr>> {
138-
// self.clear()?;
136+
pub fn begin(&mut self) -> Result<(), Hcms29xxError<PinErr>> {
137+
self.clear()?;
139138

140-
// self.update_control_word(
141-
// control::control_word_0::WAKE_BIT
142-
// | control::control_word_0::DEFAULT_CURRENT
143-
// | control::control_word_0::DEFAULT_BRIGHTNESS,
144-
// )?;
145-
// self.update_control_word(
146-
// control::CONTROL_WORD_SELECT_BIT | control::control_word_1::DEFAULT_DATA_OUT_MODE,
147-
// )?;
139+
self.update_control_word(self.control_word_0.bits())?;
140+
self.update_control_word(self.control_word_1.bits())?;
148141

149-
// Ok(())
150-
// }
142+
Ok(())
143+
}
151144

152145
pub fn clear(&mut self) -> Result<(), Hcms29xxError<PinErr>> {
153146
self.set_dot_data()?;
@@ -271,6 +264,20 @@ where
271264
Ok(())
272265
}
273266

267+
pub fn set_ext_osc_prescale_direct(&mut self) -> Result<(), Hcms29xxError<PinErr>> {
268+
self.control_word_1
269+
.set_ext_osc_prescaler_bit(ExtOscPrescaler::Direct);
270+
self.update_control_word(self.control_word_1.bits())?;
271+
Ok(())
272+
}
273+
274+
pub fn set_ext_osc_prescale_div8(&mut self) -> Result<(), Hcms29xxError<PinErr>> {
275+
self.control_word_1
276+
.set_ext_osc_prescaler_bit(ExtOscPrescaler::Div8);
277+
self.update_control_word(self.control_word_1.bits())?;
278+
Ok(())
279+
}
280+
274281
pub fn set_ext_osc(&mut self) -> Result<(), Hcms29xxError<PinErr>> {
275282
if let Some(ref osc_sel) = self.osc_sel {
276283
osc_sel

0 commit comments

Comments
 (0)