-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValveHW.php
64 lines (51 loc) · 1.28 KB
/
ValveHW.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
<?php
include_once 'i2c.php';
include_once 'mutex.php';
class ValveHW
{
const mutex = '/var/www/html/valves/i2c';
static private $i2c = null;
function __construct() {
if (is_null(self::$i2c)) {
self::$i2c = new I2C(0x20, 1);
}
}
public function hw_exists() {
$mutex = new Mutex(self::mutex);
return self::$i2c->hw_exists();
}
private function read() {
$mutex = new Mutex(self::mutex);
return self::$i2c->read_register();
}
private function write($value) {
$mutex = new Mutex(self::mutex);
return self::$i2c->write_register($value);
}
private function read_bit($bit) {
$mutex = new Mutex(self::mutex);
$reg = self::$i2c->read_register();
return ($reg & (1 << $bit)) != 0;
}
private function write_bit($bit, $value) {
$mutex = new Mutex(self::mutex);
$reg = self::$i2c->read_register();
$reg = ($reg & ~(1 << $bit)) | (($value == true) << $bit);
return self::$i2c->write_register($reg);
}
public function Close($valve) {
$this->write_bit($valve, true);
}
public function Open($valve) {
$this->write_bit($valve, false);
}
public function IsOpen($valve) {
return $this->read_bit($valve) == 0;
}
public function CanOpen($valve) {
return $this->read() == 255;
}
public function CloseAll() {
return $this->write(255);
}
}