mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
format apps/ball
This commit is contained in:
parent
faafa9078b
commit
145e156fe6
2 changed files with 76 additions and 47 deletions
|
|
@ -1,13 +1,14 @@
|
||||||
use super::*;
|
|
||||||
use gam::*;
|
|
||||||
use gam::menu::*;
|
|
||||||
use gam::menu::api::DrawStyle;
|
use gam::menu::api::DrawStyle;
|
||||||
|
use gam::menu::*;
|
||||||
|
use gam::*;
|
||||||
use locales::t;
|
use locales::t;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(PartialEq, Eq)]
|
||||||
enum BallMode {
|
enum BallMode {
|
||||||
Random,
|
Random,
|
||||||
Tilt
|
Tilt,
|
||||||
}
|
}
|
||||||
|
|
||||||
const BALL_RADIUS: i16 = 10;
|
const BALL_RADIUS: i16 = 10;
|
||||||
|
|
@ -32,25 +33,35 @@ impl Ball {
|
||||||
let xns = xous_names::XousNames::new().expect("couldn't connect to Xous Namespace Server");
|
let xns = xous_names::XousNames::new().expect("couldn't connect to Xous Namespace Server");
|
||||||
let gam = gam::Gam::new(&xns).expect("can't connect to Graphical Abstraction Manager");
|
let gam = gam::Gam::new(&xns).expect("can't connect to Graphical Abstraction Manager");
|
||||||
|
|
||||||
let token = gam.register_ux(UxRegistration {
|
let token = gam
|
||||||
app_name: xous_ipc::String::<128>::from_str(gam::APP_NAME_BALL),
|
.register_ux(UxRegistration {
|
||||||
ux_type: gam::UxType::Framebuffer,
|
app_name: xous_ipc::String::<128>::from_str(gam::APP_NAME_BALL),
|
||||||
predictor: None,
|
ux_type: gam::UxType::Framebuffer,
|
||||||
listener: sid.to_array(), // note disclosure of our SID to the GAM -- the secret is now shared with the GAM!
|
predictor: None,
|
||||||
redraw_id: AppOp::Redraw.to_u32().unwrap(),
|
listener: sid.to_array(), /* note disclosure of our SID to the GAM -- the secret is now
|
||||||
gotinput_id: None,
|
* shared with the GAM! */
|
||||||
audioframe_id: None,
|
redraw_id: AppOp::Redraw.to_u32().unwrap(),
|
||||||
focuschange_id: Some(AppOp::FocusChange.to_u32().unwrap()),
|
gotinput_id: None,
|
||||||
rawkeys_id: Some(AppOp::Rawkeys.to_u32().unwrap()),
|
audioframe_id: None,
|
||||||
}).expect("couldn't register Ux context for shellchat");
|
focuschange_id: Some(AppOp::FocusChange.to_u32().unwrap()),
|
||||||
|
rawkeys_id: Some(AppOp::Rawkeys.to_u32().unwrap()),
|
||||||
|
})
|
||||||
|
.expect("couldn't register Ux context for shellchat");
|
||||||
|
|
||||||
let gid = gam.request_content_canvas(token.unwrap()).expect("couldn't get content canvas");
|
let gid = gam.request_content_canvas(token.unwrap()).expect("couldn't get content canvas");
|
||||||
let screensize = gam.get_canvas_bounds(gid).expect("couldn't get dimensions of content canvas");
|
let screensize = gam.get_canvas_bounds(gid).expect("couldn't get dimensions of content canvas");
|
||||||
|
|
||||||
gam.draw_rectangle(gid,
|
gam.draw_rectangle(
|
||||||
Rectangle::new_coords_with_style(0, 0, screensize.x, screensize.y,
|
gid,
|
||||||
DrawStyle::new(PixelColor::Light, PixelColor::Dark, 2))
|
Rectangle::new_coords_with_style(
|
||||||
).expect("couldn't draw our rectangle");
|
0,
|
||||||
|
0,
|
||||||
|
screensize.x,
|
||||||
|
screensize.y,
|
||||||
|
DrawStyle::new(PixelColor::Light, PixelColor::Dark, 2),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.expect("couldn't draw our rectangle");
|
||||||
|
|
||||||
let trng = trng::Trng::new(&xns).unwrap();
|
let trng = trng::Trng::new(&xns).unwrap();
|
||||||
let x = ((trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
let x = ((trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
||||||
|
|
@ -74,6 +85,7 @@ impl Ball {
|
||||||
com,
|
com,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn update(&mut self) {
|
pub(crate) fn update(&mut self) {
|
||||||
// send a list of objects to draw to the GAM, to avoid race conditions in between operations
|
// send a list of objects to draw to the GAM, to avoid race conditions in between operations
|
||||||
let mut draw_list = GamObjectList::new(self.gid);
|
let mut draw_list = GamObjectList::new(self.gid);
|
||||||
|
|
@ -90,10 +102,7 @@ impl Ball {
|
||||||
// positive x => tilt to left
|
// positive x => tilt to left
|
||||||
// negative y => tilt toward top
|
// negative y => tilt toward top
|
||||||
// positive y => tilt toward bottom
|
// positive y => tilt toward bottom
|
||||||
self.momentum = Point::new(
|
self.momentum = Point::new(-(ix / 200), iy / 200);
|
||||||
-(ix / 200),
|
|
||||||
iy / 200
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
// update the ball position based on the momentum vector
|
// update the ball position based on the momentum vector
|
||||||
self.ball.translate(self.momentum);
|
self.ball.translate(self.momentum);
|
||||||
|
|
@ -124,7 +133,7 @@ impl Ball {
|
||||||
let mut x = ((self.trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
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;
|
let mut y = ((self.trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
||||||
if hit_right {
|
if hit_right {
|
||||||
x = - x.abs();
|
x = -x.abs();
|
||||||
}
|
}
|
||||||
if hit_left {
|
if hit_left {
|
||||||
x = x.abs();
|
x = x.abs();
|
||||||
|
|
@ -133,7 +142,7 @@ impl Ball {
|
||||||
y = y.abs();
|
y = y.abs();
|
||||||
}
|
}
|
||||||
if hit_bott {
|
if hit_bott {
|
||||||
y = - y.abs();
|
y = -y.abs();
|
||||||
}
|
}
|
||||||
self.momentum = Point::new(x as i16, y as i16);
|
self.momentum = Point::new(x as i16, y as i16);
|
||||||
}
|
}
|
||||||
|
|
@ -145,22 +154,35 @@ impl Ball {
|
||||||
log::trace!("ball app redraw##");
|
log::trace!("ball app redraw##");
|
||||||
self.gam.redraw().unwrap();
|
self.gam.redraw().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn focus(&mut self) {
|
pub(crate) fn focus(&mut self) {
|
||||||
// draw the background entirely
|
// draw the background entirely
|
||||||
self.gam.draw_rectangle(self.gid,
|
self.gam
|
||||||
Rectangle::new_coords_with_style(0, 0, self.screensize.x, self.screensize.y,
|
.draw_rectangle(
|
||||||
DrawStyle::new(PixelColor::Light, PixelColor::Dark, BORDER_WIDTH))
|
self.gid,
|
||||||
).expect("couldn't draw our rectangle");
|
Rectangle::new_coords_with_style(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
self.screensize.x,
|
||||||
|
self.screensize.y,
|
||||||
|
DrawStyle::new(PixelColor::Light, PixelColor::Dark, BORDER_WIDTH),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.expect("couldn't draw our rectangle");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn rawkeys(&mut self, keys: [char; 4]) {
|
pub(crate) fn rawkeys(&mut self, keys: [char; 4]) {
|
||||||
log::debug!("got rawkey {:?}", keys); // you could use the raw keypresses, but modals are easier...
|
log::debug!("got rawkey {:?}", keys); // you could use the raw keypresses, but modals are easier...
|
||||||
let mut note = String::new();
|
let mut note = String::new();
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
write!(note, "{}'{}'.\n\n{}",
|
write!(
|
||||||
|
note,
|
||||||
|
"{}'{}'.\n\n{}",
|
||||||
t!("ballapp.notification_a", locales::LANG),
|
t!("ballapp.notification_a", locales::LANG),
|
||||||
keys[0],
|
keys[0],
|
||||||
t!("ballapp.notification_b", locales::LANG),
|
t!("ballapp.notification_b", locales::LANG),
|
||||||
).unwrap();
|
)
|
||||||
|
.unwrap();
|
||||||
self.modals.show_notification(¬e, None).unwrap();
|
self.modals.show_notification(¬e, None).unwrap();
|
||||||
self.modals.add_list_item(t!("ballapp.random", locales::LANG)).unwrap();
|
self.modals.add_list_item(t!("ballapp.random", locales::LANG)).unwrap();
|
||||||
self.modals.add_list_item(t!("ballapp.tilt", locales::LANG)).unwrap();
|
self.modals.add_list_item(t!("ballapp.tilt", locales::LANG)).unwrap();
|
||||||
|
|
@ -191,21 +213,24 @@ pub(crate) fn ball_pump_thread(cid_to_main: xous::CID, pump_sid: xous::SID) {
|
||||||
run = true;
|
run = true;
|
||||||
xous::send_message(
|
xous::send_message(
|
||||||
cid_to_self,
|
cid_to_self,
|
||||||
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)
|
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0),
|
||||||
).expect("couldn't pump the main loop event thread");
|
)
|
||||||
},
|
.expect("couldn't pump the main loop event thread");
|
||||||
|
}
|
||||||
Some(PumpOp::Stop) => run = false,
|
Some(PumpOp::Stop) => run = false,
|
||||||
Some(PumpOp::Pump) => {
|
Some(PumpOp::Pump) => {
|
||||||
xous::send_message(
|
xous::send_message(
|
||||||
cid_to_main,
|
cid_to_main,
|
||||||
Message::new_blocking_scalar(AppOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)
|
Message::new_blocking_scalar(AppOp::Pump.to_usize().unwrap(), 0, 0, 0, 0),
|
||||||
).expect("couldn't pump the main loop event thread");
|
)
|
||||||
|
.expect("couldn't pump the main loop event thread");
|
||||||
if run {
|
if run {
|
||||||
tt.sleep_ms(BALL_UPDATE_RATE_MS).unwrap();
|
tt.sleep_ms(BALL_UPDATE_RATE_MS).unwrap();
|
||||||
xous::send_message(
|
xous::send_message(
|
||||||
cid_to_self,
|
cid_to_self,
|
||||||
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)
|
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0),
|
||||||
).expect("couldn't pump the main loop event thread");
|
)
|
||||||
|
.expect("couldn't pump the main loop event thread");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(PumpOp::Quit) => {
|
Some(PumpOp::Quit) => {
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,8 @@ fn main() -> ! {
|
||||||
ball.update();
|
ball.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(AppOp::Pump) => { // this is a blocking scalar so that pump calls don't initiate faster than the app can draw
|
Some(AppOp::Pump) => {
|
||||||
|
// this is a blocking scalar so that pump calls don't initiate faster than the app can draw
|
||||||
if into_allow_redraw {
|
if into_allow_redraw {
|
||||||
ball.focus();
|
ball.focus();
|
||||||
into_allow_redraw = false;
|
into_allow_redraw = false;
|
||||||
|
|
@ -92,29 +93,32 @@ fn main() -> ! {
|
||||||
allow_redraw = false; // this instantly terminates future updates, even if Pump messages are in our input queue
|
allow_redraw = false; // this instantly terminates future updates, even if Pump messages are in our input queue
|
||||||
xous::send_message(
|
xous::send_message(
|
||||||
cid_to_pump,
|
cid_to_pump,
|
||||||
Message::new_scalar(PumpOp::Stop.to_usize().unwrap(), 0, 0, 0, 0)
|
Message::new_scalar(PumpOp::Stop.to_usize().unwrap(), 0, 0, 0, 0),
|
||||||
).expect("couldn't send stop message to the pump thread");
|
)
|
||||||
|
.expect("couldn't send stop message to the pump thread");
|
||||||
}
|
}
|
||||||
gam::FocusState::Foreground => {
|
gam::FocusState::Foreground => {
|
||||||
into_allow_redraw = true;
|
into_allow_redraw = true;
|
||||||
allow_redraw = true;
|
allow_redraw = true;
|
||||||
xous::send_message(
|
xous::send_message(
|
||||||
cid_to_pump,
|
cid_to_pump,
|
||||||
Message::new_scalar(PumpOp::Run.to_usize().unwrap(), 0, 0, 0, 0)
|
Message::new_scalar(PumpOp::Run.to_usize().unwrap(), 0, 0, 0, 0),
|
||||||
).expect("couldn't send run message to the pump thread");
|
)
|
||||||
|
.expect("couldn't send run message to the pump thread");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
Some(AppOp::Quit) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
Some(AppOp::Quit) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
||||||
xous::send_message(
|
xous::send_message(
|
||||||
cid_to_pump,
|
cid_to_pump,
|
||||||
Message::new_blocking_scalar(PumpOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)
|
Message::new_blocking_scalar(PumpOp::Quit.to_usize().unwrap(), 0, 0, 0, 0),
|
||||||
).expect("couldn't send run message to the pump thread");
|
)
|
||||||
unsafe{xous::disconnect(cid_to_pump).ok()};
|
.expect("couldn't send run message to the pump thread");
|
||||||
|
unsafe { xous::disconnect(cid_to_pump).ok() };
|
||||||
xous::return_scalar(msg.sender, 1).expect("couldn't acknowledge quit message");
|
xous::return_scalar(msg.sender, 1).expect("couldn't acknowledge quit message");
|
||||||
break;
|
break;
|
||||||
}),
|
}),
|
||||||
_ => log::error!("couldn't convert opcode: {:?}", msg)
|
_ => log::error!("couldn't convert opcode: {:?}", msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// clean up our program
|
// clean up our program
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue