#[cfg(feature = "alloc")]
use core::{
result,
result::Result::{Err, Ok},
};
pub(crate) fn decode_id13_field(id13_field: u32) -> u32 {
let mut hex_gillham: u32 = 0;
if id13_field & 0x1000 != 0 {
hex_gillham |= 0x0010;
} if id13_field & 0x0800 != 0 {
hex_gillham |= 0x1000;
} if id13_field & 0x0400 != 0 {
hex_gillham |= 0x0020;
} if id13_field & 0x0200 != 0 {
hex_gillham |= 0x2000;
} if id13_field & 0x0100 != 0 {
hex_gillham |= 0x0040;
} if id13_field & 0x0080 != 0 {
hex_gillham |= 0x4000;
} if id13_field & 0x0020 != 0 {
hex_gillham |= 0x0100;
} if id13_field & 0x0010 != 0 {
hex_gillham |= 0x0001;
} if id13_field & 0x0008 != 0 {
hex_gillham |= 0x0200;
} if id13_field & 0x0004 != 0 {
hex_gillham |= 0x0002;
} if id13_field & 0x0002 != 0 {
hex_gillham |= 0x0400;
} if id13_field & 0x0001 != 0 {
hex_gillham |= 0x0004;
}
hex_gillham
}
pub(crate) fn mode_a_to_mode_c(mode_a: u32) -> result::Result<u32, &'static str> {
let mut five_hundreds: u32 = 0;
let mut one_hundreds: u32 = 0;
if (mode_a & 0xffff_8889) != 0 || (mode_a & 0x0000_00f0) == 0 {
return Err("Invalid altitude");
}
if mode_a & 0x0010 != 0 {
one_hundreds ^= 0x007;
} if mode_a & 0x0020 != 0 {
one_hundreds ^= 0x003;
} if mode_a & 0x0040 != 0 {
one_hundreds ^= 0x001;
}
if (one_hundreds & 5) == 5 {
one_hundreds ^= 2;
}
if one_hundreds > 5 {
return Err("Invalid altitude");
}
if mode_a & 0x0002 != 0 {
five_hundreds ^= 0x0ff;
} if mode_a & 0x0004 != 0 {
five_hundreds ^= 0x07f;
}
if mode_a & 0x1000 != 0 {
five_hundreds ^= 0x03f;
} if mode_a & 0x2000 != 0 {
five_hundreds ^= 0x01f;
} if mode_a & 0x4000 != 0 {
five_hundreds ^= 0x00f;
}
if mode_a & 0x0100 != 0 {
five_hundreds ^= 0x007;
} if mode_a & 0x0200 != 0 {
five_hundreds ^= 0x003;
} if mode_a & 0x0400 != 0 {
five_hundreds ^= 0x001;
}
if five_hundreds & 1 != 0 && one_hundreds <= 6 {
one_hundreds = 6 - one_hundreds;
}
let n = (five_hundreds * 5) + one_hundreds;
if n >= 13 {
Ok(n - 13)
} else {
Err("Invalid altitude")
}
}