Skip to content

Commit a1aa27c

Browse files
jonathanpallanteldruin
authored andcommitted
Improved examples in README and lib.rs
1 parent 95df37e commit a1aa27c

File tree

2 files changed

+73
-57
lines changed

2 files changed

+73
-57
lines changed

README.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,30 @@ You will need something that implements the `BlockDevice` trait, which can read
1414
// Build an SD Card interface out of an SPI device
1515
let mut spi_dev = embedded_sdmmc::SdMmcSpi::new(sdmmc_spi, sdmmc_cs);
1616
// Try and initialise the SD card
17-
write!(uart, "Init SD card...").unwrap();
18-
match spi_dev.acquire() {
19-
Ok(block) => {
20-
// The SD Card initialised, and we have a `BlockSpi` object representing
21-
// the initialised card. Now let's
22-
let mut cont = embedded_sdmmc::VolumeManager::new(block, time_source);
23-
write!(uart, "OK!\nCard size...").unwrap();
24-
match cont.device().card_size_bytes() {
25-
Ok(size) => writeln!(uart, "{}", size).unwrap(),
26-
Err(e) => writeln!(uart, "Err: {:?}", e).unwrap(),
27-
}
28-
write!(uart, "Volume 0...").unwrap();
29-
match cont.get_volume(embedded_sdmmc::VolumeIdx(0)) {
30-
Ok(v) => writeln!(uart, "{:?}", v).unwrap(),
31-
Err(e) => writeln!(uart, "Err: {:?}", e).unwrap(),
32-
}
17+
let block_dev = spi_dev.acquire()?;
18+
// The SD Card was initialised, and we have a `BlockSpi` object
19+
// representing the initialised card.
20+
write!(uart, "Card size is {} bytes", block_dev.card_size_bytes()?)?;
21+
// Now let's look for volumes (also known as partitions) on our block device.
22+
let mut cont = embedded_sdmmc::VolumeManager::new(block_dev, time_source);
23+
// Try and access Volume 0 (i.e. the first partition)
24+
let mut volume = cont.get_volume(embedded_sdmmc::VolumeIdx(0))?;
25+
writeln!(uart, "Volume 0: {:?}", v)?;
26+
// Open the root directory
27+
let root_dir = volume_mgr.open_root_dir(&volume0)?;
28+
// Open a file called "MY_FILE.TXT" in the root directory
29+
let mut my_file = volume_mgr.open_file_in_dir(
30+
&mut volume0, &root_dir, "MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
31+
// Print the contents of the file
32+
while !my_file.eof() {
33+
let mut buffer = [0u8; 32];
34+
let num_read = volume_mgr.read(&volume0, &mut my_file, &mut buffer)?;
35+
for b in &buffer[0..num_read] {
36+
print!("{}", *b as char);
3337
}
34-
Err(e) => writeln!(uart, "{:?}!", e).unwrap(),
3538
}
39+
volume_mgr.close_file(&volume0, my_file)?;
40+
volume_mgr.close_dir(&volume0, root_dir)?;
3641
```
3742

3843
### Open directories and files
@@ -41,12 +46,7 @@ By default the `VolumeManager` will initialize with a maximum number of `4` open
4146

4247
```rust
4348
// Create a volume manager with a maximum of 6 open directories and 12 open files
44-
let mut cont: VolumeManager<
45-
embedded_sdmmc::BlockSpi<DummySpi, DummyCsPin>,
46-
DummyTimeSource,
47-
6,
48-
12,
49-
> = VolumeManager::new_with_limits(block, time_source);
49+
let mut cont: VolumeManager<_, _, 6, 12> = VolumeManager::new_with_limits(block, time_source);
5050
```
5151

5252
## Supported features

src/lib.rs

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
//!
33
//! > An SD/MMC Library written in Embedded Rust
44
//!
5-
//! This crate is intended to allow you to read/write files on a FAT formatted SD
6-
//! card on your Rust Embedded device, as easily as using the `SdFat` Arduino
7-
//! library. It is written in pure-Rust, is `#![no_std]` and does not use `alloc`
8-
//! or `collections` to keep the memory footprint low. In the first instance it is
9-
//! designed for readability and simplicity over performance.
5+
//! This crate is intended to allow you to read/write files on a FAT formatted
6+
//! SD card on your Rust Embedded device, as easily as using the `SdFat` Arduino
7+
//! library. It is written in pure-Rust, is `#![no_std]` and does not use
8+
//! `alloc` or `collections` to keep the memory footprint low. In the first
9+
//! instance it is designed for readability and simplicity over performance.
1010
//!
1111
//! ## Using the crate
1212
//!
13-
//! You will need something that implements the `BlockDevice` trait, which can read and write the 512-byte blocks (or sectors) from your card. If you were to implement this over USB Mass Storage, there's no reason this crate couldn't work with a USB Thumb Drive, but we only supply a `BlockDevice` suitable for reading SD and SDHC cards over SPI.
13+
//! You will need something that implements the `BlockDevice` trait, which can
14+
//! read and write the 512-byte blocks (or sectors) from your card. If you were
15+
//! to implement this over USB Mass Storage, there's no reason this crate
16+
//! couldn't work with a USB Thumb Drive, but we only supply a `BlockDevice`
17+
//! suitable for reading SD and SDHC cards over SPI.
1418
//!
15-
//! ```rust
19+
//! ```rust,no_run
1620
//! # struct DummySpi;
1721
//! # struct DummyCsPin;
1822
//! # struct DummyUart;
@@ -32,41 +36,41 @@
3236
//! # impl std::fmt::Write for DummyUart { fn write_str(&mut self, s: &str) -> std::fmt::Result { Ok(()) } }
3337
//! # use std::fmt::Write;
3438
//! # use embedded_sdmmc::VolumeManager;
35-
//! # let mut uart = DummyUart;
39+
//! # fn main() -> Result<(), embedded_sdmmc::Error<embedded_sdmmc::SdMmcError>> {
3640
//! # let mut sdmmc_spi = DummySpi;
3741
//! # let mut sdmmc_cs = DummyCsPin;
3842
//! # let time_source = DummyTimeSource;
3943
//! let mut spi_dev = embedded_sdmmc::SdMmcSpi::new(sdmmc_spi, sdmmc_cs);
40-
//! write!(uart, "Init SD card...").unwrap();
41-
//! match spi_dev.acquire() {
42-
//! Ok(block) => {
43-
//! let mut volume_mgr: VolumeManager<
44-
//! embedded_sdmmc::BlockSpi<DummySpi, DummyCsPin>,
45-
//! DummyTimeSource,
46-
//! 4,
47-
//! 4,
48-
//! > = VolumeManager::new(block, time_source);
49-
//! write!(uart, "OK!\nCard size...").unwrap();
50-
//! match volume_mgr.device().card_size_bytes() {
51-
//! Ok(size) => writeln!(uart, "{}", size).unwrap(),
52-
//! Err(e) => writeln!(uart, "Err: {:?}", e).unwrap(),
53-
//! }
54-
//! write!(uart, "Volume 0...").unwrap();
55-
//! match volume_mgr.get_volume(embedded_sdmmc::VolumeIdx(0)) {
56-
//! Ok(v) => writeln!(uart, "{:?}", v).unwrap(),
57-
//! Err(e) => writeln!(uart, "Err: {:?}", e).unwrap(),
58-
//! }
44+
//! let block = spi_dev.acquire()?;
45+
//! println!("Card size {} bytes", block.card_size_bytes()?);
46+
//! let mut volume_mgr = VolumeManager::new(block, time_source);
47+
//! println!("Card size is still {} bytes", volume_mgr.device().card_size_bytes()?);
48+
//! let mut volume0 = volume_mgr.get_volume(embedded_sdmmc::VolumeIdx(0))?;
49+
//! println!("Volume 0: {:?}", volume0);
50+
//! let root_dir = volume_mgr.open_root_dir(&volume0)?;
51+
//! let mut my_file = volume_mgr.open_file_in_dir(
52+
//! &mut volume0, &root_dir, "MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
53+
//! while !my_file.eof() {
54+
//! let mut buffer = [0u8; 32];
55+
//! let num_read = volume_mgr.read(&volume0, &mut my_file, &mut buffer)?;
56+
//! for b in &buffer[0..num_read] {
57+
//! print!("{}", *b as char);
5958
//! }
60-
//! Err(e) => writeln!(uart, "{:?}!", e).unwrap(),
61-
//! };
59+
//! }
60+
//! volume_mgr.close_file(&volume0, my_file)?;
61+
//! volume_mgr.close_dir(&volume0, root_dir);
62+
//! # Ok(())
63+
//! # }
6264
//! ```
6365
//!
6466
//! ## Features
6567
//!
66-
//! * `defmt-log`: By turning off the default features and enabling the `defmt-log` feature you can
67-
//! configure this crate to log messages over defmt instead.
68+
//! * `defmt-log`: By turning off the default features and enabling the
69+
//! `defmt-log` feature you can configure this crate to log messages over defmt
70+
//! instead.
6871
//!
69-
//! Make sure that either the `log` feature or the `defmt-log` feature is enabled.
72+
//! Make sure that either the `log` feature or the `defmt-log` feature is
73+
//! enabled.
7074
7175
#![cfg_attr(not(test), no_std)]
7276
#![deny(missing_docs)]
@@ -99,6 +103,12 @@ pub use crate::filesystem::{
99103
pub use crate::sdmmc::Error as SdMmcError;
100104
pub use crate::sdmmc::{BlockSpi, SdMmcSpi};
101105

106+
mod volume_mgr;
107+
pub use volume_mgr::VolumeManager;
108+
109+
#[deprecated]
110+
pub use volume_mgr::VolumeManager as Controller;
111+
102112
// ****************************************************************************
103113
//
104114
// Public Types
@@ -160,8 +170,14 @@ where
160170
NotInBlock,
161171
}
162172

163-
mod volume_mgr;
164-
pub use volume_mgr::VolumeManager;
173+
impl<E> From<E> for Error<E>
174+
where
175+
E: core::fmt::Debug,
176+
{
177+
fn from(value: E) -> Error<E> {
178+
Error::DeviceError(value)
179+
}
180+
}
165181

166182
/// Represents a partition with a filesystem within it.
167183
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]

0 commit comments

Comments
 (0)