python_scripts/hx711.py
def _read(self):
        """
        _read method reads bits from hx711, converts to INT
        and validate the data.
        
        Returns: (bool || int) if it returns False then it is false reading.
            if it returns int then the reading was correct
        """
        GPIO.output(self._pd_sck, False)  # start by setting the pd_sck to 0
        ready_counter = 0
        while (not self._ready() and ready_counter <= 40):
            time.sleep(0.01)  # sleep for 10 ms because data is not ready
            ready_counter += 1
            if ready_counter == 50:  # if counter reached max value then return False
                if self._debug_mode:
                    print('self._read() not ready after 40 trials\n')
                return False

        # read first 24 bits of data
        data_in = 0  # 2's complement data from hx 711
        for _ in range(24):
            start_counter = time.perf_counter()
            # request next bit from hx 711
            GPIO.output(self._pd_sck, True)
            GPIO.output(self._pd_sck, False)
            end_counter = time.perf_counter()
            if end_counter - start_counter >= 0.00006:  # check if the hx 711 did not turn off...
                # if pd_sck pin is HIGH for 60 us and more than the HX 711 enters power down mode.
                if self._debug_mode:
                    print('Not enough fast while reading data')
                    print(
                        'Time elapsed: {}'.format(end_counter - start_counter))
                return False
            # Shift the bits as they come to data_in variable.
            # Left shift by one bit then bitwise OR with the new bit.
            data_in = (data_in << 1) | GPIO.input(self._dout)

        if self._wanted_channel == 'A' and self._gain_channel_A == 128:
            if not self._set_channel_gain(1):  # send only one bit which is 1
                return False  # return False because channel was not set properly
            else:
                self._current_channel = 'A'  # else set current channel variable
                self._gain_channel_A = 128  # and gain
        elif self._wanted_channel == 'A' and self._gain_channel_A == 64:
            if not self._set_channel_gain(3):  # send three ones
                return False  # return False because channel was not set properly
            else:
                self._current_channel = 'A'  # else set current channel variable
                self._gain_channel_A = 64
        else:
            if not self._set_channel_gain(2):  # send two ones
                return False  # return False because channel was not set properly
            else:
                self._current_channel = 'B'  # else set current channel variable

        if self._debug_mode:  # print 2's complement value
            print('Binary value as received: {}'.format(bin(data_in)))

        #check if data is valid
        if (data_in == 0x7fffff
                or  # 0x7fffff is the highest possible value from hx711
                data_in == 0x800000
           ):  # 0x800000 is the lowest possible value from hx711
            if self._debug_mode:
                print('Invalid data detected: {}\n'.format(data_in))
            return False  # rturn false because the data is invalid

        # calculate int from 2's complement
        signed_data = 0
        # 0b1000 0000 0000 0000 0000 0000 check if the sign bit is 1. Negative number.
        if (data_in & 0x800000):
            signed_data = -(
                (data_in ^ 0xffffff) + 1)  # convert from 2's complement to int
        else:  # else do not do anything the value is positive number
            signed_data = data_in

        if self._debug_mode:
            print('Converted 2\'s complement value: {}'.format(signed_data))

        return signed_data

        _read method reads bits from hx711, converts to INT
        and validate the data.
        
        Returns: (bool || int) if it returns False then it is false reading.
            if it returns int then the reading was correct
        
_read method reads bits from hx711, converts to INT
and validate the data.

