#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
#[derive(Debug, Clone)]
pub struct DeviceSettings {
pub active_time_zone: u8,
pub utc_offset: u32,
pub time_offset: Vec<u32>,
pub time_mode: Vec<typedef::TimeMode>,
pub time_zone_offset: Vec<i8>,
pub backlight_mode: typedef::BacklightMode,
pub activity_tracker_enabled: typedef::Bool,
pub clock_time: typedef::DateTime,
pub pages_enabled: Vec<u16>,
pub move_alert_enabled: typedef::Bool,
pub date_mode: typedef::DateMode,
pub display_orientation: typedef::DisplayOrientation,
pub mounting_side: typedef::Side,
pub default_page: Vec<u16>,
pub autosync_min_steps: u16,
pub autosync_min_time: u16,
pub lactate_threshold_autodetect_enabled: typedef::Bool,
pub ble_auto_upload_enabled: typedef::Bool,
pub auto_sync_frequency: typedef::AutoSyncFrequency,
pub auto_activity_detect: typedef::AutoActivityDetect,
pub number_of_screens: u8,
pub smart_notification_display_orientation: typedef::DisplayOrientation,
pub tap_interface: typedef::Switch,
pub tap_sensitivity: typedef::TapSensitivity,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl DeviceSettings {
pub const ACTIVE_TIME_ZONE: u8 = 0;
pub const UTC_OFFSET: u8 = 1;
pub const TIME_OFFSET: u8 = 2;
pub const TIME_MODE: u8 = 4;
pub const TIME_ZONE_OFFSET: u8 = 5;
pub const BACKLIGHT_MODE: u8 = 12;
pub const ACTIVITY_TRACKER_ENABLED: u8 = 36;
pub const CLOCK_TIME: u8 = 39;
pub const PAGES_ENABLED: u8 = 40;
pub const MOVE_ALERT_ENABLED: u8 = 46;
pub const DATE_MODE: u8 = 47;
pub const DISPLAY_ORIENTATION: u8 = 55;
pub const MOUNTING_SIDE: u8 = 56;
pub const DEFAULT_PAGE: u8 = 57;
pub const AUTOSYNC_MIN_STEPS: u8 = 58;
pub const AUTOSYNC_MIN_TIME: u8 = 59;
pub const LACTATE_THRESHOLD_AUTODETECT_ENABLED: u8 = 80;
pub const BLE_AUTO_UPLOAD_ENABLED: u8 = 86;
pub const AUTO_SYNC_FREQUENCY: u8 = 89;
pub const AUTO_ACTIVITY_DETECT: u8 = 90;
pub const NUMBER_OF_SCREENS: u8 = 94;
pub const SMART_NOTIFICATION_DISPLAY_ORIENTATION: u8 = 95;
pub const TAP_INTERFACE: u8 = 134;
pub const TAP_SENSITIVITY: u8 = 174;
pub const fn new() -> Self {
Self {
active_time_zone: u8::MAX,
utc_offset: u32::MAX,
time_offset: Vec::<u32>::new(),
time_mode: Vec::<typedef::TimeMode>::new(),
time_zone_offset: Vec::<i8>::new(),
backlight_mode: typedef::BacklightMode(u8::MAX),
activity_tracker_enabled: typedef::Bool(u8::MAX),
clock_time: typedef::DateTime(u32::MAX),
pages_enabled: Vec::<u16>::new(),
move_alert_enabled: typedef::Bool(u8::MAX),
date_mode: typedef::DateMode(u8::MAX),
display_orientation: typedef::DisplayOrientation(u8::MAX),
mounting_side: typedef::Side(u8::MAX),
default_page: Vec::<u16>::new(),
autosync_min_steps: u16::MAX,
autosync_min_time: u16::MAX,
lactate_threshold_autodetect_enabled: typedef::Bool(u8::MAX),
ble_auto_upload_enabled: typedef::Bool(u8::MAX),
auto_sync_frequency: typedef::AutoSyncFrequency(u8::MAX),
auto_activity_detect: typedef::AutoActivityDetect(u32::MAX),
number_of_screens: u8::MAX,
smart_notification_display_orientation: typedef::DisplayOrientation(u8::MAX),
tap_interface: typedef::Switch(u8::MAX),
tap_sensitivity: typedef::TapSensitivity(u8::MAX),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn time_zone_offset_scaled(&self) -> Vec<f64> {
if self.time_zone_offset == Vec::<i8>::new() {
return Vec::new();
}
let mut v = Vec::with_capacity(self.time_zone_offset.len());
for &x in &self.time_zone_offset {
v.push(x as f64 / 4.0 - 0.0)
}
v
}
pub fn set_time_zone_offset_scaled(&mut self, v: &Vec<f64>) -> &mut DeviceSettings {
if v.is_empty() {
self.time_zone_offset = Vec::new();
return self;
}
self.time_zone_offset = Vec::with_capacity(v.len());
for &x in v {
let unscaled = (x + 0.0) * 4.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i8::MAX as f64 {
self.time_zone_offset.push(i8::MAX);
continue;
}
self.time_zone_offset.push(unscaled as i8);
}
self
}
}
impl Default for DeviceSettings {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for DeviceSettings {
fn from(mesg: &Message) -> Self {
let mut vals: [&Value; 175] = [const { &Value::Invalid }; 175];
const KNOWN_NUMS: [u64; 4] = [1117105531807338551, 3326148608, 70368744177728, 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 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 {
active_time_zone: vals[0].as_u8(),
utc_offset: vals[1].as_u32(),
time_offset: vals[2].as_vec_u32(),
time_mode: match &vals[4] {
Value::VecUint8(v) => {
let mut vs = Vec::with_capacity(v.len());
for x in v {
vs.push(typedef::TimeMode(*x))
}
vs
}
_ => Vec::new(),
},
time_zone_offset: vals[5].as_vec_i8(),
backlight_mode: typedef::BacklightMode(vals[12].as_u8()),
activity_tracker_enabled: typedef::Bool(vals[36].as_u8()),
clock_time: typedef::DateTime(vals[39].as_u32()),
pages_enabled: vals[40].as_vec_u16(),
move_alert_enabled: typedef::Bool(vals[46].as_u8()),
date_mode: typedef::DateMode(vals[47].as_u8()),
display_orientation: typedef::DisplayOrientation(vals[55].as_u8()),
mounting_side: typedef::Side(vals[56].as_u8()),
default_page: vals[57].as_vec_u16(),
autosync_min_steps: vals[58].as_u16(),
autosync_min_time: vals[59].as_u16(),
lactate_threshold_autodetect_enabled: typedef::Bool(vals[80].as_u8()),
ble_auto_upload_enabled: typedef::Bool(vals[86].as_u8()),
auto_sync_frequency: typedef::AutoSyncFrequency(vals[89].as_u8()),
auto_activity_detect: typedef::AutoActivityDetect(vals[90].as_u32()),
number_of_screens: vals[94].as_u8(),
smart_notification_display_orientation: typedef::DisplayOrientation(vals[95].as_u8()),
tap_interface: typedef::Switch(vals[134].as_u8()),
tap_sensitivity: typedef::TapSensitivity(vals[174].as_u8()),
unknown_fields,
developer_fields: mesg.developer_fields.clone(),
}
}
}
impl From<DeviceSettings> for Message {
fn from(m: DeviceSettings) -> Self {
let mut arr = [const {
Field {
num: 0,
profile_type: ProfileType(0),
value: Value::Invalid,
is_expanded: false,
}
}; 24];
let mut len = 0usize;
if m.active_time_zone != u8::MAX {
arr[len] = Field {
num: 0,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.active_time_zone),
is_expanded: false,
};
len += 1;
}
if m.utc_offset != u32::MAX {
arr[len] = Field {
num: 1,
profile_type: ProfileType::UINT32,
value: Value::Uint32(m.utc_offset),
is_expanded: false,
};
len += 1;
}
if m.time_offset != Vec::<u32>::new() {
arr[len] = Field {
num: 2,
profile_type: ProfileType::UINT32,
value: Value::VecUint32(m.time_offset),
is_expanded: false,
};
len += 1;
}
if m.time_mode != Vec::<typedef::TimeMode>::new() {
arr[len] = Field {
num: 4,
profile_type: ProfileType::TIME_MODE,
value: Value::VecUint8({
let mut v = Vec::with_capacity(m.time_mode.len());
for x in &m.time_mode {
v.push(x.0)
}
v
}),
is_expanded: false,
};
len += 1;
}
if m.time_zone_offset != Vec::<i8>::new() {
arr[len] = Field {
num: 5,
profile_type: ProfileType::SINT8,
value: Value::VecInt8(m.time_zone_offset),
is_expanded: false,
};
len += 1;
}
if m.backlight_mode != typedef::BacklightMode(u8::MAX) {
arr[len] = Field {
num: 12,
profile_type: ProfileType::BACKLIGHT_MODE,
value: Value::Uint8(m.backlight_mode.0),
is_expanded: false,
};
len += 1;
}
if m.activity_tracker_enabled != typedef::Bool(u8::MAX) {
arr[len] = Field {
num: 36,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.activity_tracker_enabled.0),
is_expanded: false,
};
len += 1;
}
if m.clock_time != typedef::DateTime(u32::MAX) {
arr[len] = Field {
num: 39,
profile_type: ProfileType::DATE_TIME,
value: Value::Uint32(m.clock_time.0),
is_expanded: false,
};
len += 1;
}
if m.pages_enabled != Vec::<u16>::new() {
arr[len] = Field {
num: 40,
profile_type: ProfileType::UINT16,
value: Value::VecUint16(m.pages_enabled),
is_expanded: false,
};
len += 1;
}
if m.move_alert_enabled != typedef::Bool(u8::MAX) {
arr[len] = Field {
num: 46,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.move_alert_enabled.0),
is_expanded: false,
};
len += 1;
}
if m.date_mode != typedef::DateMode(u8::MAX) {
arr[len] = Field {
num: 47,
profile_type: ProfileType::DATE_MODE,
value: Value::Uint8(m.date_mode.0),
is_expanded: false,
};
len += 1;
}
if m.display_orientation != typedef::DisplayOrientation(u8::MAX) {
arr[len] = Field {
num: 55,
profile_type: ProfileType::DISPLAY_ORIENTATION,
value: Value::Uint8(m.display_orientation.0),
is_expanded: false,
};
len += 1;
}
if m.mounting_side != typedef::Side(u8::MAX) {
arr[len] = Field {
num: 56,
profile_type: ProfileType::SIDE,
value: Value::Uint8(m.mounting_side.0),
is_expanded: false,
};
len += 1;
}
if m.default_page != Vec::<u16>::new() {
arr[len] = Field {
num: 57,
profile_type: ProfileType::UINT16,
value: Value::VecUint16(m.default_page),
is_expanded: false,
};
len += 1;
}
if m.autosync_min_steps != u16::MAX {
arr[len] = Field {
num: 58,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.autosync_min_steps),
is_expanded: false,
};
len += 1;
}
if m.autosync_min_time != u16::MAX {
arr[len] = Field {
num: 59,
profile_type: ProfileType::UINT16,
value: Value::Uint16(m.autosync_min_time),
is_expanded: false,
};
len += 1;
}
if m.lactate_threshold_autodetect_enabled != typedef::Bool(u8::MAX) {
arr[len] = Field {
num: 80,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.lactate_threshold_autodetect_enabled.0),
is_expanded: false,
};
len += 1;
}
if m.ble_auto_upload_enabled != typedef::Bool(u8::MAX) {
arr[len] = Field {
num: 86,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.ble_auto_upload_enabled.0),
is_expanded: false,
};
len += 1;
}
if m.auto_sync_frequency != typedef::AutoSyncFrequency(u8::MAX) {
arr[len] = Field {
num: 89,
profile_type: ProfileType::AUTO_SYNC_FREQUENCY,
value: Value::Uint8(m.auto_sync_frequency.0),
is_expanded: false,
};
len += 1;
}
if m.auto_activity_detect != typedef::AutoActivityDetect(u32::MAX) {
arr[len] = Field {
num: 90,
profile_type: ProfileType::AUTO_ACTIVITY_DETECT,
value: Value::Uint32(m.auto_activity_detect.0),
is_expanded: false,
};
len += 1;
}
if m.number_of_screens != u8::MAX {
arr[len] = Field {
num: 94,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.number_of_screens),
is_expanded: false,
};
len += 1;
}
if m.smart_notification_display_orientation != typedef::DisplayOrientation(u8::MAX) {
arr[len] = Field {
num: 95,
profile_type: ProfileType::DISPLAY_ORIENTATION,
value: Value::Uint8(m.smart_notification_display_orientation.0),
is_expanded: false,
};
len += 1;
}
if m.tap_interface != typedef::Switch(u8::MAX) {
arr[len] = Field {
num: 134,
profile_type: ProfileType::SWITCH,
value: Value::Uint8(m.tap_interface.0),
is_expanded: false,
};
len += 1;
}
if m.tap_sensitivity != typedef::TapSensitivity(u8::MAX) {
arr[len] = Field {
num: 174,
profile_type: ProfileType::TAP_SENSITIVITY,
value: Value::Uint8(m.tap_sensitivity.0),
is_expanded: false,
};
len += 1;
}
Message {
header: 0,
num: typedef::MesgNum::DEVICE_SETTINGS,
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,
}
}
}