Skip to content

Commit

Permalink
FIX: add more methods for reading registers
Browse files Browse the repository at this point in the history
  • Loading branch information
tasoftch committed Jun 30, 2022
1 parent 9f121fa commit 40f3b30
Showing 1 changed file with 70 additions and 2 deletions.
72 changes: 70 additions & 2 deletions PHP/I2C.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public function read2Bytes() {
return ($b1 << 8) | $b2;
}

/**
* Reads 3 bytes or 24 bits from i2c bus
*
* @return int
*/
public function read3Bytes() {
$this->checkDev();
if($this->byteEncoding == self::LITTLE_ENDIAN_ENCODING)
@ list($b3, $b2, $b1) = i2c_read($this->deviceStream, 3);
else
@ list($b1, $b2, $b3) = i2c_read($this->deviceStream, 3);
return ($b1 << 16) | ($b2 << 8) | $b3;
}

/**
* Reads 4 bytes or 32 bits from i2c bus
*
Expand Down Expand Up @@ -128,14 +142,24 @@ public function writeRegister(int $register) {
return i2c_write($this->deviceStream, $register);
}

/**
* Writes one byte to a specified register
*
* @param int $register
* @param int $byte
*/
public function writeByte(int $register, int $byte) {
$this->write($register, [$byte]);
}

/**
* Writes 2 bytes to the specified register on the connected chip
*
* @param int $register
* @param int $bit16
* @return bool
*/
public function write16(int $register, int $bit16) {
public function write2Bytes(int $register, int $bit16) {
if($this->byteEncoding == self::LITTLE_ENDIAN_ENCODING)
return i2c_write($this->deviceStream, $register, [$bit16 & 0xFF, ($bit16>>8) & 0xFF]);
return i2c_write($this->deviceStream, $register, [($bit16>>8) & 0xFF, $bit16 & 0xFF]);
Expand All @@ -148,12 +172,56 @@ public function write16(int $register, int $bit16) {
* @param int $bit32
* @return bool
*/
public function write32(int $register, int $bit32) {
public function write4Bytes(int $register, int $bit32) {
if($this->byteEncoding == self::LITTLE_ENDIAN_ENCODING)
return i2c_write($this->deviceStream, $register, [$bit32 & 0xFF, ($bit32>>8) & 0xFF, ($bit32>>16) & 0xFF, ($bit32>>24) & 0xFF]);
return i2c_write($this->deviceStream, $register, [($bit32>>24) & 0xFF, ($bit32>>16) & 0xFF, ($bit32>>8) & 0xFF, $bit32 & 0xFF]);
}

/**
* Reads one byte from a specified register
*
* @param int $register
* @return int
*/
public function readRegister(int $register): int {
$this->writeRegister($register);
return $this->readByte();
}

/**
* Reads two bytes from a specified register
*
* @param int $register
* @return int
*/
public function readRegister16(int $register): int {
$this->writeRegister($register);
return $this->read2Bytes();
}

/**
* Reads three bytes from a specified register
*
* @param int $register
* @return int
*/
public function readRegister24(int $register): int {
$this->writeRegister($register);
return $this->read3Bytes();
}

/**
* Reads four bytes from a specified register
*
* @param int $register
* @return int
*/
public function readRegister32(int $register): int {
$this->writeRegister($register);
return $this->read4Bytes();
}

/**
* Interprets a given integer as signed integer using a specific bit depth
*
Expand Down

0 comments on commit 40f3b30

Please sign in to comment.