#![no_std]
pub mod register {
pub fn write(register_address: u8, value: u8) {
unsafe {
core::ptr::write_volatile(register_address as *mut u8, value);
}
}
pub fn read(register_address: u8) -> u8 {
unsafe {
core::ptr::read_volatile(register_address as *mut u8)
}
}
}
pub mod bit {
pub fn read(position: u8, byte: u8) -> u8 {
(byte >> position) & 0x01
}
pub fn set(position: u8, byte: u8) -> u8 {
byte | (1 << position)
}
pub fn unset(position: u8, byte: u8) -> u8 {
byte | (0 << position)
}
pub fn toggle(position: u8, byte: u8) -> u8 {
byte ^ (1 << position)
}
}
pub mod convert {
use super::bit;
pub fn bit_to_char(position: u8, byte: u8) -> char {
match bit::read(position, byte) as u8 {
0 => '0',
1 => '1',
_ => '\0'
}
}
pub fn byte_to_char_array(byte: u8) -> [char; 8] {
let mut char_array: [char ; 8] = ['0'; 8];
for position in 0..8 {
char_array[position] = bit_to_char(position as u8, byte);
}
char_array
}
pub fn byte_repr(byte: u8, reverse: bool) -> [char; 10] {
let mut repr: [char; 10]= ['0'; 10];
let char_array: [char; 8] = byte_to_char_array(byte);
repr[0] = '0';
repr[1] = 'b';
if reverse == false {
for i in 2..=9 {
repr[i] = char_array[9 - i];
}
}
else {
for i in 2..=9 {
repr[i] = char_array[i - 2];
}
}
repr
}
}
#[cfg(test)]
mod tests {
use super::bit;
use super::convert;
#[test]
fn test_bit_read() {
let byte: u8 = 0b1000_0001;
assert_eq!(bit::read(0, byte), 1);
assert_eq!(bit::read(1, byte), 0);
assert_eq!(bit::read(7, byte), 1);
}
#[test]
fn test_bit_set() {
let mut byte: u8 = 0b0000_0000;
byte = bit::set(0, byte);
assert_eq!(bit::read(0, byte), 1);
byte = bit::set(7, byte);
assert_eq!(bit::read(7, byte), 1);
}
#[test]
fn test_bit_toggle() {
let mut byte: u8 = 0b0000_0000;
byte = bit::toggle(0, byte);
assert_eq!(bit::read(0, byte), 1);
byte = bit::toggle(7, byte);
assert_eq!(bit::read(7, byte), 1);
byte = bit::toggle(0, byte);
assert_eq!(bit::read(0, byte), 0);
byte = bit::toggle(7, byte);
assert_eq!(bit::read(7, byte), 0);
}
#[test]
fn test_convert_bit_to_char() {
let byte: u8 = 0b0000_0001;
let mut bit: char = convert::bit_to_char(0, byte);
assert_eq!(bit, '1');
bit = convert::bit_to_char(1, byte);
assert_eq!(bit, '0');
}
#[test]
fn test_convert_byte_to_char_array() {
let byte: u8 = 0b0101_0101;
let char_array: [char; 8] = convert::byte_to_char_array(byte);
assert_eq!(char_array[0], '1');
assert_eq!(char_array[1], '0');
assert_eq!(char_array[2], '1');
assert_eq!(char_array[3], '0');
assert_eq!(char_array[4], '1');
}
#[test]
fn test_convert_byte_repr() {
let byte: u8 = 0b0101_0101;
assert_eq!(convert::byte_repr(byte, false), ['0','b','0','1','0','1','0','1','0','1']);
assert_eq!(convert::byte_repr(byte, true), ['0','b','1','0','1','0','1','0','1','0']);
}
}