<commit_before>/**************************************************************************/
/*! 
    @file     MCP342X.cpp
    @author   <NAME>
	@license  BSD (see license.txt)
	
    This is part of an Arduino library to interface with the Microchip
    MCP47X6 series of Analog-to-Digital converters which are connected
    via the I2C bus.

    MCP342X I2C device class
    Based on Microchip datasheets for the following part numbers
        MCP3421, MCP3422, MCP3423, MCP3424, MCP3425, MCP3426, MCP3427, MCP3428
    These parts share a common programming interface

    (c) Copyright 2013 by <NAME> <<EMAIL>>
    Updates should (hopefully) always be available at
        https://github.com/schnarel/MCP342X

	@section  HISTORY

    2013-Dec-24  - First release, C. Schnarel
*/
/**************************************************************************/

#include "MCP342X.h"

/*	static float	stepSizeTbl[] = {
		0.001,		// 12-bit, 1X Gain
		0.0005,		// 12-bit, 2X Gain
		0.00025,	// 12-bit, 4X Gain
		0.000125,	// 12-bit, 8X Gain
		0.00025,	// 14-bit, 1X Gain
		0.000125,	// 14-bit, 2X Gain
		0.0000625,	// 14-bit, 4X Gain
		0.00003125,	// 14-bit, 8X Gain
		0.0000625,	// 16-bit, 1X Gain
		0.00003125,	// 16-bit, 2X Gain
		0.000015625,	// 16-bit, 4X Gain
		0.0000078125,	// 16-bit, 8X Gain
		0.000015625,	// 18-bit, 1X Gain
		0.0000078125,	// 18-bit, 2X Gain
		0.00000390625,	// 18-bit, 4X Gain
		0.000001953125	// 18-bit, 8X Gain
		};
*/

/******************************************
 * Default constructor, uses default I2C address.
 * @see MCP342X_DEFAULT_ADDRESS
 */
MCP342X::MCP342X() {
    devAddr = MCP342X_DEFAULT_ADDRESS;
}

/******************************************
 * Specific address constructor.
 * @param address I2C address
 * @see MCP342X_DEFAULT_ADDRESS
 * @see MCP342X_A0GND_A1GND, etc.
 */
MCP342X::MCP342X(uint8_t address) {
    devAddr = address;
}

/******************************************
 * Verify the I2C connection.
 * Make sure the device is connected and responds as expected.
 * @return True if connection is valid, false otherwise
 */
bool MCP342X::testConnection() {
    Wire.beginTransmission(devAddr);
    return (Wire.endTransmission() == 0);
}


/******************************************
 * Set the configuration shadow register
 */
void MCP342X::configure(uint8_t configData) {
  configRegShdw = configData;
}

/******************************************
 * Get the configuration shadow register
 */
uint8_t MCP342X::getConfigRegShdw(void) {
  return configRegShdw;
}

/******************************************
 * Get the step size based on the configuration shadow register
 */
/*float MCP342X::getStepSize(void) {
  uint8_t select = configRegShdw & (MCP342X_SIZE_MASK | MCP342X_GAIN_MASK);
  return stepSizeTbl[select];
}*/

/******************************************
 * Start a conversion using configuration settings from
 *   the shadow configuration register
 */
bool MCP342X::startConversion(void) {
  Wire.beginTransmission(devAddr);
  Wire.write(configRegShdw | MCP342X_RDY);
  return (Wire.endTransmission() == 0);
}

 
/******************************************
 * Start a conversion using configuration settings from
 *   the shadow configuration register substituting the
 *   supplied channel
 */
bool MCP342X::startConversion(uint8_t channel) {
  Wire.beginTransmission(devAddr);
  configRegShdw = ((configRegShdw & !MCP342X_CHANNEL_MASK) | 
			   (channel & MCP342X_CHANNEL_MASK));
  Wire.write(configRegShdw | MCP342X_RDY);
  return (Wire.endTransmission() == 0);
}

 
/******************************************
 * Read the conversion value (12, 14 or 16 bit)
 *  Spins reading status until ready then
 *  fills in the supplied location with the 16-bit (two byte)
 *  conversion value and returns the config byte
 */
