#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
#[derive(Debug, Clone)]
pub struct AviationAttitude {
pub timestamp: typedef::DateTime,
pub timestamp_ms: u16,
pub system_time: Vec<u32>,
pub pitch: Vec<i16>,
pub roll: Vec<i16>,
pub accel_lateral: Vec<i16>,
pub accel_normal: Vec<i16>,
pub turn_rate: Vec<i16>,
pub stage: Vec<typedef::AttitudeStage>,
pub attitude_stage_complete: Vec<u8>,
pub track: Vec<u16>,
pub validity: Vec<typedef::AttitudeValidity>,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl AviationAttitude {
pub const TIMESTAMP: u8 = 253;
pub const TIMESTAMP_MS: u8 = 0;
pub const SYSTEM_TIME: u8 = 1;
pub const PITCH: u8 = 2;
pub const ROLL: u8 = 3;
pub const ACCEL_LATERAL: u8 = 4;
pub const ACCEL_NORMAL: u8 = 5;
pub const TURN_RATE: u8 = 6;
pub const STAGE: u8 = 7;
pub const ATTITUDE_STAGE_COMPLETE: u8 = 8;
pub const TRACK: u8 = 9;
pub const VALIDITY: u8 = 10;
pub const fn new() -> Self {
Self {
timestamp: typedef::DateTime(u32::MAX),
timestamp_ms: u16::MAX,
system_time: Vec::<u32>::new(),
pitch: Vec::<i16>::new(),
roll: Vec::<i16>::new(),
accel_lateral: Vec::<i16>::new(),
accel_normal: Vec::<i16>::new(),
turn_rate: Vec::<i16>::new(),
stage: Vec::<typedef::AttitudeStage>::new(),
attitude_stage_complete: Vec::<u8>::new(),
track: Vec::<u16>::new(),
validity: Vec::<typedef::AttitudeValidity>::new(),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn pitch_scaled(&self) -> Vec<f64> {
if self.pitch == Vec::<i16>::new() {
return Vec::new();
}
let mut v = Vec::with_capacity(self.pitch.len());
for &x in &self.pitch {
v.push(x as f64 / 10430.38 - 0.0)
}
v
}
pub fn set_pitch_scaled(&mut self, v: &Vec<f64>) -> &mut AviationAttitude {
if v.is_empty() {
self.pitch = Vec::new();
return self;
}
self.pitch = Vec::with_capacity(v.len());
for &x in v {
let unscaled = (x + 0.0) * 10430.38;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64 {
self.pitch.push(i16::MAX);
continue;
}
self.pitch.push(unscaled as i16);
}
self
}
pub fn roll_scaled(&self) -> Vec<f64> {
if self.roll == Vec::<i16>::new() {
return Vec::new();
}
let mut v = Vec::with_capacity(self.roll.len());
for &x in &self.roll {
v.push(x as f64 / 10430.38 - 0.0)
}
v
}
pub fn set_roll_scaled(&mut self, v: &Vec<f64>) -> &mut AviationAttitude {
if v.is_empty() {
self.roll = Vec::new();
return self;
}
self.roll = Vec::with_capacity(v.len());
for &x in v {
let unscaled = (x + 0.0) * 10430.38;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64 {
self.roll.push(i16::MAX);
continue;
}
self.roll.push(unscaled as i16);
}
self
}
pub fn accel_lateral_scaled(&self) -> Vec<f64> {
if self.accel_lateral == Vec::<i16>::new() {
return Vec::new();
}
let mut v = Vec::with_capacity(self.accel_lateral.len());
for &x in &self.accel_lateral {
v.push(x as f64 / 100.0 - 0.0)
}
v
}
pub fn set_accel_lateral_scaled(&mut self, v: &Vec<f64>) -> &mut AviationAttitude {
if v.is_empty() {
self.accel_lateral = Vec::new();
return self;
}
self.accel_lateral = Vec::with_capacity(v.len());
for &x in v {
let unscaled = (x + 0.0) * 100.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64 {
self.accel_lateral.push(i16::MAX);
continue;
}
self.accel_lateral.push(unscaled as i16);
}
self
}
pub fn accel_normal_scaled(&self) -> Vec<f64> {
if self.accel_normal == Vec::<i16>::new() {
return Vec::new();
}
let mut v = Vec::with_capacity(self.accel_normal.len());
for &x in &self.accel_normal {
v.push(x as f64 / 100.0 - 0.0)
}
v
}
pub fn set_accel_normal_scaled(&mut self, v: &Vec<f64>) -> &mut AviationAttitude {
if v.is_empty() {
self.accel_normal = Vec::new();
return self;
}
self.accel_normal = Vec::with_capacity(v.len());
for &x in v {
let unscaled = (x + 0.0) * 100.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64 {
self.accel_normal.push(i16::MAX);
continue;
}
self.accel_normal.push(unscaled as i16);
}
self
}
pub fn turn_rate_scaled(&self) -> Vec<f64> {
if self.turn_rate == Vec::<i16>::new() {
return Vec::new();
}
let mut v = Vec::with_capacity(self.turn_rate.len());
for &x in &self.turn_rate {
v.push(x as f64 / 1024.0 - 0.0)
}
v
}
pub fn set_turn_rate_scaled(&mut self, v: &Vec<f64>) -> &mut AviationAttitude {
if v.is_empty() {
self.turn_rate = Vec::new();
return self;
}
self.turn_rate = Vec::with_capacity(v.len());
for &x in v {
let unscaled = (x + 0.0) * 1024.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i16::MAX as f64 {
self.turn_rate.push(i16::MAX);
continue;
}
self.turn_rate.push(unscaled as i16);
}
self
}
pub fn track_scaled(&self) -> Vec<f64> {
if self.track == Vec::<u16>::new() {
return Vec::new();
}
let mut v = Vec::with_capacity(self.track.len());
for &x in &self.track {
v.push(x as f64 / 10430.38 - 0.0)
}
v
}
pub fn set_track_scaled(&mut self, v: &Vec<f64>) -> &mut AviationAttitude {
if v.is_empty() {
self.track = Vec::new();
return self;
}
self.track = Vec::with_capacity(v.len());
for &x in v {
let unscaled = (x + 0.0) * 10430.38;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
self.track.push(u16::MAX);
continue;
}
self.track.push(unscaled as u16);
}
self
}
}
impl Default for AviationAttitude {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for AviationAttitude {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 254] = [const { &Value::Invalid }; 254];
const KNOWN_NUMS: [u64; 4] = [2047, 0, 0, 2305843009213693952];
let mut n = 0u64;
for field in &mesg.fields {
n += (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 ^ 1
}
let mut unknown_fields: Vec<Field> = Vec::with_capacity(n as usize);
for field in &mesg.fields {
if (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 == 0 {
unknown_fields.push(field.clone());
continue;
}
vals[field.num as usize] = &field.value;
}
Self {
timestamp: typedef::DateTime(vals[253].as_u32()),
timestamp_ms: vals[0].as_u16(),
system_time: vals[1].as_vec_u32(),
pitch: vals[2].as_vec_i16(),
roll: vals[3].as_vec_i16(),
accel_lateral: vals[4].as_vec_i16(),
accel_normal: vals[5].as_vec_i16(),
turn_rate: vals[6].as_vec_i16(),
stage: match &vals[7] {
Value::VecUint8(v) => {
let mut vs = Vec::with_capacity(v.len());
for x in v {
vs.push(typedef::AttitudeStage(*x))
}
vs
}
_ => Vec::new(),
},
attitude_stage_complete: vals[8].as_vec_u8(),
track: vals[9].as_vec_u16(),
validity: match &vals[10] {
Value::VecUint16(v) => {
let mut vs = Vec::with_capacity(v.len());
for x in v {
vs.push(typedef::AttitudeValidity(*x))
}
vs
}
_ => Vec::new(),
},
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<AviationAttitude> for Message {
fn from(m: AviationAttitude) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 12];
let mut len = 0usize;
if m.timestamp != typedef::DateTime(u32::MAX) {
arr[len] = Field {
num: 253,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.timestamp.0),
is_expanded: false,
};
len += 1;
}
if m.timestamp_ms != u16::MAX {
arr[len] = Field {
num: 0,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.timestamp_ms),
is_expanded: false,
};
len += 1;
}
if m.system_time != Vec::<u32>::new() {
arr[len] = Field {
num: 1,
profile_type: ProfileType::UINT32,
value: Value::VecUint32(m.system_time),
is_expanded: false,
};
len += 1;
}
if m.pitch != Vec::<i16>::new() {
arr[len] = Field {
num: 2,
profile_type: ProfileType::SINT16,
value: Value::VecInt16(m.pitch),
is_expanded: false,
};
len += 1;
}
if m.roll != Vec::<i16>::new() {
arr[len] = Field {
num: 3,
profile_type: ProfileType::SINT16,
value: Value::VecInt16(m.roll),
is_expanded: false,
};
len += 1;
}
if m.accel_lateral != Vec::<i16>::new() {
arr[len] = Field {
num: 4,
profile_type: ProfileType::SINT16,
value: Value::VecInt16(m.accel_lateral),
is_expanded: false,
};
len += 1;
}
if m.accel_normal != Vec::<i16>::new() {
arr[len] = Field {
num: 5,
profile_type: ProfileType::SINT16,
value: Value::VecInt16(m.accel_normal),
is_expanded: false,
};
len += 1;
}
if m.turn_rate != Vec::<i16>::new() {
arr[len] = Field {
num: 6,
profile_type: ProfileType::SINT16,
value: Value::VecInt16(m.turn_rate),
is_expanded: false,
};
len += 1;
}
if m.stage != Vec::<typedef::AttitudeStage>::new() {
arr[len] = Field {
num: 7,
profile_type: ProfileType::ATTITUDE_STAGE,
value: Value::VecUint8({
let mut v = Vec::with_capacity(m.stage.len());
for x in &m.stage {
v.push(x.0)
}
v
}),
is_expanded: false,
};
len += 1;
}
if m.attitude_stage_complete != Vec::<u8>::new() {
arr[len] = Field {
num: 8,
profile_type: ProfileType::UINT8,
value: Value::VecUint8(m.attitude_stage_complete),
is_expanded: false,
};
len += 1;
}
if m.track != Vec::<u16>::new() {
arr[len] = Field {
num: 9,
profile_type: ProfileType::UINT16,
value: Value::VecUint16(m.track),
is_expanded: false,
};
len += 1;
}
if m.validity != Vec::<typedef::AttitudeValidity>::new() {
arr[len] = Field {
num: 10,
profile_type: ProfileType::ATTITUDE_VALIDITY,
value: Value::VecUint16({
let mut v = Vec::with_capacity(m.validity.len());
for x in &m.validity {
v.push(x.0)
}
v
}),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::AVIATION_ATTITUDE,
fields: {
let mut fields: Vec<Field> = Vec::with_capacity(len + m.unknown_fields.len());
fields.extend_from_slice(&arr[..len]);
fields.extend_from_slice(&m.unknown_fields);
fields
},
developer_fields: m.developer_fields,
}
}
}