~ruther/avr-guess-the-number

add27c29465e403ae6ba6e3371e946d3cc7c3f8d — František Boháček 2 years ago fb0bec9
feat: use u8 in filled_sipo instead of u16
2 files changed, 13 insertions(+), 10 deletions(-)

M src/filled_sipo.rs
M src/seven_segment.rs
M src/filled_sipo.rs => src/filled_sipo.rs +11 -5
@@ 2,7 2,7 @@ use super::sipo;

pub struct FilledSipo {
    shift_register: sipo::Sipo,
    data: u16,
    data: [u8; 2],
    update_step: u8
}



@@ 10,12 10,12 @@ impl FilledSipo {
    pub fn create(shift_register: sipo::Sipo) -> FilledSipo {
        FilledSipo {
            shift_register,
            data: 0,
            data: [0,0],
            update_step: 0
        }
    }

    pub fn set_data(&mut self, data: u16) {
    pub fn set_data(&mut self, data: [u8; 2]) {
        self.data = data;
        self.reset();
    }


@@ 27,7 27,13 @@ impl FilledSipo {
    }

    pub fn step(&mut self) -> bool {
        self.shift_register.shift_value((self.data >> (15 - self.update_step)) & 1 == 1);
        let mut index = 0;
        let mut move_factor = self.update_step;
        if self.update_step >= 8 {
            index = 1;
            move_factor -= 8;
        }
        self.shift_register.shift_value((self.data[index] >> (7 - move_factor)) & 1 == 1);

        if self.update_step >= 15 {
            self.update_step = 0;


@@ 45,7 51,7 @@ impl FilledSipo {
    }

    pub fn clear(&mut self) {
        self.set_data(0);
        self.set_data([0, 0]);
        self.reset();
    }
}

M src/seven_segment.rs => src/seven_segment.rs +2 -5
@@ 91,13 91,10 @@ impl SevenSegment {
            segments = !segments;
        }

        let segments: u16 = segments.into();
        let digit_selector: u16 = digit_selector.into();

        if self.dp {
            sipo.set_data(digit_selector << 8 | segments);
            sipo.set_data([digit_selector, segments]);
        } else {
            sipo.set_data(digit_selector << 7 | (segments & 0x7F));
            sipo.set_data([digit_selector >> 1, (digit_selector & 0x01 << 7) | (segments & 0x7F)]);
        }
        return true;
    }

Do not follow this link