Skip to content

Commit 6bd4c1b

Browse files
committed
Add with_device method
This enables access to device-specific features such as the EEPROM.
1 parent ccd0475 commit 6bd4c1b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Added
9+
- Added a `with_device` method to access device-specific features, such as the EEPROM.
10+
711
## [0.23.0] - 2025-03-09
812
### Changed
913
- Changed the SPI traits to be implemented on the `SpiDevice` struct, instead of a reference to the struct.

src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,35 @@ where
391391
E: std::error::Error,
392392
Error<E>: From<E>,
393393
{
394+
/// Executes the closure with the device.
395+
///
396+
/// Useful for accessing EEPROM, or other device-specific functionality.
397+
///
398+
/// # Example
399+
///
400+
/// ```no_run
401+
/// use ftdi_embedded_hal as hal;
402+
/// # #[cfg(feature = "libftd2xx")]
403+
/// use hal::libftd2xx::FtdiEeprom;
404+
///
405+
/// # #[cfg(feature = "libftd2xx")]
406+
/// # {
407+
/// let device = libftd2xx::Ft2232h::with_description("Dual RS232-HS A")?;
408+
/// let mut hal = hal::FtHal::init_default(device)?;
409+
/// let serial_number: String =
410+
/// hal.with_device(|d| d.eeprom_read().map(|(_, strings)| strings.serial_number()))?;
411+
/// # }
412+
/// # Ok::<(), std::boxed::Box<dyn std::error::Error>>(())
413+
/// ```
414+
pub fn with_device<T, F>(&mut self, mut f: F) -> T
415+
where
416+
F: FnMut(&mut Device) -> T,
417+
{
418+
let mut inner = self.mtx.lock().expect("Failed to aquire FTDI mutex");
419+
let result: T = f(&mut inner.ft);
420+
result
421+
}
422+
394423
/// Aquire the SPI peripheral for the FT232H.
395424
///
396425
/// Pin assignments:

0 commit comments

Comments
 (0)