(bool || int) if it returns False then it is false reading.
if it returns int then the reading was correct
_read method reads bits from hx711 converts to INT and validate the data . ( bool || int ) if it returns False then it is false reading . if it returns int then the reading was correct
def _read(self):
        GPIO.output(self._pd_sck, False)  
        ready_counter = 0
        while (not self._ready() and ready_counter <= 40):
            time.sleep(0.01)  
            ready_counter += 1
            if ready_counter == 50:  
                if self._debug_mode:
                    print('self._read() not ready after 40 trials\n')
                return False
        data_in = 0  
        for _ in range(24):
            start_counter = time.perf_counter()
            GPIO.output(self._pd_sck, True)
            GPIO.output(self._pd_sck, False)
            end_counter = time.perf_counter()
            if end_counter - start_counter >= 0.00006:  
                if self._debug_mode:
                    print('Not enough fast while reading data')
                    print(
                        'Time elapsed: {}'.format(end_counter - start_counter))
                return False
            data_in = (data_in << 1) | GPIO.input(self._dout)
        if self._wanted_channel == 'A' and self._gain_channel_A == 128:
            if not self._set_channel_gain(1):  
                return False  
            else:
                self._current_channel = 'A'  
                self._gain_channel_A = 128  
        elif self._wanted_channel == 'A' and self._gain_channel_A == 64:
            if not self._set_channel_gain(3):  
                return False  
            else:
                self._current_channel = 'A'  
                self._gain_channel_A = 64
        else:
            if not self._set_channel_gain(2):  
                return False  
            else:
                self._current_channel = 'B'  
        if self._debug_mode:  
            print('Binary value as received: {}'.format(bin(data_in)))
        if (data_in == 0x7fffff
                or  
                data_in == 0x800000
           ):  
            if self._debug_mode:
                print('Invalid data detected: {}\n'.format(data_in))
            return False  
        signed_data = 0
        if (data_in & 0x800000):
            signed_data = -(
                (data_in ^ 0xffffff) + 1)  
        else:  
            signed_data = data_in
        if self._debug_mode:
            print('Converted 2\'s complement value: {}'.format(signed_data))
        return signed_data
def _read ( self ) : GPIO . output ( self . _pd_sck , False ) ready_counter = 0 while ( not self . _ready ( ) and ready_counter <= 40 ) : time . sleep ( 0.01 ) ready_counter += 1 if ready_counter == 50 : if self . _debug_mode : print ( 'self._read() not ready after 40 trials\n' ) return False data_in = 0 for _ in range ( 24 ) : start_counter = time . perf_counter ( ) GPIO . output ( self . _pd_sck , True ) GPIO . output ( self . _pd_sck , False ) end_counter = time . perf_counter ( ) if end_counter - start_counter >= 0.00006 : if self . _debug_mode : print ( 'Not enough fast while reading data' ) print ( 'Time elapsed: {}' . format ( end_counter - start_counter ) ) return False data_in = ( data_in << 1 ) | GPIO . input ( self . _dout ) if self . _wanted_channel == 'A' and self . _gain_channel_A == 128 : if not self . _set_channel_gain ( 1 ) : return False else : self . _current_channel = 'A' self . _gain_channel_A = 128 elif self . _wanted_channel == 'A' and self . _gain_channel_A == 64 : if not self . _set_channel_gain ( 3 ) : return False else : self . _current_channel = 'A' self . _gain_channel_A = 64 else : if not self . _set_channel_gain ( 2 ) : return False else : self . _current_channel = 'B' if self . _debug_mode : print ( 'Binary value as received: {}' . format ( bin ( data_in ) ) ) if ( data_in == 0x7fffff or data_in == 0x800000 ) : if self . _debug_mode : print ( 'Invalid data detected: {}\n' . format ( data_in ) ) return False signed_data = 0 if ( data_in & 0x800000 ) : signed_data = - ( ( data_in ^ 0xffffff ) + 1 ) else : signed_data = data_in if self . _debug_mode : print ( 'Converted 2\'s complement value: {}' . format ( signed_data ) ) return signed_data
_read method reads bits from hx711, converts to INT
and validate the data.
_read method reads bits from hx711 converts to INT and validate the data .
"""
        _read method reads bits from hx711, converts to INT
        and validate the data.
        
        Returns: (bool || int) if it returns False then it is false reading.
            if it returns int then the reading was correct
        """ # start by setting the pd_sck to 0 # sleep for 10 ms because data is not ready # if counter reached max value then return False # read first 24 bits of data # 2's complement data from hx 711 # request next bit from hx 711 # check if the hx 711 did not turn off... # if pd_sck pin is HIGH for 60 us and more than the HX 711 enters power down mode. # Shift the bits as they come to data_in variable. # Left shift by one bit then bitwise OR with the new bit. # send only one bit which is 1 # return False because channel was not set properly # else set current channel variable # and gain # send three ones # return False because channel was not set properly # else set current channel variable # send two ones # return False because channel was not set properly # else set current channel variable # print 2's complement value #check if data is valid # 0x7fffff is the highest possible value from hx711 # 0x800000 is the lowest possible value from hx711 # rturn false because the data is invalid # calculate int from 2's complement # 0b1000 0000 0000 0000 0000 0000 check if the sign bit is 1. Negative number. # convert from 2's complement to int # else do not do anything the value is positive number
self