Compare commits

...

3 commits

Author SHA1 Message Date
Patrick Moessler
3cb258c5c3 more impl 2025-03-18 20:06:39 +01:00
Patrick Moessler
417fe24d07 use bytemuch const generic 2025-03-18 20:06:29 +01:00
Patrick Moessler
e4dfba6180 usr 240mhz 2025-03-18 20:06:20 +01:00
3 changed files with 110 additions and 36 deletions

View file

@ -26,7 +26,7 @@ experimental = ["esp-idf-svc/experimental"]
log = "0.4"
esp-idf-svc = { version = "0.51", features = ["critical-section", "embassy-time-driver", "embassy-sync"] }
anyhow = "1.0.97"
bytemuck = { version="1.22.0", features = ["derive"] }
bytemuck = { version="1.22.0", features = ["derive", "min_const_generics"] }
[build-dependencies]
embuild = "0.33"

View file

@ -1,5 +1,5 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000
CONFIG_ESP_MAIN_TASK_STACK_SIZE=40000
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
# This allows to use 1 ms granularity for thread sleeps (10 ms by default).
@ -8,3 +8,6 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000
# Workaround for https://github.com/espressif/esp-idf/issues/7631
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
CONFIG_DSP_MAX_FFT_SIZE_1024=y

View file

