mpr121_hal/
lib.rs

1//! # MPR121-Hal
2//!
3//! This crate follows the [Adafruit implementation](https://github.com/adafruit/Adafruit_MPR121) closely but exposes it in terms of the embedded-hal project.
4//!
5//! The main responsibility of this crate is returning the current on/off state of all the (up to) 12 pins.
6//!
7//! The chip's data sheet can be found [here](https://www.nxp.com/docs/en/data-sheet/MPR121.pdf). The implementation however mostly mirrors the Adafruit implementation,
8//! since this is probably the most widely used one.
9//!
10//! When working with this crate you can either use it in Synchronous/Blocking mode with the [embedded-hal](https://crates.io/crates/embedded-hal) or in Asynchronous mode with the [embedded-hal-async](https://crates.io/crates/embedded-hal-async).
11//!This can be done by using the features `sync` and `async`. This crate does not pull in the `std` library and thus is fully `no-std`.
12//! For MCU scale devices [Embassy](https://github.com/embassy-rs/embassy) is a valid framework to use the async feature or [Tokio](https://tokio.rs/) when using Linux/MacOS based devices.
13#![no_std]
14#![deny(unsafe_code, warnings)]
15
16use num_enum::{IntoPrimitive, TryFromPrimitive};
17use registers::Register;
18
19mod communications;
20pub mod mpr121;
21mod registers;
22
23#[cfg(all(feature = "sync", feature = "async"))]
24compile_error!("You cannot use both sync and async features at the same time. Please choose one.");
25
26#[cfg(all(not(feature = "async"), not(feature = "sync")))]
27compile_error!("You must enable either the sync or async feature. Please choose one.");
28
29/// The MPR121 Device has an Enumeration of potential driver errors, which are held in the enum below
30#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)]
31pub enum Mpr121Error {
32    ///If an operation exceeds the channel count (typically 12).
33    ChannelExceed,
34    ///If a read operation failed, contains the address that failed.
35    ReadError(Register),
36    /// If a data conversion failed, contains the address that failed to convert from
37    DataConversionError(Register),
38    ///If a write operation failed, contains the address that failed.
39    WriteError(Register),
40    ///If sending the reset signal failed, contains the register that failed.
41    ResetFailed { was_read: bool, reg: Register },
42    /// During Startup if there is an overcurrent detection, the driver will fail to initialise indicating a hardware fault
43    OverCurrent,
44    /// Wrong Device Connected, this can also happen if the device state is invalid possible due to a short circuit/overcurrent event
45    WrongDevice {
46        mismatched_register: Register,
47        expected: u8,
48        actual: u8,
49    },
50}
51
52///The four values the sensor can be addressed as. Note that the address of the device is determined by
53/// where the `ADDR` pin is connected to. Default is used if no connection, or a connection to `VSS` is made.
54///
55/// Have a look at page 4 "serial communication" for further specification.
56#[repr(u8)]
57#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, IntoPrimitive)]
58pub enum Mpr121Address {
59    Default = 0x5a,
60    Vdd = 0x5b,
61    Sda = 0x5c,
62    Scl = 0x5d,
63}
64
65#[repr(u8)]
66#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, IntoPrimitive, TryFromPrimitive, Debug)]
67/// This enum represents the channels of the sensor and is used to get the corresponding touch values
68pub enum Channel {
69    Zero,
70    One,
71    Two,
72    Three,
73    Four,
74    Five,
75    Six,
76    Seven,
77    Eight,
78    Nine,
79    Ten,
80    Eleven,
81}
82
83impl Channel {
84    pub const NUM_CHANNELS: u8 = 12;
85    /// Returns the bit mask associated with the selected channel
86    pub(crate) fn get_bit_mask(self) -> u16 {
87        1 << u8::from(self)
88    }
89}
90
91#[repr(u8)]
92#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, IntoPrimitive, TryFromPrimitive)]
93/// This enum represents the number of debounces see section 5.7 in the [MPR121 Data Sheet](https://www.nxp.com/docs/en/data-sheet/MPR121.pdf)
94pub enum DebounceNumber {
95    Zero,
96    One,
97    Two,
98    Three,
99    Four,
100    Five,
101    Six,
102    Seven,
103}