kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Remove unecessary manual conversion to byte arrays
rodzic
77839e2a6a
commit
3e56dfb14c
|
@ -162,61 +162,62 @@ namespace pimoroni {
|
|||
// Write an 8-bit register
|
||||
void VL53L1X::writeReg(uint16_t reg, uint8_t value)
|
||||
{
|
||||
uint8_t buffer[3] = {(reg >> 8) & 0xFF, reg & 0xFF, value};
|
||||
i2c->write_blocking(address, buffer, 3, false);
|
||||
alignas(2) struct u16_u32_buffer {
|
||||
uint16_t reg;
|
||||
uint8_t value;
|
||||
} buffer{reg, value};
|
||||
i2c->write_blocking(address, (uint8_t *)&buffer, 3, false);
|
||||
}
|
||||
|
||||
// Write a 16-bit register
|
||||
void VL53L1X::writeReg16Bit(uint16_t reg, uint16_t value)
|
||||
{
|
||||
uint8_t buffer[4] = {(reg >> 8) & 0xFF, reg & 0xFF, (value >> 8) & 0xFF, value & 0xFF};
|
||||
i2c->write_blocking(address, buffer, 4, false);
|
||||
uint16_t buffer[2] = {reg, value};
|
||||
i2c->write_blocking(address, (uint8_t *)buffer, 4, false);
|
||||
}
|
||||
|
||||
// Write a 32-bit register
|
||||
void VL53L1X::writeReg32Bit(uint16_t reg, uint32_t value)
|
||||
{
|
||||
uint8_t buffer[6] = {(reg >> 8) & 0xFF, reg & 0xFF,
|
||||
(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF};
|
||||
i2c->write_blocking(address, buffer, 6, false);
|
||||
alignas(2) struct u16_u32_buffer {
|
||||
uint16_t reg;
|
||||
uint32_t value;
|
||||
} buffer{reg, value};
|
||||
i2c->write_blocking(address, (uint8_t *)&buffer, 6, false);
|
||||
}
|
||||
|
||||
// Read an 8-bit register
|
||||
uint8_t VL53L1X::readReg(regAddr reg)
|
||||
{
|
||||
uint8_t regbuf[2] = {((uint8_t)reg >> 8) & 0xFF, (uint8_t)reg & 0xFF};
|
||||
uint8_t buffer[1];
|
||||
uint8_t value;
|
||||
i2c->write_blocking(address, regbuf, 2, true);
|
||||
i2c->read_blocking(address, buffer, 1, false);
|
||||
value = buffer[0];
|
||||
// TODO do we need to bswap reg?
|
||||
i2c->write_blocking(address, (uint8_t *)®, 2, true);
|
||||
i2c->read_blocking(address, &value, 1, false);
|
||||
return value;
|
||||
}
|
||||
|
||||
// Read a 16-bit register
|
||||
uint16_t VL53L1X::readReg16Bit(uint16_t reg)
|
||||
{
|
||||
uint8_t regbuf[2] = {(reg >> 8) & 0xFF, reg & 0xFF};
|
||||
uint8_t buffer[2];
|
||||
uint16_t value;
|
||||
reg= (reg << 8) + (reg >> 8);
|
||||
i2c->write_blocking(address, regbuf, 2, true);
|
||||
i2c->read_blocking(address, buffer, 2, false);
|
||||
value= (buffer[0] << 8) + buffer[1];
|
||||
return value;
|
||||
// TODO do we need to bswap reg?
|
||||
i2c->write_blocking(address, (uint8_t *)®, 2, true);
|
||||
i2c->read_blocking(address, (uint8_t *)&value, 2, false);
|
||||
|
||||
// TODO do we need to bswap this return value?
|
||||
return __bswap16(value);
|
||||
}
|
||||
|
||||
// Read a 32-bit register
|
||||
uint32_t VL53L1X::readReg32Bit(uint16_t reg)
|
||||
{
|
||||
uint8_t regbuf[2] = {(reg >> 8) & 0xFF, reg & 0xFF};
|
||||
uint8_t buffer[4];
|
||||
uint32_t value;
|
||||
reg= (reg << 8) + (reg >> 8);
|
||||
i2c->write_blocking(address, regbuf, 2, true);
|
||||
i2c->read_blocking(address, buffer, 4, false);
|
||||
value= (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
|
||||
return value;
|
||||
i2c->write_blocking(address, (uint8_t *)®, 2, true);
|
||||
i2c->read_blocking(address, (uint8_t *)&value, 4, false);
|
||||
|
||||
// TODO do we need to bswap this return value?
|
||||
return __bswap32(value);
|
||||
}
|
||||
|
||||
// set distance mode to Short, Medium, or Long
|
||||
|
@ -572,9 +573,9 @@ namespace pimoroni {
|
|||
void VL53L1X::readResults()
|
||||
{
|
||||
uint16_t reg = RESULT__RANGE_STATUS;
|
||||
uint8_t regbuf[2] = {(reg >> 8) & 0xFF, reg & 0xFF};
|
||||
// TODO do we need to bswap reg?
|
||||
uint8_t buffer[17];
|
||||
i2c->write_blocking(address, regbuf, 2, true);
|
||||
i2c->write_blocking(address, (uint8_t *)®, 2, true);
|
||||
i2c->read_blocking(address, buffer, 17, false);
|
||||
|
||||
results.range_status = buffer[0];
|
||||
|
|
Ładowanie…
Reference in New Issue