Skip to content

Commit

Permalink
Remove redundant example, update esp-now examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed Sep 7, 2023
1 parent 2760456 commit 3557fec
Show file tree
Hide file tree
Showing 27 changed files with 317 additions and 1,159 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,11 @@ To build these ensure you are in the `examples-esp32XXX` directory matching your

`cargo run --example embassy_esp_now --release --features "async,esp-now"`

### esp_now_duplex

- broadcasts, sends and asynchronously receives messages via esp-now in multiple embassy tasks

`cargo run --example esp_now_duplex --release --features "async,esp-now"`

### esp_now_async_duplex
### embassy_esp_now_duplex

- asynchronously broadcasts, receives and sends messages via esp-now in multiple embassy tasks

`cargo run --example esp_now_async_duplex --release --features "async,esp-now"`
`cargo run --example embassy_esp_now_duplex --release --features "async,esp-now"`

### embassy_dhcp

Expand Down
3 changes: 1 addition & 2 deletions examples-esp32/examples/embassy_dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use hal::clock::ClockControl;
use hal::Rng;
use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup};

#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))]
use hal::system::SystemExt;


const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down
3 changes: 0 additions & 3 deletions examples-esp32/examples/embassy_esp_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ use hal::clock::ClockControl;
use hal::Rng;
use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup};

#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))]
use hal::system::SystemExt;

#[embassy_executor::task]
async fn run(mut esp_now: EspNow<'static>) {
let mut ticker = Ticker::every(Duration::from_secs(5));
Expand Down
129 changes: 129 additions & 0 deletions examples-esp32/examples/embassy_esp_now_duplex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use embassy_executor::_export::StaticCell;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::mutex::Mutex;
#[path = "../../examples-util/util.rs"]
mod examples_util;
use examples_util::hal;

use embassy_executor::Executor;
use embassy_time::{Duration, Ticker};
use esp_backtrace as _;

use esp_println::println;
use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS};
use esp_wifi::{initialize, EspWifiInitFor};
use hal::clock::ClockControl;
use hal::Rng;
use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup};

#[embassy_executor::task]
async fn broadcaster(sender: &'static Mutex<NoopRawMutex, EspNowSender<'static>>) {
let mut ticker = Ticker::every(Duration::from_secs(1));
loop {
ticker.next().await;

println!("Send Broadcast...");
let mut sender = sender.lock().await;
let status = sender.send_async(&BROADCAST_ADDRESS, b"Hello.").await;
println!("Send broadcast status: {:?}", status);
}
}

#[embassy_executor::task]
async fn sayhello(
manager: &'static EspNowManager<'static>,
sender: &'static Mutex<NoopRawMutex, EspNowSender<'static>>,
) {
let mut ticker = Ticker::every(Duration::from_millis(500));
loop {
ticker.next().await;
let peer = match manager.fetch_peer(false) {
Ok(peer) => peer,
Err(_) => {
if let Ok(peer) = manager.fetch_peer(true) {
peer
} else {
continue;
}
}
};

println!("Send hello to peer {:?}", peer.peer_address);
let mut sender = sender.lock().await;
let status = sender.send_async(&peer.peer_address, b"Hello Peer.").await;
println!("Send hello status: {:?}", status);
}
}

#[embassy_executor::task]
async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) {
loop {
let r = receiver.receive_async().await;
println!("Received {:?}", r.get_data());
if r.info.dst_address == BROADCAST_ADDRESS {
if !manager.peer_exists(&r.info.src_address) {
manager
.add_peer(PeerInfo {
peer_address: r.info.src_address,
lmk: None,
channel: None,
encrypt: false,
})
.unwrap();
println!("Added peer {:?}", r.info.src_address);
}
}
}
}

static EXECUTOR: StaticCell<Executor> = StaticCell::new();
static ESP_NOW_MANAGER: StaticCell<EspNowManager<'static>> = StaticCell::new();
static ESP_NOW_SENDER: StaticCell<Mutex<NoopRawMutex, EspNowSender<'static>>> = StaticCell::new();

#[entry]
fn main() -> ! {
#[cfg(feature = "log")]
esp_println::logger::init_logger(log::LevelFilter::Info);

let peripherals = Peripherals::take();

let mut system = peripherals.DPORT.split();
let clocks = ClockControl::max(system.clock_control).freeze();

let timer = hal::timer::TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
)
.timer0;
let init = initialize(
EspWifiInitFor::Wifi,
timer,
Rng::new(peripherals.RNG),
system.radio_clock_control,
&clocks,
)
.unwrap();

let (wifi, ..) = peripherals.RADIO.split();
let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap();
println!("esp-now version {}", esp_now.get_version().unwrap());

let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut system.peripheral_clock_control);
embassy::init(&clocks, timer_group0.timer0);
let executor = EXECUTOR.init(Executor::new());

let (manager, sender, receiver) = esp_now.split();
let manager = ESP_NOW_MANAGER.init(manager);
let sender: &'static _ = ESP_NOW_SENDER.init(Mutex::new(sender));

executor.run(|spawner| {
spawner.spawn(listener(manager, receiver)).ok();
spawner.spawn(broadcaster(sender)).ok();
spawner.spawn(sayhello(manager, sender)).ok();
})
}
138 changes: 0 additions & 138 deletions examples-esp32/examples/esp_now_duplex.rs

This file was deleted.

3 changes: 1 addition & 2 deletions examples-esp32c2/examples/embassy_dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use hal::clock::ClockControl;
use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup};
use hal::{systimer::SystemTimer, Rng};

#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))]
use hal::system::SystemExt;


const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down
3 changes: 1 addition & 2 deletions examples-esp32c2/examples/embassy_esp_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use hal::clock::ClockControl;
use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup};
use hal::{systimer::SystemTimer, Rng};

#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))]
use hal::system::SystemExt;


#[embassy_executor::task]
async fn run(mut esp_now: EspNow<'static>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use embassy_executor::_export::StaticCell;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::mutex::Mutex;
#[path = "../../examples-util/util.rs"]
mod examples_util;
use examples_util::hal;

use embassy_executor::Executor;
Expand All @@ -14,12 +16,11 @@ use esp_backtrace as _;
use esp_println::println;
use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS};
use esp_wifi::{initialize, EspWifiInitFor};
use hal::clock::{ClockControl, CpuClock};
use hal::clock::ClockControl;
use hal::Rng;
use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc};
use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, systimer::SystemTimer};


#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))]
use hal::system::SystemExt;

#[embassy_executor::task]
async fn broadcaster(sender: &'static Mutex<NoopRawMutex, EspNowSender<'static>>) {
Expand Down Expand Up @@ -92,12 +93,10 @@ fn main() -> ! {

let peripherals = Peripherals::take();

let system = examples_util::system!(peripherals);
let mut peripheral_clock_control = system.peripheral_clock_control;
let clocks = examples_util::clocks!(system);
examples_util::rtc!(peripherals);
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::max(system.clock_control).freeze();

let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control);
let timer = SystemTimer::new(peripherals.SYSTIMER).alarm0;
let init = initialize(
EspWifiInitFor::Wifi,
timer,
Expand All @@ -107,11 +106,11 @@ fn main() -> ! {
)
.unwrap();

let wifi = examples_util::get_wifi!(peripherals);
let (wifi, ..) = peripherals.RADIO.split();
let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap();
println!("esp-now version {}", esp_now.get_version().unwrap());

let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut system.peripheral_clock_control);
embassy::init(&clocks, timer_group0.timer0);
let executor = EXECUTOR.init(Executor::new());

Expand Down
Loading

0 comments on commit 3557fec

Please sign in to comment.