This commit is contained in:
Robert Schauklies 2026-01-15 23:01:35 +01:00
parent 7791c55969
commit 5f9e718f86
6 changed files with 177 additions and 62 deletions

View file

@ -2,7 +2,7 @@
use esp_hal::{
delay::Delay,
gpio::Level,
rmt::{PulseCode, Tx,Rx,TxChannelCreator,Channel},
rmt::{Channel, PulseCode, Rx, Tx, TxChannelCreator},
};
use num_traits::float::FloatCore;
@ -112,19 +112,18 @@ impl DShotSpeed {
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct DShot<'a> {
rx_channel:&'a Channel<'a,esp_hal::Blocking,esp_hal::rmt::Rx>,
tx_channel:&'a Channel<'a,esp_hal::Blocking,esp_hal::rmt::Tx>,
rx_channel: &'a mut Channel<'static, esp_hal::Blocking, esp_hal::rmt::Rx>,
tx_channel: &'a mut Channel<'static, esp_hal::Blocking, esp_hal::rmt::Tx>,
speed: DShotSpeed,
bit_ticks: BitTicks,
}
impl<'a> DShot<'a> {
pub fn new(
rx_channel:&'a Channel<'a,esp_hal::Blocking,esp_hal::rmt::Rx>,
tx_channel: &'a Channel<'a,esp_hal::Blocking,esp_hal::rmt::Tx>,
rx_channel: &'a mut Channel<'static, esp_hal::Blocking, esp_hal::rmt::Rx>,
tx_channel: &'a mut Channel<'static, esp_hal::Blocking, esp_hal::rmt::Tx>,
speed: DShotSpeed,
clk_speed: Option<u32>,
clk_divider: Option<u8>,
@ -134,7 +133,8 @@ impl<'a> DShot<'a> {
let bit_ticks = BitTicks::from_clk(clk_speed, clk_divider, speed.bit_times());
Self {
rx_channel: rx_channel,tx_channel: tx_channel,
rx_channel: rx_channel,
tx_channel: tx_channel,
speed,
bit_ticks,
}
@ -166,14 +166,16 @@ impl<'a> DShot<'a> {
self.bit_ticks.t1_h,
Level::Low,
self.bit_ticks.t1_l,
).into()
)
.into()
} else {
PulseCode::new(
Level::High,
self.bit_ticks.t0_h,
Level::Low,
self.bit_ticks.t0_l,
).into()
)
.into()
};
}
@ -182,15 +184,15 @@ impl<'a> DShot<'a> {
pulses
}
pub fn write_throttle(
&mut self,
throttle: u16,
telemetry: bool,
) -> Result<(), &'static str> {
pub fn write_throttle(&mut self, throttle: u16, telemetry: bool) -> Result<(), &'static str> {
let pulses = self.create_pulses(throttle, telemetry);
self.channel
let tx_chann = self.tx_channel.reborrow();
let transaction = tx_chann
.transmit(&pulses)
.map_err(|_| "Failed to send frame")?;
.unwrap_or(return Err("Failed to send frame"))
.wait()
.unwrap_or(return Err("Waiting failed!"));
Ok(())
}

View file

@ -7,7 +7,7 @@
)]
use core::fmt::Write;
use alloc::string::ToString;
use esp_hal::uart::{self, Config, Uart};
use esp_hal::uart::{Config, Uart};
use esp_hal::clock::CpuClock;
use esp_hal::delay::Delay;
@ -171,7 +171,6 @@ fn main() -> ! {
// pwm_pin.set_timestamp(1000);
// Timer::after_millis(1000).await;
// }
loop {
rprintln!("Hello world!");
delay.delay_millis(3000);