Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

[WIP] Adding a tokio example #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "rust/tokio"]
path = rust/tokio
url = https://github.com/TilCreator/tokio.git
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ esp-idf-sys = {git = "https://github.com/ivmarkov/esp-idf-sys.git"}
#esp-idf-sys = {path = "../../esp-idf-sys"}
rocket = "0.4.6"
indexmap = "=1.2" # Rocket 0.4.6 fails to compile with later versions...
tokio = { features = [ "rt", "rt-multi-thread", "time", "macros" ], path = "tokio/tokio" }

[patch.crates-io]
time = { git = 'https://github.com/ivmarkov/time.git', branch = 'master' }
Expand Down
45 changes: 44 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;

use std::time::*;
use std::thread;

use std::ffi::{CString};
use esp_idf_sys::*;

use tokio::{
join,
runtime::Runtime,
time::{sleep, Duration},
};

mod wip;

#[no_mangle]
Expand All @@ -15,6 +20,8 @@ pub extern "C" fn main() {

threads_playground();

tokio_playground();

// Enough playing. Start WiFi and ignite the Rocket framework

if let Err(error) = init_peripherals() {
Expand Down Expand Up @@ -68,6 +75,42 @@ fn threads_playground() {
println!("Joins were successful.");
}

fn tokio_playground() {
Runtime::new().unwrap().block_on(async {
join!(
async { // Jessie
println!("Jessie: Prepare for trouble");
sleep(Duration::from_secs_f64(1.0)).await;
println!("Jessie: To protect the world from devastation!");
sleep(Duration::from_secs_f64(1.0)).await;
println!("Jessie: To denounce the evils of truth and love!");
sleep(Duration::from_secs_f64(1.0)).await;
println!("Jessie: Jessie!");
sleep(Duration::from_secs_f64(1.0)).await;
println!("Jessie: Team Rocket blasts off at the speed of light!");
},
async { // James
sleep(Duration::from_secs_f64(0.5)).await;
println!("James: And make it double");
sleep(Duration::from_secs_f64(1.0)).await;
println!("James: To unite all peoples within our nation!");
sleep(Duration::from_secs_f64(1.0)).await;
println!("James: To extend our reach to the stars above!");
sleep(Duration::from_secs_f64(1.0)).await;
println!("James: James!");
sleep(Duration::from_secs_f64(1.0)).await;
println!("James: Surrender now, or prepare to fight!");
},
async { // Meowth
sleep(Duration::from_secs_f64(5.5)).await;
println!("Meowth: Meowth!");
sleep(Duration::from_secs_f64(0.5)).await;
println!("Meowth: That's right!");
},
)
});
}

// NOTE:
// This is currently one big unsafe blob, relying on the C ESP-IDF API (exposed via the esp-idf-sys crate).
//
Expand Down
1 change: 1 addition & 0 deletions rust/tokio
Submodule tokio added at 5ae98a