From 6089672960a369ac2f49834c123027f88826b202 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 24 Jan 2022 04:42:35 +0800 Subject: [PATCH] add localization to the demo app --- Cargo.lock | 2 + apps/ball/Cargo.toml | 2 + apps/ball/locales/i18n.json | 27 +++++++++++ apps/ball/src/ball.rs | 61 ++++++++++++++++++++++--- locales/README.md | 5 +- services/gam/src/layouts/framebuffer.rs | 2 - xtask/src/main.rs | 19 ++++++-- 7 files changed, 102 insertions(+), 16 deletions(-) create mode 100644 apps/ball/locales/i18n.json diff --git a/Cargo.lock b/Cargo.lock index d7eb11879..c6ee6e8c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -126,8 +126,10 @@ version = "0.1.0" dependencies = [ "com", "gam", + "locales", "log", "log-server", + "modals", "num-derive", "num-traits", "ticktimer-server", diff --git a/apps/ball/Cargo.toml b/apps/ball/Cargo.toml index 0961e8c5d..88e5d2606 100644 --- a/apps/ball/Cargo.toml +++ b/apps/ball/Cargo.toml @@ -17,7 +17,9 @@ ticktimer-server = { path = "../../services/ticktimer-server" } xous-names = { path = "../../services/xous-names" } gam = {path = "../../services/gam" } trng = {path = "../../services/trng"} +modals = {path = "../../services/modals"} com = {path = "../../services/com"} # for the gyro read +locales = {path = "../../locales"} [features] default = [] diff --git a/apps/ball/locales/i18n.json b/apps/ball/locales/i18n.json new file mode 100644 index 000000000..47ead47fd --- /dev/null +++ b/apps/ball/locales/i18n.json @@ -0,0 +1,27 @@ +{ + "ballapp.notification_a": { + "en": "Bouncing ball ⛹ demo.\nInterrupted with key hit: ", + "zh": "弹跳球演示.⛹\n因击键而中断:", + "en-audio": "Bouncing ball demo. Interrupted with key hit:" + }, + "ballapp.notification_b": { + "en": "Press any key to continue.", + "zh": "按任意键继续", + "en-audio": "Press any key to continue." + }, + "ballapp.mode_prompt": { + "en": "Pick a mode of operation:", + "zh": "选择一种操作模式:", + "en-audio": "Pick a mode of operation:" + }, + "ballapp.random": { + "en": "Random", + "zh": "随机的", + "en-audio": "Random" + }, + "ballapp.tilt": { + "en": "Tilt", + "zh": "加速度计倾斜", + "en-audio": "Tilt" + } +} \ No newline at end of file diff --git a/apps/ball/src/ball.rs b/apps/ball/src/ball.rs index 47fa1ff78..9cd40597f 100644 --- a/apps/ball/src/ball.rs +++ b/apps/ball/src/ball.rs @@ -2,20 +2,29 @@ use super::*; use gam::*; use gam::menu::*; use gam::menu::api::DrawStyle; +use locales::t; + +#[derive(PartialEq, Eq)] +enum BallMode { + Random, + Tilt +} const BALL_RADIUS: i16 = 10; const MOMENTUM_LIMIT: i32 = 8; const BORDER_WIDTH: i16 = 5; pub(crate) struct Ball { gam: gam::Gam, - xns: xous_names::XousNames, gid: Gid, screensize: Point, // our security token for making changes to our record on the GAM - token: [u32; 4], + _token: [u32; 4], ball: Circle, momentum: Point, trng: trng::Trng, + modals: modals::Modals, + mode: BallMode, + com: com::Com, } impl Ball { @@ -50,24 +59,42 @@ impl Ball { let mut ball = Circle::new(Point::new(screensize.x / 2, screensize.y / 2), BALL_RADIUS); ball.style = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); gam.draw_circle(gid, ball).expect("couldn't erase ball's previous position"); - + let modals = modals::Modals::new(&xns).unwrap(); + let com = com::Com::new(&xns).unwrap(); Ball { gid, - xns, gam, screensize, - token: token.unwrap(), + _token: token.unwrap(), ball, momentum: Point::new(x as i16, y as i16), trng, + modals, + mode: BallMode::Random, + com, } } pub(crate) fn update(&mut self) { // clear the previous location of the ball self.ball.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1); self.gam.draw_circle(self.gid, self.ball).expect("couldn't erase ball's previous position"); + if self.mode == BallMode::Tilt { + let (x, y, _z, _id) = self.com.gyro_read_blocking().unwrap(); + let ix = x as i16; + let iy = y as i16; + log::debug!("x: {}, y: {}", ix, iy); + // negative x => tilt to right + // positive x => tilt to left + // negative y => tilt toward top + // positive y => tilt toward bottom + self.momentum = Point::new( + -(ix / 200), + iy / 200 + ); + } // update the ball position based on the momentum vector self.ball.translate(self.momentum); + // check if the ball hits the wall, if so, snap its position to the wall let mut hit_right = false; let mut hit_left = false; @@ -89,7 +116,8 @@ impl Ball { hit_top = true; self.ball.center.y = BALL_RADIUS + BORDER_WIDTH; } - if hit_right || hit_left || hit_bott || hit_top { + + if (hit_right || hit_left || hit_bott || hit_top) && (self.mode == BallMode::Random) { let mut x = ((self.trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT; let mut y = ((self.trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT; if hit_right { @@ -106,6 +134,7 @@ impl Ball { } self.momentum = Point::new(x as i16, y as i16); } + // draw the new location for the ball self.ball.style = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); self.gam.draw_circle(self.gid, self.ball).expect("couldn't erase ball's previous position"); @@ -119,7 +148,25 @@ impl Ball { ).expect("couldn't draw our rectangle"); } pub(crate) fn rawkeys(&mut self, keys: [char; 4]) { - log::info!("got rawkey {:?}", keys); + log::debug!("got rawkey {:?}", keys); // you could use the raw keypresses, but modals are easier... + let mut note = String::new(); + use std::fmt::Write; + write!(note, "{}'{}'.\n\n{}", + t!("ballapp.notification_a", xous::LANG), + keys[0], + t!("ballapp.notification_b", xous::LANG), + ).unwrap(); + self.modals.show_notification(¬e).unwrap(); + self.modals.add_list_item(t!("ballapp.random", xous::LANG)).unwrap(); + self.modals.add_list_item(t!("ballapp.tilt", xous::LANG)).unwrap(); + let mode = self.modals.get_radiobutton(t!("ballapp.mode_prompt", xous::LANG)).unwrap(); + if mode == t!("ballapp.random", xous::LANG) { + self.mode = BallMode::Random; + } else if mode == t!("ballapp.tilt", xous::LANG) { + self.mode = BallMode::Tilt; + } else { + log::warn!("got an unexpected response from the radio button function: {}", mode); + } } } diff --git a/locales/README.md b/locales/README.md index e7d299a55..8500f3d21 100644 --- a/locales/README.md +++ b/locales/README.md @@ -1,8 +1,7 @@ # Localization -The `lib.rs` file in this crate is auto-generated by `xtask`. It can be manually generated by -calling `xtask generate-locales`, but it is also automatically re-generated whenever `hw-image` -or `run` is selected. +The `lib.rs` file in this crate is auto-generated by `xtask`. It must be manually generated by +calling `xtask generate-locales` every time changes are made to the localization strings. ## Where are the Strings? Locale files can be added to any server by making a `locales` directory within the server, and diff --git a/services/gam/src/layouts/framebuffer.rs b/services/gam/src/layouts/framebuffer.rs index 7e488a0bf..aa4cd4de9 100644 --- a/services/gam/src/layouts/framebuffer.rs +++ b/services/gam/src/layouts/framebuffer.rs @@ -21,12 +21,10 @@ impl Framebuffer { ) -> Result { let screensize = gfx.screen_size().expect("Couldn't get screen size"); - log::info!("got screensize {:?}", screensize); let fb_canvas = Canvas::new( Rectangle::new(Point::new(0, status_canvas.clip_rect().br().y + 1), screensize), MISC_CONTEXT_DEFAULT_TRUST - TRUST_OFFSET, &trng, None, crate::api::CanvasType::Framebuffer ).expect("couldn't create modal canvas"); - log::info!("canvas: {:?}", fb_canvas); canvases.insert(fb_canvas.gid(), fb_canvas); Ok(Framebuffer { diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 9694b553d..06530d482 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -280,7 +280,7 @@ burn-kernel invoke the `usb_update.py` utility to burn the kernel burn-loader invoke the `usb_update.py` utility to burn the loader burn-soc invoke the `usb_update.py` utility to stage the SoC gateware, which must then be provisioned with secret material using the Precursor device. nuke-soc 'Factory reset' - invoke the `usb_update.py` utility to burn the SoC gateware, erasing most secrets. For developers. -generate-locales only generate the locales include for the language selected in xous-rs/src/locale.rs +generate-locales (re)generate the locales include for the language selected in xous-rs/src/locale.rs wycheproof-import generate binary test vectors for engine-25519 from whycheproof-import/x25519.json pddb-dev PDDB testing only for live hardware pddb-hosted PDDB testing in a hosted environment @@ -964,11 +964,22 @@ fn project_root() -> PathBuf { .to_path_buf() } -/// Force the locales to be regenerated. This simply `touches` -/// the `build.rs` for locales, causing a rebuild next time. +/// Regenerate the locales files. This is only done when the command is explicitly run. fn generate_locales() -> Result<(), std::io::Error> { let ts = filetime::FileTime::from_system_time(std::time::SystemTime::now()); - filetime::set_file_mtime("locales/src/lib.rs", ts) + filetime::set_file_mtime("locales/src/lib.rs", ts)?; + let status = Command::new(cargo()) + .current_dir(project_root()) + .args(&[ + "build", + "--package", + "locales", + ]) + .status()?; + if !status.success() { + return Err(std::io::Error::new(std::io::ErrorKind::Other, "Couldn't generate the locales")); + } + return Ok(()); } fn whycheproof_import() -> Result<(), DynError> {