7
7
use crate :: pac;
8
8
use tiny_led_matrix:: DisplayControl ;
9
9
10
- const fn bit_range ( lo : usize , count : usize ) -> u32 {
11
- ( ( 1 << count) - 1 ) << lo
10
+ const fn pin_bits ( pins : & [ usize ] ) -> u32 {
11
+ let mut i: usize = 0 ;
12
+ let mut bits: u32 = 0 ;
13
+ while i < pins. len ( ) {
14
+ bits |= 1 << pins[ i] ;
15
+ i += 1 ;
16
+ }
17
+ bits
12
18
}
13
19
14
20
pub ( crate ) const MATRIX_COLS : usize = 9 ;
15
- const FIRST_COL_PIN : usize = 4 ;
16
- const LAST_COL_PIN : usize = FIRST_COL_PIN + MATRIX_COLS - 1 ;
17
- const COL_BITS : u32 = bit_range ( FIRST_COL_PIN , MATRIX_COLS ) ;
21
+ const COLS : [ usize ; MATRIX_COLS ] = [ 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ] ;
22
+ const COL_BITS : u32 = pin_bits ( & COLS ) ;
18
23
19
24
pub ( crate ) const MATRIX_ROWS : usize = 3 ;
20
- const FIRST_ROW_PIN : usize = 13 ;
21
- const LAST_ROW_PIN : usize = FIRST_ROW_PIN + MATRIX_ROWS - 1 ;
22
- const ROW_BITS : u32 = bit_range ( FIRST_ROW_PIN , MATRIX_ROWS ) ;
25
+ const ROWS : [ usize ; MATRIX_ROWS ] = [ 13 , 14 , 15 ] ;
26
+ const ROW_BITS : u32 = pin_bits ( & ROWS ) ;
23
27
24
28
/// Wrapper for `nrf51::GPIO` for passing to the display code.
25
29
///
@@ -28,9 +32,14 @@ const ROW_BITS: u32 = bit_range(FIRST_ROW_PIN, MATRIX_ROWS);
28
32
/// [`DisplayControl`]: tiny_led_matrix::DisplayControl
29
33
pub ( crate ) struct MicrobitGpio < ' a > ( pub & ' a pac:: GPIO ) ;
30
34
31
- /// Returns the GPIO pin numbers corresponding to the columns in a ColumnSet.
32
- fn column_pins ( cols : u32 ) -> u32 {
33
- cols << FIRST_COL_PIN
35
+ /// Returns the GPIO pin numbers corresponding to the columns in a Columnt et.
36
+ fn column_pins ( mut cols : u32 ) -> u32 {
37
+ let mut result = 0u32 ;
38
+ for pin in COLS . iter ( ) {
39
+ result |= ( cols & 1 ) << pin;
40
+ cols >>= 1 ;
41
+ }
42
+ result
34
43
}
35
44
36
45
/// Implementation of [`DisplayControl`] for the micro:bit's GPIO peripheral.
@@ -44,23 +53,24 @@ fn column_pins(cols: u32) -> u32 {
44
53
impl DisplayControl for MicrobitGpio < ' _ > {
45
54
fn initialise_for_display ( & mut self ) {
46
55
let gpio = & self . 0 ;
47
- for ii in FIRST_COL_PIN ..= LAST_COL_PIN {
48
- gpio. pin_cnf [ ii] . write ( |w| w. dir ( ) . output ( ) ) ;
56
+ for ii in COLS . iter ( ) {
57
+ gpio. pin_cnf [ * ii] . write ( |w| w. dir ( ) . output ( ) ) ;
49
58
}
50
- for ii in FIRST_ROW_PIN ..= LAST_ROW_PIN {
51
- gpio. pin_cnf [ ii] . write ( |w| w. dir ( ) . output ( ) ) ;
59
+ for ii in ROWS . iter ( ) {
60
+ gpio. pin_cnf [ * ii] . write ( |w| w. dir ( ) . output ( ) ) ;
52
61
}
53
62
54
63
// Set all cols high.
55
64
gpio. outset
56
- . write ( |w| unsafe { w. bits ( ( FIRST_COL_PIN ..= LAST_COL_PIN ) . map ( |pin| 1 << pin) . sum ( ) ) } ) ;
65
+ . write ( |w| unsafe { w. bits ( COLS . iter ( ) . map ( |pin| 1 << pin) . sum ( ) ) } ) ;
57
66
}
58
67
59
68
fn display_row_leds ( & mut self , row : usize , cols : u32 ) {
60
69
let gpio = & self . 0 ;
61
70
// To light an LED, we set the row bit and clear the col bit.
62
- let rows_to_set = 1 << ( FIRST_ROW_PIN + row) ;
71
+ let rows_to_set = 1 << ROWS [ row] ;
63
72
let rows_to_clear = ROW_BITS ^ rows_to_set;
73
+
64
74
let cols_to_clear = column_pins ( cols) ;
65
75
let cols_to_set = COL_BITS ^ cols_to_clear;
66
76
0 commit comments