-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Update state in serial message callback
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
api/system/examples/update-state-in-serial-message-callback.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#![no_std] | ||
extern crate alloc; | ||
|
||
#[macro_use] | ||
extern crate sys; | ||
extern crate playdate_system as system; | ||
|
||
use alloc::string::String; | ||
use core::ptr::NonNull; | ||
|
||
use sys::EventLoopCtrl; | ||
use sys::ffi::*; | ||
use system::System; | ||
use system::event::SystemEventExt as _; | ||
use system::prelude::*; | ||
|
||
struct State { | ||
latest_message: Option<String>, | ||
} | ||
|
||
impl State { | ||
fn new() -> Self { Self { latest_message: None } } | ||
|
||
/// System event handler | ||
fn event(&'static mut self, event: SystemEvent) -> EventLoopCtrl { | ||
match event { | ||
SystemEvent::Init => { | ||
System::Default().set_serial_message_callback(Some(|msg| { | ||
self.latest_message = Some(msg); | ||
})); | ||
|
||
self.set_update_handler(); | ||
|
||
println!("Game init complete"); | ||
}, | ||
_ => {}, | ||
} | ||
EventLoopCtrl::Continue | ||
} | ||
} | ||
|
||
impl Update for State { | ||
fn update(&mut self) -> UpdateCtrl { | ||
if let Some(latest_message) = self.latest_message.clone() { | ||
println!("Latest message: {:#?}", latest_message); | ||
} | ||
UpdateCtrl::Continue | ||
} | ||
} | ||
|
||
/// Entry point | ||
#[no_mangle] | ||
pub fn event_handler(_api: NonNull<PlaydateAPI>, event: SystemEvent, _sim_key_code: u32) -> EventLoopCtrl { | ||
pub static mut STATE: Option<State> = None; | ||
|
||
if unsafe { STATE.is_none() } { | ||
let state = State::new(); | ||
unsafe { STATE = Some(state) } | ||
} | ||
|
||
unsafe { STATE.as_mut().expect("impossible").event(event) } | ||
} | ||
|
||
// Needed for debug build | ||
ll_symbols!(); |