-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventLoop.php
executable file
·91 lines (72 loc) · 2.41 KB
/
EventLoop.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
85
86
87
88
89
90
91
<?php
namespace RemotePlayer;
require_once "vendor/autoload.php";
const PIN = [
"button" => 26,
"LED" => 13,
"sensor" => [
"man" => 17,
]
];
use \Calcinai\PHPi as Pi;
use \Calcinai\PHPi\Pin\PinFunction;
use \Calcinai\PHPi\Pin;
use Calcinai\PHPi\External\Generic\LED;
use Calcinai\PHPi\External\Generic\Button;
$queue = new \SplQueue();
$board = Pi\Factory::create();
$led = new LED($board->getPin(PIN['LED']));
//$led2 = new LED($board->getPin(24));
//$led2->on();
// $led->flash(3, 1, 0.5);
$loop = $board->getLoop();
$loop->addPeriodicTimer(5, function () use($queue, $board, $loop, $led) {
if(!$queue->isEmpty()){
$led->flash(2, 0.5, 0.5);
}
});
$button = new Button($board->getPin(PIN['button']));
$button->on("press", function() use ($queue, $led){
if(!$queue->isEmpty()){
playSound($queue->pop());
}else{
$led->flash(1, 0.5, 1);
}
});
$connector = new \React\Socket\Connector($loop);
$connector->connect("unix:///home/pi/RemotePlayer/ws.sock")->then(function($conn) use ($board, $loop, $queue){
$conn->on("data", function($data) use ($board, $loop, $queue){
echo "RECV: ".$data.PHP_EOL;
$data = json_decode($data, 1);
switch($data["type"]){
case "sendVoice":
$voice = base64_decode($data['voice']);
$autoplay = $data['autoplay'];
if($autoplay && checkSensorMan($board->getPin(PIN['sensor']['man']))){
playSound($voice);
}else{
$queue->push($voice);
}
default:
return;
}
});
});
function checkSensorMan($pin){
$pin->setFunction(PinFunction::INPUT);
if($pin->getLevel() == Pin::LEVEL_HIGH){
return true;
}else{
return false;
}
}
function playSound($sound){
global $led;
$led->on();
file_put_contents("./temp.sound.mp3", $sound);
system("mplayer ./temp.sound.mp3");
echo "played sound\r\n";
$led->off();
}
$board->getLoop()->run();
//$led2->off();