The ESP32 has a wonk, in the Arduino IDE, about passing uint8_t's and uint16_t's as function parameter arguments.
This function:
int myMPU9250::setAccelRange( int range )
{
SPI_Err = 0;
if ( ACCEL_RANGE_2G == range )
{
// setting the accel range to 2G
SPI_Err = _spi.fWriteSPIdata8bits( ACCEL_CONFIG, ACCEL_FS_SEL_2G );
_accelScale = G * 2.0f / 32767.5f; // setting the accel scale to 2G
return SPI_Err;
}
works great. range is a uint8_t.
When the function was written:
int myMPU9250::setAccelRange( uint8_t range )
{
SPI_Err = 0;
if ( ACCEL_RANGE_2G == range )
{
// setting the accel range to 2G
SPI_Err = _spi.fWriteSPIdata8bits( ACCEL_CONFIG, ACCEL_FS_SEL_2G );
_accelScale = G * 2.0f / 32767.5f; // setting the accel scale to 2G
return SPI_Err;
}
it would not compile in the Arduino IDE.
This function call _spi.fWriteSPIdata8bits( ACCEL_CONFIG, ACCEL_FS_SEL_2G ); is using the SPI_API. The SPI API uses uint8_t and uint16_t but the Arduino IDE will not compile if the _spi.fWriteSPIdata8bits( ACCEL_CONFIG, ACCEL_FS_SEL_2G ); was written:
int ESP32_SPI_API::fWriteSPIdata8bits( uint8_t _address, uint8_t _DataToSend)
{
uint8_t txData[2] = { (uint8_t)_address, (uint8_t)_DataToSend };
spi_transaction_t trans_desc;
trans_desc = { };
trans_desc.addr = 0;
trans_desc.cmd = 0;
trans_desc.flags = 0;
trans_desc.length = 8 * 2; // total data bits
trans_desc.tx_buffer = txData;
return spi_device_transmit( _h, &trans_desc);
} // void fSendSPI( uint8_t count, uint8_t address, uint8_t DataToSend)
But works fine when written
int ESP32_SPI_API::fWriteSPIdata8bits( int _address, int _DataToSend)
{
uint8_t txData[2] = { (uint8_t)_address, (uint8_t)_DataToSend };
spi_transaction_t trans_desc;
trans_desc = { };
trans_desc.addr = 0;
trans_desc.cmd = 0;
trans_desc.flags = 0;
trans_desc.length = 8 * 2; // total data bits
trans_desc.tx_buffer = txData;
return spi_device_transmit( _h, &trans_desc);
} // void fSendSPI( uint8_t count, uint8_t address, uint8_t DataToSend)
Just be aware the ESP32 with the Arduino IDE has a wonk about uint8_t and uint16_t.
Pass an int up is fine, no worries, but passing an int down one should be aware of the info one may lose.
Passing up :
int u = 1;
uint8_t d = u;
Passing down:
int u = 8265;
uint8_t d = u; You'll not get an error but you will lose info.