Skip to content

Commit

Permalink
FIX: prepare for PHP 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tasoftch committed May 7, 2024
1 parent 3177529 commit ef82dfe
Show file tree
Hide file tree
Showing 5 changed files with 556 additions and 165 deletions.
63 changes: 26 additions & 37 deletions PHP/I2C.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@
*/
class I2C
{
const LITTLE_ENDIAN_ENCODING = 1;
const BIG_ENDIAN_ENCODING = 2;
const LITTLE_ENDIAN_ENCODING = PHP_I2C_ENDOCING_LITTLE_ENDIAN;
const BIG_ENDIAN_ENCODING = PHP_I2C_ENDOCING_BIG_ENDIAN;

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

private $byteEncoding = self::BIG_ENDIAN_ENCODING;

/**
* I2C constructor.
* @param int $address
* @param int $bus
*/
public function __construct(int $address, int $bus = 1)
public function __construct(int $address, int $bus = 1, int $encoding = self::BIG_ENDIAN_ENCODING)
{
$this->deviceStream = i2c_open("/dev/i2c-$bus");
$this->deviceStream = i2c_open("/dev/i2c-$bus", $encoding);
if($this->deviceStream)
i2c_select($this->deviceStream, $address);
if(!$this->deviceStream)
Expand Down Expand Up @@ -75,7 +73,7 @@ public function read($length) {
*/
public function readByte() {
$this->checkDev();
return i2c_read($this->deviceStream, 1) [0] ?? 0;
return i2c_read_byte($this->deviceStream);
}

/**
Expand All @@ -85,11 +83,7 @@ public function readByte() {
*/
public function read2Bytes() {
$this->checkDev();
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;
return i2c_read_2_bytes($this->deviceStream);
}

/**
Expand All @@ -99,11 +93,7 @@ public function read2Bytes() {
*/
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;
return i2c_read_3_bytes($this->deviceStream);
}

/**
Expand All @@ -113,11 +103,7 @@ public function read3Bytes() {
*/
public function read4Bytes() {
$this->checkDev();
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;
return i2c_read_4_bytes($this->deviceStream);
}

/**
Expand All @@ -127,7 +113,8 @@ public function read4Bytes() {
* @param array $bytes
* @return bool
*/
public function write(int $register, array $bytes) {
public function write(int $register, array $bytes): bool
{
$this->checkDev();
return i2c_write($this->deviceStream, $register, $bytes);
}
Expand All @@ -138,18 +125,21 @@ public function write(int $register, array $bytes) {
* @param int $register
* @return bool
*/
public function writeRegister(int $register) {
return i2c_write($this->deviceStream, $register);
public function writeRegister(int $register): bool
{
return i2c_write_register($this->deviceStream, $register);
}

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

/**
Expand All @@ -159,10 +149,9 @@ public function writeByte(int $register, int $byte) {
* @param int $bit16
* @return bool
*/
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]);
public function write2Bytes(int $register, int $bit16): bool
{
return i2c_write_2_bytes($this->deviceStream, $register, $bit16);
}

/**
Expand All @@ -172,10 +161,9 @@ public function write2Bytes(int $register, int $bit16) {
* @param int $bit32
* @return bool
*/
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]);
public function write4Bytes(int $register, int $bit32): bool
{
return i2c_write_byte($this->deviceStream, $register, $bit32);
}

/**
Expand Down Expand Up @@ -245,19 +233,20 @@ public function getAddress(): int

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

/**
* @param int $byteEncoding
* @return I2C
* @deprecated
*/
public function setByteEncoding(int $byteEncoding): I2C
{
$this->byteEncoding = $byteEncoding;
return $this;
}
}
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ The extension adds five function to the global scope:
This opens the device bus.
1. ```i2c_select```
This selects an address of a connected chip.
1. ```i2c_read```
1. ```i2c_read``` ```i2c_read_byte``` ```i2c_read_2_bytes``` ```i2c_read_3_bytes``` ```i2c_read_4_bytes```
Reads data from the i2c bus.
1. ```i2c_write```
Writes data to the i2c bus
1. ```i2c_write``` ```i2c_write_byte``` ```i2c_write_2_bytes``` ```i2c_write_3_bytes``` ```i2c_write_4_bytes```
Writes data to the i2c bus
1. ```i2c_close```
Closes the bus.

Expand All @@ -48,6 +48,9 @@ i2c_select($fd, 0x48);
for($e=0;$e<30;$e++) {
// Read for 30 times the value between channel AIN_0 and GND, 4.096 V, 128 samples/s
i2c_write($fd, 1, [0xc3, 0x85]);
// or
// i2c_write_2_bytes( 0x01c385 );

// Wait for conversion completed
usleep(9000);
i2c_write($fd, 0);
Expand Down
Loading

0 comments on commit ef82dfe

Please sign in to comment.