K
K
Kalombyr2019-06-01 15:00:49
Arduino
Kalombyr, 2019-06-01 15:00:49

Why is type casting used like this?

Good day!
I'm trying to adapt the library from Arduino to Raspberry PI (c ++ WiringPI)
For example, there is such a function (Arduino):

int16_t ina219GetShuntVoltage(void)
{
  uint16_t value;
  ina219Read16(INA219_REG_SHUNTVOLTAGE, &value);
  return value;
}

As you can see, the function returns type int16_t, inside the function the result is first written to uint16_t,
code ina219Read16
static void ina219Read16(uint8_t reg, uint16_t *value)
{
  // Clear write buffers
  uint32_t i;
  for ( i = 0; i < I2C_BUFSIZE; i++ )
  {
    I2CMasterBuffer[i] = 0x00;
  }

  I2CWriteLength = 2;
  I2CReadLength = 2;
  I2CMasterBuffer[0] = INA219_ADDRESS;            // I2C device address
  I2CMasterBuffer[1] = reg;                       // Command register
  // Append address w/read bit
  I2CMasterBuffer[2] = INA219_ADDRESS | INA219_READ;  
  i2cEngine();

  // Shift values to create properly formed integer
  *value = ((I2CSlaveBuffer[0] << 8) | I2CSlaveBuffer[1]);
}

Stupidly I do it like this:
int16_t ina219GetShuntVoltage(void)
{
   uint16_t value = wiringPiI2CReadReg16(node->fd, INA219_REG_SHUNTVOLTAGE);
   return (int16_t)value;
}

The wiringPiI2CReadReg16 header: The problem is that I do this by "stupidly" copying. After all, the sizes of types in Arduino are different. In general, I would like to understand why it is done so that the result is first driven from uint16_t into int16_t and how can I do it right if my result comes to int.
int wiringPiI2CReadReg16 (int fd, int reg)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-06-01
@Kalombyr

why is it done like this

    8.6.3 Data Output Registers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question