-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_bus.php
84 lines (72 loc) · 3.07 KB
/
i2c_bus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
// Found at https://github.com/tbrianjones/raspberry-pi-i2c-bus/blob/master/peripherals/i2c_bus.php
// class to deal with i2c communications on the raspberry pi
//
// - this class makes use of i2c-tools
//
class i2c_bus
{
private $block = 1; // the i2c block (0 on first gen rpi's, 1 on subsequnet rpi's)
private $i2c_address; // the i2c bus address of the unit being communicated with (set when instantiated)
function __construct(
$i2c_address // the i2c address of the device we're communicating with
) {
$this->i2c_address = $i2c_address;
}
function __destruct() {
}
// --- READERS --------------------------------------------------------------------
public function read_register(
$register // register location ( eg. 0x29 )
) {
return trim( shell_exec( 'sudo i2cget -y ' . $this->block . ' ' . $this->i2c_address . ' ' . $register ) );
}
public function read_signed_byte(
$msb_register // byte register location
) {
$msb = intval( $this->read_register( $msb_register ), 16 );
$array = unpack( 's', pack( 'v', $msb << 8 ) );
$decimal_value = $array[1]/256;
return $decimal_value;
}
public function read_signed_short(
$msb_register // most significant byte register location
) {
$msb = intval( $this->read_register( $msb_register ), 16 );
$lsb = intval( $this->read_register( $msb_register + 1 ), 16 );
$val = ( $msb << 8 ) + $lsb;
$array = unpack( 's', pack( 'v', $val ) );
$decimal_value = $array[1];
return $decimal_value;
}
/* public function read_unsigned_short(
$msb_register // most significant byte register location
) {
$msb = intval( $this->read_register( $msb_register ), 16 );
$lsb = intval( $this->read_register( $msb_register + 1 ), 16 );
$val = ( $msb << 8 ) + $lsb;
$array = unpack( 'S', pack( 'v', $val ) );
$decimal_value = $array[1];
return $decimal_value;
}
public function read_unsigned_long(
$msb_register // most significant byte register location
) {
$msb = intval( $this->read_register( $msb_register ), 16 );
$lsb = intval( $this->read_register( $msb_register + 1 ), 16 );
$xlsb = intval( $this->read_register( $msb_register + 2 ), 16 );
$val = ( $msb << 16 ) + ( $lsb << 8 ) + $xlsb;
$array = unpack( 'l', pack( 'V', $val ) );
$decimal_value = $array[1];
return $decimal_value;
}
*/
// --- WRITERS --------------------------------------------------------------------
public function write_register(
$register, // register address ( eg. 0x29 )
$value // value to set at register address ( must be a decimal value )
) {
shell_exec( 'sudo i2cset -y ' . $this->block . ' ' . $this->i2c_address . ' ' . $register . ' ' . $value );
}
} // class i2c_bus
?>