aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/tsc/acquisition_banks.rs
blob: 097cf7942d945fbc00f79c8b1ee7136657a73e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use super::TSC_NUM_GROUPS;
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::{G1, G2, G3, G4, G5, G6, pin_roles};
use super::types::{Group, GroupStatus};

/// Represents a collection of TSC (Touch Sensing Controller) pins for an acquisition bank.
///
/// 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(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 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<IOPinWithRole<G7, pin_roles::Channel>>,
    #[cfg(tsc_v3)]
    pub g8_pin: Option<IOPinWithRole<G8, pin_roles::Channel>>,
}

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) -> AcquisitionBankPinsIterator<'_> {
        AcquisitionBankPinsIterator(AcquisitionBankIterator::new(self))
    }
}

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

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

    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(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(IOPinWithRole::get_pin),
                #[cfg(tsc_v3)]
                7 => self.pins.g8_pin.map(IOPinWithRole::get_pin),
                _ => None,
            };
            self.current_group += 1;
            if pin.is_some() {
                return pin;
            }
        }
        None
    }
}

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

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

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

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

/// Represents a collection of TSC pins to be acquired simultaneously.
///
/// 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 AcquisitionBank {
    pub(super) pins: AcquisitionBankPins,
    pub(super) mask: u32,
}

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

    /// Returns the mask for this bank.
    pub fn mask(&self) -> u32 {
        self.mask
    }

    /// Retrieves the TSC I/O pin for a given group in this acquisition bank.
    ///
    /// # Arguments
    /// * `group` - The TSC group to retrieve the pin for.
    ///
    /// # Returns
    /// 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),
            Group::Three => self.pins.g3_pin.map(|p| p.pin),
            Group::Four => self.pins.g4_pin.map(|p| p.pin),
            Group::Five => self.pins.g5_pin.map(|p| p.pin),
            Group::Six => self.pins.g6_pin.map(|p| p.pin),
            #[cfg(any(tsc_v2, tsc_v3))]
            Group::Seven => self.pins.g7_pin.map(|p| p.pin),
            #[cfg(tsc_v3)]
            Group::Eight => self.pins.g8_pin.map(|p| p.pin),
        }
    }
}

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

impl AcquisitionBankStatus {
    /// Check if all groups in the bank are complete
    pub fn all_complete(&self) -> bool {
        self.groups
            .iter()
            .all(|&status| status.map_or(true, |s| s == GroupStatus::Complete))
    }

    /// Check if any group in the bank is ongoing
    pub fn any_ongoing(&self) -> bool {
        self.groups.iter().any(|&status| status == Some(GroupStatus::Ongoing))
    }

    /// Get the status of a specific group, if the group is present in the bank
    pub fn get_group_status(&self, group: Group) -> Option<GroupStatus> {
        let index: usize = group.into();
        self.groups[index]
    }

    /// Iterator for groups present in the bank
    pub fn iter(&self) -> impl Iterator<Item = (Group, GroupStatus)> + '_ {
        self.groups.iter().enumerate().filter_map(|(group_num, status)| {
            status.and_then(|s| Group::try_from(group_num).ok().map(|group| (group, s)))
        })
    }
}

/// Represents the result of a Touch Sensing Controller (TSC) acquisition for a specific pin.
///
/// 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 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 `tsc::IOPin` associated with this reading.
    /// This allows for easy identification of which pin the reading corresponds to.
    pub tsc_pin: IOPin,
}

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

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<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 = ChannelReading> + '_ {
        self.groups.iter().filter_map(|&x| x)
    }
}