Skip to content

Commit

Permalink
chore: temp dbg
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgomesdev committed Dec 24, 2023
1 parent e612590 commit b20975a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
28 changes: 21 additions & 7 deletions server/src/ps_move/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct PsMoveController {
button_state: HashMap<Button, ButtonState>,
pub trigger: f32,
pub connection_type: ConnectionType,
pub has_changed_since_last_update: bool,
}

impl PsMoveController {
Expand Down Expand Up @@ -58,6 +59,7 @@ impl PsMoveController {
last_button_state: HashMap::new(),
button_state: HashMap::new(),
trigger: 0.0,
has_changed_since_last_update: true,
}
}

Expand Down Expand Up @@ -124,13 +126,15 @@ impl PsMoveController {
pub fn set_led_effect(&mut self, effect: LedEffect) {
let mut kind = effect.kind;

self.setting.led = kind.get_updated_hsv(self.setting.led);
self.setting.led = kind.get_initial_hsv().unwrap();
self.led_effect = effect;
self.has_changed_since_last_update = true;
}

pub fn set_led_effect_with_hsv(&mut self, effect: LedEffect, hsv: Hsv) {
self.setting.led = hsv;
self.led_effect = effect;
self.has_changed_since_last_update = true;
}

pub fn set_rumble_effect(&mut self, effect: RumbleEffect) {
Expand Down Expand Up @@ -167,6 +171,7 @@ impl PsMoveController {
};

self.rumble_effect = effect;
self.has_changed_since_last_update = true;
}

pub fn update(&mut self) -> Result<(), ()> {
Expand All @@ -189,7 +194,7 @@ impl PsMoveController {

pub fn get_changed_buttons(&self) -> HashMap<Button, ButtonState> {
if self.last_button_state.is_empty() {
return HashMap::new()
return HashMap::new();
}

HashMap::<Button, ButtonState>::from_iter(self.button_state
Expand All @@ -213,7 +218,10 @@ impl PsMoveController {
}
};

self.setting.led = led_effect.kind.get_updated_hsv(current_hsv);
if let Some(hsv) = led_effect.kind.get_updated_hsv(current_hsv) {
self.setting.led = hsv;
self.has_changed_since_last_update = true;
}
}

pub fn transform_rumble(&mut self) {
Expand All @@ -233,10 +241,14 @@ impl PsMoveController {
}

fn update_hsv_and_rumble(&self) -> Result<(), ()> {
// temp
if SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() % 1000 == 0 {
debug!("Current effect on controller update is {} (with {:?})", self.led_effect.kind, self.setting.led);
// temp logs
if !self.has_changed_since_last_update {
if SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() % 1000 == 0 {
debug!("Has not changed. ({})". self.led_effect.kind);
}
return Ok(());
}
debug!("Has changed! Sending {} (with {:?})", self.led_effect.kind, self.setting.led);

let request = build_set_led_and_rumble_request(self.setting.led, self.setting.rumble);

Expand Down Expand Up @@ -288,7 +300,6 @@ impl PsMoveController {
}
}

#[allow(dead_code)]
fn build_set_led_pwm_request(frequency: u64) -> [u8; 7] {
if !(MIN_LED_PWM_FREQUENCY..=MAX_LED_PWM_FREQUENCY).contains(&frequency) {
panic!("Frequency must be between {MIN_LED_PWM_FREQUENCY} and {MAX_LED_PWM_FREQUENCY}!")
Expand All @@ -311,12 +322,15 @@ fn build_set_led_and_rumble_request(hsv: Hsv, rumble: f32) -> [u8; 8] {

[
MoveRequestType::SetLED as u8,
// Must be zero
0,
rgb[0],
rgb[1],
rgb[2],
// Unknown should be zero
0,
f32_to_u8(rumble),
// Must be zero
0,
]
}
Expand Down
10 changes: 5 additions & 5 deletions server/src/ps_move/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ impl LedEffectKind {
}
}

pub fn get_updated_hsv(&mut self, current_hsv: Hsv) -> Hsv {
/// Returns Some when the Hsv has changed (note: some effects always change, such as Breathing, others never change like static)
pub fn get_updated_hsv(&mut self, current_hsv: Hsv) -> Option<Hsv> {
match *self {
LedEffectKind::Off => *LED_OFF,
LedEffectKind::Static { hsv } => hsv,
LedEffectKind::Off | LedEffectKind::Static => None,
LedEffectKind::Breathing {
initial_hsv,
time_to_peak,
Expand Down Expand Up @@ -253,7 +253,7 @@ impl LedEffectKind {
*LED_OFF
}
} else {
current_hsv
None
}
}
LedEffectKind::Candle {
Expand All @@ -274,7 +274,7 @@ impl LedEffectKind {

Hsv::from_components((hue, saturation, new_value))
} else {
current_hsv
None
}
}
}
Expand Down

0 comments on commit b20975a

Please sign in to comment.