uint8_t MCP342X::getResult(int16_t *dataPtr) {
  uint8_t adcStatus;
  if((configRegShdw & MCP342X_SIZE_MASK) == MCP342X_SIZE_18BIT) {
    return 0xFF;
  }

  do {
     if(Wire.requestFrom(devAddr, (uint8_t) 3) == 3) {
       ((char*)dataPtr)[1] = Wire.read();
       ((char*)dataPtr)[0] = Wire.read();
       adcStatus = Wire.read();
     }
     else return 0xFF;
  } while((adcStatus & MCP342X_RDY) != 0x00);
  return adcStatus;
}

 
/******************************************
 * Read the conversion value (18 bit)
 *  Spins reading status until ready then
 *  fills in the supplied location (32 bit) with
 *  the 24-bit (three byte) conversion value
 *  and returns the config byte
 */
uint8_t MCP342X::getResult(int32_t *dataPtr) {
  uint8_t adcStatus;
  if((configRegShdw & MCP342X_SIZE_MASK) != MCP342X_SIZE_18BIT) {
    return 0xFF;
  }

  do {
     if(Wire.requestFrom((uint8_t) 0x6B, (uint8_t) 4) == 4) {
       ((char*)dataPtr)[3] = Wire.read();
       ((char*)dataPtr)[2] = Wire.read();
       ((char*)dataPtr)[1] = Wire.read();
       adcStatus = Wire.read();
     }
     else return 0xFF;
  } while((adcStatus & MCP342X_RDY) != 0x00);
  *dataPtr = (*dataPtr)>>8;
  return adcStatus;
}

<commit_msg>Fixed bitwise invert of channel mask<commit_after>/**************************************************************************/
/*! 
    @file     MCP342X.cpp
    @author   <NAME>
	@license  BSD (see license.txt)
	
    This is part of an Arduino library to interface with the Microchip
    MCP47X6 series of Analog-to-Digital converters which are connected
    via the I2C bus.

    MCP342X I2C device class
    Based on Microchip datasheets for the following part numbers
        MCP3421, MCP3422, MCP3423, MCP3424, MCP3425, MCP3426, MCP3427, MCP3428
    These parts share a common programming interface

    (c) Copyright 2013 by <NAME> <<EMAIL>>
    Updates should (hopefully) always be available at
        https://github.com/schnarel/MCP342X

	@section  HISTORY

    2013-Dec-24  - First release, <NAME>
*/
/**************************************************************************/

#include "MCP342X.h"

/*	static float	stepSizeTbl[] = {
		0.001,		// 12-bit, 1X Gain
		0.0005,		// 12-bit, 2X Gain
		0.00025,	// 12-bit, 4X Gain
		0.000125,	// 12-bit, 8X Gain
		0.00025,	// 14-bit, 1X Gain
		0.000125,	// 14-bit, 2X Gain
		0.0000625,	// 14-bit, 4X Gain
		0.00003125,	// 14-bit, 8X Gain
		0.0000625,	// 16-bit, 1X Gain
		0.00003125,	// 16-bit, 2X Gain
		0.000015625,	// 16-bit, 4X Gain
		0.0000078125,	// 16-bit, 8X Gain
		0.000015625,	// 18-bit, 1X Gain
		0.0000078125,	// 18-bit, 2X Gain
		0.00000390625,	// 18-bit, 4X Gain
		0.000001953125	// 18-bit, 8X Gain
		};
*/

/******************************************
 * Default constructor, uses default I2C address.
 * @see MCP342X_DEFAULT_ADDRESS
 */
MCP342X::MCP342X() {
    devAddr = MCP342X_DEFAULT_ADDRESS;
}

