Skip to content

Commit

Permalink
Refactor TSC module: Remove redundant 'Tsc' prefixes for improved nam…
Browse files Browse the repository at this point in the history
…ing consistency
  • Loading branch information
michelrandahl committed Oct 10, 2024
1 parent fbfe234 commit f8fd757
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 385 deletions.
108 changes: 54 additions & 54 deletions embassy-stm32/src/tsc/acquisition_banks.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
use super::io_pin::*;
#[cfg(any(tsc_v2, tsc_v3))]
use super::pin_groups::G7;
#[cfg(tsc_v3)]
use super::pin_groups::G8;
use super::pin_groups::{tsc_pin_roles, G1, G2, G3, G4, G5, G6};
use super::tsc_io_pin::*;
use super::pin_groups::{pin_roles, G1, G2, G3, G4, G5, G6};
use super::types::{Group, GroupStatus};
use super::TSC_NUM_GROUPS;

/// Represents a collection of TSC (Touch Sensing Controller) pins for an acquisition bank.
///
/// This struct holds optional `TscIOPin` values for each TSC group, allowing for flexible
/// This struct holds optional `tsc::IOPin` values for each TSC group, allowing for flexible
/// configuration of TSC acquisition banks. Each field corresponds to a specific TSC group
/// and can be set to `Some(TscIOPin)` if that group is to be included in the acquisition,
/// and can be set to `Some(tsc::IOPin)` if that group is to be included in the acquisition,
/// or `None` if it should be excluded.
#[allow(missing_docs)]
#[derive(Default)]
pub struct TscAcquisitionBankPins {
pub g1_pin: Option<TscIOPinWithRole<G1, tsc_pin_roles::Channel>>,
pub g2_pin: Option<TscIOPinWithRole<G2, tsc_pin_roles::Channel>>,
pub g3_pin: Option<TscIOPinWithRole<G3, tsc_pin_roles::Channel>>,
pub g4_pin: Option<TscIOPinWithRole<G4, tsc_pin_roles::Channel>>,
pub g5_pin: Option<TscIOPinWithRole<G5, tsc_pin_roles::Channel>>,
pub g6_pin: Option<TscIOPinWithRole<G6, tsc_pin_roles::Channel>>,
pub struct AcquisitionBankPins {
pub g1_pin: Option<IOPinWithRole<G1, pin_roles::Channel>>,
pub g2_pin: Option<IOPinWithRole<G2, pin_roles::Channel>>,
pub g3_pin: Option<IOPinWithRole<G3, pin_roles::Channel>>,
pub g4_pin: Option<IOPinWithRole<G4, pin_roles::Channel>>,
pub g5_pin: Option<IOPinWithRole<G5, pin_roles::Channel>>,
pub g6_pin: Option<IOPinWithRole<G6, pin_roles::Channel>>,
#[cfg(any(tsc_v2, tsc_v3))]
pub g7_pin: Option<TscIOPinWithRole<G7, tsc_pin_roles::Channel>>,
pub g7_pin: Option<IOPinWithRole<G7, pin_roles::Channel>>,
#[cfg(tsc_v3)]
pub g8_pin: Option<TscIOPinWithRole<G8, tsc_pin_roles::Channel>>,
pub g8_pin: Option<IOPinWithRole<G8, pin_roles::Channel>>,
}

impl TscAcquisitionBankPins {
impl AcquisitionBankPins {
/// Returns an iterator over the pins in this acquisition bank.
///
/// This method allows for easy traversal of all configured pins in the bank.
pub fn iter(&self) -> TscAcquisitionBankPinsIterator {
TscAcquisitionBankPinsIterator(TscAcquisitionBankIterator::new(self))
pub fn iter(&self) -> AcquisitionBankPinsIterator {
AcquisitionBankPinsIterator(AcquisitionBankIterator::new(self))
}
}

/// Iterator for TSC acquisition banks.
///
/// This iterator allows traversing through the pins of a `TscAcquisitionBankPins` struct,
/// This iterator allows traversing through the pins of a `AcquisitionBankPins` struct,
/// yielding each configured pin in order of the TSC groups.
pub struct TscAcquisitionBankIterator<'a> {
pins: &'a TscAcquisitionBankPins,
pub struct AcquisitionBankIterator<'a> {
pins: &'a AcquisitionBankPins,
current_group: u8,
}

impl<'a> TscAcquisitionBankIterator<'a> {
fn new(pins: &'a TscAcquisitionBankPins) -> Self {
impl<'a> AcquisitionBankIterator<'a> {
fn new(pins: &'a AcquisitionBankPins) -> Self {
Self { pins, current_group: 0 }
}

fn next_pin(&mut self) -> Option<TscIOPin> {
fn next_pin(&mut self) -> Option<IOPin> {
while self.current_group < TSC_NUM_GROUPS as u8 {
let pin = match self.current_group {
0 => self.pins.g1_pin.map(TscIOPinWithRole::get_pin),
1 => self.pins.g2_pin.map(TscIOPinWithRole::get_pin),
2 => self.pins.g3_pin.map(TscIOPinWithRole::get_pin),
3 => self.pins.g4_pin.map(TscIOPinWithRole::get_pin),
4 => self.pins.g5_pin.map(TscIOPinWithRole::get_pin),
5 => self.pins.g6_pin.map(TscIOPinWithRole::get_pin),
0 => self.pins.g1_pin.map(IOPinWithRole::get_pin),
1 => self.pins.g2_pin.map(IOPinWithRole::get_pin),
2 => self.pins.g3_pin.map(IOPinWithRole::get_pin),
3 => self.pins.g4_pin.map(IOPinWithRole::get_pin),
4 => self.pins.g5_pin.map(IOPinWithRole::get_pin),
5 => self.pins.g6_pin.map(IOPinWithRole::get_pin),
#[cfg(any(tsc_v2, tsc_v3))]
6 => self.pins.g7_pin.map(TscIOPinWithRole::get_pin),
6 => self.pins.g7_pin.map(IOPinWithRole::get_pin),
#[cfg(tsc_v3)]
7 => self.pins.g8_pin.map(TscIOPinWithRole::get_pin),
7 => self.pins.g8_pin.map(IOPinWithRole::get_pin),
_ => None,
};
self.current_group += 1;
Expand All @@ -77,21 +77,21 @@ impl<'a> TscAcquisitionBankIterator<'a> {

/// Iterator for TSC acquisition bank pins.
///
/// This iterator yields `TscIOPin` values for each configured pin in the acquisition bank.
pub struct TscAcquisitionBankPinsIterator<'a>(TscAcquisitionBankIterator<'a>);
/// This iterator yields `tsc::IOPin` values for each configured pin in the acquisition bank.
pub struct AcquisitionBankPinsIterator<'a>(AcquisitionBankIterator<'a>);

impl<'a> Iterator for TscAcquisitionBankPinsIterator<'a> {
type Item = TscIOPin;
impl<'a> Iterator for AcquisitionBankPinsIterator<'a> {
type Item = IOPin;

fn next(&mut self) -> Option<Self::Item> {
self.0.next_pin()
}
}

impl TscAcquisitionBankPins {
impl AcquisitionBankPins {
/// Returns an iterator over the available pins in the bank
pub fn pins_iterator(&self) -> TscAcquisitionBankPinsIterator {
TscAcquisitionBankPinsIterator(TscAcquisitionBankIterator::new(self))
pub fn pins_iterator(&self) -> AcquisitionBankPinsIterator {
AcquisitionBankPinsIterator(AcquisitionBankIterator::new(self))
}
}

Expand All @@ -100,14 +100,14 @@ impl TscAcquisitionBankPins {
/// This struct contains a set of pins to be used in a TSC acquisition with a pre-computed and
/// verified mask for efficiently setting up the TSC peripheral before performing an acquisition.
/// It ensures that only one channel pin per TSC group is included, adhering to hardware limitations.
pub struct TscAcquisitionBank {
pub(super) pins: TscAcquisitionBankPins,
pub struct AcquisitionBank {
pub(super) pins: AcquisitionBankPins,
pub(super) mask: u32,
}

impl TscAcquisitionBank {
impl AcquisitionBank {
/// Returns an iterator over the available pins in the bank.
pub fn pins_iterator(&self) -> TscAcquisitionBankPinsIterator {
pub fn pins_iterator(&self) -> AcquisitionBankPinsIterator {
self.pins.pins_iterator()
}

Expand All @@ -122,8 +122,8 @@ impl TscAcquisitionBank {
/// * `group` - The TSC group to retrieve the pin for.
///
/// # Returns
/// An `Option<TscIOPin>` containing the pin if it exists for the given group, or `None` if not.
pub fn get_pin(&self, group: Group) -> Option<TscIOPin> {
/// An `Option<tsc::IOPin>` containing the pin if it exists for the given group, or `None` if not.
pub fn get_pin(&self, group: Group) -> Option<IOPin> {
match group {
Group::One => self.pins.g1_pin.map(|p| p.pin),
Group::Two => self.pins.g2_pin.map(|p| p.pin),
Expand All @@ -141,11 +141,11 @@ impl TscAcquisitionBank {

/// Represents the status of all TSC groups in an acquisition bank
#[derive(Default)]
pub struct TscAcquisitionBankStatus {
pub struct AcquisitionBankStatus {
pub(super) groups: [Option<GroupStatus>; TSC_NUM_GROUPS],
}

impl TscAcquisitionBankStatus {
impl AcquisitionBankStatus {
/// Check if all groups in the bank are complete
pub fn all_complete(&self) -> bool {
self.groups
Expand Down Expand Up @@ -174,36 +174,36 @@ impl TscAcquisitionBankStatus {

/// Represents the result of a Touch Sensing Controller (TSC) acquisition for a specific pin.
///
/// This struct contains a reference to the `TscIOPin` from which a value was read,
/// This struct contains a reference to the `tsc::IOPin` from which a value was read,
/// along with the actual sensor reading for that pin. It provides a convenient way
/// to associate TSC readings with their corresponding pins after an acquisition.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug)]
pub struct TscChannelReading {
pub struct ChannelReading {
/// The sensor reading value obtained from the TSC acquisition.
/// Lower values typically indicate a detected touch, while higher values indicate no touch.
pub sensor_value: u16,

/// The `TscIOPin` associated with this reading.
/// The `tsc::IOPin` associated with this reading.
/// This allows for easy identification of which pin the reading corresponds to.
pub tsc_pin: TscIOPin,
pub tsc_pin: IOPin,
}

/// Represents the readings from all TSC groups
#[derive(Default)]
pub struct TscAcquisitionBankReadings {
pub(super) groups: [Option<TscChannelReading>; TSC_NUM_GROUPS],
pub struct AcquisitionBankReadings {
pub(super) groups: [Option<ChannelReading>; TSC_NUM_GROUPS],
}

impl TscAcquisitionBankReadings {
impl AcquisitionBankReadings {
/// Get the reading for a specific group, if the group is present in the bank
pub fn get_group_reading(&self, group: Group) -> Option<TscChannelReading> {
pub fn get_group_reading(&self, group: Group) -> Option<ChannelReading> {
let index: usize = group.into();
self.groups[index]
}

/// Iterator for readings for groups present in the bank
pub fn iter(&self) -> impl Iterator<Item = TscChannelReading> + '_ {
pub fn iter(&self) -> impl Iterator<Item = ChannelReading> + '_ {
self.groups.iter().filter_map(|&x| x)
}
}
Loading

0 comments on commit f8fd757

Please sign in to comment.