add comments to project rs

This commit is contained in:
David Westgate 2024-12-11 10:31:22 -08:00
parent fba6ccacdb
commit aac7065340

View File

@ -16,6 +16,7 @@ use microbit::{
gpio::{p0::P0_05, Floating, Input},
saadc::SaadcConfig,
timer::Timer,
timer::Timer,
Saadc,
},
pac::TIMER1,
@ -27,9 +28,8 @@ use ordered_float::NotNan;
use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};
// Microbit-spectrum
// Microbit-spectrum ported constants
const FFT_WIDTH: usize = 64;
/// ADC is signed; it is configured for a sample precision of 10 bits. Must match below.
const ADC_MAX: usize = 512;
const NOTES: [&str; 12] = [
@ -96,6 +96,7 @@ const NOTE_IMAGE_BITMAPS: [[[u8; 5]; 5]; 7] = [
],
];
// Read FFT-WIDTH numer of samples from the saadc channel. Collect these sequentially and return the array
fn read_samples(saadc: &mut Saadc, mic_in: &mut P0_05<Input<Floating>>) -> [i16; FFT_WIDTH] {
let mut result = [0; FFT_WIDTH];
for r in &mut result {
@ -104,7 +105,7 @@ fn read_samples(saadc: &mut Saadc, mic_in: &mut P0_05<Input<Floating>>) -> [i16;
result
}
// Microbit-spectrum - read function
// Adapted from Microbit-spectrum - `fn spectrum`
fn samples_to_freq(
samples: [i16; FFT_WIDTH],
window: [f32; FFT_WIDTH],
@ -133,11 +134,15 @@ fn samples_to_freq(
*freqs
}
// When complete, getting the dominant frequency should be about as follows:
// 1) window the data in a sensible way
// 2) Take an FFT of the window
// 3) Identify the fft frequency with the largest magnitude in the window and return it
fn get_dominant_freq(_freqs: [Complex<f32>; FFT_WIDTH / 2]) -> f32 {
// todo!();
0.0
}
// Simple frequencry to rounded note/octave value
fn get_note(freq: f32) -> (usize, f32) {
if freq > 0.0 {
let note_num = log2f(freq / 440.0) * 12.0;
@ -148,6 +153,7 @@ fn get_note(freq: f32) -> (usize, f32) {
(0, 0.0)
}
// Return the pre-defined base image, which matches the offset of the node
fn get_image(noctive: (usize, f32)) -> [[u8; 5]; 5] {
let (note, _octive) = noctive; // TODO - Add octive and flat modifier to display
if note > 0 {
@ -171,7 +177,7 @@ fn init() -> ! {
let _ = mic_run.set_high();
let saadc_config: SaadcConfig = SaadcConfig::default();
let saadc_config: SaadcConfig = SaadcConfig::default(); // Testing may show that more explicit Saadc configuration is needed
let mut saadc: Saadc = Saadc::new(board.ADC, saadc_config);
@ -186,10 +192,10 @@ fn init() -> ! {
let samples: [i16; FFT_WIDTH] = read_samples(&mut saadc, &mut mic_in);
let freqs: [Complex<f32>; FFT_WIDTH / 2] = samples_to_freq(samples, window);
let dominant_freq: f32 = get_dominant_freq(freqs);
let noctive: (usize, f32) = get_note(dominant_freq); //note + octive tuple
let noctive: (usize, f32) = get_note(dominant_freq); //noctive = note + octive tuple
let bitmap: [[u8; 5]; 5] = get_image(noctive);
DISPLAY.with_lock(|display: &mut Display<TIMER1>| {
display.show(&BitImage::new(&bitmap)); // XXX Not the most memory efficient - Improve
display.show(&BitImage::new(&bitmap)); // A better place can be found for the new constructor
});
rprintln!("{}", NOTES[noctive.0]);
timer.delay(400);