@ -1,11 +1,11 @@
use bytemuck::{bytes_of, bytes_of_mut, Pod, Zeroable};
use esp_idf_svc::{
hal::{gpio::AnyIOPin, i2s, peripherals::Peripherals, spi, units::FromValueType},
sys::TickType_t,
hal::{
delay::FreeRtos, gpio::AnyIOPin, i2s, peripherals::Peripherals, spi, units::FromValueType,
},
sys::{esp_dsp, esp_nofail, esp_random, TickType_t},
};
use esp_idf_svc::sys::esp_dsp;
use anyhow::{bail, Result};
const LED_COUNT: usize = 72;
@ -27,36 +27,65 @@ struct Rgbv {
}
impl Rgbv {
const _O_ONES: u8 = 0xE0;
#[rustfmt::skip] const fn black(o: u8) -> Self { assert!(o<=Self::MAX_O); Self {r: 0x00, g: 0x00, b: 0x00, _o: Self::_O_ONES | o } }
#[rustfmt::skip] const fn white(o: u8) -> Self { assert!(o<=Self::MAX_O); Self {r: 0xFF, g: 0xFF, b: 0xFF, _o: Self::_O_ONES | o } }
#[rustfmt::skip] const fn red(o: u8) -> Self { assert!(o<=Self::MAX_O); Self {r: 0xFF, g: 0x00, b: 0x00, _o: Self::_O_ONES | o } }
#[rustfmt::skip] const fn green(o: u8) -> Self { assert!(o<=Self::MAX_O); Self {r: 0x00, g: 0xFF, b: 0x00, _o: Self::_O_ONES | o } }
#[rustfmt::skip] const fn blue(o: u8) -> Self { assert!(o<=Self::MAX_O); Self {r: 0x00, g: 0x00, b: 0xFF, _o: Self::_O_ONES | o } }
#[rustfmt::skip] const fn cyan(o: u8) -> Self { assert!(o<=Self::MAX_O); Self {r: 0x00, g: 0xFF, b: 0xFF, _o: Self::_O_ONES | o } }
#[rustfmt::skip] const fn orange(o: u8) -> Self { assert!(o<=Self::MAX_O); Self {r: 0xFF, g: 0x80, b: 0x00, _o: Self::_O_ONES | o } }
#[rustfmt::skip] const fn yellow(o: u8) -> Self { assert!(o<=Self::MAX_O); Self {r: 0xFF, g: 0xFF, b: 0x00, _o: Self::_O_ONES | o } }
#[rustfmt::skip] const fn pink(o: u8) -> Self { assert!(o<=Self::MAX_O); Self {r: 0xFF, g: 0x00, b: 0xFF, _o: Self::_O_ONES | o } }
const MAX_O: u8 = 31;
pub fn new(r: u8, g: u8, b: u8, o: u8) -> Self {
assert!(o <= Self::MAX_O);
Self {
r,
g,
b,
_o: o | 0xE0,
_o: o | Self::_O_ONES,
}
}
pub fn o(self) -> u8 {
self._o & !0xE0
self._o & !Self::_O_ONES
}
#[inline(always)]
pub fn set_o(mut self, o: u8) -> Self {
self._o = o | 0xE0;
assert!(o <= Self::MAX_O);
self._o = o | Self::_O_ONES;
self
}
#[inline(always)]
pub fn increase(mut self, r: u8, g: u8, b: u8, o: u8) -> Self {
self.r = self.r.saturating_add(r);
self.g = self.g.saturating_add(g);
self.b = self.b.saturating_add(b);
self.set_o(std::cmp::min(self.o() + o, Self::MAX_O));
self
}
#[inline(always)]
pub fn decrease(mut self, r: u8, g: u8, b: u8, o: u8) -> Self {
self.r -= r;
self.g -= g;
self.b -= b;
self.set_o(self.o() - o);
self.r = self.r.saturating_sub(r);
self.g = self.g.saturating_sub(g);
self.b = self.b.saturating_sub(b);
self.set_o(self.o().saturating_sub(o));
self
}
/// Converts hue, saturation, value to RGB // copied from rmt_neopixel example
/// Converts hue, saturation, value to RGB
/// // copied from rmt_neopixel example
pub fn from_hsv(h: u32, s: u32, v: u32, o: u8) -> Result<Self> {
assert!(o <= Self::MAX_O);
if h > 360 || s > 100 || v > 100 {
bail!("The given HSV values are not in valid range");
}
@ -77,7 +106,7 @@ impl Rgbv {
r: ((r + m) * 255.0) as u8,
g: ((g + m) * 255.0) as u8,
b: ((b + m) * 255.0) as u8,
_o: o | 0xE0,
_o: o | Self::_O_ONES,
})
}
}
@ -109,6 +138,31 @@ fn falloff_f(old: f32, new: f32) -> f32 {
(old / 2.0f32) + (old / 4.0f32) + (new / 4.0f32)
}
fn random_at_most(max: u32) -> u32 {
// impl from https://stackoverflow.com/a/6852396, adapted to uint32/2
// Assumes 0 <= max <= INT32_MAX
// Returns in the closed interval [0, max]
assert!(max < u32::MAX);
let num_bins = max + 1;
let num_rand = i32::MAX as u32 + 1;
let bin_size = num_rand / num_bins;
let defect = num_rand % num_bins;
let mut x: u32;
loop {
unsafe {
x = esp_random() >> 1; // This is carefully written not to overflow
if num_rand - defect > x {
break;
}
}
}
// Truncated division is intentional
x / bin_size
}
struct AudioProcessor {
floating_max: i32,
current_powers: [f32; AUDIO_BANDS],
@ -164,7 +218,7 @@ impl AudioProcessor {
/* do fft */
let half_sample_count = (AUDIO_SAMPLES_PER_BUF / 2) as i32;
unsafe {
esp_dsp::dsps_mul_f32_ae32(
esp_nofail!(esp_dsp::dsps_mul_f32_ae32(
proc_fft_buffer.as_ptr(),
self.fft_window.as_ptr(),
proc_fft_buffer.as_mut_ptr(),
@ -172,20 +226,23 @@ impl AudioProcessor {
1,
1,
1,
);
));
esp_dsp::dsps_fft2r_fc32_aes3_(
esp_nofail!(esp_dsp::dsps_fft2r_fc32_aes3_(
proc_fft_buffer.as_mut_ptr(),
half_sample_count,
esp_dsp::dsps_fft_w_table_fc32,
); // operating on half length but complex
esp_dsp::dsps_bit_rev2r_fc32(proc_fft_buffer.as_mut_ptr(), half_sample_count); // operating on half length but complex
esp_dsp::dsps_cplx2real_fc32_ae32_(
)); // operating on half length but complex
esp_nofail!(esp_dsp::dsps_bit_rev2r_fc32(
proc_fft_buffer.as_mut_ptr(),
half_sample_count
)); // operating on half length but complex
esp_nofail!(esp_dsp::dsps_cplx2real_fc32_ae32_(
proc_fft_buffer.as_mut_ptr(),
half_sample_count,
esp_dsp::dsps_fft_w_table_fc32,
esp_dsp::dsps_fft_w_table_size,
); // operating on half length but complex
)); // operating on half length but complex
for i in 0..half_sample_count as usize {
proc_fft_buffer[i] = (proc_fft_buffer[i * 2] * proc_fft_buffer[i * 2]
@ -229,12 +286,13 @@ impl LedEffect for LedEffectBassSparks {
bass_color.decrease(3, 5, 5, 0);
if processed.floating_max > 10100000
if true /*processed.floating_max > 10100000
&& (processed.current_powers[1] > 1.35 * processed.avg_powers[1])
&& (processed.current_powers[2] > 1.35 * processed.avg_powers[2])
&& (processed.current_powers[2] > 1.35 * processed.avg_powers[2])*/
{
for i in 0..10 {
let led_index = 0;
for _ in 0..10 {
let led_index = random_at_most(LED_COUNT as u32 - 1) as usize;
leds[led_index] = Rgbv::white(31);
}
}
}
@ -308,21 +366,27 @@ fn main() -> anyhow::Result<()> {
)?;
unsafe {
let esp_err =
esp_dsp::dsps_fft2r_init_fc32(std::ptr::null_mut(), (AUDIO_SAMPLES_PER_BUF / 2) as i32);
if esp_err != esp_dsp::ESP_OK {
log::error!("fft2 failed to init")
};
let esp_err =
esp_dsp::dsps_fft4r_init_fc32(std::ptr::null_mut(), (AUDIO_SAMPLES_PER_BUF / 2) as i32);
if esp_err != esp_dsp::ESP_OK {
log::error!("fft4 failed to init")
};
esp_nofail!(esp_dsp::dsps_fft2r_init_fc32(
std::ptr::null_mut(),
(AUDIO_SAMPLES_PER_BUF / 2) as i32
));
esp_nofail!(esp_dsp::dsps_fft4r_init_fc32(
std::ptr::null_mut(),
(AUDIO_SAMPLES_PER_BUF / 2) as i32
));
}
let mut processor = AudioProcessor::new();
let mut effect = LedEffectBassSparks {};
// loop {
// leds.leds[0] = Rgbv::red(4);
// let output_buffer = bytes_of(&leds);
// led_drv.write(output_buffer)?;
// FreeRtos::delay_ms(10);
// }
mic_drv.rx_enable()?;
loop {
// let buffer: &mut [u8; AUDIO_SAMPLES_PER_BUF*4] = cast_slice_mut(&mut audio[next_audio_buf]);
@ -333,11 +397,18 @@ fn main() -> anyhow::Result<()> {
log::error!("buffer underflow");
}
// log::info!("a: {:08x}", audio[next_audio_buf][0]);
let current_fft_buf = processor.process(&audio[next_audio_buf]);
effect.render(&processor, current_fft_buf, &leds.leds);
let output_buffer = bytes_of(&leds);
led_drv.write(output_buffer)?;
next_audio_buf = (next_audio_buf + 1) % AUDIO_BUFFERS;
FreeRtos::delay_ms(10);
}
}