Browse Source

cleaned up the example a bit

master
Weird Constructor 1 year ago
parent
commit
0b813ec4fe
  1. 2
      README.md
  2. 5
      sd_card_spi/Cargo.toml
  3. 152
      sd_card_spi/src/main.rs

2
README.md

@ -5,3 +5,5 @@ A link to the resource / link collection I made: ![rp2040 Rust resources](resour
An overview of this repository:
- [8-Bit PWM DAC with an RC filter synthesizing sine and saw waveforms](pwm_dac_saw_sampling)
- [WS2812 on the Raspberry Pi Pico with Rust](ws2812_led_cube)
- [SD/MMC Card with FAT32 filesystem via SPI on Raspberry Pi Pico](sd_card_spi)

5
sd_card_spi/Cargo.toml

@ -2,7 +2,7 @@
authors = ["Weird Constructor"]
edition = "2018"
readme = "README.md"
name = "rp2040-ws2812-led-cube"
name = "rp2040-sd-card-spi"
version = "0.1.0"
resolver = "2"
@ -22,9 +22,6 @@ rp2040-hal = { git = "https://github.com/rp-rs/rp-hal", branch="main", features=
rp2040-boot2 = { git = "https://github.com/rp-rs/rp2040-boot2-rs", branch="main" }
pico = { git = "https://github.com/rp-rs/rp-hal.git", branch="main" }
smart-leds = "0.3.0"
ws2812-pio = { path = "ws2812-pio-rs" }
nb = "1.0"
[features]

152
sd_card_spi/src/main.rs

@ -14,88 +14,18 @@ use embedded_sdmmc;
use rp2040_hal as rphal;
use embedded_hal::adc::OneShot;
use pico::hal::{
pac,
clocks::{Clock, init_clocks_and_plls},
sio::Sio,
adc::Adc,
watchdog::Watchdog,
timer::Timer,
};
use pico::hal::pio::PIOExt;
//use rp2040_hal::pio::PIOExt;
use smart_leds::{brightness, SmartLedsWrite, RGB8};
use ws2812_pio::Ws2812;
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
const LEN : usize = 302;
struct MovingAvg {
buf: [u16; 30],
ptr: u16,
}
impl MovingAvg {
pub fn new() -> Self {
Self {
buf: [0xFFFF; 30],
ptr: 0,
}
}
pub fn next(&mut self, input: u16) -> u16 {
self.buf[self.ptr as usize] = input;
self.ptr = ((self.ptr as usize + 1) % self.buf.len()) as u16;
let mut sum : usize = 0;
for b in &self.buf {
sum += *b as usize;
}
(sum / self.buf.len()) as u16
}
}
pub fn hsv2rgb(hue: f32, sat: f32, val: f32) -> (f32, f32, f32) {
let c = val * sat;
let v = (hue / 60.0) % 2.0 - 1.0;
let v = if v < 0.0 { -v } else { v };
let x = c * (1.0 - v);
let m = val - c;
let (r_, g_, b_) =
if hue >= 0.0 && hue < 60.0 {
(c, x, 0.0)
} else if hue >= 60.0 && hue < 120.0 {
(x, c, 0.0)
} else if hue >= 120.0 && hue < 180.0 {
(0.0, c, x)
} else if hue >= 180.0 && hue < 240.0 {
(0.0, x, c)
} else if hue >= 240.0 && hue < 300.0 {
(x, 0.0, c)
} else { // if hue >= 300.0 && hue < 360.0 {
(c, 0.0, x)
};
// println!("in: h={}, s={}, v={}, r:{}, g:{}, b: {}", hue, sat, val,
// (r_ + m) * 255.0,
// (g_ + m) * 255.0,
// (b_ + m) * 255.0);
(r_ + m, g_ + m, b_ + m)
}
pub fn hsv2rgb_u8(h: f32, s: f32, v: f32) -> (u8, u8, u8) {
let r = hsv2rgb(h, s, v);
(
(r.0 * 255.0) as u8,
(r.1 * 255.0) as u8,
(r.2 * 255.0) as u8
)
}
pub struct DummyTimesource {
}
@ -151,14 +81,7 @@ fn main() -> ! {
let timer = Timer::new(pac.TIMER, &mut pac.RESETS);
let mut delay = timer.count_down();
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let mut ws = Ws2812::new(
pins.gpio16.into_mode(),
&mut pio,
sm0,
clocks.peripheral_clock.freq(),
timer.count_down(),
);
// let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let _spi_sclk = pins.gpio2.into_mode::<rphal::gpio::FunctionSpi>();
let _spi_mosi = pins.gpio3.into_mode::<rphal::gpio::FunctionSpi>();
@ -167,7 +90,7 @@ fn main() -> ! {
let spi = rphal::spi::Spi::<_, _, 8>::new(pac.SPI0);
// Exchange the uninitialised SPI driver for an initialised one
let mut spi = spi.init(
let spi = spi.init(
&mut pac.RESETS,
clocks.peripheral_clock.freq(),
16_000_000u32.Hz(),
@ -185,7 +108,7 @@ fn main() -> ! {
info!("OK!\nCard size...");
match cont.device().card_size_bytes() {
Ok(size) => info!("card size={}", size),
Err(e) => info!("Err: 1"),
Err(e) => info!("Err: {}", defmt::Debug2Format(&e)),
}
info!("Volume 0...");
@ -196,11 +119,10 @@ fn main() -> ! {
Ok(dir) => {
info!("Root!");
cont.iterate_dir(&v, &dir, |ent| {
use core::fmt;
info!("/{}.{}",
core::str::from_utf8(ent.name.base_name()).unwrap(),
core::str::from_utf8(ent.name.extension()).unwrap());
});
}).unwrap();
let mut file =
cont.open_file_in_dir(
@ -209,7 +131,7 @@ fn main() -> ! {
let mut buf = [0u8; 32];
let nr = cont.read(&mut v, &mut file, &mut buf).unwrap();
info!("READ {} bytes: {}", nr, buf);
cont.close_file(&v, file);
cont.close_file(&v, file).unwrap();
let mut file =
cont.open_file_in_dir(
@ -217,7 +139,7 @@ fn main() -> ! {
// embedded_sdmmc::filesystem::Mode::ReadWriteCreateOrAppend).unwrap();
embedded_sdmmc::filesystem::Mode::ReadWriteCreateOrTruncate).unwrap();
cont.write(&mut v, &mut file, b"foobar123\n").unwrap();
cont.close_file(&v, file);
cont.close_file(&v, file).unwrap();
},
Err(e) => {
info!("Err: Root {}", defmt::Debug2Format(&e));
@ -229,77 +151,17 @@ fn main() -> ! {
cont.free();
let mut adc = Adc::new(pac.ADC, &mut pac.RESETS);
let mut adc_pin_0 = pins.gpio26.into_floating_input();
let mut n: u8 = 0;
let mut leds_off : [RGB8; LEN] = [(0,0,0).into(); LEN];
let mut leds : [RGB8; LEN] = [(0,0,0).into(); LEN];
for i in 0..LEN {
leds[i] = (255, 128, 64).into();
}
// leds[149] = (255, 255, 255).into();
// leds[70] = (255, 0, 255).into();
// leds[100] = (255, 0, 0).into();
let colors : [RGB8; 3] = [
hsv2rgb_u8(0.0, 1.0, 1.0).into(),
(0, 255, 0).into(),
(0, 0, 255).into(),
];
let amperes = 8.0;
let all_on_amp = (LEN as f32 * 3.0 * 60.0) / 1000.0;
let vbrightness = ((amperes / all_on_amp) * 255.0) as u8;
info!("brightness={} / 255", vbrightness);
ws.write(brightness(leds_off.iter().copied(), vbrightness)).unwrap();
// for i in 0..LEN {
// leds[i] = colors[1];
// }
// info!("LEDS={}", leds.len());
let mut cnt = 0;
let mut j = 0;
let mut led_pin = pins.led.into_push_pull_output();
loop {
cnt += 1;
if cnt > 400 {
cnt = 0;
}
let clr = hsv2rgb_u8((cnt as f32 / 400.0) * 360.0, 0.0, 1.0);
// info!("[{}] clr : {}", cnt, clr);
for i in 0..LEN {
leds[i] = clr.into();
}
ws.write(brightness(leds.iter().copied(), vbrightness)).unwrap();
delay.start(16.milliseconds());
let _ = nb::block!(delay.wait());
info!("on!");
led_pin.set_high().unwrap();
delay.start(500.milliseconds());
let _ = nb::block!(delay.wait());
info!("off!");
led_pin.set_low().unwrap();
delay.start(500.milliseconds());
let _ = nb::block!(delay.wait());
}
}
pub fn rot(slice: &mut [RGB8]) {
let first = slice[0];
for i in 1..slice.len() {
slice[i - 1] = slice[i];
}
slice[slice.len() - 1] = first;
}

Loading…
Cancel
Save