From 4fe57b7a7b87e642e4a70b80cda6645308d339ce Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 29 Oct 2023 14:23:03 +0800 Subject: [PATCH] 1 --- f9/Cargo.toml | 11 ++++ f9/src/app.rs | 88 ++++++++++++++++++++++++++++ f9/src/lib.rs | 14 +++++ f9/src/timer.rs | 115 +++++++++++++++++++++++++++++++++++++ r9_macro/Cargo.toml | 8 +++ r9_macro/src/lib.rs | 15 +++++ r9_macro_derive/Cargo.toml | 13 +++++ r9_macro_derive/src/lib.rs | 33 +++++++++++ 8 files changed, 297 insertions(+) create mode 100644 f9/Cargo.toml create mode 100644 f9/src/app.rs create mode 100644 f9/src/lib.rs create mode 100644 f9/src/timer.rs create mode 100644 r9_macro/Cargo.toml create mode 100644 r9_macro/src/lib.rs create mode 100644 r9_macro_derive/Cargo.toml create mode 100644 r9_macro_derive/src/lib.rs diff --git a/f9/Cargo.toml b/f9/Cargo.toml new file mode 100644 index 0000000..932e6a5 --- /dev/null +++ b/f9/Cargo.toml @@ -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" } diff --git a/f9/src/app.rs b/f9/src/app.rs new file mode 100644 index 0000000..853911d --- /dev/null +++ b/f9/src/app.rs @@ -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::>, +} + +impl App { + + pub fn instance() -> Rc::> { + static mut _INSTANCE: Option>> = 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; + } + +} diff --git a/f9/src/lib.rs b/f9/src/lib.rs new file mode 100644 index 0000000..bc762dd --- /dev/null +++ b/f9/src/lib.rs @@ -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; diff --git a/f9/src/timer.rs b/f9/src/timer.rs new file mode 100644 index 0000000..7b6b354 --- /dev/null +++ b/f9/src/timer.rs @@ -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::>, + _self_wp: Weak::>, +} + +impl Timer { + + pub fn instance() -> Rc::> { + static mut _INSTANCE: Option>> = 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>>) { + 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(); + } + +} diff --git a/r9_macro/Cargo.toml b/r9_macro/Cargo.toml new file mode 100644 index 0000000..ac12bc6 --- /dev/null +++ b/r9_macro/Cargo.toml @@ -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] diff --git a/r9_macro/src/lib.rs b/r9_macro/src/lib.rs new file mode 100644 index 0000000..d11b016 --- /dev/null +++ b/r9_macro/src/lib.rs @@ -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::>; +} diff --git a/r9_macro_derive/Cargo.toml b/r9_macro_derive/Cargo.toml new file mode 100644 index 0000000..cd7b344 --- /dev/null +++ b/r9_macro_derive/Cargo.toml @@ -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" \ No newline at end of file diff --git a/r9_macro_derive/src/lib.rs b/r9_macro_derive/src/lib.rs new file mode 100644 index 0000000..33b97b2 --- /dev/null +++ b/r9_macro_derive/src/lib.rs @@ -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::> { + return self._self_wp.upgrade().unwrap(); + } + } + }; + gen.into() +}