Remove trouble example, update edge-net
This commit is contained in:
parent
9084c5f195
commit
d44c012502
@ -43,13 +43,12 @@ sha2 = { version = "0.10.8", default-features = false }
|
||||
smoltcp = { version = "0.12.0", default-features = false, features = [ "medium-ethernet", "socket-raw"] }
|
||||
embedded-time = "=0.12.1"
|
||||
static_cell = { version = "2.1.0", features = ["nightly"] }
|
||||
trouble-host = { git = "https://github.com/embassy-rs/trouble", package = "trouble-host", rev = "4f1114ce58e96fe54f5ed7e726f66e1ad8d9ce54", features = [ "log", "gatt" ] }
|
||||
usb-device = "0.3.2"
|
||||
usbd-serial = "0.2.2"
|
||||
edge-dhcp = "0.3.0"
|
||||
edge-raw = "0.3.0"
|
||||
edge-nal = "0.3.0"
|
||||
edge-nal-embassy = "0.3.0"
|
||||
edge-dhcp = { version = "0.5.0", git = "https://github.com/bugadani/edge-net", rev = "b72678e15bb7c6e72809df235778bb7b48ac146d" }
|
||||
edge-raw = { version = "0.5.0", git = "https://github.com/bugadani/edge-net", rev = "b72678e15bb7c6e72809df235778bb7b48ac146d" }
|
||||
edge-nal = { version = "0.5.0", git = "https://github.com/bugadani/edge-net", rev = "b72678e15bb7c6e72809df235778bb7b48ac146d" }
|
||||
edge-nal-embassy = { version = "0.5.0", git = "https://github.com/bugadani/edge-net", rev = "b72678e15bb7c6e72809df235778bb7b48ac146d" }
|
||||
|
||||
[features]
|
||||
esp32 = ["esp-hal/esp32", "esp-backtrace/esp32", "esp-hal-embassy?/esp32", "esp-println/esp32", "esp-storage?/esp32", "esp-wifi?/esp32"]
|
||||
@ -72,8 +71,3 @@ incremental = false
|
||||
opt-level = 3
|
||||
lto = 'fat'
|
||||
overflow-checks = false
|
||||
|
||||
[patch.crates-io]
|
||||
# Patch until https://github.com/ivmarkov/edge-net/pull/50 is merged
|
||||
edge-nal = { git = "https://github.com/ivmarkov/edge-net", rev = "5a85bcc8b8726e8b7044e9526f01cdba1fd684da"}
|
||||
edge-nal-embassy = { git = "https://github.com/ivmarkov/edge-net", rev = "5a85bcc8b8726e8b7044e9526f01cdba1fd684da"}
|
||||
|
||||
@ -1,169 +0,0 @@
|
||||
//! Embassy BLE Example using Trouble
|
||||
//!
|
||||
//! - starts Bluetooth advertising
|
||||
//! - offers a GATT service providing a battery percentage reading
|
||||
//! - automatically notifies subscribers every second
|
||||
//!
|
||||
|
||||
//% FEATURES: embassy esp-wifi esp-wifi/ble esp-hal/unstable
|
||||
//% CHIPS: esp32 esp32s3 esp32c2 esp32c3 esp32c6 esp32h2
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use bt_hci::controller::ExternalController;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_futures::join::join3;
|
||||
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use esp_alloc as _;
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::{clock::CpuClock, rng::Rng, timer::timg::TimerGroup};
|
||||
use esp_wifi::{ble::controller::BleConnector, init, EspWifiController};
|
||||
use log::*;
|
||||
use static_cell::StaticCell;
|
||||
use trouble_host::{
|
||||
advertise::{AdStructure, Advertisement, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE},
|
||||
attribute::{AttributeTable, CharacteristicProp, Service, Uuid},
|
||||
Address,
|
||||
BleHost,
|
||||
BleHostResources,
|
||||
PacketQos,
|
||||
};
|
||||
|
||||
// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
|
||||
macro_rules! mk_static {
|
||||
($t:ty,$val:expr) => {{
|
||||
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
|
||||
#[deny(unused_attributes)]
|
||||
let x = STATIC_CELL.uninit().write(($val));
|
||||
x
|
||||
}};
|
||||
}
|
||||
|
||||
#[esp_hal_embassy::main]
|
||||
async fn main(_s: Spawner) {
|
||||
esp_println::logger::init_logger_from_env();
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
esp_alloc::heap_allocator!(72 * 1024);
|
||||
|
||||
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
||||
|
||||
let init = &*mk_static!(
|
||||
EspWifiController<'static>,
|
||||
init(
|
||||
timg0.timer0,
|
||||
Rng::new(peripherals.RNG),
|
||||
peripherals.RADIO_CLK,
|
||||
)
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
#[cfg(feature = "esp32")]
|
||||
{
|
||||
let timg1 = TimerGroup::new(peripherals.TIMG1);
|
||||
esp_hal_embassy::init(timg1.timer0);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "esp32"))]
|
||||
{
|
||||
let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER);
|
||||
esp_hal_embassy::init(systimer.alarm0);
|
||||
}
|
||||
|
||||
let mut bluetooth = peripherals.BT;
|
||||
let connector = BleConnector::new(&init, &mut bluetooth);
|
||||
let controller: ExternalController<_, 20> = ExternalController::new(connector);
|
||||
|
||||
static HOST_RESOURCES: StaticCell<BleHostResources<8, 8, 256>> = StaticCell::new();
|
||||
let host_resources = HOST_RESOURCES.init(BleHostResources::new(PacketQos::None));
|
||||
|
||||
let mut ble: BleHost<'_, _> = BleHost::new(controller, host_resources);
|
||||
|
||||
ble.set_random_address(Address::random([0xff, 0x9f, 0x1a, 0x05, 0xe4, 0xff]));
|
||||
let mut table: AttributeTable<'_, NoopRawMutex, 10> = AttributeTable::new();
|
||||
|
||||
let id = b"Trouble ESP32";
|
||||
let appearance = [0x80, 0x07];
|
||||
let mut bat_level = [0; 1];
|
||||
// Generic Access Service (mandatory)
|
||||
let mut svc = table.add_service(Service::new(0x1800));
|
||||
let _ = svc.add_characteristic_ro(0x2a00, id);
|
||||
let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
|
||||
svc.build();
|
||||
|
||||
// Generic attribute service (mandatory)
|
||||
table.add_service(Service::new(0x1801));
|
||||
|
||||
// Battery service
|
||||
let bat_level_handle = table.add_service(Service::new(0x180f)).add_characteristic(
|
||||
0x2a19,
|
||||
&[CharacteristicProp::Read, CharacteristicProp::Notify],
|
||||
&mut bat_level,
|
||||
);
|
||||
|
||||
let mut adv_data = [0; 31];
|
||||
AdStructure::encode_slice(
|
||||
&[
|
||||
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
|
||||
AdStructure::ServiceUuids16(&[Uuid::Uuid16([0x0f, 0x18])]),
|
||||
AdStructure::CompleteLocalName(b"Trouble ESP32"),
|
||||
],
|
||||
&mut adv_data[..],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let server = ble.gatt_server::<NoopRawMutex, 10, 256>(&table);
|
||||
|
||||
info!("Starting advertising and GATT service");
|
||||
// Run all 3 tasks in a join. They can also be separate embassy tasks.
|
||||
let _ = join3(
|
||||
// Runs the BLE host task
|
||||
ble.run(),
|
||||
// Processing events from GATT server (if an attribute was written)
|
||||
async {
|
||||
loop {
|
||||
match server.next().await {
|
||||
Ok(_event) => {
|
||||
info!("Gatt event!");
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Error processing GATT events: {:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
// Advertise our presence to the world.
|
||||
async {
|
||||
loop {
|
||||
let mut advertiser = ble
|
||||
.advertise(
|
||||
&Default::default(),
|
||||
Advertisement::ConnectableScannableUndirected {
|
||||
adv_data: &adv_data[..],
|
||||
scan_data: &[],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let conn = advertiser.accept().await.unwrap();
|
||||
// Keep connection alive and notify with value change
|
||||
let mut tick: u8 = 0;
|
||||
loop {
|
||||
if !conn.is_connected() {
|
||||
break;
|
||||
}
|
||||
Timer::after(Duration::from_secs(1)).await;
|
||||
tick = tick.wrapping_add(1);
|
||||
server
|
||||
.notify(&ble, bat_level_handle, &conn, &[tick])
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user