Skip to content

Commit

Permalink
orb-ui: Dynamically change language (#75)
Browse files Browse the repository at this point in the history
* orb-ui: fix replay event format

JSON

* orb-ui: load new sounds on new language

dynamically on dbus event
  • Loading branch information
fouge committed Apr 16, 2024
1 parent fde9d2d commit 7ed194c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
20 changes: 9 additions & 11 deletions orb-ui/examples/ui-replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::BufRead;
use std::str::FromStr;
use tokio::time::sleep;
use tracing::level_filters::LevelFilter;
use tracing::{debug, info};
use tracing::{debug, info, warn};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, EnvFilter};
Expand All @@ -21,7 +21,7 @@ const RECORDS_FILE: &str = "worldcoin-ui-logs.txt";
interface = "org.worldcoin.OrbUiState1"
)]
trait SignupState {
fn orb_signup_state_event(&self, serialized_event: String) -> zbus::Result<String>;
fn orb_signup_state_event(&self, serialized_event: String) -> zbus::Result<()>;
}

/// Utility args
Expand Down Expand Up @@ -56,7 +56,7 @@ impl FromStr for EventRecord {
// split line to take everything after "UI event:"
let (_, event) = line
.split_once("UI event: ")
.wrap_err("Unable to split line")?;
.wrap_err(format!("Unable to split line: {}", line))?;
let event = event.to_string();
match timestamp_str.parse::<DateTime<Utc>>() {
Ok(timestamp) => {
Expand All @@ -83,13 +83,6 @@ async fn main() -> Result<()> {
let connection = Connection::session().await?;
let proxy = SignupStateProxy::new(&connection).await?;

// set initial state
let _ = proxy.orb_signup_state_event("\"Bootup\"".to_string()).await;
let _ = proxy.orb_signup_state_event("\"Idle\"".to_string()).await;
let _ = proxy
.orb_signup_state_event("\"SoundVolume {{ level: 10 }}\"".to_string())
.await;

// get path to records file from program arguments or use default
let path = args.path.unwrap_or(RECORDS_FILE.to_string());
let file =
Expand All @@ -107,7 +100,12 @@ async fn main() -> Result<()> {
let event = record.event;
info!("Sending: {}", event);
// send the event to orb-ui over dbus
let _ = proxy.orb_signup_state_event(format!("\"{event}\"")).await;
if let Err(e) = proxy
.orb_signup_state_event(event.clone().to_string())
.await
{
warn!("Error sending event {event}: {:?}", e);
}

last_timestamp = Some(record.timestamp);
}
Expand Down
4 changes: 1 addition & 3 deletions orb-ui/src/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::engine;
use crate::engine::Event;
use tokio::sync::mpsc;
use tracing::debug;
use zbus::interface;

/// Dbus interface object for OrbUiState1.
Expand All @@ -23,14 +22,13 @@ impl Interface {
/// Forward events to UI engine by sending serialized engine::Event to the event channel.
async fn orb_signup_state_event(&mut self, event: String) -> zbus::fdo::Result<()> {
// parse event to engine::Event using json_serde
debug!("received JSON event: {}", event);
tracing::debug!("received JSON event: {}", event);
let event: engine::Event = serde_json::from_str(&event).map_err(|e| {
zbus::fdo::Error::InvalidArgs(format!(
"invalid event: failed to parse {}",
e
))
})?;
debug!("received event: {:?}", event);
self.events.send(event).map_err(|e| {
zbus::fdo::Error::Failed(format!("failed to queue event: {}", e))
})?;
Expand Down
14 changes: 11 additions & 3 deletions orb-ui/src/engine/diamond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
#[allow(clippy::too_many_lines)]
fn event(&mut self, event: &Event) -> Result<()> {
tracing::info!("UI event: {:?}", event);
tracing::info!("UI event: {}", serde_json::to_string(event)?.as_str());
match event {
Event::Bootup => {
self.stop_ring(LEVEL_NOTICE, true);
Expand Down Expand Up @@ -790,8 +790,16 @@ impl EventHandler for Runner<DIAMOND_RING_LED_COUNT, DIAMOND_CENTER_LED_COUNT> {
Event::SoundVolume { level } => {
self.sound.set_master_volume(*level);
}
Event::SoundLanguage { lang: _lang } => {
// fixme
Event::SoundLanguage { lang } => {
let language = lang.clone();
let sound = self.sound.clone();
// spawn a new task because we need some async work here
tokio::task::spawn(async move {
let language: Option<&str> = language.as_deref();
if let Err(e) = sound.load_sound_files(language, true).await {
tracing::error!("Error loading sound files: {:?}", e);
}
});
}
}
Ok(())
Expand Down
22 changes: 16 additions & 6 deletions orb-ui/src/engine/pearl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::f64::consts::PI;
use std::time::Duration;

use async_trait::async_trait;
use eyre::Result;
use futures::channel::mpsc;
Expand All @@ -6,13 +9,12 @@ use futures::future::Either;
use futures::{future, StreamExt};
use orb_messages::mcu_main::mcu_message::Message;
use orb_messages::mcu_main::{jetson_to_mcu, JetsonToMcu};
use pid::{InstantTimer, Timer};
use std::f64::consts::PI;
use std::time::Duration;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::time;
use tokio_stream::wrappers::{IntervalStream, UnboundedReceiverStream};

use pid::{InstantTimer, Timer};

use crate::engine::rgb::Argb;
use crate::engine::{
center, operator, ring, Animation, AnimationsStack, CenterFrame, Event,
Expand Down Expand Up @@ -184,7 +186,7 @@ impl Runner<PEARL_RING_LED_COUNT, PEARL_CENTER_LED_COUNT> {
impl EventHandler for Runner<PEARL_RING_LED_COUNT, PEARL_CENTER_LED_COUNT> {
#[allow(clippy::too_many_lines)]
fn event(&mut self, event: &Event) -> Result<()> {
tracing::info!("UI event: {:?}", event);
tracing::info!("UI event: {}", serde_json::to_string(event)?.as_str());
match event {
Event::Bootup => {
self.stop_ring(LEVEL_NOTICE, true);
Expand Down Expand Up @@ -777,8 +779,16 @@ impl EventHandler for Runner<PEARL_RING_LED_COUNT, PEARL_CENTER_LED_COUNT> {
Event::SoundVolume { level } => {
self.sound.set_master_volume(*level);
}
Event::SoundLanguage { lang: _lang } => {
// fixme
Event::SoundLanguage { lang } => {
let language = lang.clone();
let sound = self.sound.clone();
// spawn a new task because we need some async work here
tokio::task::spawn(async move {
let language: Option<&str> = language.as_deref();
if let Err(e) = sound.load_sound_files(language, true).await {
tracing::error!("Error loading sound files: {:?}", e);
}
});
}
}
Ok(())
Expand Down

0 comments on commit 7ed194c

Please sign in to comment.