use flipperzero_sys as sys;
use ufmt::derive::uDebug;
use crate::info;
use super::Bluetooth;
pub struct CarrierTx {
_service: Bluetooth,
}
impl Drop for CarrierTx {
fn drop(&mut self) {
self.stop();
}
}
impl CarrierTx {
pub fn prepare() -> Self {
let _service = Bluetooth::open();
unsafe { sys::furi_hal_bt_reinit() };
Self { _service }
}
pub fn start(&mut self, channel: u8, power: u8) -> Result<(), Error> {
if !(0..=39).contains(&channel) {
Err(Error::InvalidChannel)
} else if !(0..=6).contains(&power) {
Err(Error::InvalidPower)
} else {
unsafe { sys::furi_hal_bt_start_tone_tx(channel, 0x19 + power) };
Ok(())
}
}
pub fn stop(&mut self) {
unsafe { sys::furi_hal_bt_stop_tone_tx() };
}
}
pub struct PacketTx {
_service: Bluetooth,
}
impl PacketTx {
pub fn prepare() -> Self {
let _service = Bluetooth::open();
unsafe { sys::furi_hal_bt_reinit() };
Self { _service }
}
pub fn start(&mut self, channel: u8, pattern: Pattern, datarate: u8) -> Result<(), Error> {
if !(0..=39).contains(&channel) {
Err(Error::InvalidChannel)
} else if !(1..=2).contains(&datarate) {
Err(Error::InvalidDatarate)
} else {
unsafe { sys::furi_hal_bt_start_packet_tx(channel, pattern.to_raw(), datarate) };
Ok(())
}
}
pub fn stop(&mut self) -> u16 {
let sent_count = unsafe { sys::furi_hal_bt_stop_packet_test() };
info!("Sent {} packets", sent_count);
sent_count
}
}
pub struct PacketRx {
_service: Bluetooth,
}
impl Drop for PacketRx {
fn drop(&mut self) {
self.stop();
}
}
impl PacketRx {
pub fn prepare() -> Self {
let _service = Bluetooth::open();
unsafe { sys::furi_hal_bt_reinit() };
Self { _service }
}
pub fn start(&mut self, channel: u8, datarate: u8) -> Result<(), Error> {
if !(0..=39).contains(&channel) {
Err(Error::InvalidChannel)
} else if !(1..=2).contains(&datarate) {
Err(Error::InvalidDatarate)
} else {
unsafe { sys::furi_hal_bt_start_packet_rx(channel, datarate) };
Ok(())
}
}
pub fn rssi(&self) -> f32 {
unsafe { sys::furi_hal_bt_get_rssi() }
}
pub fn stop(&mut self) -> u16 {
let received_count = unsafe { sys::furi_hal_bt_stop_packet_test() };
info!("Received {} packets", received_count);
received_count
}
}
#[derive(Clone, Copy, Debug, uDebug, PartialEq, Eq)]
pub enum Pattern {
PseudoRandomBitSequence9,
AlternatingNibbles,
AlternatingBits,
PseudoRandomBitSequence15,
AllOnes,
AllZeros,
}
impl Pattern {
fn to_raw(self) -> u8 {
match self {
Pattern::PseudoRandomBitSequence9 => 0,
Pattern::AlternatingNibbles => 1,
Pattern::AlternatingBits => 2,
Pattern::PseudoRandomBitSequence15 => 3,
Pattern::AllOnes => 4,
Pattern::AllZeros => 5,
}
}
}
pub enum Error {
InvalidChannel,
InvalidDatarate,
InvalidPower,
}