Struct ina3221::Voltage

source ·
pub struct Voltage { /* private fields */ }
Expand description

Represents a voltage value, stored as whole microvolts (μV). This value can be positive or negative.

Reminder: 1000 μV = 1 mV, 1000 mV = 1 V, 1000 V = 1k V

This is an immutable type. Any math operators return a new Voltage value.

Creating a Voltage value

You can create a Voltage value using the from_micro_volts method, or using one of the extension methods on i32 and f32:

use ohms::prelude::*;

let v1 = Voltage::from_micro_volts(325); // 325μV

// More ergonomic:
let v2 = 900.milli_volts(); // 900mV
let v3 = 12.volts(); // 12V
let v4 = 3.3f32.volts(); // 3.3V

Comparing Voltage values

You can compare two Voltage values using the ==, !=, <, >, <= and >= operators.

use ohms::prelude::*;

let v1 = 3.3f32.volts(); // 3.3V
let v2 = 5.2f32.volts(); // 5.2V

if v1 > v2 {
    println!("{:?} is greater than {:?}", v1, v2);
} else {
    println!("{:?} is less than or equal to {:?}", v1, v2);
}

Combining Voltage values

You can use the + and - operators to add and subtract Voltage values from each other. The result is a new Voltage value, rounded down to the nearest whole microvolt (μV).

If the result of the operation would overflow or underflow the i32 value, the operation will panic.

use ohms::prelude::*;

let v1 = 3.7f32.volts(); // 3.7V
let v2 = 9.volts(); // 9V

let v3 = v1 + v2; // 12.7V
let v4 = v2 - 6.volts(); // 3V

Scaling Voltage values

You can use the * and / operators to scale Voltage values by a scalar i32 or f32 value. The result is a new Voltage value, rounded down to the nearest whole microvolt (μV).

If the result of operation would overflow or underflow the i32 value, the operation will panic.

If the result of the operation would be infinite or NaN, the operation will panic.

use ohms::prelude::*;

let v1 = 6.volts(); // 6V
let v2 = v1 * 2; // 12V

let v3 = 250.micro_volts(); // 250μV
let v4 = v3 / 2f32; // 125μV

Converting to other denominations

You can use the micro_volts, milli_volts, volts, and kilo_volts methods to convert a Voltage value to a i32 or f32 value in the specified denomination.

use ohms::prelude::*;

let v1 = 3.3f32.volts(); // 3.3V

println!("{:.2}V is {:.1}mV", v1.volts(), v1.milli_volts());

Implementations§

source§

impl Voltage

source

pub const fn from_micro_volts(microvolts: i32) -> Voltage

Creates a new Voltage from a number of whole microvolts (μV).

It is recommended to use the micro_volts, milli_volts, volts, and kilo_volts extension methods on i32 and f32 instead of this method for ergonomics.

source

pub fn micro_volts(&self) -> i32

Returns the voltage value in whole microvolts (μV).

source

pub fn milli_volts(&self) -> f32

Returns the voltage value in fractional millivolts (mV).

source

pub fn volts(&self) -> f32

Returns the voltage value in fractional volts (V).

source

pub fn kilo_volts(&self) -> f32

Returns the voltage value in fractional kilovolts (kV).

source

pub const fn is_zero(&self) -> bool

Returns whether the voltage value is zero volts (0V).

source

pub const fn is_positive(&self) -> bool

Returns whether the voltage value is positive.

This returns true if the voltage value is greater than or equal to zero volts (0V).

source

pub const fn is_negative(&self) -> bool

Returns whether the voltage value is negative.

This returns true if the voltage value is less than zero volts (0V).

source

pub const fn abs(&self) -> Voltage

Returns the absolute value of the voltage value.

source

pub const fn invert(&self) -> Voltage

Inverts the voltage value from positive to negative or negative to positive.

source

pub const fn zero() -> Voltage

Returns a Voltage value of zero volts (0V).

Trait Implementations§

source§

impl Add<Voltage> for Voltage

§

type Output = Voltage

The resulting type after applying the + operator.
source§

fn add(self, other: Voltage) -> Voltage

Performs the + operation. Read more
source§

impl Clone for Voltage

source§

fn clone(&self) -> Voltage

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Voltage

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Div<Current> for Voltage

source§

fn div(self, current: Current) -> <Voltage as Div<Current>>::Output

Calculates the resistance of a resistive load given the voltage across it and the current.

Will be rounded down to the nearest whole milliohm (mΩ). Panics if the current is zero.

§

type Output = Resistance

The resulting type after applying the / operator.
source§

impl Div<Resistance> for Voltage

source§

fn div(self, resistance: Resistance) -> <Voltage as Div<Resistance>>::Output

Calculates the current through a resistive load given the voltage across it.

Will be rounded down to the nearest whole microamp (μA). Panics if the resistance is zero.

§

type Output = Current

The resulting type after applying the / operator.
source§

impl Div<f32> for Voltage

§

type Output = Voltage

The resulting type after applying the / operator.
source§

fn div(self, other: f32) -> Voltage

Performs the / operation. Read more
source§

impl Div<i32> for Voltage

§

type Output = Voltage

The resulting type after applying the / operator.
source§

fn div(self, other: i32) -> Voltage

Performs the / operation. Read more
source§

impl Mul<f32> for Voltage

§

type Output = Voltage

The resulting type after applying the * operator.
source§

fn mul(self, other: f32) -> Voltage

Performs the * operation. Read more
source§

impl Mul<i32> for Voltage

§

type Output = Voltage

The resulting type after applying the * operator.
source§

fn mul(self, other: i32) -> Voltage

Performs the * operation. Read more
source§

impl Ord for Voltage

source§

fn cmp(&self, other: &Voltage) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Voltage> for Voltage

source§

fn eq(&self, other: &Voltage) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Voltage> for Voltage

source§

fn partial_cmp(&self, other: &Voltage) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Sub<Voltage> for Voltage

§

type Output = Voltage

The resulting type after applying the - operator.
source§

fn sub(self, other: Voltage) -> Voltage

Performs the - operation. Read more
source§

impl Copy for Voltage

source§

impl Eq for Voltage

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.