mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
refactor cram-console so that ball is in a separate file
This commit is contained in:
parent
d015099c83
commit
e5cec6da29
3 changed files with 110 additions and 115 deletions
|
|
@ -23,6 +23,8 @@ modals = { path = "../modals", features = ["cramium-soc"] }
|
||||||
gam = { path = "../gam" }
|
gam = { path = "../gam" }
|
||||||
locales = { path = "../../locales" }
|
locales = { path = "../../locales" }
|
||||||
usb-device-xous = { path = "../usb-device-xous" }
|
usb-device-xous = { path = "../usb-device-xous" }
|
||||||
|
cramium-hal = { path = "../../libs/cramium-hal", features = ["std"] }
|
||||||
|
cram-hal-service = { path = "../cram-hal-service" }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
"cramium-fpga" = ["utralib/cramium-fpga", "xous-pl230/cramium-fpga"]
|
"cramium-fpga" = ["utralib/cramium-fpga", "xous-pl230/cramium-fpga"]
|
||||||
|
|
|
||||||
103
services/cram-console/src/ball.rs
Normal file
103
services/cram-console/src/ball.rs
Normal file
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
#[cfg(feature = "hwtest")]
|
#[cfg(feature = "hwtest")]
|
||||||
mod hwtest;
|
mod hwtest;
|
||||||
|
|
||||||
|
mod ball;
|
||||||
|
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
|
use cram_hal_service::trng;
|
||||||
use graphics_server::api::GlyphStyle;
|
use graphics_server::api::GlyphStyle;
|
||||||
use graphics_server::Gid;
|
use graphics_server::Gid;
|
||||||
use graphics_server::*;
|
use graphics_server::*;
|
||||||
|
|
@ -38,7 +41,7 @@ fn main() {
|
||||||
#[cfg(feature = "early-ball")]
|
#[cfg(feature = "early-ball")]
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let xns = xous_api_names::XousNames::new().unwrap();
|
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");
|
log::info!("starting ball");
|
||||||
loop {
|
loop {
|
||||||
ball.update();
|
ball.update();
|
||||||
|
|
@ -50,13 +53,6 @@ fn main() {
|
||||||
|
|
||||||
let usb = usb_device_xous::UsbHid::new();
|
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 =
|
let status_gam_getter =
|
||||||
xns.register_name(SERVER_NAME_STATUS_GID, Some(1)).expect("can't register server");
|
xns.register_name(SERVER_NAME_STATUS_GID, Some(1)).expect("can't register server");
|
||||||
let mut canvas_gid: [u32; 4] = [0; 4];
|
let mut canvas_gid: [u32; 4] = [0; 4];
|
||||||
|
|
@ -138,8 +134,6 @@ fn main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
let modals = modals::Modals::new(&xns).unwrap();
|
let modals = modals::Modals::new(&xns).unwrap();
|
||||||
modals.show_notification("This is a test", Some("This is a test")).ok();
|
|
||||||
|
|
||||||
xous::send_message(
|
xous::send_message(
|
||||||
cb_cid,
|
cb_cid,
|
||||||
xous::Message::new_blocking_scalar(StatusOpcode::Quit.to_usize().unwrap(), 0, 0, 0, 0),
|
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");
|
.expect("couldn't exit the gutter server");
|
||||||
gutter.join().expect("status boot gutter server did not exit gracefully");
|
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");
|
log::info!("starting ball");
|
||||||
loop {
|
loop {
|
||||||
ball.update();
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue