Skip to content

Commit

Permalink
orb-ui: use Duration::ZERO instead of Option<Duration> (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
fouge authored Oct 3, 2024
1 parent 6b57e96 commit b464077
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 79 deletions.
12 changes: 6 additions & 6 deletions orb-ui/sound/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct SoundBuilder<'a> {
reader: Option<Box<dyn Reader>>,
cancel_all: bool,
priority: u8,
delay: Option<Duration>,
initial_delay: Duration,
max_delay: Duration,
volume: f64,
}
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Queue {
reader: reader.map(|reader| Box::new(reader) as _),
cancel_all: false,
priority: 0,
delay: None,
initial_delay: Duration::ZERO,
max_delay: Duration::MAX,
volume: 1.0,
}
Expand Down Expand Up @@ -203,8 +203,8 @@ impl SoundBuilder<'_> {

/// Adds delay before playing the sound.
#[must_use]
pub fn delay(mut self, delay: Option<Duration>) -> Self {
self.delay = delay;
pub fn delay(mut self, delay: Duration) -> Self {
self.initial_delay = delay;
self
}

Expand All @@ -221,7 +221,7 @@ impl SoundBuilder<'_> {
reader,
cancel_all,
priority,
delay,
initial_delay,
max_delay,
volume,
} = self;
Expand All @@ -241,7 +241,7 @@ impl SoundBuilder<'_> {
.duration_since(UNIX_EPOCH)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?
.saturating_add(max_delay);
let start_time = Instant::now().add(delay.unwrap_or(Duration::ZERO));
let start_time = Instant::now().add(initial_delay);
let state = Arc::new(Mutex::new(State::Pending));
let sound = Sound {
id,
Expand Down
89 changes: 54 additions & 35 deletions orb-ui/src/engine/diamond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,14 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
Event::NetworkConnectionSuccess => {
self.sound.queue(
sound::Type::Melody(sound::Melody::InternetConnectionSuccessful),
None,
Duration::ZERO,
)?;
}
Event::BootComplete { api_mode } => {
self.sound
.queue(sound::Type::Melody(sound::Melody::BootUp), None)?;
self.sound.queue(
sound::Type::Melody(sound::Melody::BootUp),
Duration::ZERO,
)?;
self.operator_pulse.stop(Transition::PlayOnce)?;
self.operator_idle.api_mode(*api_mode);
self.is_api_mode = *api_mode;
Expand All @@ -269,8 +271,10 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
);
}
Event::Shutdown { requested: _ } => {
self.sound
.queue(sound::Type::Melody(sound::Melody::PoweringDown), None)?;
self.sound.queue(
sound::Type::Melody(sound::Melody::PoweringDown),
Duration::ZERO,
)?;
self.set_ring(
LEVEL_NOTICE,
animations::Static::<DIAMOND_RING_LED_COUNT>::new(Argb::OFF, None),
Expand All @@ -280,8 +284,10 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
}
Event::SignupStartOperator => {
self.capture_sound.reset();
self.sound
.queue(sound::Type::Melody(sound::Melody::StartSignup), None)?;
self.sound.queue(
sound::Type::Melody(sound::Melody::StartSignup),
Duration::ZERO,
)?;
// starting signup sequence
// animate from left to right (`operator_action`)
// and then keep first LED on as a background (`operator_signup_phase`)
Expand Down Expand Up @@ -328,7 +334,7 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
self.operator_idle.no_wlan();
self.sound.queue(
sound::Type::Voice(sound::Voice::ShowWifiHotspotQrCode),
None,
Duration::ZERO,
)?;
}
QrScanSchema::User => {
Expand All @@ -345,8 +351,10 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
};
}
Event::QrScanCapture => {
self.sound
.queue(sound::Type::Melody(sound::Melody::QrCodeCapture), None)?;
self.sound.queue(
sound::Type::Melody(sound::Melody::QrCodeCapture),
Duration::ZERO,
)?;
}
Event::QrScanCompleted { schema } => {
self.stop_center(LEVEL_FOREGROUND, Transition::ForceStop);
Expand Down Expand Up @@ -375,13 +383,13 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
QrScanUnexpectedReason::Invalid => {
self.sound.queue(
sound::Type::Voice(sound::Voice::QrCodeInvalid),
None,
Duration::ZERO,
)?;
}
QrScanUnexpectedReason::WrongFormat => {
self.sound.queue(
sound::Type::Voice(sound::Voice::WrongQrCodeFormat),
None,
Duration::ZERO,
)?;
}
}
Expand All @@ -396,8 +404,10 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
}
}
Event::QrScanFail { schema } => {
self.sound
.queue(sound::Type::Melody(sound::Melody::SoundError), None)?;
self.sound.queue(
sound::Type::Melody(sound::Melody::SoundError),
Duration::ZERO,
)?;
match schema {
QrScanSchema::User | QrScanSchema::Operator => {
self.operator_signup_phase.failure();
Expand All @@ -418,14 +428,14 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
QrScanSchema::Operator => {
self.sound.queue(
sound::Type::Melody(sound::Melody::QrLoadSuccess),
None,
Duration::ZERO,
)?;
self.operator_signup_phase.operator_qr_captured();
}
QrScanSchema::User => {
self.sound.queue(
sound::Type::Melody(sound::Melody::UserQrLoadSuccess),
None,
Duration::ZERO,
)?;
self.operator_signup_phase.user_qr_captured();
self.set_ring(
Expand All @@ -440,13 +450,13 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
QrScanSchema::Wifi => {
self.sound.queue(
sound::Type::Melody(sound::Melody::QrLoadSuccess),
None,
Duration::ZERO,
)?;
}
},
Event::QrScanTimeout { schema } => {
self.sound
.queue(sound::Type::Voice(sound::Voice::Timeout), None)?;
.queue(sound::Type::Voice(sound::Voice::Timeout), Duration::ZERO)?;
match schema {
QrScanSchema::User | QrScanSchema::Operator => {
self.stop_center(LEVEL_FOREGROUND, Transition::FadeOut(1.0));
Expand Down Expand Up @@ -482,7 +492,8 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
} else {
sound::Melody::SoundError
};
self.sound.queue(sound::Type::Melody(melody), None)?;
self.sound
.queue(sound::Type::Melody(melody), Duration::ZERO)?;
// This justs sets the operator LEDs yellow
// to inform the operator to press the button.
self.operator_signup_phase.failure();
Expand All @@ -494,7 +505,7 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
// to biometric capture are already set in `QrScanSuccess`
self.sound.queue(
sound::Type::Melody(sound::Melody::UserStartCapture),
None,
Duration::ZERO,
)?;
// pulsing wave animation displayed
// while we wait for the user to be in position
Expand Down Expand Up @@ -616,8 +627,10 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
}
}
Event::BiometricCaptureSuccess => {
self.sound
.queue(sound::Type::Melody(sound::Melody::IrisScanSuccess), None)?;
self.sound.queue(
sound::Type::Melody(sound::Melody::IrisScanSuccess),
Duration::ZERO,
)?;
// custom alert animation on ring
// a bit off for 500ms then on with fade out animation
// twice: first faster than the other
Expand Down Expand Up @@ -726,32 +739,34 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
);
self.sound.queue(
sound::Type::Melody(sound::Melody::SoundError),
Some(Duration::from_millis(2000)),
Duration::from_millis(2000),
)?;
match reason {
SignupFailReason::Timeout => {
self.sound
.queue(sound::Type::Voice(sound::Voice::Timeout), None)?;
self.sound.queue(
sound::Type::Voice(sound::Voice::Timeout),
Duration::ZERO,
)?;
}
SignupFailReason::FaceNotFound => {
self.sound.queue(
sound::Type::Voice(sound::Voice::FaceNotFound),
None,
Duration::ZERO,
)?;
}
SignupFailReason::Server
| SignupFailReason::UploadCustodyImages => {
self.sound.queue(
sound::Type::Voice(sound::Voice::ServerError),
None,
Duration::ZERO,
)?;
}
SignupFailReason::Verification => {
self.sound.queue(
sound::Type::Voice(
sound::Voice::VerificationNotSuccessfulPleaseTryAgain,
),
None,
Duration::ZERO,
)?;
}
SignupFailReason::SoftwareVersionDeprecated => {
Expand Down Expand Up @@ -816,8 +831,10 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
);
}
Event::SignupSuccess => {
self.sound
.queue(sound::Type::Melody(sound::Melody::SignupSuccess), None)?;
self.sound.queue(
sound::Type::Melody(sound::Melody::SignupSuccess),
Duration::ZERO,
)?;

self.operator_signup_phase.signup_successful();

Expand Down Expand Up @@ -893,7 +910,7 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
Event::RecoveryImage => {
self.sound.queue(
sound::Type::Voice(sound::Voice::PleaseDontShutDown),
None,
Duration::ZERO,
)?;
// check that ring is not already in recovery mode
if self
Expand Down Expand Up @@ -921,15 +938,15 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
sound::Type::Voice(
sound::Voice::InternetConnectionTooSlowToPerformSignups,
),
None,
Duration::ZERO,
)?;
}
Event::SlowInternetForSignup => {
self.sound.queue(
sound::Type::Voice(
sound::Voice::InternetConnectionTooSlowSignupsMightTakeLonger,
),
None,
Duration::ZERO,
)?;
}
Event::SoundVolume { level } => {
Expand All @@ -947,8 +964,10 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
});
}
Event::SoundTest => {
self.sound
.queue(sound::Type::Melody(sound::Melody::BootUp), None)?;
self.sound.queue(
sound::Type::Melody(sound::Melody::BootUp),
Duration::ZERO,
)?;
}
Event::Gimbal { x, y } => {
self.gimbal = Some((*x, *y));
Expand Down
Loading

0 comments on commit b464077

Please sign in to comment.