This commit is contained in:
aozhiwei 2023-10-29 14:23:03 +08:00
parent b77e1c96cb
commit 4fe57b7a7b
8 changed files with 297 additions and 0 deletions

11
f9/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "f9"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
r9 = { path = "../../third_party/r9" }
r9_macro = { path = "../../third_party/r9_macro" }
r9_macro_derive = { path = "../../third_party/r9_macro_derive" }

88
f9/src/app.rs Normal file
View File

@ -0,0 +1,88 @@
use std::rc::{Rc, Weak};
use std::cell::RefCell;
use std::thread::sleep;
use std::time::{Duration, SystemTime};
use r9_macro::SharedFromSelf;
use r9_macro_derive::SharedFromSelf;
pub trait UserApp {
fn get_pkg_name(&self) -> String;
fn init(&mut self);
fn update(&mut self);
fn uninit(&mut self);
}
#[derive(SharedFromSelf)]
pub struct App {
zone_id: i32,
node_id: i32,
instance_id: i32,
_self_wp: Weak::<RefCell::<Self>>,
}
impl App {
pub fn instance() -> Rc::<RefCell::<Self>> {
static mut _INSTANCE: Option<Rc::<RefCell::<App>>> = None;
unsafe {
match &_INSTANCE {
Some(v) => {
return v.clone();
}
None => {
_INSTANCE = Some(Rc::new(RefCell::new(
App{
zone_id: 0,
node_id: 0,
instance_id: 0,
_self_wp: Default::default(),
}
)));
_INSTANCE.clone().unwrap().borrow_mut()._self_wp =
Rc::downgrade(&_INSTANCE.clone().unwrap());
}
}
return _INSTANCE.clone().unwrap().clone();
}
}
pub fn init(&mut self) {
crate::Timer::instance().borrow_mut().init();
}
pub fn uninit(&mut self) {
crate::Timer::instance().borrow_mut().uninit();
}
pub fn run(&mut self) {
while true {
crate::Timer::instance().borrow_mut().update();
sleep(Duration::from_secs(1));
}
}
pub fn get_pkg_name(&self) -> String {
return "".to_string();
}
pub fn new_uuid(&self) -> i64 {
return 0;
}
pub fn get_instance_id(&self) -> i32 {
return 0;
}
pub fn get_zone_id(&self) -> i32 {
return 0;
}
pub fn get_node_id(&self) -> i32 {
return 0;
}
pub fn has_flag(&self) -> bool {
return false;
}
}

14
f9/src/lib.rs Normal file
View File

@ -0,0 +1,14 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
pub mod timer;
pub mod app;
pub use timer::Timer;
//pub use app::app;

115
f9/src/timer.rs Normal file
View File

@ -0,0 +1,115 @@
use std::rc::{Rc, Weak};
use std::any::Any;
use std::cell::RefCell;
use std::time::{Duration, Instant};
use r9::xtimer::{TimerCb, XTimerWp, XTimerAttacherRp, TimerEvent};
use r9_macro::SharedFromSelf;
use r9_macro_derive::SharedFromSelf;
pub type TimerWp = XTimerWp;
pub type TimerAttacherRp = XTimerAttacherRp;
#[derive(SharedFromSelf)]
pub struct Timer {
base: Rc::<RefCell::<r9::xtimer::XTimer>>,
_self_wp: Weak::<RefCell::<Self>>,
}
impl Timer {
pub fn instance() -> Rc::<RefCell::<Self>> {
static mut _INSTANCE: Option<Rc::<RefCell::<Timer>>> = None;
unsafe {
match &_INSTANCE {
Some(v) => {
return v.clone();
}
None => {
_INSTANCE = Some(Rc::new(RefCell::new(
Timer{
base: r9::xtimer::XTimer::new(),
_self_wp: Default::default(),
}
)));
_INSTANCE.clone().unwrap().borrow_mut()._self_wp =
Rc::downgrade(&_INSTANCE.clone().unwrap());
}
}
return _INSTANCE.clone().unwrap().clone();
}
}
pub fn init(&mut self) {
let now = Instant::now();
self.base.borrow_mut().init
(
Box::new(
move || -> i64 {
println!("tick:{}", now.elapsed().as_millis());
return now.elapsed().as_millis() as i64;
}
),
10,
10
);
}
pub fn uninit(&mut self) {
self.base.borrow_mut().uninit();
}
pub fn update(&mut self) {
r9::xtimer::XTimer::update(&self.base);
//return self.base.borrow_mut().update();
}
pub fn set_timeout(&mut self, time: i32, cb: TimerCb) -> TimerWp {
return self.base.borrow_mut().set_timeout(time, cb);
}
pub fn set_timeout_ex
(&mut self, time: i32, cb: TimerCb, attacher: TimerAttacherRp) -> TimerWp {
return self.base.borrow_mut().set_timeout_ex(time, cb, attacher);
}
pub fn set_interval(&mut self, time: i32, cb: TimerCb) -> TimerWp {
return self.base.borrow_mut().set_interval(time, cb);
}
pub fn set_interval_ex
(&mut self, time: i32, cb: TimerCb, attacher: TimerAttacherRp) -> TimerWp {
return self.base.borrow_mut().set_interval_ex(time, cb, attacher);
}
pub fn fire_event(&mut self,
timer_wp: TimerWp,
e: TimerEvent,
args: Option<Vec<Rc::<dyn Any>>>) {
return self.base.borrow_mut().fire_event(timer_wp, e, args);
}
pub fn modify(&mut self, timer_wp: TimerWp, expire_time: i32) {
self.base.borrow_mut().modify(timer_wp, expire_time);
}
pub fn delete_current_timer(&mut self) {
self.base.borrow_mut().delete_current_timer();
}
pub fn is_running(&self) -> bool {
return self.base.borrow().is_running();
}
pub fn delete(&mut self, timer_wp: TimerWp) {
self.base.borrow_mut().delete(timer_wp);
}
pub fn get_remain_time(&mut self, timer_wp: TimerWp) -> i64 {
return self.base.borrow_mut().get_remain_time(timer_wp);
}
pub fn get_idle_time(&self) -> i64 {
return self.base.borrow().get_idle_time();
}
}

8
r9_macro/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "r9_macro"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

15
r9_macro/src/lib.rs Normal file
View File

@ -0,0 +1,15 @@
use std::rc::Rc;
use std::cell::RefCell;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
pub trait SharedFromSelf {
fn shared_from_self(&self) -> Rc::<RefCell::<Self>>;
}

View File

@ -0,0 +1,13 @@
[package]
name = "r9_macro_derive"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
proc-macro = true
[dependencies]
syn = "1.0"
quote = "1.0"

View File

@ -0,0 +1,33 @@
use proc_macro::TokenStream;
use quote::quote;
use syn;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
#[proc_macro_derive(SharedFromSelf, attributes(helper))]
pub fn shared_from_self_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
// Build the trait implementation
impl_shared_from_self_macro(&ast)
}
fn impl_shared_from_self_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let (impl_generics, _, _) = ast.generics.split_for_impl();
let gen = quote! {
impl #impl_generics SharedFromSelf for #name #impl_generics {
fn shared_from_self(&self) -> Rc::<RefCell::<Self>> {
return self._self_wp.upgrade().unwrap();
}
}
};
gen.into()
}