1
1
#![ no_std]
2
2
3
- pub mod control;
3
+
4
+ mod control_word;
4
5
mod font5x7;
5
6
6
- use crate :: control:: * ;
7
+ pub use control_word:: PeakCurrent ;
8
+
9
+ use control_word:: * ;
7
10
use core:: cell:: RefCell ;
8
11
use embedded_hal:: digital:: { ErrorType , OutputPin } ;
9
12
@@ -46,7 +49,8 @@ pub struct Hcms29xx<
46
49
reset : Option < RefCell < ResetPin > > ,
47
50
control_word_0 : ControlWord0 ,
48
51
control_word_1 : ControlWord1 ,
49
- data_out_mode : DataOutModeBit , // we keep track of just this to avoid more bookkeeping when updating local state of control word before updating
52
+ // state kept locally to simplify/reduce overall code size
53
+ data_out_mode : DataOutMode ,
50
54
font_ascii_start_index : u8 ,
51
55
}
52
56
@@ -125,29 +129,29 @@ where
125
129
reset : reset_ref_cell,
126
130
control_word_0 : ControlWord0 :: new ( ) ,
127
131
control_word_1 : ControlWord1 :: new ( ) ,
128
- data_out_mode : DataOutModeBit :: Serial ,
132
+ data_out_mode : DataOutMode :: Serial ,
129
133
font_ascii_start_index : font5x7:: FONT5X7 . load_at ( 0 ) - 1 ,
130
134
} )
131
135
}
132
136
133
- pub fn begin ( & mut self ) -> Result < ( ) , Hcms29xxError < PinErr > > {
134
- self . clear ( ) ?;
137
+ // pub fn begin(&mut self) -> Result<(), Hcms29xxError<PinErr>> {
138
+ // self.clear()?;
135
139
136
- self . update_control_word (
137
- control:: control_word_0:: WAKE_BIT
138
- | control:: control_word_0:: DEFAULT_CURRENT
139
- | control:: control_word_0:: DEFAULT_BRIGHTNESS ,
140
- ) ?;
141
- self . update_control_word (
142
- control:: CONTROL_WORD_SELECT_BIT | control:: control_word_1:: DEFAULT_DATA_OUT_MODE ,
143
- ) ?;
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
+ // )?;
144
148
145
- Ok ( ( ) )
146
- }
149
+ // Ok(())
150
+ // }
147
151
148
152
pub fn clear ( & mut self ) -> Result < ( ) , Hcms29xxError < PinErr > > {
149
153
self . set_dot_data ( ) ?;
150
- for _ in 0 ..NUM_CHARS * control :: CHAR_WIDTH {
154
+ for _ in 0 ..NUM_CHARS * control_word :: CHAR_WIDTH {
151
155
self . send_byte ( 0x00 ) ?;
152
156
}
153
157
self . end_transfer ( ) ?;
@@ -161,8 +165,8 @@ where
161
165
break ;
162
166
}
163
167
let char_start_index: usize =
164
- ( c_str[ i] - self . font_ascii_start_index ) as usize * control :: CHAR_WIDTH ;
165
- for j in 0 ..control :: CHAR_WIDTH {
168
+ ( c_str[ i] - self . font_ascii_start_index ) as usize * control_word :: CHAR_WIDTH ;
169
+ for j in 0 ..control_word :: CHAR_WIDTH {
166
170
self . send_byte ( font5x7:: FONT5X7 . load_at ( char_start_index + j) ) ?;
167
171
}
168
172
}
@@ -292,24 +296,32 @@ where
292
296
}
293
297
294
298
pub fn set_serial_data_out ( & mut self ) -> Result < ( ) , Hcms29xxError < PinErr > > {
295
- self . control_word_1 . set_data_out_mode_bit ( DataOutModeBit :: Serial ) ;
299
+ self . control_word_1
300
+ . set_data_out_mode_bit ( DataOutMode :: Serial ) ;
296
301
self . update_control_word ( self . control_word_1 . bits ( ) ) ?;
302
+
303
+ // update local state once change is sent to device
304
+ self . data_out_mode = DataOutMode :: Serial ;
305
+
297
306
Ok ( ( ) )
298
307
}
299
308
300
309
pub fn set_simultaneous_data_out ( & mut self ) -> Result < ( ) , Hcms29xxError < PinErr > > {
301
- self . control_word_1 . set_data_out_mode_bit ( DataOutModeBit :: Simultaneous ) ;
310
+ self . control_word_1
311
+ . set_data_out_mode_bit ( DataOutMode :: Simultaneous ) ;
302
312
self . update_control_word ( self . control_word_1 . bits ( ) ) ?;
313
+
314
+ // update local state once change is sent to device
315
+ self . data_out_mode = DataOutMode :: Simultaneous ;
316
+
303
317
Ok ( ( ) )
304
318
}
305
319
306
320
fn update_control_word ( & mut self , control_word : u8 ) -> Result < ( ) , Hcms29xxError < PinErr > > {
307
- // read current data out mode before potentially changing it
308
- let times_to_send = if ( self . control_word_1 & control:: control_word_1:: DATA_OUT_BIT ) != 0
309
- {
310
- 1
321
+ let times_to_send = if self . data_out_mode == DataOutMode :: Serial {
322
+ NUM_CHARS as u8 / control_word:: DEVICE_CHARS as u8
311
323
} else {
312
- NUM_CHARS as u8 / control :: DEVICE_CHARS as u8
324
+ 1
313
325
} ;
314
326
315
327
self . set_control_data ( ) ?;
@@ -318,12 +330,6 @@ where
318
330
}
319
331
self . end_transfer ( ) ?;
320
332
321
- if ( control_word & control:: CONTROL_WORD_SELECT_BIT ) != 0 {
322
- self . control_word_1 = control_word;
323
- } else {
324
- self . control_word_0 = control_word;
325
- }
326
-
327
333
Ok ( ( ) )
328
334
}
329
335
0 commit comments