From 89f20100b0bd2c0bde61f452625b9dd9c633e2d0 Mon Sep 17 00:00:00 2001 From: buncram Date: Mon, 29 Jan 2024 02:08:27 +0800 Subject: [PATCH] add cheesy ball demo to the cram-console server --- services/cram-console/Cargo.toml | 2 + services/cram-console/src/main.rs | 101 ++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/services/cram-console/Cargo.toml b/services/cram-console/Cargo.toml index 4c88f7f2b..268abac50 100644 --- a/services/cram-console/Cargo.toml +++ b/services/cram-console/Cargo.toml @@ -14,6 +14,8 @@ log-server = { package = "xous-api-log", version = "0.1.53" } log = "0.4.14" xous-pio = { path = "../../libs/xous-pio", optional = true } xous-pl230 = { path = "../../libs/xous-pl230", optional = true } +# this is temporary, until we get GAM/modals pulled in +graphics-server = { path = "../graphics-server" } [features] "cramium-fpga" = ["utralib/cramium-fpga", "xous-pl230/cramium-fpga"] diff --git a/services/cram-console/src/main.rs b/services/cram-console/src/main.rs index 6e5139186..f2e22f150 100644 --- a/services/cram-console/src/main.rs +++ b/services/cram-console/src/main.rs @@ -124,4 +124,105 @@ fn main() { #[cfg(feature = "hwsim")] core_csr.wfo(utra::main::DONE_DONE, 1); // this should stop the simulation log::info!("message passing test done at {}ms!", tt.elapsed_ms()); + + let xns = xous_api_names::XousNames::new().unwrap(); + let mut ball = Ball::new(&xns); + loop { + ball.update(); + /* // for testing full-frame graphics drawing + ball.draw_boot(); + tt.sleep_ms(100).ok(); + */ + } +} + +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) { + 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(); + } }