/******************************************
 * Specific address constructor.
 * @param address I2C address
 * @see MCP342X_DEFAULT_ADDRESS
 * @see MCP342X_A0GND_A1GND, etc.
 */
MCP342X::MCP342X(uint8_t address) {
    devAddr = address;
}

/******************************************
 * Verify the I2C connection.
 * Make sure the device is connected and responds as expected.
 * @return True if connection is valid, false otherwise
 */
bool MCP342X::testConnection() {
    Wire.beginTransmission(devAddr);
    return (Wire.endTransmission() == 0);
}


/******************************************
 * Set the configuration shadow register
 */
void MCP342X::configure(uint8_t configData) {
  configRegShdw = configData;
}

/******************************************
 * Get the configuration shadow register
 */
uint8_t MCP342X::getConfigRegShdw(void) {
  return configRegShdw;
}

/******************************************
 * Get the step size based on the configuration shadow register
 */
/*float MCP342X::getStepSize(void) {
  uint8_t select = configRegShdw & (MCP342X_SIZE_MASK | MCP342X_GAIN_MASK);
  return stepSizeTbl[select];
}*/

/******************************************
 * Start a conversion using configuration settings from
 *   the shadow configuration register
 */
bool MCP342X::startConversion(void) {
  Wire.beginTransmission(devAddr);
  Wire.write(configRegShdw | MCP342X_RDY);
  return (Wire.endTransmission() == 0);
}

 
/******************************************
 * Start a conversion using configuration settings from
 *   the shadow configuration register substituting the
 *   supplied channel
 */
bool MCP342X::startConversion(uint8_t channel) {
  Wire.beginTransmission(devAddr);
  configRegShdw = ((configRegShdw & ~MCP342X_CHANNEL_MASK) | 
			   (channel & MCP342X_CHANNEL_MASK));
  Wire.write(configRegShdw | MCP342X_RDY);
  return (Wire.endTransmission() == 0);
}

 
/******************************************
 * Read the conversion value (12, 14 or 16 bit)
 *  Spins reading status until ready then
 *  fills in the supplied location with the 16-bit (two byte)
 *  conversion value and returns the config byte
 */
uint8_t MCP342X::getResult(int16_t *dataPtr) {
  uint8_t adcStatus;
  if((configRegShdw & MCP342X_SIZE_MASK) == MCP342X_SIZE_18BIT) {
    return 0xFF;
  }

  do {
     if(Wire.requestFrom(devAddr, (uint8_t) 3) == 3) {
       ((char*)dataPtr)[1] = Wire.read();
       ((char*)dataPtr)[0] = Wire.read();
       adcStatus = Wire.read();
     }
     else return 0xFF;
  } while((adcStatus & MCP342X_RDY) != 0x00);
  return adcStatus;
}

 
/******************************************
 * Read the conversion value (18 bit)
 *  Spins reading status until ready then
 *  fills in the supplied location (32 bit) with
 *  the 24-bit (three byte) conversion value
 *  and returns the config byte
 */
uint8_t MCP342X::getResult(int32_t *dataPtr) {
  uint8_t adcStatus;
  if((configRegShdw & MCP342X_SIZE_MASK) != MCP342X_SIZE_18BIT) {
    return 0xFF;
  }

  do {
     if(Wire.requestFrom((uint8_t) 0x6B, (uint8_t) 4) == 4) {
       ((char*)dataPtr)[3] = Wire.read();
       ((char*)dataPtr)[2] = Wire.read();
       ((char*)dataPtr)[1] = Wire.read();
       adcStatus = Wire.read();
     }
     else return 0xFF;
  } while((adcStatus & MCP342X_RDY) != 0x00);
  *dataPtr = (*dataPtr)>>8;
  return adcStatus;
}


{'fraction_non_alphanumeric': 0.22023571713943269, 'fraction_numerical': 0.07810627247303237, 'mean_word_length': 3.8512596899224807, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2423969', 'n_tokens_mistral': 3988, 'n_tokens_neox': 3297, 'n_words': 840}