#![allow(unused, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
use alloc::vec::Vec;
#[derive(Debug, Clone)]
pub struct ZonesTarget {
pub max_heart_rate: u8,
pub threshold_heart_rate: u8,
pub functional_threshold_power: u16,
pub hr_calc_type: typedef::HrZoneCalc,
pub pwr_calc_type: typedef::PwrZoneCalc,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl ZonesTarget {
pub const MAX_HEART_RATE: u8 = 1;
pub const THRESHOLD_HEART_RATE: u8 = 2;
pub const FUNCTIONAL_THRESHOLD_POWER: u8 = 3;
pub const HR_CALC_TYPE: u8 = 5;
pub const PWR_CALC_TYPE: u8 = 7;
pub const fn new() -> Self {
Self {
max_heart_rate: u8::MAX,
threshold_heart_rate: u8::MAX,
functional_threshold_power: u16::MAX,
hr_calc_type: typedef::HrZoneCalc(u8::MAX),
pwr_calc_type: typedef::PwrZoneCalc(u8::MAX),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
fn count_valid_fields(&self) -> usize {
(self.max_heart_rate != u8::MAX) as usize
+ (self.threshold_heart_rate != u8::MAX) as usize
+ (self.functional_threshold_power != u16::MAX) as usize
+ (self.hr_calc_type != typedef::HrZoneCalc(u8::MAX)) as usize
+ (self.pwr_calc_type != typedef::PwrZoneCalc(u8::MAX)) as usize
}
}
impl Default for ZonesTarget {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for ZonesTarget {
fn from(mesg: &Message) -> Self {
const KNOWN_NUMS: [u64; 4] = [174, 0, 0, 0];
let mut n = 0u64;
for field in &mesg.fields {
n += (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 ^ 1
}
let mut v = Self::new();
v.unknown_fields = Vec::<Field>::with_capacity(n as usize);
v.developer_fields = mesg.developer_fields.clone();
for field in &mesg.fields {
match field.num {
1 => v.max_heart_rate = field.value.as_u8(),
2 => v.threshold_heart_rate = field.value.as_u8(),
3 => v.functional_threshold_power = field.value.as_u16(),
5 => v.hr_calc_type = typedef::HrZoneCalc(field.value.as_u8()),
7 => v.pwr_calc_type = typedef::PwrZoneCalc(field.value.as_u8()),
_ => v.unknown_fields.push(field.clone()),
};
}
v
}
}
impl From<ZonesTarget> for Message {
fn from(m: ZonesTarget) -> Self {
let mut fields =
Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());
if m.max_heart_rate != u8::MAX {
fields.push(Field {
num: 1,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.max_heart_rate),
is_expanded: false,
});
};
if m.threshold_heart_rate != u8::MAX {
fields.push(Field {
num: 2,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.threshold_heart_rate),
is_expanded: false,
});
};
if m.functional_threshold_power != u16::MAX {
fields.push(Field {
num: 3,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.functional_threshold_power),
is_expanded: false,
});
};
if m.hr_calc_type != typedef::HrZoneCalc(u8::MAX) {
fields.push(Field {
num: 5,
profile_type: ProfileType::HR_ZONE_CALC,
value: Value::Uint8(m.hr_calc_type.0),
is_expanded: false,
});
};
if m.pwr_calc_type != typedef::PwrZoneCalc(u8::MAX) {
fields.push(Field {
num: 7,
profile_type: ProfileType::PWR_ZONE_CALC,
value: Value::Uint8(m.pwr_calc_type.0),
is_expanded: false,
});
};
fields.extend_from_slice(&m.unknown_fields);
Self {
header: 0,
num: typedef::MesgNum::ZONES_TARGET,
fields,
developer_fields: m.developer_fields,
}
}
}