• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eldruin / mcp4x-rs / 12241440399

09 Dec 2024 05:57PM UTC coverage: 74.227% (-25.8%) from 100.0%
12241440399

Pull #4

github

web-flow
Merge 7eae9cc67 into 6701d772e
Pull Request #4: Support for MCP4017/18/19 I2C potentiometers

17 of 36 new or added lines in 2 files covered. (47.22%)

6 existing lines in 1 file now uncovered.

72 of 97 relevant lines covered (74.23%)

4.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.85
/src/device_impl.rs
1
//! Device implementation
2

3
use crate::{ic, interface, private, Channel, Command, Error, Mcp4x};
4
use core::marker::PhantomData;
5

6
#[doc(hidden)]
7
pub trait CheckParameters<CommE>: private::Sealed {
8
    fn check_if_channel_is_appropriate(channel: Channel) -> Result<(), Error<CommE>>;
9
    fn check_if_position_is_appropriate(position: u8) -> Result<(), Error<CommE>>;
10
}
11

12
impl<CommE> CheckParameters<CommE> for ic::Mcp401x {
UNCOV
13
    fn check_if_channel_is_appropriate(channel: Channel) -> Result<(), Error<CommE>> {
×
UNCOV
14
        if channel == Channel::Ch0 || channel == Channel::All {
×
UNCOV
15
            Ok(())
×
16
        } else {
UNCOV
17
            Err(Error::WrongChannel)
×
18
        }
UNCOV
19
    }
×
20

NEW
21
    fn check_if_position_is_appropriate(position: u8) -> Result<(), Error<CommE>> {
×
NEW
22
        if position <= 127 {
×
NEW
23
            Ok(())
×
24
        } else {
NEW
25
            Err(Error::OutOfBounds)
×
26
        }
NEW
27
    }
×
28
}
29

30
impl<CommE> CheckParameters<CommE> for ic::Mcp41x {
31
    fn check_if_channel_is_appropriate(channel: Channel) -> Result<(), Error<CommE>> {
6✔
32
        if channel == Channel::Ch0 || channel == Channel::All {
6✔
33
            Ok(())
4✔
34
        } else {
35
            Err(Error::WrongChannel)
2✔
36
        }
37
    }
6✔
38

39
    fn check_if_position_is_appropriate(_position: u8) -> Result<(), Error<CommE>> {
2✔
40
        Ok(())
2✔
41
    }
2✔
42
}
43

44
impl<CommE> CheckParameters<CommE> for ic::Mcp42x {
45
    fn check_if_channel_is_appropriate(_: Channel) -> Result<(), Error<CommE>> {
6✔
46
        Ok(())
6✔
47
    }
6✔
48

49
    fn check_if_position_is_appropriate(_position: u8) -> Result<(), Error<CommE>> {
3✔
50
        Ok(())
3✔
51
    }
3✔
52
}
53

54
impl<DI, IC, CommE> Mcp4x<DI, IC>
55
where
56
    DI: interface::WriteCommand<Error = Error<CommE>>,
57
    IC: CheckParameters<CommE>,
58
{
59
    /// Set a channel to a position.
60
    ///
61
    /// Will return `Error::WrongChannel` if the channel provided is not available
62
    /// on the device.
63
    pub fn set_position(&mut self, channel: Channel, position: u8) -> Result<(), Error<CommE>> {
6✔
64
        IC::check_if_channel_is_appropriate(channel)?;
6✔
65
        IC::check_if_position_is_appropriate(position)?;
5✔
66
        self.iface
5✔
67
            .write_command(Command::SetPosition(channel, position))
5✔
68
    }
6✔
69

70
    /// Shutdown a channel.
71
    ///
72
    /// Will return `Error::WrongChannel` if the channel provided is not available
73
    /// on the device.
74
    pub fn shutdown(&mut self, channel: Channel) -> Result<(), Error<CommE>> {
6✔
75
        IC::check_if_channel_is_appropriate(channel)?;
6✔
76
        self.iface.write_command(Command::Shutdown(channel))
5✔
77
    }
6✔
78
}
79

80
impl<I2C> Mcp4x<interface::I2cInterface<I2C>, ic::Mcp401x> {
81
    /// Create new MCP401x device instance
NEW
82
    pub fn new_mcp401x(i2c: I2C) -> Self {
×
NEW
83
        Mcp4x {
×
NEW
84
            iface: interface::I2cInterface { i2c },
×
NEW
85
            _ic: PhantomData,
×
NEW
86
        }
×
NEW
87
    }
×
88

89
    /// Destroy driver instance, return I2C bus instance.
NEW
90
    pub fn destroy_mcp401x(self) -> I2C {
×
NEW
91
        self.iface.i2c
×
UNCOV
92
    }
×
93
}
94

95
impl<SPI> Mcp4x<interface::SpiInterface<SPI>, ic::Mcp41x> {
96
    /// Create new MCP41x device instance
97
    pub fn new_mcp41x(spi: SPI) -> Self {
6✔
98
        Mcp4x {
6✔
99
            iface: interface::SpiInterface { spi },
6✔
100
            _ic: PhantomData,
6✔
101
        }
6✔
102
    }
6✔
103

104
    /// Destroy driver instance, return SPI bus instance and CS output pin.
105
    pub fn destroy_mcp41x(self) -> SPI {
6✔
106
        self.iface.spi
6✔
107
    }
6✔
108
}
109

110
impl<SPI> Mcp4x<interface::SpiInterface<SPI>, ic::Mcp42x> {
111
    /// Create new MCP42x device instance
112
    pub fn new_mcp42x(spi: SPI) -> Self {
6✔
113
        Mcp4x {
6✔
114
            iface: interface::SpiInterface { spi },
6✔
115
            _ic: PhantomData,
6✔
116
        }
6✔
117
    }
6✔
118

119
    /// Destroy driver instance, return SPI bus instance and CS output pin.
120
    pub fn destroy_mcp42x(self) -> SPI {
6✔
121
        self.iface.spi
6✔
122
    }
6✔
123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc