fn update_buffer_with_float(
        &mut self,
        index: Index,
        mut value: f32,
        mut fractional_digits: u8,
        base: u8,
    ) -> Result<(), Error> {
        let index = u8::from(index);

        // Available digits on display
        let mut numeric_digits = 4 - index;

        let is_negative = if value < 0. {
            // The sign will take up one digit
            numeric_digits -= 1;
            // Flip the sign to do the rest of the formatting
            value *= -1.;
            true
        } else {
            false
        };

        let base = base as u32;
        let basef = base as f32;

        // Work out the multiplier needed to get all fraction digits into an integer
        let mut to_int_factor = base.pow(fractional_digits as u32) as f32;

        // Get an integer containing digits to be displayed
        let mut display_number = ((value * to_int_factor) + 0.5) as u32;

        // Calculate the upper bound given the number of digits available
        let too_big = base.pow(numeric_digits as u32);

        // If the number is too large, reduce fractional digits
        while display_number >= too_big {
            fractional_digits -= 1;
            to_int_factor /= basef;
            display_number = ((value * to_int_factor) + 0.5) as u32;
        }

        // Did we lose the decimal?
        if to_int_factor < 1. {
            return Err(Error::InsufficientDigits);
        }

        // Digit we're working on, less the start position
        let mut display_pos = (3 - index) as i8;

        if display_number == 0 {
            // Write out the 0
            self.update_buffer_with_digit((index + (display_pos as u8)).into(), 0);
            // Move the current pos along
            display_pos -= 1;
        } else {
            let mut i = 0;
            while display_number != 0 || i <= fractional_digits {
                let digit_index: Index = (index + (display_pos as u8)).into();
                // Write out the current digit
                self.update_buffer_with_digit(digit_index, (display_number % base) as u8);
                // Add the decimal if necessary
                if fractional_digits != 0 && i == fractional_digits {
                    self.update_buffer_with_dot(digit_index, true);
                }
                // Move the current pos along
                display_pos -= 1;
                // Move the number along
                display_number /= base;
                i += 1;
            }
        }

        if is_negative {
            // Add the minus sign
            update_bits(self, (index + (display_pos as u8)).into(), MINUS_SIGN);
            // Move the current pos along
            display_pos -= 1;
        }

        // Clear any remaining segments
        while display_pos >= 0 {
            update_bits(self, (index + (display_pos as u8)).into(), 0);
            // Move the current pos along
            display_pos -= 1;
        }

        Ok(())
    }
/// Update the buffer with a formatted float not starting before the specified index
/// The logic for this is copied mostly from from the adafruit library. Only difference is this allows the start index to be > 0
///
/// # Arguments
///
/// * `index` - Digit index.
/// * `value` - float value.
/// * `fractional_digits` - Number of fractional digits.
/// * `base` - Base to use.
///
/// # Examples
///
/// ```
/// use ht16k33::i2c_mock::I2cMock;
/// use ht16k33::HT16K33;
/// use adafruit_7segment::{SevenSegment, Index};
///
/// // Create an I2C device.
/// let mut i2c = I2cMock::new();
///
/// // The I2C device address.
/// const DISP_I2C_ADDR: u8 = 112;
///
/// let mut ht16k33 = HT16K33::new(i2c, DISP_I2C_ADDR);
///
/// // Write 9.9 from pos 2
/// ht16k33.update_buffer_with_float(Index::Two, 9.9, 1, 10);
/// ```
Update the buffer with a formatted float not starting before the specified index
The logic for this is copied mostly from from the adafruit library. Only difference is this allows the start index to be > 0
Update the buffer with a formatted float not starting before the specified index The logic for this is copied mostly from from the adafruit library . Only difference is this allows the start index to be > 0
fn update_buffer_with_float(
        &mut self,
        index: Index,
        mut value: f32,
        mut fractional_digits: u8,
        base: u8,
    ) -> Result<(), Error> {
        let index = u8::from(index);
        // Available digits on display
        let mut numeric_digits = 4 - index;
        let is_negative = if value < 0. {
            // The sign will take up one digit
            numeric_digits -= 1;
            // Flip the sign to do the rest of the formatting
            value *= -1.;
            true
        } else {
            false
        };
        let base = base as u32;
        let basef = base as f32;
        // Work out the multiplier needed to get all fraction digits into an integer
        let mut to_int_factor = base.pow(fractional_digits as u32) as f32;
        // Get an integer containing digits to be displayed
        let mut display_number = ((value * to_int_factor) + 0.5) as u32;
        // Calculate the upper bound given the number of digits available
        let too_big = base.pow(numeric_digits as u32);
        // If the number is too large, reduce fractional digits
        while display_number >= too_big {
            fractional_digits -= 1;
            to_int_factor /= basef;
            display_number = ((value * to_int_factor) + 0.5) as u32;
        }
        // Did we lose the decimal?
        if to_int_factor < 1. {
            return Err(Error::InsufficientDigits);
        }
        // Digit we're working on, less the start position
        let mut display_pos = (3 - index) as i8;
        if display_number == 0 {
            // Write out the 0
            self.update_buffer_with_digit((index + (display_pos as u8)).into(), 0);
            // Move the current pos along
            display_pos -= 1;
        } else {
            let mut i = 0;
            while display_number != 0 || i <= fractional_digits {
                let digit_index: Index = (index + (display_pos as u8)).into();
                // Write out the current digit
                self.update_buffer_with_digit(digit_index, (display_number % base) as u8);
                // Add the decimal if necessary
                if fractional_digits != 0 && i == fractional_digits {
                    self.update_buffer_with_dot(digit_index, true);
                }
                // Move the current pos along
                display_pos -= 1;
                // Move the number along
                display_number /= base;
                i += 1;
            }
        }
        if is_negative {
            // Add the minus sign
            update_bits(self, (index + (display_pos as u8)).into(), MINUS_SIGN);
            // Move the current pos along
            display_pos -= 1;
        }
        // Clear any remaining segments
        while display_pos >= 0 {
            update_bits(self, (index + (display_pos as u8)).into(), 0);
            // Move the current pos along
            display_pos -= 1;
        }
        Ok(())
    }
fn update_buffer_with_float ( & mut self , index : Index , mut value : f32 , mut fractional_digits : u8 , base : u8 , ) -> Result < ( ) , Error > { let index = u8 :: from ( index ) ; // Available digits on display let mut numeric_digits = 4 - index ; let is_negative = if value < 0. { // The sign will take up one digit numeric_digits -= 1 ; // Flip the sign to do the rest of the formatting value *= - 1. ; true } else { false } ; let base = base as u32 ; let basef = base as f32 ; // Work out the multiplier needed to get all fraction digits into an integer let mut to_int_factor = base . pow ( fractional_digits as u32 ) as f32 ; // Get an integer containing digits to be displayed let mut display_number = ( ( value * to_int_factor ) + 0.5 ) as u32 ; // Calculate the upper bound given the number of digits available let too_big = base . pow ( numeric_digits as u32 ) ; // If the number is too large, reduce fractional digits while display_number >= too_big { fractional_digits -= 1 ; to_int_factor /= basef ; display_number = ( ( value * to_int_factor ) + 0.5 ) as u32 ; } // Did we lose the decimal? if to_int_factor < 1. { return Err ( Error :: InsufficientDigits ) ; } // Digit we're working on, less the start position let mut display_pos = ( 3 - index ) as i8 ; if display_number == 0 { // Write out the 0 self . update_buffer_with_digit ( ( index + ( display_pos as u8 ) ) . into ( ) , 0 ) ; // Move the current pos along display_pos -= 1 ; } else { let mut i = 0 ; while display_number != 0 || i <= fractional_digits { let digit_index : Index = ( index + ( display_pos as u8 ) ) . into ( ) ; // Write out the current digit self . update_buffer_with_digit ( digit_index , ( display_number % base ) as u8 ) ; // Add the decimal if necessary if fractional_digits != 0 && i == fractional_digits { self . update_buffer_with_dot ( digit_index , true ) ; } // Move the current pos along display_pos -= 1 ; // Move the number along display_number /= base ; i += 1 ; } } if is_negative { // Add the minus sign update_bits ( self , ( index + ( display_pos as u8 ) ) . into ( ) , MINUS_SIGN ) ; // Move the current pos along display_pos -= 1 ; } // Clear any remaining segments while display_pos >= 0 { update_bits ( self , ( index + ( display_pos as u8 ) ) . into ( ) , 0 ) ; // Move the current pos along display_pos -= 1 ; } Ok ( ( ) ) }
Update the buffer with a formatted float not starting before the specified index
The logic for this is copied mostly from from the adafruit library.
Update the buffer with a formatted float not starting before the specified index The logic for this is copied mostly from from the adafruit library .
&mut self
index
Index
value
f32
fractional_digits
u8
base
u8
&mut self
index
Index
value
f32
fractional_digits
u8
base
u8
examples


Create an I2C device.
let mut i2c = I2cMock::new().

The I2C device address.




Create an I2C device . let mut i2c = I2cMock :: new () . The I2C device address .