Skip to content

Commit

Permalink
FIX: Make big and little endian support
Browse files Browse the repository at this point in the history
  • Loading branch information
tasoftch committed May 22, 2022
1 parent 0709fa4 commit 9f121fa
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions PHP/I2C.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
*/
class I2C
{
const LITTLE_ENDIAN_ENCODING = 1;
const BIG_ENDIAN_ENCODING = 2;

/** @var resource */
private $deviceStream;
/** @var int */
private $address;

private $byteEncoding = self::BIG_ENDIAN_ENCODING;

/**
* I2C constructor.
* @param int $address
Expand Down Expand Up @@ -80,7 +85,10 @@ public function readByte() {
*/
public function read2Bytes() {
$this->checkDev();
@ list($b1, $b2) = i2c_read($this->deviceStream, 2);
if($this->byteEncoding == self::LITTLE_ENDIAN_ENCODING)
@ list($b2, $b1) = i2c_read($this->deviceStream, 2);
else
@ list($b1, $b2) = i2c_read($this->deviceStream, 2);
return ($b1 << 8) | $b2;
}

Expand All @@ -91,7 +99,10 @@ public function read2Bytes() {
*/
public function read4Bytes() {
$this->checkDev();
@ list($b1, $b2, $b3, $b4) = i2c_read($this->deviceStream, 4);
if($this->byteEncoding == self::LITTLE_ENDIAN_ENCODING)
@ list($b4, $b3, $b2, $b1) = i2c_read($this->deviceStream, 4);
else
@ list($b1, $b2, $b3, $b4) = i2c_read($this->deviceStream, 4);
return ($b1 << 24) | ($b2 << 16) | ($b3 << 8) | $b4;
}

Expand Down Expand Up @@ -125,6 +136,8 @@ public function writeRegister(int $register) {
* @return bool
*/
public function write16(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 @@ -136,6 +149,8 @@ public function write16(int $register, int $bit16) {
* @return bool
*/
public function write32(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]);
}

Expand All @@ -159,4 +174,22 @@ public function getAddress(): int
{
return $this->address;
}

/**
* @return int
*/
public function getByteEncoding(): int
{
return $this->byteEncoding;
}

/**
* @param int $byteEncoding
* @return I2C
*/
public function setByteEncoding(int $byteEncoding): I2C
{
$this->byteEncoding = $byteEncoding;
return $this;
}
}

0 comments on commit 9f121fa

Please sign in to comment.