From e5cec6da29be8bc733b613c70b7b74046706c1bc Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 13 Jun 2024 16:29:43 +0800 Subject: [PATCH] refactor cram-console so that ball is in a separate file --- services/cram-console/Cargo.toml | 2 + services/cram-console/src/ball.rs | 103 +++++++++++++++++++++++++ services/cram-console/src/main.rs | 120 ++---------------------------- 3 files changed, 110 insertions(+), 115 deletions(-) create mode 100644 services/cram-console/src/ball.rs diff --git a/services/cram-console/Cargo.toml b/services/cram-console/Cargo.toml index 094cf271e..8640376dc 100644 --- a/services/cram-console/Cargo.toml +++ b/services/cram-console/Cargo.toml @@ -23,6 +23,8 @@ modals = { path = "../modals", features = ["cramium-soc"] } gam = { path = "../gam" } locales = { path = "../../locales" } usb-device-xous = { path = "../usb-device-xous" } +cramium-hal = { path = "../../libs/cramium-hal", features = ["std"] } +cram-hal-service = { path = "../cram-hal-service" } [features] "cramium-fpga" = ["utralib/cramium-fpga", "xous-pl230/cramium-fpga"] diff --git a/services/cram-console/src/ball.rs b/services/cram-console/src/ball.rs new file mode 100644 index 000000000..ab6ce56a1 --- /dev/null +++ b/services/cram-console/src/ball.rs @@ -0,0 +1,103 @@ +const BALL_RADIUS: i16 = 10; +const MOMENTUM_LIMIT: i32 = 8; +const BORDER_WIDTH: i16 = 5; +use graphics_server::{Circle, ClipObjectList, ClipObjectType, DrawStyle, PixelColor, Point, Rectangle}; + +pub struct Ball { + gfx: graphics_server::Gfx, + screensize: Point, + ball: Circle, + momentum: Point, + clip: Rectangle, + trng: trng::Trng, +} +impl Ball { + pub fn new(xns: &xous_api_names::XousNames) -> Ball { + let gfx = graphics_server::Gfx::new(xns).unwrap(); + gfx.draw_boot_logo().unwrap(); + + let screensize = gfx.screen_size().unwrap(); + let mut ball = Circle::new(Point::new(screensize.x / 2, screensize.y / 2), BALL_RADIUS); + ball.style = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); + let clip = Rectangle::new(Point::new(0, 0), screensize); + gfx.draw_circle(ball).unwrap(); + let mut trng = trng::Trng::new(&xns).unwrap(); + let x = ((trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT; + let y = ((trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT; + Ball { gfx, screensize, ball, momentum: Point::new(x as i16, y as i16), clip, trng } + } + + #[allow(dead_code)] + pub fn draw_boot(&self) { self.gfx.draw_boot_logo().ok(); } + + pub fn update(&mut self) { + /* // for testing fonts, etc. + use std::fmt::Write; + let mut tv = graphics_server::TextView::new( + graphics_server::Gid::new([0, 0, 0, 0]), + graphics_server::TextBounds::BoundingBox(self.clip), + ); + tv.clip_rect = Some(self.clip); + tv.set_dry_run(false); + tv.set_op(graphics_server::TextOp::Render); + tv.style = graphics_server::api::GlyphStyle::Tall; + write!(tv.text, "hello world! 😀").ok(); + self.gfx.draw_textview(&mut tv).ok(); + */ + let mut draw_list = ClipObjectList::default(); + + // clear the previous location of the ball + self.ball.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1); + draw_list.push(ClipObjectType::Circ(self.ball), self.clip).unwrap(); + + // 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; + let mut hit_top = false; + let mut hit_bott = false; + if self.ball.center.x + (BALL_RADIUS + BORDER_WIDTH) >= self.screensize.x { + hit_right = true; + self.ball.center.x = self.screensize.x - (BALL_RADIUS + BORDER_WIDTH); + } + if self.ball.center.x - (BALL_RADIUS + BORDER_WIDTH) <= 0 { + hit_left = true; + self.ball.center.x = BALL_RADIUS + BORDER_WIDTH; + } + if self.ball.center.y + (BALL_RADIUS + BORDER_WIDTH) >= self.screensize.y { + hit_bott = true; + self.ball.center.y = self.screensize.y - (BALL_RADIUS + BORDER_WIDTH); + } + if self.ball.center.y - (BALL_RADIUS + BORDER_WIDTH) <= 0 { + hit_top = true; + self.ball.center.y = BALL_RADIUS + BORDER_WIDTH; + } + + if hit_right || hit_left || hit_bott || hit_top { + 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 { + x = -x.abs(); + } + if hit_left { + x = x.abs(); + } + if hit_top { + y = y.abs(); + } + if hit_bott { + y = -y.abs(); + } + 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); + draw_list.push(ClipObjectType::Circ(self.ball), self.clip).unwrap(); + + self.gfx.draw_object_list_clipped(draw_list).ok(); + self.gfx.flush().unwrap(); + } +} diff --git a/services/cram-console/src/main.rs b/services/cram-console/src/main.rs index 179c56b1c..3aa47989b 100644 --- a/services/cram-console/src/main.rs +++ b/services/cram-console/src/main.rs @@ -1,9 +1,12 @@ #[cfg(feature = "hwtest")] mod hwtest; +mod ball; + use std::fmt::Write; use std::thread; +use cram_hal_service::trng; use graphics_server::api::GlyphStyle; use graphics_server::Gid; use graphics_server::*; @@ -38,7 +41,7 @@ fn main() { #[cfg(feature = "early-ball")] thread::spawn(move || { let xns = xous_api_names::XousNames::new().unwrap(); - let mut ball = Ball::new(&xns); + let mut ball = ball::Ball::new(&xns); log::info!("starting ball"); loop { ball.update(); @@ -50,13 +53,6 @@ fn main() { let usb = usb_device_xous::UsbHid::new(); - tt.sleep_ms(25_000).ok(); - log::info!("sending hello world"); - usb.send_str("hello world"); - loop { - tt.sleep_ms(1000).ok(); - } - let status_gam_getter = xns.register_name(SERVER_NAME_STATUS_GID, Some(1)).expect("can't register server"); let mut canvas_gid: [u32; 4] = [0; 4]; @@ -138,8 +134,6 @@ fn main() { }); let modals = modals::Modals::new(&xns).unwrap(); - modals.show_notification("This is a test", Some("This is a test")).ok(); - xous::send_message( cb_cid, xous::Message::new_blocking_scalar(StatusOpcode::Quit.to_usize().unwrap(), 0, 0, 0, 0), @@ -147,113 +141,9 @@ fn main() { .expect("couldn't exit the gutter server"); gutter.join().expect("status boot gutter server did not exit gracefully"); - let mut ball = Ball::new(&xns); + let mut ball = ball::Ball::new(&xns); log::info!("starting ball"); loop { ball.update(); } } - -fn crappy_rng_u32() -> u32 { xous::create_server_id().unwrap().to_u32().0 } - -const BALL_RADIUS: i16 = 10; -const MOMENTUM_LIMIT: i32 = 8; -const BORDER_WIDTH: i16 = 5; -use graphics_server::{Circle, ClipObjectList, ClipObjectType, DrawStyle, PixelColor, Point, Rectangle}; - -struct Ball { - gfx: graphics_server::Gfx, - screensize: Point, - ball: Circle, - momentum: Point, - clip: Rectangle, -} -impl Ball { - pub fn new(xns: &xous_api_names::XousNames) -> Ball { - let gfx = graphics_server::Gfx::new(xns).unwrap(); - gfx.draw_boot_logo().unwrap(); - - let screensize = gfx.screen_size().unwrap(); - let mut ball = Circle::new(Point::new(screensize.x / 2, screensize.y / 2), BALL_RADIUS); - ball.style = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); - let clip = Rectangle::new(Point::new(0, 0), screensize); - gfx.draw_circle(ball).unwrap(); - let x = ((crappy_rng_u32() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT; - let y = ((crappy_rng_u32() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT; - Ball { gfx, screensize, ball, momentum: Point::new(x as i16, y as i16), clip } - } - - #[allow(dead_code)] - pub fn draw_boot(&self) { self.gfx.draw_boot_logo().ok(); } - - pub fn update(&mut self) { - /* // for testing fonts, etc. - use std::fmt::Write; - let mut tv = graphics_server::TextView::new( - graphics_server::Gid::new([0, 0, 0, 0]), - graphics_server::TextBounds::BoundingBox(self.clip), - ); - tv.clip_rect = Some(self.clip); - tv.set_dry_run(false); - tv.set_op(graphics_server::TextOp::Render); - tv.style = graphics_server::api::GlyphStyle::Tall; - write!(tv.text, "hello world! 😀").ok(); - self.gfx.draw_textview(&mut tv).ok(); - */ - let mut draw_list = ClipObjectList::default(); - - // clear the previous location of the ball - self.ball.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1); - draw_list.push(ClipObjectType::Circ(self.ball), self.clip).unwrap(); - - // 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; - let mut hit_top = false; - let mut hit_bott = false; - if self.ball.center.x + (BALL_RADIUS + BORDER_WIDTH) >= self.screensize.x { - hit_right = true; - self.ball.center.x = self.screensize.x - (BALL_RADIUS + BORDER_WIDTH); - } - if self.ball.center.x - (BALL_RADIUS + BORDER_WIDTH) <= 0 { - hit_left = true; - self.ball.center.x = BALL_RADIUS + BORDER_WIDTH; - } - if self.ball.center.y + (BALL_RADIUS + BORDER_WIDTH) >= self.screensize.y { - hit_bott = true; - self.ball.center.y = self.screensize.y - (BALL_RADIUS + BORDER_WIDTH); - } - if self.ball.center.y - (BALL_RADIUS + BORDER_WIDTH) <= 0 { - hit_top = true; - self.ball.center.y = BALL_RADIUS + BORDER_WIDTH; - } - - if hit_right || hit_left || hit_bott || hit_top { - let mut x = ((crappy_rng_u32() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT; - let mut y = ((crappy_rng_u32() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT; - if hit_right { - x = -x.abs(); - } - if hit_left { - x = x.abs(); - } - if hit_top { - y = y.abs(); - } - if hit_bott { - y = -y.abs(); - } - 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); - draw_list.push(ClipObjectType::Circ(self.ball), self.clip).unwrap(); - - self.gfx.draw_object_list_clipped(draw_list).ok(); - self.gfx.flush().unwrap(); - } -}