This commit is contained in:
2026-02-10 02:30:45 +02:00
parent cbfc8743f7
commit f55d8d4bd2
3 changed files with 570 additions and 70 deletions
+185 -38
View File
@@ -15,14 +15,17 @@ use defmt::info;
use embassy_executor::Spawner;
use embassy_futures::join::{join3, join4};
use embassy_nrf::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pull};
use embassy_nrf::gpio::{AnyPin, Input, OutputDrive, Pull};
use embassy_nrf::interrupt::Priority;
use embassy_nrf::pwm::{DutyCycle, Prescaler, SimplePwm};
use embassy_nrf::Peri;
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::mutex::Mutex;
use embassy_sync::signal::Signal;
use embassy_time::{with_timeout, Duration, Timer};
// use nrf_softdevice::{raw, Softdevice};
use nrf_softdevice::{raw, Softdevice};
use nrf_softdevice::ble::advertisement_builder::{Flag, LegacyAdvertisementBuilder, LegacyAdvertisementPayload, ServiceList, ServiceUuid16};
use nrf_softdevice::ble::{peripheral, gatt_server};
#[derive(Copy, Clone, Eq, PartialEq, Format)]
enum SysAction {
@@ -61,51 +64,196 @@ static SYS_ACTION: Mutex<ThreadModeRawMutex, SysAction> = Mutex::new(SysAction::
static TEXT_ANIMATION: Mutex<ThreadModeRawMutex, TextLightType> = Mutex::new(TextLightType::Hue);
static HEART_ANIMATION: Mutex<ThreadModeRawMutex, HeartLightType> = Mutex::new(HeartLightType::HeartBeat);
#[nrf_softdevice::gatt_service(uuid = "9e7312e0-2354-11eb-9f10-fbc30a62cf38")]
struct AnimationService {
#[characteristic(uuid = "9e7312e0-2354-11eb-9f10-fbc30a63cf38", read, write, notify, indicate)]
text_mode: u16,
#[characteristic(uuid = "9e7312e0-2354-11eb-9f10-fbc30a64cf38", read, write, notify, indicate)]
text_color: u16,
#[characteristic(uuid = "9e7312e0-2354-11eb-9f10-fbc30a65cf38", read, write, notify, indicate)]
heart_mode: u16,
#[characteristic(uuid = "9e7312e0-2354-11eb-9f10-fbc30a66cf38", read, write, notify, indicate)]
heart_color: u16,
}
#[nrf_softdevice::gatt_service(uuid = "180f")]
struct BatteryService {
#[characteristic(uuid = "2a19", read, notify)]
battery_level: u8,
}
#[nrf_softdevice::gatt_server]
struct Server {
bas: BatteryService,
light: AnimationService,
}
#[embassy_executor::main]
async fn main(spawner: Spawner) {
// let config = nrf_softdevice::Config {
// clock: Some(raw::nrf_clock_lf_cfg_t {
// source: raw::NRF_CLOCK_LF_SRC_RC as u8,
// rc_ctiv: 16,
// rc_temp_ctiv: 2,
// accuracy: raw::NRF_CLOCK_LF_ACCURACY_500_PPM as u8,
// }),
// conn_gap: Some(raw::ble_gap_conn_cfg_t {
// conn_count: 6,
// event_length: 24,
// }),
// conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 256 }),
// gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t {
// attr_tab_size: raw::BLE_GATTS_ATTR_TAB_SIZE_DEFAULT,
// }),
// gap_role_count: Some(raw::ble_gap_cfg_role_count_t {
// adv_set_count: 1,
// periph_role_count: 3,
// central_role_count: 3,
// central_sec_count: 0,
// _bitfield_1: raw::ble_gap_cfg_role_count_t::new_bitfield_1(0),
// }),
// gap_device_name: Some(raw::ble_gap_cfg_device_name_t {
// p_value: b"HelloRust" as *const u8 as _,
// current_len: 9,
// max_len: 9,
// write_perm: unsafe { mem::zeroed() },
// _bitfield_1: raw::ble_gap_cfg_device_name_t::new_bitfield_1(raw::BLE_GATTS_VLOC_STACK as u8),
// }),
// ..Default::default()
// };
//
// let _sd = Softdevice::enable(&config);
let mut config = embassy_nrf::config::Config::default();
config.gpiote_interrupt_priority = Priority::P2;
config.time_interrupt_priority = Priority::P2;
let p = embassy_nrf::init(config);
let config = nrf_softdevice::Config {
clock: Some(raw::nrf_clock_lf_cfg_t {
source: raw::NRF_CLOCK_LF_SRC_RC as u8,
rc_ctiv: 16,
rc_temp_ctiv: 2,
accuracy: raw::NRF_CLOCK_LF_ACCURACY_500_PPM as u8,
}),
conn_gap: Some(raw::ble_gap_conn_cfg_t {
conn_count: 6,
event_length: 24,
}),
conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 256 }),
gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t {
attr_tab_size: raw::BLE_GATTS_ATTR_TAB_SIZE_DEFAULT,
}),
gap_role_count: Some(raw::ble_gap_cfg_role_count_t {
adv_set_count: 1,
periph_role_count: 3,
central_role_count: 3,
central_sec_count: 0,
_bitfield_1: raw::ble_gap_cfg_role_count_t::new_bitfield_1(0),
}),
gap_device_name: Some(raw::ble_gap_cfg_device_name_t {
p_value: b"Odesa!" as *const u8 as _,
current_len: 9,
max_len: 9,
write_perm: unsafe { mem::zeroed() },
_bitfield_1: raw::ble_gap_cfg_device_name_t::new_bitfield_1(raw::BLE_GATTS_VLOC_STACK as u8),
}),
..Default::default()
};
let sd = Softdevice::enable(&config);
let server = Server::new(sd).expect("failed to enable softdevice");
let p = embassy_nrf::init(Default::default());
let pwm_text = SimplePwm::new_3ch(p.PWM0, p.P1_15, p.P1_13, p.P1_11, &Default::default());
let pwm_heart = SimplePwm::new_3ch(p.PWM2, p.P0_31, p.P0_29, p.P0_02, &Default::default());
spawner.spawn(radar_task()).expect("failed to spawn radar task");
spawner.spawn(touch_button(p.P0_20.into())).expect("failed to spawn touch task");
spawner.spawn(actions_task()).expect("failed to spawn actions task");
spawner.spawn(softdevice_task(sd)).expect("failed to spawn softdevice task");
join4(touch_button(p.P0_20.into()), moving_radar(p.P0_22.into()), heartbeat_task(pwm_heart), light_task(pwm_text)).await;
join4(gatt_task(server, sd), moving_radar(p.P0_22.into()), heartbeat_task(pwm_heart), light_task(pwm_text)).await;
}
async fn gatt_task(server: Server, sd: &'static Softdevice) {
static ADV_DATA: LegacyAdvertisementPayload = LegacyAdvertisementBuilder::new()
.flags(&[Flag::GeneralDiscovery, Flag::LE_Only])
.services_16(ServiceList::Complete, &[ServiceUuid16::BATTERY])
.full_name("Odesa!")
.build();
static SCAN_DATA: LegacyAdvertisementPayload = LegacyAdvertisementBuilder::new()
.services_128(
ServiceList::Complete,
&[0x9e7312e0_2354_11eb_9f10_fbc30a62cf38_u128.to_le_bytes()],
)
.build();
loop {
let config = peripheral::Config::default();
let adv = peripheral::ConnectableAdvertisement::ScannableUndirected {
adv_data: &ADV_DATA,
scan_data: &SCAN_DATA,
};
let conn = peripheral::advertise_connectable(sd, adv, &config).await.expect("failed to advertise");
info!("advertising done!");
// Run the GATT server on the connection. This returns when the connection gets disconnected.
//
// Event enums (ServerEvent's) are generated by nrf_softdevice::gatt_server
// proc macro when applied to the Server struct above
let e = gatt_server::run(&conn, &server, |e| match e {
ServerEvent::Bas(e) => match e {
BatteryServiceEvent::BatteryLevelCccdWrite { notifications } => {
info!("battery notifications: {}", notifications)
}
},
ServerEvent::Light(e) => match e {
AnimationServiceEvent::TextModeWrite(val) => {
let val = val % 3;
info!("wrote text mode: {}", val);
if let Err(e) = server.light.text_mode_notify(&conn, &(val)) {
info!("send notification error: {:?}", e);
}
let next = match val {
0 => TextLightType::Hue,
1 => TextLightType::ConstantColor,
2 => TextLightType::White,
_ => TextLightType::Hue,
};
TEXT_ANIMATION_SIGNAL.signal(next);
}
AnimationServiceEvent::TextColorWrite(val) => {
info!("wrote text color: {}", val);
if let Err(e) = server.light.text_color_notify(&conn, &(val)) {
info!("send notification error: {:?}", e);
}
}
AnimationServiceEvent::HeartModeWrite(val) => {
let val = val % 3;
info!("wrote heart mode: {}", val);
if let Err(e) = server.light.heart_mode_notify(&conn, &(val)) {
info!("send notification error: {:?}", e);
}
let next = match val {
0 => HeartLightType::HeartBeat,
1 => HeartLightType::Hue,
2 => HeartLightType::ConstantColor,
_ => HeartLightType::HeartBeat,
};
HEART_ANIMATION_SIGNAL.signal(next);
}
AnimationServiceEvent::HeartColorWrite(val) => {
info!("wrote heart color: {}", val);
if let Err(e) = server.light.heart_color_notify(&conn, &(val)) {
info!("send notification error: {:?}", e);
}
}
AnimationServiceEvent::TextModeCccdWrite {
indications,
notifications,
} => {
info!("text mode indications: {}, notifications: {}", indications, notifications)
}
AnimationServiceEvent::TextColorCccdWrite {
indications,
notifications,
} => {
info!("text color indications: {}, notifications: {}", indications, notifications)
}
AnimationServiceEvent::HeartModeCccdWrite {
indications,
notifications,
} => {
info!("text mode indications: {}, notifications: {}", indications, notifications)
}
AnimationServiceEvent::HeartColorCccdWrite {
indications,
notifications,
} => {
info!("text color indications: {}, notifications: {}", indications, notifications)
}
},
})
.await;
info!("gatt_server run exited with error: {:?}", e);
}
}
#[embassy_executor::task]
async fn softdevice_task(sd: &'static Softdevice) -> ! {
sd.run().await
}
#[embassy_executor::task]
async fn touch_button(pin: Peri<'static, AnyPin>) {
let mut button = Input::new(pin, Pull::Down);
@@ -368,7 +516,6 @@ fn hue_to_rgb(hue: u16, max: u16) -> (u16, u16, u16) {
async fn heartbeat_task(mut pwm: SimplePwm<'static>) {
const MAX: u16 = 16384;
const MIN: u16 = 2048;
pwm.set_max_duty(MAX);
pwm.set_prescaler(Prescaler::Div1);