Initial working exampel of test console
This commit is contained in:
@ -1,81 +1,54 @@
|
||||
//use esp_idf_hal::peripherals::Peripherals;
|
||||
|
||||
use esp_idf_svc::timer::EspTaskTimerService;
|
||||
use core::time::Duration;
|
||||
use log::{info, error};
|
||||
use anyhow::Result;
|
||||
use async_executor::Executor;
|
||||
use futures_lite::future;
|
||||
|
||||
use esp32_nimble::{enums::*, uuid128, BLEAdvertisementData, BLEDevice, NimbleProperties};
|
||||
use esp_idf_hal::delay::FreeRtos;
|
||||
use esp_idf_sys as _;
|
||||
mod test_console;
|
||||
|
||||
fn main() {
|
||||
esp_idf_sys::link_patches();
|
||||
|
||||
// Bind the log crate to the ESP Logging facilities
|
||||
// Do basic initialization
|
||||
esp_idf_svc::sys::link_patches();
|
||||
esp_idf_svc::log::EspLogger::initialize_default();
|
||||
info!("Logging initialized");
|
||||
|
||||
// Take ownership of device
|
||||
let ble_device = BLEDevice::take();
|
||||
//let peripherals = Peripherals::take().unwrap();
|
||||
|
||||
// Obtain handle for peripheral advertiser
|
||||
let ble_advertiser = ble_device.get_advertising();
|
||||
match future::block_on(main_loop()) {
|
||||
Ok(_) => {error!("Exited main loop normally, but this should be impossible.")}
|
||||
Err(e) => {error!("Exited main loop with error {}", e)}
|
||||
};
|
||||
|
||||
// Configure Device Security
|
||||
ble_device
|
||||
.security()
|
||||
.set_auth(AuthReq::all())
|
||||
.set_passkey(123456)
|
||||
.set_io_cap(SecurityIOCap::DisplayOnly)
|
||||
.resolve_rpa();
|
||||
}
|
||||
|
||||
// Obtain handle for server
|
||||
let server = ble_device.get_server();
|
||||
|
||||
// Define server connect behaviour
|
||||
server.on_connect(|server, clntdesc| {
|
||||
// Print connected client data
|
||||
println!("{:?}", clntdesc);
|
||||
// Update connection parameters
|
||||
server
|
||||
.update_conn_params(clntdesc.conn_handle(), 24, 48, 0, 60)
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
// Define server disconnect behaviour
|
||||
server.on_disconnect(|_desc, _reason| {
|
||||
println!("Disconnected, back to advertising");
|
||||
});
|
||||
|
||||
// Create a service with custom UUID
|
||||
let my_service = server.create_service(uuid128!("9b574847-f706-436c-bed7-fc01eb0965c1"));
|
||||
|
||||
// Create a characteristic to associate with created service
|
||||
let my_service_characteristic = my_service.lock().create_characteristic(
|
||||
uuid128!("681285a6-247f-48c6-80ad-68c3dce18585"),
|
||||
NimbleProperties::READ | NimbleProperties::READ_ENC,
|
||||
);
|
||||
|
||||
// Modify characteristic value
|
||||
my_service_characteristic.lock().set_value(b"Start Value");
|
||||
|
||||
// Configure Advertiser Data
|
||||
ble_advertiser
|
||||
.lock()
|
||||
.set_data(
|
||||
BLEAdvertisementData::new()
|
||||
.name("ESP32 Server")
|
||||
.add_service_uuid(uuid128!("9b574847-f706-436c-bed7-fc01eb0965c1")),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Start Advertising
|
||||
ble_advertiser.lock().start().unwrap();
|
||||
|
||||
// (Optional) Print dump of local GATT table
|
||||
// server.ble_gatts_show_local();
|
||||
|
||||
// Init a value to pass to characteristic
|
||||
let mut val = 0;
|
||||
async fn test_loop() -> Result<()> {
|
||||
let timer_service = EspTaskTimerService::new()?;
|
||||
|
||||
let mut async_timer = timer_service.timer_async()?;
|
||||
loop {
|
||||
FreeRtos::delay_ms(1000);
|
||||
my_service_characteristic.lock().set_value(&[val]).notify();
|
||||
val = val.wrapping_add(1);
|
||||
async_timer.after(Duration::from_secs(2)).await?;
|
||||
}
|
||||
}
|
||||
|
||||
async fn main_loop() -> Result<()> {
|
||||
info!("Entering main loop");
|
||||
|
||||
let executor = Executor::new();
|
||||
let mut tasks:Vec<_> = Vec::new();
|
||||
|
||||
//Queueu up our async tasks
|
||||
tasks.push(executor.spawn(test_console::start_cli()));
|
||||
tasks.push(executor.spawn(test_loop()));
|
||||
|
||||
//Once we have all our tasks, await on them all to run them in parallel.
|
||||
for task in tasks {
|
||||
executor.run(task).await?;
|
||||
}
|
||||
|
||||
info!("exiting main loop");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user