Write multiple bits in a 16-bit device register. * @param devAddr I2C slave device address * @param regAddr Register regAddr to write to * @param bitStart First bit position to write (0-15) * @param length Number of bits to write (not more than 16) * @param data Right-aligned value to write * @return Status of operation (true = success) */
| 528 | * @return Status of operation (true = success) |
| 529 | */ |
| 530 | bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data, void *wireObj) { |
| 531 | // 010 value to write |
| 532 | // fedcba9876543210 bit numbers |
| 533 | // xxx args: bitStart=12, length=3 |
| 534 | // 0001110000000000 mask word |
| 535 | // 1010111110010110 original value (sample) |
| 536 | // 1010001110010110 original & ~mask |
| 537 | // 1010101110010110 masked | value |
| 538 | uint16_t w; |
| 539 | if (readWord(devAddr, regAddr, &w, I2Cdev::readTimeout, wireObj) != 0) { |
| 540 | uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1); |
| 541 | data <<= (bitStart - length + 1); // shift data into correct position |
| 542 | data &= mask; // zero all non-important bits in data |
| 543 | w &= ~(mask); // zero all important bits in existing word |
| 544 | w |= data; // combine data with existing word |
| 545 | return writeWord(devAddr, regAddr, w, wireObj); |
| 546 | } else { |
| 547 | return false; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /** Write single byte to an 8-bit device register. |
| 552 | * @param devAddr I2C slave device address |
nothing calls this directly
no outgoing calls
no test coverage detected