#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
#[derive(Debug, Clone)]
pub struct DiveAlarm {
pub message_index: typedef::MessageIndex,
pub depth: u32,
pub time: i32,
pub enabled: typedef::Bool,
pub alarm_type: typedef::DiveAlarmType,
pub sound: typedef::Tone,
pub dive_types: Vec<typedef::SubSport>,
pub id: u32,
pub popup_enabled: typedef::Bool,
pub trigger_on_descent: typedef::Bool,
pub trigger_on_ascent: typedef::Bool,
pub repeating: typedef::Bool,
pub speed: i32,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl DiveAlarm {
pub const MESSAGE_INDEX: u8 = 254;
pub const DEPTH: u8 = 0;
pub const TIME: u8 = 1;
pub const ENABLED: u8 = 2;
pub const ALARM_TYPE: u8 = 3;
pub const SOUND: u8 = 4;
pub const DIVE_TYPES: u8 = 5;
pub const ID: u8 = 6;
pub const POPUP_ENABLED: u8 = 7;
pub const TRIGGER_ON_DESCENT: u8 = 8;
pub const TRIGGER_ON_ASCENT: u8 = 9;
pub const REPEATING: u8 = 10;
pub const SPEED: u8 = 11;
pub const fn new() -> Self {
Self {
message_index: typedef::MessageIndex(u16::MAX),
depth: u32::MAX,
time: i32::MAX,
enabled: typedef::Bool(u8::MAX),
alarm_type: typedef::DiveAlarmType(u8::MAX),
sound: typedef::Tone(u8::MAX),
dive_types: Vec::<typedef::SubSport>::new(),
id: u32::MAX,
popup_enabled: typedef::Bool(u8::MAX),
trigger_on_descent: typedef::Bool(u8::MAX),
trigger_on_ascent: typedef::Bool(u8::MAX),
repeating: typedef::Bool(u8::MAX),
speed: i32::MAX,
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn depth_scaled(&self) -> f64 {
if self.depth == u32::MAX {
return f64::from_bits(u64::MAX);
}
self.depth as f64 / 1000.0 - 0.0
}
pub fn set_depth_scaled(&mut self, v: f64) -> &mut DiveAlarm {
let unscaled = (v + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
self.depth = u32::MAX;
return self;
}
self.depth = unscaled as u32;
self
}
pub fn speed_scaled(&self) -> f64 {
if self.speed == i32::MAX {
return f64::from_bits(u64::MAX);
}
self.speed as f64 / 1000.0 - 0.0
}
pub fn set_speed_scaled(&mut self, v: f64) -> &mut DiveAlarm {
let unscaled = (v + 0.0) * 1000.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i32::MAX as f64 {
self.speed = i32::MAX;
return self;
}
self.speed = unscaled as i32;
self
}
}
impl Default for DiveAlarm {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for DiveAlarm {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 255] = [const { &Value::Invalid }; 255];
const KNOWN_NUMS: [u64; 4] = [4095, 0, 0, 4611686018427387904];
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 {
message_index: typedef::MessageIndex(vals[254].as_u16()),
depth: vals[0].as_u32(),
time: vals[1].as_i32(),
enabled: typedef::Bool(vals[2].as_u8()),
alarm_type: typedef::DiveAlarmType(vals[3].as_u8()),
sound: typedef::Tone(vals[4].as_u8()),
dive_types: match &vals[5] {
Value::VecUint8(v) => {
let mut vs = Vec::with_capacity(v.len());
for x in v {
vs.push(typedef::SubSport(*x))
}
vs
}
_ => Vec::new(),
},
id: vals[6].as_u32(),
popup_enabled: typedef::Bool(vals[7].as_u8()),
trigger_on_descent: typedef::Bool(vals[8].as_u8()),
trigger_on_ascent: typedef::Bool(vals[9].as_u8()),
repeating: typedef::Bool(vals[10].as_u8()),
speed: vals[11].as_i32(),
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<DiveAlarm> for Message {
fn from(m: DiveAlarm) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 13];
let mut len = 0usize;
if m.message_index != typedef::MessageIndex(u16::MAX) {
arr[len] = Field {
num: 254,
profile_type: ProfileType::MESSAGE_INDEX,
value: Value::Uint16(m.message_index.0),
is_expanded: false,
};
len += 1;
}
if m.depth != u32::MAX {
arr[len] = Field {
num: 0,
profile_type: ProfileType::UINT32,
value: Value::Uint32(m.depth),
is_expanded: false,
};
len += 1;
}
if m.time != i32::MAX {
arr[len] = Field {
num: 1,
profile_type: ProfileType::SINT32,
value: Value::Int32(m.time),
is_expanded: false,
};
len += 1;
}
if m.enabled != typedef::Bool(u8::MAX) {
arr[len] = Field {
num: 2,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.enabled.0),
is_expanded: false,
};
len += 1;
}
if m.alarm_type != typedef::DiveAlarmType(u8::MAX) {
arr[len] = Field {
num: 3,
profile_type: ProfileType::DIVE_ALARM_TYPE,
value: Value::Uint8(m.alarm_type.0),
is_expanded: false,
};
len += 1;
}
if m.sound != typedef::Tone(u8::MAX) {
arr[len] = Field {
num: 4,
profile_type: ProfileType::TONE,
value: Value::Uint8(m.sound.0),
is_expanded: false,
};
len += 1;
}
if m.dive_types != Vec::<typedef::SubSport>::new() {
arr[len] = Field {
num: 5,
profile_type: ProfileType::SUB_SPORT,
value: Value::VecUint8({
let mut v = Vec::with_capacity(m.dive_types.len());
for x in &m.dive_types {
v.push(x.0)
}
v
}),
is_expanded: false,
};
len += 1;
}
if m.id != u32::MAX {
arr[len] = Field {
num: 6,
profile_type: ProfileType::UINT32,
value: Value::Uint32(m.id),
is_expanded: false,
};
len += 1;
}
if m.popup_enabled != typedef::Bool(u8::MAX) {
arr[len] = Field {
num: 7,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.popup_enabled.0),
is_expanded: false,
};
len += 1;
}
if m.trigger_on_descent != typedef::Bool(u8::MAX) {
arr[len] = Field {
num: 8,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.trigger_on_descent.0),
is_expanded: false,
};
len += 1;
}
if m.trigger_on_ascent != typedef::Bool(u8::MAX) {
arr[len] = Field {
num: 9,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.trigger_on_ascent.0),
is_expanded: false,
};
len += 1;
}
if m.repeating != typedef::Bool(u8::MAX) {
arr[len] = Field {
num: 10,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.repeating.0),
is_expanded: false,
};
len += 1;
}
if m.speed != i32::MAX {
arr[len] = Field {
num: 11,
profile_type: ProfileType::SINT32,
value: Value::Int32(m.speed),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::DIVE_ALARM,
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,
}
}
}